<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Toolstrip &#8211; Undocumented Matlab</title>
	<atom:link href="https://undocumentedmatlab.com/articles/tag/toolstrip/feed" rel="self" type="application/rss+xml" />
	<link>https://undocumentedmatlab.com</link>
	<description>Professional Matlab consulting, development and training</description>
	<lastBuildDate>Sun, 10 Feb 2019 17:00:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.2</generator>
	<item>
		<title>Matlab toolstrip – part 9 (popup figures)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-9-popup-figures</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 10 Feb 2019 17:00:10 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8402</guid>

					<description><![CDATA[<p>Custom popup figures can be attached to Matlab GUI toolstrip controls. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures">Matlab toolstrip – part 9 (popup figures)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">previous posts</a> I showed how we can create custom Matlab app toolstrips using various controls. Today I will show how we can incorporate popup forms composed of Matlab figures into our Matlab toolstrip. These are similar in concept to drop-down and gallery selectors, in the sense that when we click the toolstrip button a custom popup is displayed. In the case of a popup form, this is a fully-customizable Matlab GUI figure.<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_popup_figure.png" alt="Popup figure in Matlab toolstrip" title="Popup figure in Matlab toolstrip" width="80%" style="max-width:605px;" /></center><br />
Toolstrips can be a bit complex to develop so I&#8217;m proceeding slowly, with each post in the miniseries building on the previous posts. <span id="more-8402"></span> I encourage you to review the earlier posts in the <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">Toolstrip miniseries</a> before reading this post.<br />
Also, remember to add the following code snippet at the beginning of your code so that the relevant toolstrip classes will be recognized by Matlab:</p>
<pre lang="matlab">import matlab.ui.internal.toolstrip.*</pre>
<h3 id="steps">Main steps and usage example</h3>
<p>To attach a figure popup to a toolstrip control, follow these steps:</p>
<ol>
<li>Create a new figure, using GUIDE or the <i><b>figure</b></i> function. The figure should typically be created modal and non-visible, unless there&#8217;s a good reason to avoid this. Note that the figure needs to be a legacy (Java-based) figure, created with GUIDE or the <i><b>figure</b></i> function &#8212; web-based uifigure (created with AppDesigner or the <i><b>uifigure</b></i> function) is not [currently] supported. </li>
<li>Create a callback function that opens and initializes this figure, and then moves it to the expected screen location using the following syntax: <code>hToolGroup.showFigureDialog(hFig,hAnchor)</code>, where <code>hFig</code> is the figure&#8217;s handle, and <code>hAnchor</code> is the handle for the triggering toolstrip control.</li>
<li>Attach the callback function to the triggering toolstrip control.</li>
</ol>
<p>Here&#8217;s a simple usage example, in which I present a file-selector popup:</p>
<pre lang="matlab">
% Create a toolstrip section, column & push-button
hSection = hTab.addSection('Popup');
hColumn = hSection.addColumn();
hButton = Button('Open',Icon.OPEN_24);
hButton.ButtonPushedFcn = {@popupFigure,hButton};  % attach popup callback to the button
hColumn.add(hButton);
% Callback function invoked when the toolstrip button is clicked
function popupFigure(hAction, hEventData, hButton)
    % Create a new non-visible modal figure
    hFig = figure('MenuBar','none', 'ToolBar','none', 'WindowStyle','modal', ...
                  'Visible','off', 'NumberTitle','off', 'Name','Select file:');
    % Add interactive control(s) to the figure (in this case, a file chooser initialized to current folder)
    jFileChooser = handle(javaObjectEDT(javax.swing.JFileChooser(pwd)), 'CallbackProperties');
    [jhFileChooser, hComponent] = javacomponent(jFileChooser, [0,0,200,200], hFig);
    set(hComponent, 'Units','normalized', 'Position',[0,0,1,1]);  % resize component within containing figure
    % Set popup control's callback (in this case, display the selected file and close the popup)
    jhFileChooser.ActionPerformedCallback = @popupActionPerformedCallback;
    function popupActionPerformedCallback(jFileChooser, jEventData)
        fprintf('Selected file: %s\n', char(jFileChooser.getSelectedFile));
        delete(hFig);
    end
    % Display the popup figure onscreen, just beneath the triggering button
    showFigureDialog(hToolGroup,hFig,hButton);
    % Wait for the modal popup figure to close before resuming GUI interactivity
    waitfor(hFig);
end
</pre>
<p>This leads to the popup figure as shown in the screenshot above.<br />
The popup figure initially appears directly beneath the triggering button. The figure can then be moved away from that position, by dragging its title bar or border frame.<br />
Note how the popup is an independent heavy-weight figure window, having a border frame, title bar and a separate task-bar icon. Removing the border frame and title-bar of Matlab figures can be done using an <a href="https://undocumentedmatlab.com/articles/frameless-undecorated-figure-windows" target="_blank">undocumented visual illusion</a> &#8211; this can make the popup less obtrusive, but also prevent its moving/resizing. An entirely different and probably better approach is to present a light-weight popup panel using the Toolpack framework, which I plan to discuss in the following post(s). The <a href="https://undocumentedmatlab.com/articles/builtin-popuppanel-widget" target="_blank"><code>PopupPanel</code> container</a> that I discussed in another post <i>cannot</i> be used, because it is displayed as a sub-component of a Matlab figure, and in this case the popup is not attached to any figure (the toolstrip and ToolGroup are not Matlab figures, <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" target="_blank">as explained here</a>).<br />
The astute reader may wonder why I bothered going to all the trouble of displaying a modal popup with a <code>JFileChooser</code>, when I could have simply used the built-in <i><b>uigetfile</b></i> or <i><b>uiputfile</b></i> functions in the button&#8217;s callback. The answer is that (a) this mechanism displays the popup directly beneath the triggering button using <code>hToolGroup.showFigureDialog()</code>, and also (b) enables complex popups (dialogs) that have no direct builtin Matlab function (for example, a <a href="https://undocumentedmatlab.com/articles/uigetfile-uiputfile-customizations" target="_blank">file-selector with preview</a>, or a <a href="https://undocumentedmatlab.com/articles/using-spinners-in-matlab-gui" target="_blank">multi-component input form</a>).</p>
<h3 id="compatibility">Compatibility considerations for R2018a or older</h3>
<p>In Matlab releases R2018a or older that do not have the <i>hToolGroup.showFigureDialog()</i> function, you can create it yourself in a separate <i>showFigureDialog.m</i> file, as follows:</p>
<pre lang="matlab">
function showFigureDialog(hToolGroup, hFig, hAnchor)
    %   showFigureDialog - Display a figure-based dialog below a toolstrip control.
    %
    %   Usage example:
    %       showFigureDialog(hToolGroup, hFig, hAnchor);
    %   where:
    %       "hToolGroup" must be a "matlab.ui.internal.desktop.ToolGroup" handle
    %       "hFig" must be a "figure" handle, not a "uifigure"
    %       "hAnchor" must be a "matlab.ui.internal.toolstrip.***" handle
    %hWarn = ctrlMsgUtils.SuspendWarnings('MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame'); %#ok<nasgu>
    hWarn = warning('off','MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame');
    jf = get(hFig, 'JavaFrame');
    if isempty(jf)
        error('UI figure cannot be added to "ToolGroup". Use a regular figure instead.')
    else
        screen_size = get(0,'ScreenSize');
        old_pos = get(hFig,'OuterPosition');
        dpi_ratio = com.mathworks.util.ResolutionUtils.scaleSize(100)/100;
        jAnchor = hToolGroup.ToolstripSwingService.Registry.getWidgetById(hAnchor.getId());
        pt = javaMethodEDT('getLocationOnScreen',jAnchor); % pt is anchor top left
        pt.y = pt.y + jAnchor.getVisibleRect().height;     % pt is anchor bottom left
        new_x = pt.getX()/dpi_ratio-5;                           % figure outer left
        new_y = screen_size(end)-(pt.getY/dpi_ratio+old_pos(4)); % figure outer bottom
        hFig.OuterPosition = [new_x new_y old_pos(3) old_pos(4)];
        hFig.Visible = 'on';
    end
    warning(hWarn);
end
</pre>
<h3 id="showFigureDialog">Under the hood of <i>showFigureDialog()</i></h3>
<p>How does <i>showFigureDialog()</i> know where to place the figure, directly beneath the triggering toolstrip anchor?<br />
The answer is really quite simple, if you look at this method&#8217;s source-code in <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+desktop/ToolGroup.m</i> (around line 500, depending on the Matlab release).<br />
The function first checks whether the input <code>hFig</code> handle belongs to a figure or uifigure, and issues an error message in case it&#8217;s a uifigures (only legacy figures are currently supported).<br />
Then the function fetches the toolstrip control&#8217;s underlying Java control handle using the following code (slightly modified for clarity), <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization#Java" target="_blank">as explained here</a>:</p>
<pre lang="matlab">jAnchor = hToolGroup.ToolstripSwingService.Registry.getWidgetById(hAnchor.getId());</pre>
<p>Next, it uses the Java control&#8217;s <i>getLocationOnScreen()</i> to get the control&#8217;s onscreen position, accounting for monitor DPI variation that affects the X location.<br />
The figure&#8217;s <b>OuterPosition</b> property is then set so that the figure&#8217;s top-left corner is exactly next to the control&#8217;s bottom-left corner.<br />
Finally, the figure&#8217;s <b>Visible</b> property is set to &#8216;on&#8217; to make the figure visible in its new position.<br />
The popup figure&#8217;s location is recomputed by <i>showFigureDialog()</i> whenever the toolstrip control is clicked, so the popup figure is presented in the expected position even when you move or resize the tool-group window.</p>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The following post(s) will present the Toolpack framework. Non-figure (lightweight) popup toolpack panels can be created, which appear more polished/stylish than the popup figures that I presented today. The drawdown is that toolpack panels may be somewhat more complex to program than figures, and IMHO are more likely to change across Matlab releases. In addition to the benefit of popup toolpack panels, toolpack presents an alternative way for toolstrip creation and customization, enabling programmers to choose between using the toolstrip framework (that I discussed so far), and the new toolpack framework.<br />
In a succeeding post, I&#8217;ll discuss toolstrip collapsibility, i.e. what happens when the user resizes the window, reducing the toolstrip width. Certain toolstrip controls will drop their labels, and toolstrip sections shrink into a drop-down. The priority of control/section collapsibility can be controlled, so that less-important controls will collapse before more-important ones.<br />
In future posts, I plan to discuss docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order.<br />
Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures">Matlab toolstrip – part 9 (popup figures)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 8 (galleries)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-8-galleries</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 03 Feb 2019 17:00:55 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8321</guid>

					<description><![CDATA[<p>Matlab toolstrips can contain customizable gallery panels of items. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries">Matlab toolstrip – part 8 (galleries)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">previous posts</a> I showed how we can create custom Matlab app toolstrips using various controls (buttons, checkboxes, drop-downs, lists etc.). Today I will show how we can incorporate gallery panels into our Matlab toolstrip.<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Gallery.png" alt="Toolstrip Gallery (in-line &#038; drop-down)" title="Toolstrip Gallery (in-line &#038; drop-down)" width="90%" style="max-width:747px;" height="102" /></center><br />
Toolstrips can be a bit complex to develop so I&#8217;m proceeding slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in the <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">Toolstrip miniseries</a> before reading this post.<br />
<span id="more-8321"></span><br />
Also, remember to add the following code snippet at the beginning of your code so that the relevant toolstrip classes will be recognized by Matlab:</p>
<pre lang="matlab">import matlab.ui.internal.toolstrip.*</pre>
<h3 id="components">Gallery sub-components</h3>
<p><figure style="width: 300px" class="wp-caption alignright"><img fetchpriority="high" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Gallery_hierarchy.png" alt="Toolstrip gallery popup components" title="Toolstrip gallery popup components" width="300" height="205" /><figcaption class="wp-caption-text">Toolstrip gallery popup components</figcaption></figure> Toolstrip galleries are panels of buttons (typically large icons with an attached text label), which are grouped in &#8220;categories&#8221;. The gallery content can be presented either in-line within the toolstrip (a <code>Gallery</code> control), or as a drop-down button&#8217;s popup panel (a <code>DropDownGalleryButton</code> control). In either case, the displayed popup panel is a <code>GalleryPopup</code> object, that is composed of one or more <code>GalleryCategory</code>, each of which has one or more <code>GalleryItem</code> (push-button) and/or <code>ToggleGalleryItem</code> (toggle-button).</p>
<ul>
<li><code>Gallery</code> or <code>DropDownGalleryButton</code>
<ul>
<li><code>GalleryPopup</code>
<ul>
<li><code>GalleryCategory</code>
<ul>
<li><code>GalleryItem</code> or <code>ToggleGalleryItem</code></li>
<li><code>GalleryItem</code> or <code>ToggleGalleryItem</code></li>
<li>&#8230;</li>
</ul>
</li>
<li><code>GalleryCategory</code></li>
<li>&#8230;</li>
</ul>
</li>
</ul>
</ul>
<p><!-- center>[caption id="" align="aligncenter" width="492" caption="Toolstrip gallery popup components"]<img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Gallery_hierarchy.png" alt="Toolstrip gallery popup components" title="Toolstrip gallery popup components" width="492" height="337" />[/caption]</center --><br />
For a demonstration of toolstrip Galleries, see the code files in <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+desktop/</i>, specifically <i>showcaseToolGroup.m</i> and <i>showcaseBuildTab_Gallery.m</i>.</p>
<h3 id="GalleryPopup">GalleryPopup</h3>
<p>We first create the <code>GalleryPopup</code> object, then add to it a few <code>GalleryCategory</code> groups of <code>GalleryItem</code>, <code>ToggleGalleryItem</code> buttons. In the example below, we use a <code>ButtonGroup</code> to ensure that only a single <code>ToggleGalleryItem</code> button is selected:</p>
<pre lang="matlab">
import matlab.ui.internal.toolstrip.*
popup = GalleryPopup('ShowSelection',true);
% Create gallery categories
cat1 = GalleryCategory('CATEGORY #1 SINGLE'); popup.add(cat1);
cat2 = GalleryCategory('CATEGORY #2 SINGLE'); popup.add(cat2);
cat3 = GalleryCategory('CATEGORY #3 SINGLE'); popup.add(cat3);
% Create a button-group to control item selectability
group = matlab.ui.internal.toolstrip.ButtonGroup;
% Add items to the gallery categories
fpath = [fullfile(matlabroot,'toolbox','matlab','toolstrip','web','image') filesep];  % icons path
item1 = ToggleGalleryItem('Biology', Icon([fpath 'biology_app_24.png']), group);
item1.Description = 'Select the Biology gizmo';
item1.ItemPushedFcn = @(x,y) ItemPushedCallback(x,y);
cat1.add(item1);
item2 = ToggleGalleryItem('Code Generation', Icon([fpath 'code_gen_app_24.png']), group);
cat1.add(item2);
item3 = ToggleGalleryItem('Control', Icon([fpath 'control_app_24.png']), group);
cat1.add(item3);
item4 = ToggleGalleryItem('Database', Icon([fpath 'database_app_24.png']), group);
cat1.add(item4);
...
</pre>
<p><center><figure style="width: 461px" class="wp-caption aligncenter"><img decoding="async" src="https://undocumentedmatlab.com/images/GalleryPopup_single_icon_view.png" alt="Single-selection GalleryPopup (icon view)" title="Single-selection GalleryPopup (icon view)" width="461" height="337" /><figcaption class="wp-caption-text">Single-selection GalleryPopup (icon view)</figcaption></figure></center><br />
Note that in a real-world situation, we&#8217;d assign a <b>Description</b>, <b>Tag</b> and <b>ItemPushedFcn</b> to all gallery items. This was elided from the code snippet above for readability, but should be part of any actual GUI. The <b>Description</b> only appears as tooltip popup in icon-view (shown above), but appears as a visible label in list-view (see below).</p>
<h3 id="items">Gallery items selection: push-button action, single-selection toggle, multiple selection toggle</h3>
<p>If we use <code>ToggleGalleryItem</code> without a <code>ButtonGroup</code>, multiple gallery items can be selected, rather than just a single selection as shown above:</p>
<pre lang="matlab" highlight="2,7,10,13">
...
item1 = ToggleGalleryItem('Biology', Icon([fpath 'biology_app_24.png']));
item1.Description = 'Select the Biology gizmo';
item1.ItemPushedFcn = @(x,y) ItemPushedCallback(x,y);
cat1.add(item1);
item2 = ToggleGalleryItem('Code Generation', Icon([fpath 'code_gen_app_24.png']));
cat1.add(item2);
item3 = ToggleGalleryItem('Control', Icon([fpath 'control_app_24.png']));
cat1.add(item3);
item4 = ToggleGalleryItem('Database', Icon([fpath 'database_app_24.png']));
cat1.add(item4);
...
</pre>
<p><center><figure style="width: 460px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/GalleryPopup_multiple_icon_view.png" alt="Multiple-selection GalleryPopup (icon view)" title="Multiple-selection GalleryPopup (icon view)" width="460" height="337" /><figcaption class="wp-caption-text">Multiple-selection GalleryPopup (icon view)</figcaption></figure></center><br />
Alternatively, if we use <code>GalleryItem</code> instead of <code>ToggleGalleryItem</code>, the gallery items would be push-buttons rather than toggle-buttons. This enables us to present a gallery of single-action state-less push-buttons, rather than state-full toggle-buttons. The ability to customize the gallery items as either state-less push-buttons or single/multiple toggle-buttons supports a wide range of application use-cases.</p>
<h3 id="customizing">Customizing the GalleryPopup</h3>
<p>Properties that affect the <code>GalleryPopup</code> appearance are:</p>
<ul>
<li><b>DisplayState</b> &#8211; initial display mode of gallery items (string; default=&#8217;icon_view&#8217;, valid values: &#8216;icon_view&#8217;,&#8217;list_view&#8217;)</li>
<li><b>GalleryItemRowCount</b> &#8211; number of rows used in the display of the in-line gallery (integer; default=1, valid values: 0,1,2). A Value of 2 should typically be used with a small icon and <b>GalleryItemWidth</b> (see below)</li>
<li><b>GalleryItemTextLineCount</b> &#8211; number of rows used for display of the item label (integer; default=2, valid values: 0,1,2)</li>
<li><b>ShowSelection</b> &#8211; whether or not to display the last-selected item (logical; default=false). Needs to be true for <code>Gallery</code> and false for <code>DropDownGalleryButton</code>.</li>
<li><b>GalleryItemWidth</b> &#8211; number of pixels to allocate for each gallery item (integer, hidden; default=80)</li>
<li><b>FavoritesEnabled</b> &#8211; whether or not to enable a &#8220;Favorites&#8221; category (logical, hidden; default=false)</li>
</ul>
<p>All of these properties are defined as private in the <code>GalleryPopup</code> class, and can only be specified during the class object&#8217;s construction. For example, instead of the default icon-view, we can display the gallery items as a list, by setting the <code>GalleryPopup</code>&#8216;s <b>DisplayState</b> property to <code>'list_view'</code> during construction:</p>
<pre lang="matlab">
popup = GalleryPopup('DisplayState','list_view');
</pre>
<p><center><figure style="width: 460px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/GalleryPopup_single_list_view.png" alt="GalleryPopup (list view)" title="GalleryPopup (list view)" width="460" height="415" /><figcaption class="wp-caption-text">GalleryPopup (list view)</figcaption></figure></center><br />
Switching from icon-view to list-view and back can also be done by clicking the corresponding icon near the popup&#8217;s top-right corner (next to the interactive search-box).</p>
<h3 id="Gallery">Gallery and DropDownGalleryButton</h3>
<p>Now that we have prepared <code>GalleryPopup</code>, let&#8217;s integrate it in our toolstrip.<br />
We have two choices &#8212; either in-line within the toolstrip section (using <code>Gallery</code>), or as a compact drop-down button (using <code>DropDownGalleryButton</code>):</p>
<pre lang="matlab">
% Inline gallery
section = hTab.addSection('Multiple Selection Gallery');
column = section.addColumn();
popup = GalleryPopup('ShowSelection',true);
% add the GalleryPopup creation code above
gallery = Gallery(popup, 'MinColumnCount',2, 'MaxColumnCount',4);
column.add(gallery);
% Drop-down gallery
section = hTab.addSection('Drop Down Gallery');
column = section.addColumn();
popup = GalleryPopup();
% add the GalleryPopup creation code above
button = DropDownGalleryButton(popup, 'Examples', Icon.MATLAB_24);
button.MinColumnCount = 5;
column.add(button);
</pre>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Gallery.png" alt="Toolstrip Gallery (in-line &#038; drop-down)" title="Toolstrip Gallery (in-line &#038; drop-down)" width="90%" style="max-width:747px;" height="102" /></center><br />
Clicking any of the drop-down (arrow) widgets will display the associated <code>GalleryPopup</code>.<br />
The <code>Gallery</code> and <code>DropDownGalleryButton</code> objects have several useful settable properties:</p>
<ul>
<li><b>Popup</b> &#8211; a <code>GalleryPopup</code> object handle, which is displayed when the user clicks the drop-down (arrow) widget. Only settable in the constructor, not after object creation.</li>
<li><b>MinColumnCount</b> &#8211; minimum number of item columns to display (integer; default=1). In <code>Gallery</code>, this property is only settable in the constructor, not after object creation; if not enough width is available to display these columns, the control collapses into a drop-down. In <code>DropDownGalleryButton</code>, this property can be set even after object creation (despite incorrect internal documentation), and controls the width of the popup panel.</li>
<li><b>MaxColumnCount</b> &#8211; maximal number of items columns to display (integer; default=10). In <code>Gallery</code>, this property is only settable in the constructor, not after object creation. In <code>DropDownGalleryButton</code>, this property can be set even after object creation but in any case seems to have no visible effect.</li>
<li><b>Description</b> &#8211; tooltip text displayed when the mouse hovers over the <code>Gallery</code> area (outside the area of the internal gallery items, which have their own individual Descriptions), or over the <code>DropDownGalleryButton</code> control.</li>
<li><b>TextOverlay</b> &#8211; a semi-transparent text label overlaid on top of the gallery panel (string, default=&#8221;). Only available in <code>Gallery</code>, not <code>DropDownGalleryButton</code>.</li>
</ul>
<p>For example:</p>
<pre lang="matlab">
gallery = Gallery(popup, 'MinColumnCount',2, 'MaxColumnCount',4);
gallery.TextOverlay = 'Select from these items';
</pre>
<p><center><figure style="width: 310px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_TextOverlay.png" alt="Effect of TextOverlay" title="Effect of TextOverlay" width="310" height="74" /><figcaption class="wp-caption-text">Effect of TextOverlay</figcaption></figure></center></p>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The next post will discuss popup forms. These are similar in concept to galleries, in the sense that when we click the drop-down widget a custom popup panel is displayed. In the case of a popup form, this is a fully-customizable Matlab GUI figure.<br />
Following that, I plan to discuss toolstrip collapsibility, the Toolpack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order.<br />
Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries">Matlab toolstrip – part 8 (galleries)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries/feed</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 7 (selection controls)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-7-selection-controls</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 27 Jan 2019 17:00:34 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8257</guid>

					<description><![CDATA[<p>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls">Matlab toolstrip – part 7 (selection controls)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">previous posts</a> I showed how we can create custom Matlab app toolstrips using controls such as buttons, checkboxes, sliders and spinners. Today I will show how we can incorporate even more complex selection controls into our toolstrip: lists, drop-downs, popups etc.<br />
<center><figure style="width: 371px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_SplitButton.png" alt="Toolstrip SplitButton with dynamic popup and static sub-menu" title="Toolstrip SplitButton with dynamic popup and static sub-menu" width="371" height="184" /><figcaption class="wp-caption-text">Toolstrip SplitButton with dynamic popup and static sub-menu</figcaption></figure></center><br />
Toolstrips can be a bit complex to develop so I&#8217;m proceeding slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in the <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">Toolstrip miniseries</a> before reading this post.<br />
<span id="more-8257"></span><br />
Also, remember to add the following code snippet at the beginning of your code so that the relevant toolstrip classes will be recognized by Matlab:</p>
<pre lang="matlab">import matlab.ui.internal.toolstrip.*</pre>
<p>There are 4 types of popups in toolstrip controls:</p>
<ol>
<li>Builtin dropdown (combo-box) selector similar to the familiar <i><b>uicontrol</b>(&#8216;style&#8217;,&#8217;popup&#8217;,&#8230;)</i>. In toolstrips, this is implemented using the <code>DropDown</code> control.</li>
<li>A more complex dropdown selector having icons and tooltips, implemented using the <code>DropDownButton</code> and <code>SplitButton</code> toolstrip controls.</li>
<li>An even-more complex drop-down selector, which presents a gallery of options. This will be discussed in detail in the next post.</li>
<li>A fully-customizable form panel (&#8220;popup form&#8221;). This will be discussed separately, in the following post.</li>
</ol>
<h3 id="DropDown">DropDown</h3>
<p>The simple <code>DropDown</code> toolstrip control is very easy to set up and use:</p>
<pre lang="matlab">
hPopup = DropDown({'Label1';'Label2';'Label3'});
hPopup.Value = 'Label3';
hPopup.ValueChangedFcn = @ValueChangedCallback;
</pre>
<p><center><figure style="width: 100px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_DropDown.png" alt="Toolstrip DropDown" title="Toolstrip DropDown" width="56" height="64" /><figcaption class="wp-caption-text">Toolstrip DropDown</figcaption></figure></center> Note that the drop-down items (labels) need to be specified as a column cell-array (i.e. {a;b;c}) &#8211; a row cell-array ({a,b,c}) will result in run-time error.<br />
We can have the control hold a different value for each of the displayed labels, by specifying the input items as an Nx2 cell-array:</p>
<pre lang="matlab">
items = {'One',   'Label1'; ...
         'Two',   'Label2'; ...
         'Three', 'Label3'}
hPopup = DropDown(items);
hPopup.Value = 'Two';
hPopup.ValueChangedFcn = @ValueChangedCallback;
</pre>
<p>This drop-down control will display the labels &#8220;Label1&#8221;, &#8220;Label2&#8221; (initially selected), and &#8220;Label3&#8221;. Whenever the selected drop-down item is changed, the corresponding popup <b>Value</b> will change to the corresponding value. For example, when &#8220;Label3&#8221; is selected in the drop-down, <code>hPopup.Value</code> will change to &#8216;Three&#8217;.<br />
Another useful feature of the toolstrip <code>DropDown</code> control is the <b>Editable</b> property (logical true/false, default=false), which enables the user to modify the entry in the drop-down&#8217;s editbox. Any custom text entered within the editbox will update the control&#8217;s <b>Value</b> property to that string.</p>
<h3 id="ListBox">ListBox</h3>
<p>We can create a <code>ListBox</code> in a very similarly manner to <code>DropDown</code>. For example, the following code snippet creates a list-box that spans the entire toolstrip column height and has 2 of its items initially selected:</p>
<pre lang="matlab">
hColumn = hSection.addColumn('Width',100);
allowMultiSelection = true;
items = {'One','Label1'; 'Two','Label2'; 'Three','Label3'; 'Four','Label4'; 'Five','Label5'};
hListBox = ListBox(items, allowMultiSelection);
hListBox.Value = {'One'; 'Three'};
hListBox.ValueChangedFcn = @ValueChangedCallback;
hColumn.add(hListBox);
</pre>
<p><center><figure style="width: 102px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_ListBox.png" alt="Toolstrip ListBox (multi-selection)" title="Toolstrip ListBox (multi-selection)" width="102" height="75" /><figcaption class="wp-caption-text">Toolstrip ListBox (multi-selection)</figcaption></figure></center><br />
The <code>DropDown</code> and <code>ListBox</code> controls are nearly identical in terms of their properties, methods and events/callbacks, with the following notable exceptions:</p>
<ul>
<li><code>ListBox</code> controls do not have an <b>Editable</b> property</li>
<li><code>ListBox</code> controls have a <b>MultiSelect</b> property (logical, default=false), which <code>DropDown</code>s do not have. Note that this property can only be set during the <code>ListBox</code>&#8216;s creation, as shown in the code snippet above.</li>
</ul>
<h3 id="PopupList">DropDownButton and SplitButton</h3>
<p>A more elaborate drop-down selector can be created using the <code>DropDownButton</code> and <code>SplitButton</code> toolstrip controls. For such controls, we create a <code>PopupList</code> object, and add elements to it, which could be any of the following, in whichever order that you wish:</p>
<ol>
<li><code>PopupListHeader</code> &#8211; a section header (title), non-selectable</li>
<li><code>ListItem</code> &#8211; a selectable list item, with optional <b>Icon</b>, <b>Text</b>, and <b>Description</b> (tooltip string, which for some reason [probably a bug] is not actually shown). For some reason (perhaps a bug), the Description is not shown in a tooltip (no tooltip is displayed). However, it is displayed as a label beneath the list-item&#8217;s main label, unless we set <b>ShowDescription</b> to false.</li>
<li><code>ListItemWithCheckBox</code> &#8211; a selectable list item that toggles a checkmark icon based on the list item&#8217;s selection <b>Value</b> (on/off). The checkmark icon is not customizable (alas).</li>
<li><code>ListItemWithPopup</code> &#8211; a non-selectable list item, that displays a sub-menu (another <code>PopupList</code> that should be set to the parent list-item&#8217;s <b>Popup</b> property).</li>
</ol>
<p>A simple usage example (adapted from the <code>showcaseToolGroup</code> demo):<br />
<figure style="width: 303px" class="wp-caption alignright"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_PopupList.png" alt="Toolstrip PopupList" title="Toolstrip PopupList" width="303" height="289" /><figcaption class="wp-caption-text">Toolstrip PopupList</figcaption></figure></p>
<pre lang="matlab">
function hPopup = createPopup()
    import matlab.ui.internal.toolstrip.*
    hPopup = PopupList();
    % list header #1
    header = PopupListHeader('List Items');
    hPopup.add(header);
    % list item #1
    item = ListItem('This is item 1', Icon.MATLAB_16);
    item.Description = 'this is the description for item #1';
    item.ShowDescription = true;
    item.ItemPushedFcn = @ActionPerformedCallback;
    hPopup.add(item);
    % list item #2
    item = ListItem('This is item 2', Icon.SIMULINK_16);
    item.Description = 'this is the description for item #2';
    item.ShowDescription = false;
    addlistener(item, 'ItemPushed', @ActionPerformedCallback);
    hPopup.add(item);
    % list header #2
    header = PopupListHeader('List Item with Checkboxes');
    hPopup.add(header);
    % list item with checkbox
    item = ListItemWithCheckBox('This is item 3', true);
    item.ValueChangedFcn = @PropertyChangedCallback;
    hPopup.add(item);
    % list item with popup
    item = ListItemWithPopup('This is item 4',Icon.ADD_16);
    item.ShowDescription = false;
    hPopup.add(item);
    % Sub-popup
    hSubPopup = PopupList();
    item.Popup = hSubPopup;
    % sub list item #1
    sub_item1 = ListItem('This is sub item 1', Icon.MATLAB_16);
    sub_item1.ShowDescription = false;
    sub_item1.ItemPushedFcn = @ActionPerformedCallback;
    hSubPopup.add(sub_item1);
    % sub list item #2
    sub_item2 = ListItem('This is sub item 2', Icon.SIMULINK_16);
    sub_item2.ShowDescription = false;
    sub_item2.ItemPushedFcn = @ActionPerformedCallback;
    hSubPopup.add(sub_item2);
end  % createPopup()
</pre>
<p>We now have two alternatives for attaching this popup to the <code>DropDownButton</code> or <code>SplitButton</code>: <figure style="width: 371px" class="wp-caption alignright"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_SplitButton.png" alt="Toolstrip SplitButton with dynamic popup and static sub-menu" title="Toolstrip SplitButton with dynamic popup and static sub-menu" width="371" height="184" /><figcaption class="wp-caption-text">Toolstrip SplitButton with dynamic popup and static sub-menu</figcaption></figure></p>
<ul>
<li><i>Static popup</i> &#8211; set the <b>Popup</b> property of the button or <code>ListItemWithPopup</code> to the popup-creation function (or <code>hPopup</code>). The popup will be created once and will remain unchanged throughout the program execution. For example:
<pre lang="matlab">
hButton = DropDownButton('Vertical', Icon.OPEN_24);
hButton.Popup = createPopup();
</pre>
</li>
<li><i>Dynamic popup</i> &#8211; set the <b>DynamicPopupFcn</b> of the button or <code>ListItemWithPopup</code> to the popup creation function. This function will be invoked separately whenever the user clicks on the drop-down selector widget. Inside our popup-creation function we can have state-dependent code that modifies the displayed list items depending on the state of our program/environment. For example:
<pre lang="matlab">
hButton = SplitButton('Vertical', Icon.OPEN_24);
hButton.ButtonPushedFcn = @ActionPerformedCallback;  % invoked when user clicks the main split-button part
hButton.DynamicPopupFcn = @(h,e) createPopup();      % invoked when user clicks the drop-down selector widget
</pre>
</li>
</ul>
<p><!-- center>[caption id="" align="aligncenter" width="371" caption="Toolstrip SplitButton with dynamic popup and static sub-menu"]<img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_SplitButton.png" alt="Toolstrip SplitButton with dynamic popup and static sub-menu" title="Toolstrip SplitButton with dynamic popup and static sub-menu" width="371" height="184" />[/caption]</center --><br />
<code>DropDownButton</code> and <code>SplitButton</code> are exactly the same as far as the popup-list is concerned: If it is set via the <b>Popup</b> property then the popup is static (in the sense that it is only evaluated once, when created), and if it is set via <b>DynamicPopupFcn</b> then the popup is dynamic (re-created before display). The only difference between <code>DropDownButton</code> and <code>SplitButton</code> is that in addition to the drop-down control, a <code>SplitButton</code> also includes a regular push-button control (with its corresponding <b>ButtonPushedFcn</b> callback).<br />
In summary:</p>
<ul>
<li>If <b>DynamicPopupFcn</b> is set to a function handle, then the <code>PopupList</code> that is returned by that function will be re-evaluated and displayed whenever the user clicks the main button of a <code>DropDownButton</code> or the down-arrow part of a <code>SplitButton</code>. This happens even if the <b>Popup</b> property is also set i.e., <b>DynamicPopupFcn</b> has precedence over <b>Popup</b>; when both of them are set, <b>Popup</b> is silently ignored (it would be useful for Matlab to display a warning in such cases, hopefully in a future release).</li>
<li>If <b>DynamicPopupFcn</b> is not set but <b>Popup</b> is (to a <code>PopupList</code> object handle), then this <code>PopupList</code> will be computed only once (when first created) and then it will be displayed whenever the user clicks the main button of a <code>DropDownButton</code> or the down-arrow part of a <code>SplitButton</code>.</li>
<li>Separately from the above, if a <code>SplitButton</code>&#8216;s <b>ButtonPushedFcn</b> property is set to a function handle, then that function will be evaluated whenever the user clicks the main button of the <code>SplitButton</code>. No popup is presented, unless of course the callback function displays a popup programmatically. Note that <b>ButtonPushedFcn</b> is a property of <code>SplitButton</code>; this property does not exist in a <code>DropDownButton</code>. </li>
</ul>
<p>Important note: whereas <code>DropDown</code> and <code>ListBox</code> have a <b>ValueChangedFcn</b> callback that is invoked whenever the drop-down/listbox <b>Value</b> has changed, the callback mechanism is very different with <code>DropDownButton</code> and <code>SplitButton</code>: here, each menu item has its own individual callback that is invoked when that item is selected (clicked): <b>ItemPushedFcn</b> for <code>ListItem</code>; <b>ValueChangedFcn</b> for <code>ListItemWithCheckBox</code>; and <b>DynamicPopupFcn</b> for <code>ListItemWithPopup</code>. As we shall see later, the same is true for gallery items &#8211; each item has its own separate callback.</p>
<h3 id="Galleries">Galleries</h3>
<p>Toolstrip galleries are panels of buttons (typically large icons with an attached text label), which are grouped in &#8220;categories&#8221;.<br />
The general idea is to first create the <code>GalleryPopup</code> object, then add to it a few <code>GalleryCategory</code> groups, each consisting of <code>GalleryItem</code> (push-buttons) and/or <code>ToggleGalleryItem</code> (toggle-buttons) objects. Once this <code>GalleryPopup</code> is created, we can either integrate it in-line within the toolstrip section (using <code>Gallery</code>), or as a compact drop-down button (using <code>DropDownGalleryButton</code>):</p>
<pre lang="matlab">
% Inline gallery
section = hTab.addSection('Multiple Selection Gallery');
column = section.addColumn();
popup = GalleryPopup('ShowSelection',true);
% add the GalleryPopup creation code (see next week's post)
gallery = Gallery(popup, 'MaxColumnCount',4, 'MinColumnCount',2);
column.add(gallery);
% Drop-down gallery
section = hTab.addSection('Drop Down Gallery');
column = section.addColumn();
popup = GalleryPopup();
% add the GalleryPopup creation code (see next week's post)
button = DropDownGalleryButton(popup, 'Examples', Icon.MATLAB_24);
button.MinColumnCount = 5;
column.add(button);
</pre>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Gallery.png" alt="Toolstrip Gallery (in-line &#038; drop-down)" title="Toolstrip Gallery (in-line &#038; drop-down)" width="90%" style="max-width:747px;" height="102" /></center><br />
I initially planned to include all the relevant Gallery discussion here, but it turned out to require so much space that I decided to devote a separate article for it &#8212; this will be the topic of next week&#8217;s blog post.</p>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The next post will discuss Galleries in depth, followed by popup forms.<br />
Following that, I plan to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order.<br />
Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls">Matlab toolstrip – part 7 (selection controls)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls/feed</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 6 (complex controls)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-6-complex-controls</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 21 Jan 2019 16:00:38 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8235</guid>

					<description><![CDATA[<p>Multiple types of customizable controls can be added to Matlab toolstrips</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls">Matlab toolstrip – part 6 (complex controls)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">previous posts</a> I showed how we can create custom Matlab app toolstrips using simple controls such as buttons and checkboxes. Today I will show how we can incorporate more complex controls into our toolstrip: button groups, edit-boxes, spinners, sliders etc.<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Controls.png" alt="Some custom Toolstrip Controls" title="Some custom Toolstrip Controls" width="80%" style="max-width:727px;"/></center><br />
Toolstrips can be a bit complex to develop so I’m proceeding slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in the <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">Toolstrip miniseries</a> before reading this post.<br />
<span id="more-8235"></span><br />
The first place to search for potential toostrip components/controls is in <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1" target="_blank">Matlab&#8217;s built-in toolstrip demos</a>. The <code>showcaseToolGroup</code> demo displays a large selection of generic components grouped by function. These controls&#8217; callbacks do little less than simply output a text message in the Matlab console. On the other hand, the <code>showcaseMPCDesigner</code> demo shows a working demo with controls that interact with some docked figures and their plot axes. The combination of these demos should provide plenty of ideas for your own toolstrip implementation. Their m-file source code is available in the <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+desktop/</i> folder. To see the available toolstrip controls in action and how they could be integrated, refer to the source-code of these two demos.<br />
All toolstrip controls are defined by classes in the <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+toolstrip/</i> folder and use the <code>matlab.ui.internal.toolstrip</code> package prefix, for example:</p>
<pre lang="matlab">
% Alternative 1:
hButton = matlab.ui.internal.toolstrip.Button;
% Alternative 2:
import matlab.ui.internal.toolstrip.*
hButton = Button;
</pre>
<p>For the remainder of today&#8217;s post it is assumed that you are using one of these two alternatives whenever you access any of the toolstrip classes.</p>
<h3 id="controls">Top-level toolstrip controls</h3>
<table>
<tbody>
<tr>
<th>Control</th>
<th>Description</th>
<th>Important properties</th>
<th>Callbacks</th>
<th>Events</th>
</tr>
<tr>
<td><code>EmptyControl</code></td>
<td>Placeholder (filler) in container column</td>
<td>(none)</td>
<td>(none)</td>
<td>(none)</td>
</tr>
<tr>
<td><code>Label</code></td>
<td>Simple text label (no action)</td>
<td><b>Icon</b>, <b>Text</b> (string)</td>
<td>(none)</td>
<td>(none)</td>
</tr>
<tr>
<td><code>Button</code></td>
<td>Push-button</td>
<td><b>Icon</b>, <b>Text</b> (string)</td>
<td><b>ButtonPushedFcn</b></td>
<td>ButtonPushed</td>
</tr>
<tr>
<td><code>ToggleButton</code></td>
<td>Toggle (on/off) button</td>
<td><b>Icon</b>, <b>Text</b> (string), <b>Value</b> (logical true/false), <b>ButtonGroup</b> (a <code>ButtonGroup</code> object)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged</td>
</tr>
<tr>
<td><code>RadioButton</code></td>
<td>Radio-button (on/off)</td>
<td><b>Text</b> (string), <b>Value</b> (logical true/false), <b>ButtonGroup</b> (a <code>ButtonGroup</code> object)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged</td>
</tr>
<tr>
<td><code>CheckBox</code></td>
<td>Check-box (on/off)</td>
<td><b>Text</b> (string), <b>Value</b> (logical true/false)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged</td>
</tr>
<tr>
<td><code>EditField</code></td>
<td>Single-line editbox</td>
<td><b>Value</b> (string)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged, FocusGained, FocusLost</td>
</tr>
<tr>
<td><code>TextArea</code></td>
<td>Multi-line editbox</td>
<td><b>Value</b> (string)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged, FocusGained, FocusLost</td>
</tr>
<tr>
<td><code>Spinner</code></td>
<td>A numerical spinner control of values between min,max</td>
<td><b>Limits</b> ([min,max]), <b>StepSize</b> (integer), <b>NumberFormat</b> (&#8216;integer&#8217; or &#8216;double&#8217;), <b>DecimalFormat</b> (string), <b>Value</b> (numeric)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged, ValueChanging</td>
</tr>
<tr>
<td><code>Slider</code></td>
<td>A horizontal slider of values between min,max</td>
<td><b>Limits</b> ([min,max]), <b>Labels</b> (cell-array), <b>Ticks</b> (integer), <b>UseSmallFont</b> (logical true/false, R2018b onward), <b>ShowButton</b> (logical true/false, undocumented), <b>Steps</b> (integer, undocumented), <b>Value</b> (numeric)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged, ValueChanging</td>
</tr>
<tr>
<td><code>ListBox</code></td>
<td>List-box selector with multiple items</td>
<td><b>Items</b> (cell-array), <b>SelectedIndex</b> (integer), <b>MultiSelect</b> (logical true/false), <b>Value</b> (cell-array of strings)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged</td>
</tr>
<tr>
<td><code>DropDown</code></td>
<td>Single-selection drop-down (combo-box) selector</td>
<td><b>Items</b> (cell-array), <b>SelectedIndex</b> (integer), <b>Editable</b> (logical true/false), <b>Value</b> (string)</td>
<td><b>ValueChangedFcn</b></td>
<td>ValueChanged</td>
</tr>
<tr>
<td><code>DropDownButton</code></td>
<td>Button that has an associated drop-down selector</td>
<td><b>Icon</b>, <b>Text</b> (string), <b>Popup</b> (a <code>PopupList</code> object)</td>
<td><b>DynamicPopupFcn</b></td>
<td>(none)</td>
</tr>
<tr>
<td><code>SplitButton</code></td>
<td>Split button: main clickable part next to a drop-down selector</td>
<td><b>Icon</b>, <b>Text</b> (string), <b>Popup</b> (a <code>PopupList</code> object)</td>
<td><b>ButtonPushedFcn</b>, <b>DynamicPopupFcn</b></td>
<td>ButtonPushed, DropDownPerformed (undocumented)</td>
</tr>
<tr>
<td><code>Gallery</code></td>
<td>A gallery of selectable options, displayed in-panel</td>
<td><b>MinColumnCount</b> (integer), <b>MaxColumnCount</b> (integer), <b>Popup</b> (a <code>GalleryPopup</code> object), <b>TextOverlay</b> (string)</td>
<td>(none)</td>
<td>(none)</td>
</tr>
<tr>
<td><code>DropDownGalleryButton</code></td>
<td>A gallery of selectable options, displayed as a drop-down</td>
<td><b>MinColumnCount</b> (integer), <b>MaxColumnCount</b> (integer), <b>Popup</b> (a <code>GalleryPopup</code> object), <b>TextOverlay</b> (string)</td>
<td>(none)</td>
<td>(none)</td>
</tr>
</tbody>
</table>
<p>In addition to the control properties listed in the table above, all toolstrip controls share some common properties:</p>
<ul>
<li><b>Description</b> &#8211; a string that is shown in a tooltip when you hover the mouse over the control</li>
<li><b>Enabled</b> &#8211; a logical value (default: true) that controls whether we can interact with the control. A disabled control is typically grayed-over. Note that the value is a logical true/false, not &#8216;on&#8217;/&#8217;off&#8217;</li>
<li><b>Tag</b> &#8211; a string that can be used to uniquely identify/locate the control via their container&#8217;s <i>find(tag)</i> and <i>findAll(tag)</i> methods. Can contain spaces and special symbols &#8211; does not need to be a valid Matlab identifier</li>
<li><b>Children</b> &#8211; contains a list of sub-component (if any); useful with complex controls</li>
<li><b>Parent</b> &#8211; the handle of the container that contains the control</li>
<li><b>Type</b> &#8211; the type of control, typically its class-name</li>
<li><b>Mnemonic</b> &#8211; an undocumented string property, currently unused (?)</li>
<li><b>Shortcut</b> &#8211; an undocumented string property, currently unused (?)</li>
</ul>
<p>The <code>EmptyControl</code>, <code>Button</code>, <code>ToggleButton</code> and <code>CheckBox</code> controls were discussed in an <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization#example" target="_blank">earlier post</a> of this miniseries. The bottom 6 selection controls (<code>ListBox</code>, <code>DropDown</code>, <code>DropDownButton</code>, <code>SplitButton</code>, <code>Gallery</code> and <code>DropDownGalleryButton</code>) will be discussed in the next post. The rest of the controls are described below.</p>
<h3 id="ButtonGroup">Button groups</h3>
<p>A ButtonGroup binds several <code>CheckBox</code> and <code>ToggleButton</code> components such that only one of them is selected (pressed) at any point in time. For example:</p>
<pre lang="matlab">
hSection = hTab.addSection('Radio-buttons');
hColumn = hSection.addColumn();
% Grouped RadioButton controls
hButtonGroup = ButtonGroup;
hRadio = RadioButton(hButtonGroup, 'Option choice #1');
hRadio.ValueChangedFcn = @ValueChangedCallback;
hColumn.add(hRadio);
hRadio = RadioButton(hButtonGroup, 'Option choice #2');
hRadio.ValueChangedFcn = @ValueChangedCallback;
hRadio.Value = true;
hColumn.add(hRadio);
</pre>
<p><center><figure style="width: 119px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_RadioButton.png" alt="Toolstrip ButtonGroup" title="Toolstrip ButtonGroup" width="119" height="82" /><figcaption class="wp-caption-text">Toolstrip ButtonGroup</figcaption></figure></center> Note that unlike the <i><b>uibuttongroup</b></i> object in &#8220;standard&#8221; figure GUI, the toolstrip&#8217;s <code>ButtonGroup</code> object does not have a SelectionChangedFcn callback property (or corresponding event). Instead, we need to set the <b>ValueChangedFcn</b> callback property (or listen to the ValueChanged event) separately for each individual control. This is really a shame &#8211; I think it would make good design sense to have a SelectionChangedFcn callback at the <code>ButtonGroup</code> level, as we do for <i><b>uibuttongroup</b></i> (in addition to the individual control callbacks).<br />
Also note that the internal documentation of <code>ButtonGroup</code> has an error &#8211; it provides an example usage with <code>RadioButton</code> that has its constructor inputs switched: the correct constructor is <code>RadioButton(hButtonGroup,labelStr)</code>. On the other hand, for <code>ToggleButton</code>, the hButtonGroup input is the [optional] 3rd input arg of the constructor: <code>ToggleButton(labelStr,Icon,hButtonGroup)</code>. I think that it would make much more sense for the <code>RadioButton</code> constructor to follow the documentation and the style of <code>ToggleButton</code> and make the hButtonGroup input the last (2nd, optional) input arg, rather than the 1st. In other words, it would make more sense for RadioButton(labelStr,hButtonGroup), but unfortunately this is currently not the case.</p>
<h3 id="Edit">Label, EditField and TextArea</h3>
<p>A <code>Label</code> control is a simple non-clickable text label with an optional <b>Icon</b>, whose text is controlled via the <b>Text</b> property. The label&#8217;s alignment is controlled by the containing column&#8217;s <b>HorizontalAlignment</b> property.<br />
An <code>EditField</code> is a single-line edit-box. Its string contents can be fetched/updated via the <b>Value</b> property, and when the user updates the edit-box contents the <b>ValueChangedFcn</b> callback is invoked (upon each modification of the string, i.e. every key-click). This is a pretty simple control actually.<br />
The <code>EditField</code> control has a hidden (undocumentented) settable property called <b>PlaceholderText</b>, which presumably aught to display a gray initial prompt within the editbox. However, as far as I could see this property has no effect (perhaps, as the name implies, it is a place-holder for a future functionality&#8230;).<br />
A <code>TextArea</code> is another edit-box control, but enables entering multiple lines of text, unlike <code>EditField</code> which is a single-line edit-box. <code>TextArea</code> too is a very simple control, having a settable <b>Value</b> string property and a <b>ValueChangedFcn</b> callback. Whereas <code>EditField</code> controls, being single-line, would typically be included in 2- or 3-element toolstrip columns, the <code>TextArea</code> would typically be placed in a single-element column, so that it would span the entire column height.<br />
A peculiarity of toolstrip columns is that unless you specify their <b>Width</b> property, the internal controls are displayed with a minimal width (the width is only controllable at the column level, not the control-level). This is especially important with <code>EditField</code> and <code>TextArea</code> controls, which are often empty by default, causing their assigned width to be minimal (only a few pixels). This is corrected by setting their containing column&#8217;s <b>Width</b>:</p>
<pre lang="matlab">
% EditField controls
column1 = hSection.addColumn('HorizontalAlignment','right');
column1.add(Label('Yaba:'))
column1.add(Label('Daba doo:'))
column2 = hSection.addColumn('Width',70);
column2.add(EditField);
column2.add(EditField('Initial text'));
% TextArea control
column3 = hSection.addColumn('Width',90);
hEdit = TextArea;
hEdit.ValueChangedFcn = @ValueChangedCallback;
column3.add(hEdit);
</pre>
<p><center><figure style="width: 233px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Editboxes.png" alt="Toolstrip Label, EditField and TextArea" title="Toolstrip Label, EditField and TextArea" width="233" height="100" /><figcaption class="wp-caption-text">Toolstrip Label, EditField and TextArea</figcaption></figure></center></p>
<h3 id="Spinner">Spinner</h3>
<p><code>Spinner</code> is a single-line numeric editbox that has an attached side-widget where you can increase/decrease the editbox value by a specified amount, subject to predefined min/max values. If you try to enter an illegal value, Matlab will beep and the editbox will revert to its last acceptable value. You can only specify a <b>NumberFormat</b> of &#8216;integer&#8217; or &#8216;double&#8217; (default: &#8216;integer&#8217;) and a <b>DecimalFormat</b> which is a string composed of the number of sub-decimal digits to display and the format (&#8216;e&#8217; or &#8216;f&#8217;). For example, <b>DecimalFormat</b>=&#8217;4f&#8217; will display 4 digits after the decimal in floating-point format (&#8216;e&#8217; means engineering format). Here is a short usage example (notice the different ways that we can set the callbacks):</p>
<pre lang="matlab">
hColumn = hSection.addColumn('Width',100);
% Integer spinner (-100 : 10 : 100)
hSpinner = Spinner([-100 100], 0);  % [min,max], initialValue
hSpinner.Description = 'this is a tooltip description';
hSpinner.StepSize = 10;
hSpinner.ValueChangedFcn = @ValueChangedCallback;
hColumn.add(hSpinner);
% Floating-point spinner (-10 : 0.0001 : 10)
hSpinner = Spinner([-10 10], pi);  % [min,max], initialValue
hSpinner.NumberFormat = 'double';
hSpinner.DecimalFormat = '4f';
hSpinner.StepSize = 1e-4;
addlistener(hSpinner,'ValueChanged', @ValueChangedCallback);
addlistener(hSpinner,'ValueChanging',@ValueChangingCallback);
hColumn.add(hSpinner);
</pre>
<p><center><figure style="width: 110px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Spinner.png" alt="Toolstrip Spinner" title="Toolstrip Spinner" width="110" height="95" /><figcaption class="wp-caption-text">Toolstrip Spinner</figcaption></figure></center> A logical extension of the toolstrip spinner implementation would be for <a href="https://undocumentedmatlab.com/articles/using-spinners-in-matlab-gui" target="_blank">non-numeric spinners</a>, as well as custom <b>Value</b> display formatting. Perhaps this will become available at some future Matlab release.</p>
<h3 id="Slider">Slider</h3>
<p><code>Slider</code> is a horizontal ruler on which you can move a knob from the left (min <b>Value</b>) to the right (max <b>Value</b>). The ticks and labels are optional and customizable. Here is a simple example showing a plain slider (values between 0-100, initial value 70, ticks every 5, labels every 20, step size 1), followed by a custom slider (notice again the different ways that we can set the callbacks):</p>
<pre lang="matlab">
hColumn = hSection.addColumn('Width',200);
hSlider = Slider([0 100], 70);  % [min,max], initialValue
hSlider.Description = 'this is a tooltip';
tickVals = 0 : 20 : 100;
hSlider.Labels = [compose('%d',tickVals); num2cell(tickVals)]';  % {'0',0; '20',20; ...}
hSlider.Ticks = 21;  % =numel(0:5:100)
hSlider.ValueChangedFcn = @ValueChangedCallback;
hColumn.add(hSlider);
hSlider = Slider([0 100], 40);  % [min,max], initialValue
hSlider.Labels = {'Stop' 0; 'Slow' 20; 'Fast' 50; 'Too fast' 75; 'Crash!' 100};
try hSlider.UseSmallFont = true; catch, end  % UseSmallFont was only added in R2018b
hSlider.Ticks = 11;  % =numel(0:10:100)
addlistener(hSlider,'ValueChanged', @ValueChangedCallback);
addlistener(hSlider,'ValueChanging',@ValueChangingCallback);
hColumn.add(hSlider);
</pre>
<p><center><figure style="width: 217px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_Slider.png" alt="Toolstrip Slider" title="Toolstrip Slider" width="217" height="95" /><figcaption class="wp-caption-text">Toolstrip Slider</figcaption></figure></center></p>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The next post will discuss complex selection components, including listbox, drop-down, split-button, and gallery.<br />
Following that, I plan to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order.<br />
Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls">Matlab toolstrip – part 6 (complex controls)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 5 (icons)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-5-icons</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 06 Jan 2019 17:00:37 +0000</pubDate>
				<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Icons]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8188</guid>

					<description><![CDATA[<p>Icons can be specified in various ways for toolstrip controls and the app window itself. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons">Matlab toolstrip – part 5 (icons)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In a <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" target="_blank">previous post</a> I showed how we can create custom Matlab app toolstrips. Toolstrips can be a bit complex to develop so I’m trying to proceed slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">the Toolstrip miniseries</a> before reading this post. Today&#8217;s post describes how we can set various icons, based on the toolstrip created in the previous posts:<br />
<center><figure style="width: 440px" class="wp-caption aligncenter"><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_basic_controls.png" alt="Toolstrip example (basic controls)" title="Toolstrip example (basic controls)" width="100%" style="max-width:553px; margin:0" /><figcaption class="wp-caption-text">Toolstrip example (basic controls)</figcaption></figure></center><br />
<span id="more-8188"></span></p>
<h3 id="components">Component icons</h3>
<p>Many toolstrip controls (such as buttons, but not checkboxes for example) have a settable <b>Icon</b> property. The standard practice is to use a 16&#215;16 icon for a component within a multi-component toolstrip column (i.e., when 2 or 3 components are displayed on top of each other), and a 24&#215;24 icon for a component that spans the entire column height (i.e., when the column contains only a single component).<br />
We can use one of the following methods to specify the icon. Note that you need to <code>import matlab.ui.internal.toolstrip.*</code> if you wish to use the <code>Icon</code> class without the preceding package name.</p>
<ul>
<li>The <b>Icon</b> property value is typically empty (<code>[]</code>) by default, meaning that no icon is displayed.<br />
&nbsp;
</li>
<li>We can use one of ~150 standard icons using the format <code>Icon.&lt;icon-name&gt;</code>. For example: <code>icon = Icon.REFRESH_24</code>. These icons typically come in 2 sizes: 16&#215;16 pixels (e.g. Icon.REFRESH_16) that we can use with the small-size components (which are displayed when the column has 2-3 controls), and 24&#215;24 pixels (e.g. REFRESH_24) that we can use with the large-size components (which are displayed when the column contains only a single control). You can see the list of the standard icons by running
<pre lang="matlab">matlab.ui.internal.toolstrip.Icon.showStandardIcons</pre>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_standard_icons.png" alt="Standard toolstrip control Icons" title="Standard toolstrip control Icons" width="50%" style="max-width:658px;" /></center></li>
<li>We can use the <code>Icon</code> constructor by specifying the full filepath for any PNG or JPG image file. Note that other file type (such as GIF) are not supported by this method. For example:
<pre lang="matlab">icon = Icon(fullfile(matlabroot,'toolbox','matlab','icons','tool_colorbar.png')); % PNG/JPG image file (not GIF!)</pre>
<p>In fact, the ~150 standard icons above use this mechanism under the hood: <code>Icon.REFRESH_24</code> is basically a public static method of the <code>Icon</code> class, which simply calls <code>Icon('REFRESH_24','Refresh_24')</code>  (note the undocumented use of a 2-input <code>Icon</code> constructor). This method in turn uses the <i>Refresh_24.png</i> file in Matlab&#8217;s standard toolstrip resources folder: <i>%matlabroot%/toolbox/shared/controllib/general/resources/toolstrip_icons/Refresh_24.png</i>.</p>
<li>We can also use the <code>Icon</code> constructor by specifying a PNG or JPG file contained within a JAR file, using the standard <code>jar:file:...jar!/</code> notation. There are numerous icons included in Matlab&#8217;s JAR files &#8211; simply open these files in WinZip or WinRar and browse. In addition, you can include images included in any external JAR file. For example:
<pre lang="matlab">icon = Icon(['jar:file:/' matlabroot '/java/jar/mlwidgets.jar!/com/mathworks/mlwidgets/actionbrowser/resources/uparrow.png']);</pre>
</li>
<li>We can also use the <code>Icon</code> constructor by specifying a Java <code>javax.swing.ImageIcon</code> object. Fortunately we can create such objects from a variety of image formats (including GIFs). For example:
<pre lang="matlab">
iconFilename = fullfile(matlabroot,'toolbox','matlab','icons','boardicon.gif');
jIcon = javax.swing.ImageIcon(iconFilename);  % Java ImageIcon from file (inc. GIF)
icon = Icon(jIcon);
</pre>
<p>If we need to resize the Java image (for example, from 16&#215;16 to 24&#215;24 or vise versa), we can use the following method:</p>
<pre lang="matlab">
% Resize icon to 24x24 pixels
jIcon = javax.swing.ImageIcon(iconFilename);  % get Java ImageIcon from file (inc. GIF)
jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(24,24,jIcon.getImage.SCALE_SMOOTH))  % resize to 24x24
icon = Icon(jIcon);
</pre>
</li>
<li>We can apparently also use a CSS class-name to load images. This is only relevant for the JavaScript-based uifigures, not legacy Java-based figures that I discussed so far. Perhaps I will explore this in some later post that will discuss toolstrip integration in uifigures.</li>
</ul>
<h3 id="app">App window icon</h3>
<p>The app window&#8217;s icon can also be set. By default, the window uses the standard Matlab membrane icon (<i>%matlabroot%/toolbox/matlab/icons/matlabicon.gif</i>). This can be modified using the <code>hToolGroup.setIcon</code> method, which currently [R2018b] expects a Java <code>ImageIcon</code> object as input. For example:</p>
<pre lang="matlab">
iconFilename = fullfile(matlabroot,'toolbox','matlab','icons','reficon.gif');
jIcon = javax.swing.ImageIcon(iconFilename);
hToolGroup.setIcon(jIcon)
</pre>
<p>This icon should be set before the toolgroup window is shown (<code>hToolGroup.open</code>).<br />
<center><figure style="width: 400px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_app_icon.gif" alt="Custom app window icon" title="Custom app window icon" width="400" height="230" /><figcaption class="wp-caption-text">Custom app window icon</figcaption></figure></center><br />
An odd caveat here is that the icon size needs to be 16&#215;16 &#8211; setting a larger icon results in the icon being ignored and the default Matlab membrane icon used. For example, if we try to set &#8216;boardicon.gif&#8217; (16&#215;17) instead of &#8216;reficon.gif&#8217; (16&#215;16) we&#8217;d get the default icon instead. If our icon is too large, we can resize it to 16&#215;16, as shown above:</p>
<pre lang="matlab">
% Resize icon to 16x16 pixels
jIcon = javax.swing.ImageIcon(iconFilename);  % get Java ImageIcon from file (inc. GIF)
jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(16,16,jIcon.getImage.SCALE_SMOOTH))  % resize to 16x16
hToolGroup.setIcon(jIcon)
</pre>
<p>It&#8217;s natural to expect that <code>hToolGroup</code>, which is a pure-Matlab MCOS wrapper class, would have an <b>Icon</b> property that accepts <code>Icon</code> objects, just like for controls as described above. For some reason, this is not the case. It&#8217;s very easy to fix it though &#8211; after all, the <code>Icon</code> class is little more than an MCOS wrapper class for the underlying Java <code>ImageIcon</code> (not exactly, but close enough). Adapting <code>ToolGroup</code>&#8216;s code to accept an <code>Icon</code> is quite easy, and I hope that MathWorks will indeed implement this in a near-term future release. I also hope that MathWorks will remove the 16&#215;16 limitation, or automatically resize icons to 16&#215;16, or at the very least issue a console warning when a larger icon is specified by the user. Until then, we can use the <code>setIcon(jImageIcon)</code> method and take care to send it the 16&#215;16 <code>ImageIcon</code> object that it expects.</p>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The next post will discuss complex components, including button-group, drop-down, listbox, split-button, slider, popup form, gallery etc.<br />
Following that, my plan is to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order. Matlab toolstrips can be a bit complex, so I plan to proceed in small steps, each post building on top of its predecessors.<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons">Matlab toolstrip – part 5 (icons)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 4 (control customization)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-4-control-customization</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization#respond</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 30 Dec 2018 16:00:00 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[UI controls]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Callbacks]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Hidden property]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8110</guid>

					<description><![CDATA[<p>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization">Matlab toolstrip – part 4 (control customization)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In a <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" target="_blank">previous post</a> I showed how we can create custom Matlab app toolstrips. Toolstrips can be a bit complex to develop so I’m trying to proceed slowly, with each post in the miniseries building on the previous posts. I encourage you to review the earlier posts in <a href="https://undocumentedmatlab.com/articles/tag/toolstrip" target="_blank">the Toolstrip miniseries</a> before reading this post. In today&#8217;s post we continue the discussion of the toolstrip created in the previous post:<br />
<center><figure style="width: 440px" class="wp-caption aligncenter"><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_basic_controls.png" alt="Toolstrip example (basic controls)" title="Toolstrip example (basic controls)" width="100%" style="max-width:553px; margin:0" /><figcaption class="wp-caption-text">Toolstrip example (basic controls)</figcaption></figure></center><br />
Today&#8217;s post will show how to attach user-defined functionality to toolstrip components, as well as some additional customizations. At the end of today&#8217;s article, you should be able to create a fully-functional custom Matlab toolstrip. <span id="more-8110"></span> Today&#8217;s post will remain within the confines of a Matlab &#8220;app&#8221;, i.e. a tool-group that displays docked figures. Future posts will discuss lower-level toolstrip mechanisms, that enable advanced customizations as well as integration in legacy (Java-based, even GUIDE-created) Matlab figures.</p>
<h3 id="callbacks">Control callbacks</h3>
<p>Controls are useless without settable callbacks that affect the program state based on user interactions. There are two different mechanisms for setting callbacks for Matlab toolstrip controls. Refer to the example in the <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization#example" target="_blank">previous post</a>:</p>
<ol>
<li>Setting the control&#8217;s callback property or properties &#8211; the property names differ across components (no, for some reason it&#8217;s never as simple as <b>Callback</b> in standard uicontrols). For example, the main action callback for push-buttons is <b>ButtonPushedFcn</b>, for toggle-buttons and checkboxes it&#8217;s <b>ValueChangedFcn</b> and for listboxes it&#8217;s <b></b>. Setting the callback is relatively easy:
<pre lang="matlab">
hColorbar.ValueChangedFcn = @toggleColorbar;
function toggleColorbar(hAction,hEventData)
    if hAction.Selected
        colorbar;
    else
        colorbar('off');
    end
end
</pre>
<p>The <code>hAction</code> object that is passed to the callback function as the first input arg contains various fields of interest, but for some reason the most important object property (<b>Value</b>) is renamed as the <b>Selected</b> property (most confusing). Also, a back-reference to the originating control (<code>hColorbar</code> in this example), which is important for many callbacks, is also missing (and no &#8211; I couldn&#8217;t find it in the hidden properties either):</p>
<pre lang="matlab">
>> hAction
hAction =
  Action with properties:
            Description: 'Toggle colorbar display'
                Enabled: 1
               Shortcut: ''
               Selected: 1
        QuickAccessIcon: []
    SelectionChangedFcn: @toggleColorbar
                   Text: 'Colorbar'
        IsInQuickAccess: 0
            ButtonGroup: []
                   Icon: [1×1 matlab.ui.internal.toolstrip.Icon]
>> hEventData
hEventData =
  ToolstripEventData with properties:
    EventData: [1×1 struct]
       Source: [0×0 handle]
    EventName: ''
>> hEventData.EventData
ans =
  struct with fields:
    Property: 'Value'
    NewValue: 1
    OldValue: 0
</pre>
<p>Note that <code>hEventData.Source</code> is an empty handle for some unknown reason.<br />
The bottom line is that to reference the button state using this callback mechanism we need to either:</p>
<ol>
<li>Access <code>hAction</code>&#8216;s <b>Selected</b> property which stands-in for the originating control&#8217;s <b>Value</b> property (this is what I have shown in the short code snippet above)</li>
<li>Access <code>hEventData.EventData</code> and use its reported Property, NewValue and OldValue fields</li>
<li>Pass the originating control handle as an extra (3rd) input arg to the callback function, and then access it from within the callback. For example:
<pre lang="matlab">
hColorbar.ValueChangedFcn = {@toggleColorbar, hColorbar};
function toggleColorbar(hAction,hEventData,hButton)
    if hButton.Value %hAction.Selected
        colorbar;
    else
        colorbar('off');
    end
end
</pre>
</ol>
</li>
<li>As an alternative, we can use the <i><b>addlistener</b></i> function to attach a callback to control events. Practically all toolstrip components expose public events that can be listened-to using this mechanism. In most cases the control&#8217;s callback property name(s) closely follow the corresponding events. For example, for buttons we have the <b>ValueChanged</b> event that corresponds to the <b>ValueChangedFcn</b> property. We can use listeners as follows:
<pre lang="matlab">
hCheckbox.addlistener('ValueChanged',@toggleLogY);
function toggleLogY(hCheckbox,hEventData)
    if hCheckbox.Value, type = 'log'; else, type = 'linear'; end
    set(gca, 'XScale',type, 'YScale',type, 'ZScale',type);
end
</pre>
<p>Note that when we use the <i><b>addlistener</b></i> mechanism to attach callbacks, we don&#8217;t need any of the tricks above &#8211; we get the originating control handle as the callback function&#8217;s first input arg, and we can access it directly.<br />
Unfortunately, we cannot pass extra args to the callback that we specify using <i><b>addlistener</b></i> (this seems like a trivial and natural thing to have, for MathWorks&#8217; attention&#8230;). In other words, <i><b>addlistener</b></i> only accepts a function handle as callback, not a cell array. To bypass this limitation in uicontrols, we typically add the extra parameters to the control&#8217;s <b>UserData</b> or <b>ApplicationData</b> properties (the latter via the <i><b>setappdata</b></i> function). But alas &#8211; toolstrip components have neither of these properties, nor can we add them in runtime (<a href="https://undocumentedmatlab.com/articles/adding-custom-properties-to-gui-objects" target="_blank">as with for other GUI controls</a>). So we need to find some other way to pass these extra values, such as using global variables, or making the callback function nested so that it could access the parent function&#8217;s workspace.
</li>
</ol>
<h3 id="props">Additional component properties</h3>
<p>Component text labels, where relevant, can be set using the component&#8217;s <b>Text</b> property, and the tooltip can be set via the <b>Description</b> property. As I noted in my previous post, I believe that this is an unfortunate choice of property names. In addition, components have control-specific properties such as <b>Value</b> (checkboxes and toggle buttons). These properties can generally be modified in runtime, in order to reflect the program state. For example, we can disable/enable controls, and modify their label, tooltip and state depending on the control&#8217;s new state and the program state in general.<br />
The component icon can be set via the <b>Icon</b> property, where available (for example, buttons have an icon, but checkboxes do not). There are several different ways in which we can set this <b>Icon</b>. I will discuss this in detail in the following post; in the meantime you can review the usage examples in the previous post.<br />
There are a couple of additional hidden component properties that seem promising, most notably <b>Shortcut</b> and <b>Mnemonic</b> (the latter (Mnemonic) is also available in <code>Section</code> and <code>Tab</code>, not just in components). Unfortunately, at least as of R2018b these properties do not seem to be connected yet to any functionality. In the future, I would expect them to correspond to keyboard shortcuts and underlined mnemonic characters, as these functionalities behave in standard menu items.</p>
<h3 id="Java">Accessing the underlying Java control</h3>
<p>As long as we&#8217;re not displaying the toolstrip on a browser page (i.e., inside a uifigure or Matlab Online), the toolstrip is basically composed of Java Swing components from the <code>com.mathworks.toolstrip.components</code> package (such as <code>TSButton</code> or <code>TSCheckBox</code>). I will discuss these Java classes and their customizations in a later post, but for now I just wish to show how to access the underlying Java component of any Matlab MCOS control. This can be done using a central registry of toolstrip components (so-called &#8220;widgets&#8221;), which is accessible via the <code>ToolGroup</code>&#8216;s hidden <b>ToolstripSwingService</b> property, and then via each component&#8217;s hidden widget Id. For example:</p>
<pre lang="matlab">
>> widgetRegistry = hToolGroup.ToolstripSwingService.Registry;
>> jButton = widgetRegistry.getWidgetById(hButton.getId)  % get the hButton's underlying Java control
ans =
com.mathworks.toolstrip.components.TSToggleButton[,"Colorbar",layout<>,NORMAL]
</pre>
<p>We can now apply a wide variety of Java-based customizations to the retrieved <code>jButton</code>, as I have shown in many other articles on this website over the past decade.<br />
Another way to access the toolstrip Java component hierarchy is via <code>hToolGroup.Peer.get(tabIndex).getComponent</code>. This returns the top-level Java control representing the tab whose index in <code>tabIndex</code> (0=left-most tab):</p>
<pre lang="matlab">
>> jToolGroup = hToolGroup.Peer;  % or: =hToolGroup.ToolstripSwingService.SwingToolGroup;
>> jDataTab = jToolGroup.get(0).getComponent;  % Get tab #0 (first tab: "Data")
>> jDataTab.list   % The following is abridged for brevity
com.mathworks.toolstrip.impl.ToolstripTabContentPanel[tab0069230a-52b0-4973-b025-2171cd96301b,0,0,831x93,...]
 SectionWrapper(section54fb084c-934d-4d31-9468-7e4d66cd85e5)
  com.mathworks.toolstrip.impl.ToolstripSectionComponentWithHeader[,0,0,241x92,...]
   com.mathworks.toolstrip.components.TSPanel[section54fb084c-934d-4d31-9468-7e4d66cd85e5,,layout<horizontal>,NORMAL]
    TSColumn -> layout<> :
     com.mathworks.toolstrip.components.TSButton[,"Refresh all",layout<>,NORMAL]
    TSColumn -> layout<> :
     com.mathworks.toolstrip.components.TSButton[,"Refresh X,Y",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSButton[,"Refresh Y,Z",layout<>,NORMAL]
    TSColumn -> layout<> :
     com.mathworks.toolstrip.components.TSButton[,"Refresh X",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSButton[,"Refresh Y",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSButton[,"Refresh Z",layout<>,NORMAL]
 SectionWrapper(sectionebd8ab95-fd33-4a3d-8f24-152589713994)
  com.mathworks.toolstrip.impl.ToolstripSectionComponentWithHeader[,0,0,159x92,...]
   com.mathworks.toolstrip.components.TSPanel[sectionebd8ab95-fd33-4a3d-8f24-152589713994,,layout<horizontal>,NORMAL]
    TSColumn -> layout<> :
     com.mathworks.toolstrip.components.TSCheckBox[,"Axes borders",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSCheckBox[,"Log scaling",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSCheckBox[,"Inverted Y",layout<>,NORMAL]
 SectionWrapper(section01995bfd-61de-490f-aa22-de50bae1af75)
  com.mathworks.toolstrip.impl.ToolstripSectionComponentWithHeader[,0,0,125x92,...]
   com.mathworks.toolstrip.components.TSPanel[section01995bfd-61de-490f-aa22-de50bae1af75,,layout<horizontal>,NORMAL]
    TSColumn -> layout<> :
     com.mathworks.toolstrip.components.TSToggleButton[,"Legend",layout<>,NORMAL]
    TSColumn -> layout<> :
     com.mathworks.toolstrip.components.TSLabel[null," ",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSToggleButton[,"Colorbar",layout<>,NORMAL]
     com.mathworks.toolstrip.components.TSLabel[null," ",layout<>,NORMAL]
 com.mathworks.mwswing.MJButton[toolstrip.header.collapseButton,808,70,20x20,...]
</pre>
<h3 id="roadmap">Toolstrip miniseries roadmap</h3>
<p>The next post will discuss icons, for both toolstrip controls as well as the ToolGroup app window.<br />
I plan to discuss complex components in subsequent posts. Such components include button-group, drop-down, listbox, split-button, slider, popup form, gallery etc.<br />
Following that, my plan is to discuss toolstrip collapsibility, the ToolPack framework, docking layout, DataBrowser panel, QAB (Quick Access Bar), underlying Java controls, and adding toolstrips to figures &#8211; not necessarily in this order.<br />
Have I already mentioned that Matlab toolstrips can be a bit complex?<br />
If you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.<br />
Happy New Year, everyone!</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization">Matlab toolstrip – part 4 (control customization)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-7-selection-controls" rel="bookmark" title="Matlab toolstrip – part 7 (selection controls)">Matlab toolstrip – part 7 (selection controls) </a> <small>Matlab toolstrips can contain a wide variety of selection controls: popups, combo-boxes, and galleries. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 3 (basic customization)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-3-basic-customization</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Sun, 16 Dec 2018 16:00:29 +0000</pubDate>
				<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8077</guid>

					<description><![CDATA[<p>Matlab toolstrips can be created and customized in a variety of ways. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization">Matlab toolstrip – part 3 (basic customization)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>In the <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" target="_blank">previous post</a> I showed how we can create custom Matlab apps. In such apps, the toolstrip is very often an important part. Today I continue my miniseries on toolstrips. Toolstrips can be a bit complex so I&#8217;m trying to proceed slowly, with each post in the miniseries building on the previous posts. So I encourage you to review the earlier posts in the miniseries (<a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1" target="_blank">part1</a>, <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" target="_blank">part2</a>) before reading this post.<br />
A Matlab toolstrip is composed of a hierarchy of user-interface objects as follows (all objects are classes within the <code>matlab.ui.internal.toolstrip</code> package):<br />
<figure style="width: 440px" class="wp-caption alignright"><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_anatomy.png" alt="Anatomy of a Matlab app with toolstrip" title="Anatomy of a Matlab app with toolstrip" width="100%" style="max-width:673px; margin:0"/><figcaption class="wp-caption-text">Anatomy of a Matlab app with toolstrip</figcaption></figure></p>
<ul>
<li>TabGroup
<ul>
<li>Tab
<ul>
<li>Section
<ul>
<li>Column
<ul>
<li>Component
<ul>
					</ul>
</li>
<li>Component</li>
<li>&#8230;</li>
</ul>
</li>
<li>Column</li>
<li>&#8230;</li>
</ul>
</li>
<li>Section</li>
<li>&#8230;</li>
</ul>
</li>
<li>Tab</li>
<li>&#8230;</li>
</ul>
<li>TabGroup</li>
<li>&#8230;</li>
</ul>
<p>In this post I explain how we can create a custom toolstrip that contains tabs, sections, and basic controls that interact with the user and the docked figures. The following posts will show more advanced customizations and more complex controls, as well as showing alternative ways of creating the toolstrip.<br />
<span id="more-8077"></span></p>
<h3 id="tabs">1. Creating a bare toolstrip and new tabs</h3>
<p>We start with a new <code>ToolGroup</code> that has a bare toolstrip and a docked figure (for details and explanations refer to the previous post):</p>
<pre lang="matlab">
% Create a new ToolGroup ("app") with a hidden DataBrowser
hToolGroup = matlab.ui.internal.desktop.ToolGroup('Toolstrip example on UndocumentedMatlab.com');
hToolGroup.disableDataBrowser();
hToolGroup.open();  % this may be postponed further down for improved performance
% Store toolgroup reference handle so that app will stay in memory
jToolGroup = hToolGroup.Peer;
internal.setJavaCustomData(jToolGroup, hToolGroup);
% Create two figures and dock them into the ToolGroup
hFig1 = figure('Name','3D');  surf(peaks);
hToolGroup.addFigure(hFig1);
</pre>
<p>We now create a new <code>TabGroup</code> and and it to our <code>ToolGroup</code>:</p>
<pre lang="matlab">
import matlab.ui.internal.toolstrip.*  % for convenience below
hTabGroup = TabGroup();
hToolGroup.addTabGroup(hTabGroup);
</pre>
<p>We can add a new <code>Tab</code> to the <code>TabGroup</code> using either of two methods:</p>
<ol>
<li>Create a new <code>Tab</code> object and then use <code>TabGroup.<i>add(hTab,index)</i></code> to add it to a parent <code>TabGroup</code>. The index argument is optional &#8211; if specified the section is inserted at that index location; if not, it is added at the end of the tab-group. Sample usage:
<pre lang="matlab">
hTab = Tab('Data');
hTabGroup.add(hTab);  % add to tab as the last section
hTabGroup.add(hTab,3);  % add to tab as the 3rd section
</pre>
</li>
<li>Call <code>TabGroup.<i>addTab(title)</i></code>. This creates a new tab with the specified title (default: &#8221;) and adds it at the end of the tab-group. The new tab&#8217;s handle is returned by the function. Sample usage:
<pre lang="matlab">hTabGroup.addTab('Data');  % add to tab-group as the last tab</pre>
</li>
</ol>
<p>This creates an empty &#8220;Data&#8221; tab in our app toolstrip. Note that the tab title is capitalized (&#8220;DATA&#8221;), despite the fact that we set its <b>Title</b> property to <code>'Data'</code>. Also note that while the tab&#8217;s <b>Title</b> property can be updated after the tab is created, in practice the tab title does not seem to change.<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_new_tab.png" alt="New (empty) toolstrip tab" title="New (empty) toolstrip tab" width="80%" style="max-width:504px;" height="413" /></center><br />
Lastly, note that a &#8220;VIEW&#8221; tab is automatically added to our toolstrip. As explained in the previous post, we can remove it using <code>hToolGroup.hideViewTab;</code> (refer to the previous post for details).</p>
<h3 id="sections">2. Adding sections to a toolstrip tab</h3>
<p>Each toolstrip <code>Tab</code> is composed of <code>Section</code>s, that holds the actual components. We cannot add components directly to a <code>Tab</code>: they have to be contained within a <code>Section</code>. A toolstrip <code>Tab</code> can only contain <code>Section</code>s as direct children.<br />
We can add a new section to a <code>Tab</code> using either of two methods, in a similar way to the that way we added a new tab above:</p>
<ol>
<li>Create a new <code>Section</code> object and then use <code>Tab.<i>add(hSection,index)</i></code> to add it to a parent <code>Tab</code>. The index argument is optional &#8211; if specified the section is inserted at that index location; if not, it is added at the end of the tab. Sample usage:
<pre lang="matlab">
hSection = Section('Section title');
hTab.add(hSection);  % add to tab as the last section
hTab.add(hSection,3);  % add to tab as the 3rd section
</pre>
</li>
<li>Call <code>Tab.<i>addSection(title)</i></code>. This creates a new section with the specified title (default: &#8221;) and adds it at the end of the tab. The new section&#8217;s handle is returned by the function. Sample usage:
<pre lang="matlab">hTab.addSection('Section title');  % add to tab as the last section</pre>
</li>
</ol>
<p>Note that the help section for <code>Tab.<i>addSection()</i></code> indicates that it&#8217;s possible to specify 2 string input args (presumably Title and Tag), but this is in fact wrong and causes a run-time error, since <code>Section</code> constructor only accepts a single argument (Title), at least as of R2018b.<br />
The <code>Section</code>&#8216;s <b>Title</b> property can be set both in the constructor, as well as updated later. In addition, we can also set the <b>Tag</b> and <b>CollapsePriority</b> properties after the section object is created (these properties cannot be set in the constructor call):</p>
<pre lang="matlab">
hSection.Title = 'New title';    % can also be set in constructor call
hSection.Tag = 'section #1';     % cannot be set in constructor call
hSection.CollapsePriority = 10;  % cannot be set in constructor call
</pre>
<p>The <b>CollapsePriority</b> property is responsible for controlling the order in which sections and their internal components collapse into a drop-down when the window is resized to a smaller width.<br />
Like tabs, section titles also appear capitalized. However, unlike the section titles can indeed be modified in run-time.</p>
<h3 id="columns">3. Adding columns to a tab section</h3>
<p>Each <code>Section</code> in a toolstrip <code>Tab</code> is composed of <code>Column</code>s, and each <code>Column</code> can contain 1-3 <code>Component</code>s. This is a very effective layout for toolstrip controls that answers the vast majority of use-cases. In some special cases we might need more flexibility with the component layout within a <code>Tab</code> &#8211; I will explain this in a future post. But for now let&#8217;s stick to the standard <code>Tab-Section-Column-Component</code> framework.<br />
We can add columns to a section using (guess what?) either of two methods, as above:</p>
<ol>
<li>Create a new <code>Column</code> object and then use <code>Section.<i>add(hColumn,index)</i></code> to add it to a parent <code>Section</code>. The index argument is optional &#8211; if specified the column is inserted at that index location; if not, it is added at the end of the section. Sample usage:
<pre lang="matlab">
hColumn = Column('HorizontalAlignment','center', 'Width',150);
hSection.add(hColumn);  % add to section as the last column
hSection.add(hColumn,3);  % add to section as the 3rd column
</pre>
</li>
<li>Call <code>Tab.<i>addSection(title)</i></code>. This creates a new section with the specified title (default: &#8221;) and adds it at the end of the tab. The new section&#8217;s handle is returned by the function. Sample usage:
<pre lang="matlab">hSection.addColumn('HorizontalAlignment','center', 'Width',150);  % add to section as the last column</pre>
</li>
</ol>
<p>We can set the <code>Column</code>&#8216;s <b>HorizontalAlignment</b> and <b>Width</b> properties only in the constructor call, not later via direct assignments. In contrast, the <b>Tag</b> property cannot be set in the constructor, only via direct assignment:</p>
<pre lang="matlab">
hColumn.HorizontalAlignment = 'right';  % error: can only be set via constructor call: Column('HorizontalAlignment','right', ...)
hColumn.Width = 150;                    % error: can only be set via constructor call: Column('Width',150, ...)
hColumn.Tag = 'column #2';              % ok: cannot be set via the constructor call!
</pre>
<p>This is indeed confusing and non-intuitive. Perhaps this is part of the reason that the toolstrip API is still not considered stable enough for a general documented release.</p>
<h3 id="controls">4. Adding controls to a section column</h3>
<p>Each section column contains 1 or more <code>Component</code>s. These can be push/toggle/split/radio buttons, checkboxes, drop-downs, sliders, spinners, lists etc. Take a look at <i>matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+toolstrip/</i> for a full list of available controls. I&#8217;ll discuss a few basic controls in this post, and more complex ones in future posts.<br />
As above, there are two methods for adding components to a section column, but they have different purposes:</p>
<ol>
<li><code>Column.<i>addEmptyControl()</i></code> adds a filler space in the next position of the column. This is used to display the colorbar control at the center of the column in the usage snippet below.</li>
<li>Create a new <code>Component</code> object and then use <code>Column.<i>add(hComponent, index)</i></code> to add it to a parent <code>Column</code>. The index argument is optional &#8211; if specified the component is inserted at that index location; if not, it is added at the end of the column. Sample usage:
<pre lang="matlab">
hButton = Button('Click me!');
hColumn.add(hButton);  % add to column as the last component
hColumn.add(hButton,3);  % add to column as the 3rd component
</pre>
</li>
</ol>
<p><code>Component</code> objects (<code>matlab.ui.internal.toolstrip.base.Component</code>, which all usable toolstrip controls inherit) have several common properties. Leaving aside the more complex components for now, most usable controls include the following properties:</p>
<ul>
<li><b>Text</b> &#8211; text label, displayed next to the control icon (pity that MathWorks didn&#8217;t call this property <b>String</b> or <b>Label</b>, in line with uicontrols/menu-items)</li>
<li><b>Description</b> &#8211; tooltip, displayed when hovering the mouse over the control (pity that MathWorks didn&#8217;t call this property <b>Tooltip</b> in line with uicontrols/menu-items)</li>
<li><b>Tag</b> &#8211; a string, as all other Matlab HG objects. Controls are searchable by their <b>Tag</b> via their container&#8217;s <i>find(tag)</i> and <i>findAll(tag)</i> methods (again, I don&#8217;t quite understand why not use <i><b>findobj</b></i> and <i><b>findall</b></i> as with the rest of Matlab HG&#8230;).</li>
<li><b>Enabled</b> &#8211; a logical value (<code>true/false</code>), <code>true</code> by default</li>
<li><b>Icon</b> &#8211; the icon used next to the <b>Text</b> label. We can use the <code>Icon</code> constructor (that expects the full path of a PNG/JPG file), or one of its static icons (e.g. Icon.REFRESH_16). Icons will be discussed in detail in the following post; in the meantime you can see various usage examples below.</li>
</ul>
<p>Each control also has one or more callbacks that can be specified, as settable properties and/or as events that can be listened-to using the <i><b>addlistener</b></i> function. This too will be discussed in detail in the next post, but in the meantime you can see various usage examples below.<br />
Columns can have 1-3 components:</p>
<ul>
<li>If only 1 component is specified, it is allocated the full column height, effectively creating a large control, with the <b>Icon</b> on top (typically a 24&#215;24 icon) and the <b>Text</b> label beneath.</li>
<li>If 2 or 3 components are specified, then smaller controls are displayed, with the <b>Text</b> label to the right of the <b>Icon</b> (typically 16&#215;16), and the controls evenly spaced within the column.</li>
<li>If you try to add more than 3 components to a <code>Column</code>, you&#8217;ll get a run-time error.</li>
</ul>
<h3 id="example">5. Usage example</h3>
<p>Here is a short usage example showing the above concepts. The code is not pretty by any means &#8211; I intentionally wanted to display multiple different ways of adding components, specifying properties and callbacks etc. It is meant merely as an educational tool, and is not close to being ready for production code. So please don&#8217;t complain about the known fact that the code is ugly, non-robust, and in general exhibits bad programming practices. The complete runnable code can be <a href="/files/toolstrip_demo1.m" target="_blank">downloaded here</a>.<br />
The following code snippets assume that you have already ran the code in <a href="#tabs">paragraph 1 above</a>:</p>
<h5 id="Section1">Push-buttons section (3 columns)</h5>
<figure style="width: 440px" class="wp-caption alignright"><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_basic_controls.png" alt="Toolstrip example (basic controls)" title="Toolstrip example (basic controls)" width="100%" style="max-width:553px; margin:0" /><figcaption class="wp-caption-text">Toolstrip example (basic controls)</figcaption></figure>
<pre lang="matlab">
section1 = hTab.addSection('Push buttons');
column1a = section1.addColumn();
icon = Icon.REFRESH_24; % built-in: see Icon.showStandardIcons()
button = Button('Refresh all',icon);
button.Description = 'Refresh the charted data - all axes';
button.ButtonPushedFcn = @refreshAllData;
column1a.add(button);
function refreshAllData(hAction,hEventData)
    hAxes = gca;
    hChildren = hAxes.Children;
    for idx = 1 : numel(hChildren)
        hChild = hChildren(idx);
        hChild.XData = -hChild.XData;
        hChild.YData = -hChild.YData;
        hChild.ZData = -hChild.ZData;
    end
end
column1b = section1.addColumn();
addRefresh2Button('X','Y');
addRefresh2Button('Y','Z');
function addRefresh2Button(type1, type2)
    import matlab.ui.internal.toolstrip.*
    hButton = Button(['Refresh ' type1 ',' type2], Icon.RESTORE_16);
    hButton.Description = ['Refresh the charted data - ' type1 ',' type2 ' axes'];
    hButton.ButtonPushedFcn = {@refres2AxisData, type1, type2};
    column1b.add(hButton);
    function refres2AxisData(~,~,type1,type2)
        hAxes = gca;
        hChildren = hAxes.Children;
        for idx = 1 : numel(hChildren)
            hChild = hChildren(idx);
            hChild.([type1 'Data']) = -hChild.([type1 'Data']);
            hChild.([type2 'Data']) = -hChild.([type2 'Data']);
        end
    end
end
column1c = section1.addColumn();
addRefresh1Button('X');
addRefresh1Button('Y');
addRefresh1Button('Z');
function addRefresh1Button(type)
    import matlab.ui.internal.toolstrip.*
    hButton = Button(['Refresh ' type], Icon.REDO_16);
    hButton.Description = ['Refresh the charted data - ' type ' axes'];
    addlistener(hButton, 'ButtonPushed', @refres1AxisData);  % {} not supported!
    column1c.add(hButton);
    function refres1AxisData(h,e)
        hAxes = gca;
        hChildren = hAxes.Children;
        for idx = 1 : numel(hChildren)
            hChild = hChildren(idx);
            hChild.([type 'Data']) = -hChild.([type 'Data']);
        end
    end
end
</pre>
<h5 id="Section2">Toggle buttons section (2 columns)</h5>
<pre lang="matlab">
section2 = hTab.addSection('Toggle buttons');
section2.CollapsePriority = 2;
column1 = Column();
section2.add(column1);
%icon = Icon.LEGEND_24;
icon = Icon(fullfile(matlabroot,'toolbox','shared','controllib','general','resources','toolstrip_icons','Legend_24.png')); % PNG/JPG image file (not GIF!)
button = ToggleButton('Legend',icon);
button.Description = 'Toggle legend display';
addlistener(button, 'ValueChanged', @(h,e)legend('toggle'));
column1.add(button);
column2 = section2.addColumn();
imagefile = fullfile(matlabroot,'toolbox','matlab','icons','tool_colorbar.png');
jIcon = javax.swing.ImageIcon(imagefile); % Java ImageIcon from file (inc. GIF)
%jIcon = javax.swing.ImageIcon(jIcon.getImage.getScaledInstance(24,24,jIcon.getImage.SCALE_SMOOTH))  % Resize icon to 24x24
icon = Icon(jIcon);
button = ToggleButton('Colorbar',icon);
button.Description = 'Toggle colorbar display';
button.ValueChangedFcn = @toggleColorbar;
column2.addEmptyControl();
column2.add(button);
column2.addEmptyControl();
function toggleColorbar(hAction,hEventData)
    if hAction.Selected
        colorbar;
    else
        colorbar('off');
    end
end
</pre>
<h5 id="Section3">Checkboxes section (1 column 150px-wide), placed after the push-buttons section</h5>
<pre lang="matlab">
section3 = Section('Checkboxes');
section3.CollapsePriority = 1;
hTab.add(section3, 2);
column3 = section3.addColumn('HorizontalAlignment','left', 'Width',150);
button = CheckBox('Axes borders', true);
button.ValueChangedFcn = @toggleAxes;
button.Description = 'Axes borders';
column3.add(button);
function toggleAxes(hAction,hEventData)
    if hAction.Selected
        set(gca,'Visible','on');
    else
        set(gca,'Visible','off');
    end
end
button = CheckBox('Log scaling', false);
button.addlistener('ValueChanged',@toggleLogY);
button.Description = 'Log scaling';
column3.add(button);
function toggleLogY(hCheckbox,hEventData)
    if hCheckbox.Value, type = 'log'; else, type = 'linear'; end
    set(gca, 'XScale',type, 'YScale',type, 'ZScale',type);
end
button = CheckBox('Inverted Y', false);
button.addlistener('ValueChanged',@toggleInvY);
button.Description = 'Invert Y axis';
column3.add(button);
function toggleInvY(hCheckbox,~)
    if hCheckbox.Value, type = 'reverse'; else, type = 'normal'; end
    set(gca, 'YDir',type);
end
</pre>
<h3 id="summary">Summary</h3>
<p>Creating a custom app toolstrip requires careful planning of the tabs, sections, controls and their layout, as well as preparation of the icons, labels and callbacks. Once you start playing with the toolstrip API, you&#8217;ll see that it&#8217;s quite easy to understand and to use. I think MathWorks did a good job in general with this API, and it&#8217;s a pity that they did not make it public or official long ago (the MCOS API discussed above existed since 2014-2015; earlier versions existed at least as far back as 2011). Comparing the changes made in the API between R2018a and R2018b shows quite minor differences, which may possibly means that the API is now considered stable, and therefore that it might well be made public in some near-term future. Still, note that this API may well change in future releases (for example, naming of the control properties that I mentioned above). It works well in R2018b, as well as in the past several Matlab releases, but this could well change in the future, so beware.<br />
In the following posts I will discuss advanced control customizations (icons, callbacks, collapsibility etc.), complex controls (drop-downs, pop-ups, lists, button groups, items gallery etc.) and low-level toolstrip creation and customization. As I said above, Matlab toolstrips are quite an extensive subject and so I plan to proceed slowly, with each post building on its predecessors. Stay tuned!<br />
In the meantime, if you would like me to assist you in building a custom toolstrip or GUI for your Matlab program, <a href="https://undocumentedmatlab.com/consulting" target="_blank">please let me know</a>.</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization">Matlab toolstrip – part 3 (basic customization)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-6-complex-controls" rel="bookmark" title="Matlab toolstrip – part 6 (complex controls)">Matlab toolstrip – part 6 (complex controls) </a> <small>Multiple types of customizable controls can be added to Matlab toolstrips...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization/feed</wfw:commentRss>
			<slash:comments>21</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip – part 2 (ToolGroup App)</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-2-toolgroup-app</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 05 Dec 2018 17:00:48 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[Figure window]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Figure]]></category>
		<category><![CDATA[schema.prop]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=8059</guid>

					<description><![CDATA[<p>Matlab users can create custom Apps with toolstrips and docked figures. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app">Matlab toolstrip – part 2 (ToolGroup App)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1" rel="bookmark" title="Matlab toolstrip &#8211; part 1">Matlab toolstrip &#8211; part 1 </a> <small>Matlab contains extensive toolstrip (ribbon) functionality that can be integrated in user programs (GUI). ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>A while ago I posted the first of my planned miniseries on the <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1" target="_blank">Matlab toolstrip (ribbon)</a>. Today I will expand that post by discussing how toolstrips can be added to Matlab GUIs. This post will remain at a high-level as the previous post, with followup posts drilling into the technical details of the toolstrip components (inner packages and classes).<br />
We can add a Matlab toolstrip to 3 types of Matlab GUI windows:</p>
<ol>
<li>To a Java-based Matlab figure (so-called &#8220;legacy&#8221; figures, created using GUIDE or the <i><b>figure</b></i> function)</li>
<li>To a container window of docked Java-based figures, typically called an &#8220;App&#8221; (marketing name) or &#8220;Tool Group&#8221; (internal technical name)</li>
<li>To a JavaScript/HTML-based Matlab figure (so called &#8220;web&#8221; figures, created using App Designer or the <i><b>uifigure</b></i> function)</li>
</ol>
<p>Today I will show how to add a basic dynamic toolstrip to a ToolGroup (App, window type #2):<br />
<center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_2c_animated.gif" alt="ToolGroup with clients and dynamic toolstrip" title="ToolGroup with clients and dynamic toolstrip" width="450" height="314" /><figcaption class="wp-caption-text">ToolGroup with clients and dynamic toolstrip</figcaption></figure></center><br />
<span id="more-8059"></span></p>
<h3 id="ToolGroup">Figure containers (&#8220;Tool Groups&#8221;)</h3>
<p>Most Matlab users are familiar with window types #1 and #3 (legacy and web-based figures), but type #2 may seem strange. In fact, it shouldn&#8217;t be: All the Matlab &#8220;Apps&#8221; and Desktop components use such a container of docked clients. For example, both the Matlab Editor and Desktop are containers of individual client windows (individual files in the Editor; Command Window, Workspace etc. in the desktop).<br />
Similarly, when we dock figures, they dock as client windows into a container called &#8220;Figures&#8221; (this can be controlled programmatically: see my <a href="https://www.mathworks.com/matlabcentral/fileexchange/16650-setfigdockgroup" rel="nofollow" target="_blank"><i><b>setFigDockGroup</b></i> utility</a> on the File Exchange). This is the basis for all Matlab &#8220;Apps&#8221;, as far as I am aware (some Apps may possibly use a different GUI container, after all there are ~100 Matlab Apps and I&#8217;m not familiar with all of them). Such Apps are basically stand-alone Tool Groups (client container windows) that contain one or more docked figures, a toolstrip, and a side-panel with controls (so-called &#8220;Data Browser&#8221;).<br />
Note: MathWorks uses confusing terminology here, using the same term &#8220;App&#8221; for both MathWorks-created GUIs containers (that have toolstrips, Data Browser and docked figures) and also user-created utilities on the File Exchange (that do not have these). Unfortunately, MathWorks has chosen not [yet] to release to the general public its set of tools that enable creating true &#8220;Apps&#8221;, i.e. those that have a toolstrip, Data Browser and docked figures.<br />
Today&#8217;s post will attempt to fill this gap, by showing how we can create user Apps that have a toolstrip and docked figures. I will ignore the Data Browser today, and will describe it in a future post. Since docking figures into a standalone user-created container is a solved problem (using my <a href="https://www.mathworks.com/matlabcentral/fileexchange/16650-setfigdockgroup" rel="nofollow" target="_blank"><i><b>setFigDockGroup</b></i> utility</a>), this post will focus on adding a toolstrip to such a container.<br />
A ToolGroup object (<code>matlab.ui.internal.desktop.ToolGroup</code>) is created either implicitly (by docking a figure into a group that has a new name), or explicitly (by invoking its constructor):</p>
<pre lang="matlab">
% Create a new non-visible empty App (Tool Group)
hToolGroup = matlab.ui.internal.desktop.ToolGroup('Toolstrip example on UndocumentedMatlab.com');
</pre>
<p>Some things only work properly after the app is displayed, so let&#8217;s display the ToolGroup (however, note that for improved performance it is better to do whatever customizations and GUI updates that you can before the app is made visible):</p>
<pre lang="matlab">
% Display the ToolGroup window
hToolGroup.open();
</pre>
<p><center><figure style="width: 435px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_2b.png" alt="Basic empty ToolGroup (without toolstrip or clients)" title="Basic empty ToolGroup (without toolstrip or clients)" width="435" height="347" /><figcaption class="wp-caption-text">Basic empty ToolGroup (without toolstrip or clients)</figcaption></figure></center><br />
An annoying quirk with ToolGroups is that they automatically close when their reference handle is deleted from Matlab memory. The specific behavior changes depending on the contents of the container and the Matlab release, but in general it&#8217;s safest to preserve the <code>hToolGroup</code> variable, to prevent the window from closing, when this variable goes out of scope, when the function (in which we create the ToolGroup) returns. There are many ways to persist this variable. Here&#8217;s one alternative, in which we persist it in itself (or rather, attached to its internal Java peer control):</p>
<pre lang="matlab">
% Store toolgroup reference handle so that app will stay in memory
jToolGroup = hToolGroup.Peer;
internal.setJavaCustomData(jToolGroup, hToolGroup);
</pre>
<p><b><i>internal.setJavaCustomData</i></b> is an undocumented Matlab function that adds a new custom property to a Java reference handle. In our case, it adds a <b>CustomData</b> property to the <code>jToolGroup</code> handle and sets its value to the Matlab <code>hToolGroup</code> handle. The source code for <b><i>internal.setJavaCustomData</i></b> is available in <i>%matlabroot%/toolbox/shared/controllib/general/+internal/setJavaCustomData.m</i> and is very simple: it essentially uses the old <code>schema</code>-based <a href="https://undocumentedmatlab.com/articles/adding-dynamic-properties-to-graphic-handles" target="_blank"><i><b>schema.prop</b></i></a> method for adding new properties to handles. <code>Schema</code> is an old deprecated mechanism that is mostly replaced by the newer MCOS (Matlab Class Object System), but for some specific cases such as this it&#8217;s still very useful (the standard <a href="https://undocumentedmatlab.com/articles/adding-custom-properties-to-gui-objects" target="_blank"><i><b>addprop</b></i> function</a> can add new properties to Matlab GUI handles, but not to Java reference handles).<br />
Finally, let&#8217;s discard the Data Browser side panel (I&#8217;ll discuss it in a separate future post):</p>
<pre lang="matlab">
% Discard the Data-browser left panel
hToolGroup.disableDataBrowser();
</pre>
<h3 id="toolstrip">Adding a toolstrip to the ToolGroup</h3>
<p>Now that we have the basic container ready, let&#8217;s add a toolstrip. To simplify matters in this introductory post (after all, I have still not described the internal packages and classes that make up a toolstrip), we&#8217;ll use some of the tabs used in the <code>showcaseToolGroup</code> example that I discussed in <a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1" target="_blank">my previous post</a>. You can see the relevant source code in <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+desktop/*.m</i>, in case you want to jump ahead and customize your own toolstrip tabs, groups and buttons. In the code snippet below, we first create an empty <code>TabGroup</code>, then add toolstrip tabs into it, and finally add this <code>TabGroup</code> into our ToolGroup using its <i>addTabGroup(hTabGroup)</i> method:</p>
<pre lang="matlab">
% Create a new tab group
%hTabGroup = matlab.ui.internal.desktop.showcaseBuildTabGroup('swing');
hTabGroup = matlab.ui.internal.toolstrip.TabGroup();
hTab1 = matlab.ui.internal.desktop.showcaseBuildTab_Buttons('swing');
hTabGroup.add(hTab1);
%hTabGroup.add(matlab.ui.internal.desktop.showcaseBuildTab_Gallery());
hTabGroup.add(matlab.ui.internal.desktop.showcaseBuildTab_Layout('swing'));
% Select tab #1 (common)
hTabGroup.SelectedTab = hTab1;
% Add the tab group to the built-in toolstrip
hToolGroup.addTabGroup(hTabGroup);
</pre>
<p>We now have an &#8220;App&#8221; that has a toolstrip, but no clients (yet), and a hidden Data Browser side-panel:<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_2a.png" alt='ToolGroup "App" with a simple toolstrip (no clients yet)' title='ToolGroup "App" with a simple toolstrip (no clients yet)' width="80%" style="max-width:673px;" /></center><br />
Now let&#8217;s add some clients (docked figures):</p>
<h3 id="clients">Adding clients (docked figures) to the ToolGroup</h3>
<p>There are two easy variants for adding docked figures in a ToolGroup: The easiest is to use the ToolGroup&#8217;s <i>addFigure(hFigure)</i> method:</p>
<pre lang="matlab">
% Create a figure and dock it into the tool-group
hFig1 = figure('Name','3D');
surf(peaks);
hToolGroup.addFigure(hFig1);
</pre>
<p>The second variant enables to dock a figure that has a specific set of toolstrip tabs attached to it. These tabs will only display in the toolstrip when that particular figure has focus. We do this by creating a new TabGroup (just as we have done above), and then add the figure and TabGroup to the container using the ToolGroup&#8217;s <i>addClientTabGroup(hFigure,hTabGroup)</i> method:</p>
<pre lang="matlab">
% Create the 2nd figure
hFig2 = figure('Name','2D');
plot(rand(5)); drawnow
% Add a few tabs that are only relevant to this specific figure
hTabGroup2 = matlab.ui.internal.toolstrip.TabGroup();
hTab2 = matlab.ui.internal.desktop.showcaseBuildTab_Selections();
hTabGroup2.add(hTab2);
hTabGroup2.add(matlab.ui.internal.desktop.showcaseBuildTab_EditValue());
% Add the figure and tabs to the ToolGroup
hToolGroup.addClientTabGroup(hFig2, hTabGroup2);
</pre>
<p><center><figure style="width: 450px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://undocumentedmatlab.com/images/Toolstrip_2c_animated.gif" alt="ToolGroup with clients and dynamic toolstrip" title="ToolGroup with clients and dynamic toolstrip" width="450" height="314" /><figcaption class="wp-caption-text">ToolGroup with clients and dynamic toolstrip</figcaption></figure></center><br />
In this example, the &#8220;Selection&#8221; and &#8220;Values&#8221; toolstrip tabs only appear when the 2nd figure (&#8220;2D&#8221;) has focus. A similar behavior exists in the Matlab Desktop and Editor, where some tabs are only shown when certain clients have focus.</p>
<h3 id="View">Removing the View tab</h3>
<p>Note that the &#8220;View&#8221; toolstrip tab (which enables setting the appearance of the docked figures: layout, tab positions (top/bottom/left/right), ordering etc.) is automatically added to the toolstrip and always appears last. We can remove this View tab using the ToolGroup&#8217;s <i>hideViewTab()</i> method. The tab will not immediately be removed, only when the toolstrip is repainted, for example, when we switch focus between the docked figures:</p>
<pre lang="matlab">
hToolGroup.hideViewTab;  % toolstrip View tab is still visible at this point
figure(hFig1);  % change focus to hFig1 - toolstrip is repainted without View tab
</pre>
<h3 id="conclusion">Conclusion</h3>
<p>It&#8217;s relatively easy to dock figures into a standalone &#8220;App&#8221; window that has a custom toolstrip, which can even be dynamically modified based on the figure which is currently in focus. Naturally, this has little benefit if we cannot customize the toolstrip components: labels, icons, control type, grouping and most importantly &#8211; callbacks. This topic deserves a dedicated post, which I plan to be the next in this miniseries. Stay tuned &#8211; hopefully the next post will not take me as long to publish as this post (I was quite busy recently)&#8230;</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app">Matlab toolstrip – part 2 (ToolGroup App)</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1" rel="bookmark" title="Matlab toolstrip &#8211; part 1">Matlab toolstrip &#8211; part 1 </a> <small>Matlab contains extensive toolstrip (ribbon) functionality that can be integrated in user programs (GUI). ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-9-popup-figures" rel="bookmark" title="Matlab toolstrip – part 9 (popup figures)">Matlab toolstrip – part 9 (popup figures) </a> <small>Custom popup figures can be attached to Matlab GUI toolstrip controls. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app/feed</wfw:commentRss>
			<slash:comments>17</slash:comments>
		
		
			</item>
		<item>
		<title>Matlab toolstrip &#8211; part 1</title>
		<link>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=matlab-toolstrip-part-1</link>
					<comments>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Mon, 03 Sep 2018 15:00:22 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Semi-documented feature]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=7952</guid>

					<description><![CDATA[<p>Matlab contains extensive toolstrip (ribbon) functionality that can be integrated in user programs (GUI). </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1">Matlab toolstrip &#8211; part 1</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>The Matlab toolstrip (ribbon) has been around officially <a href="https://blogs.mathworks.com/loren/2012/09/12/the-matlab-r2012b-desktop-part-1-introduction-to-the-toolstrip" rel="nofollow" target="_blank">since R2012a</a>, and unofficially for <a href="https://undocumentedmatlab.com/articles/matlab-installation-woes#Java" target="_blank">a couple of years earlier</a>. Since then, I blogged about the toolstrip only rarely (<a href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2" target="_blank">example</a>). I believe the time has come to start a short mini-series about this functionality, eventually showing how users can use toolstrips in their own custom applications.<br />
My plan is to start the miniseries with a discussion of the built-in showcase examples, followed by a post on the built-in classes that make up the toolstrip building-blocks. Finally, I&#8217;ll describe how toolstrips can be added to figures, not just in client/tool groups.</p>
<h3 id="showcase">Matlab&#8217;s internal showcase examples</h3>
<p>I start the discussion with a description of built-in examples for the toolstrip functionality, located in <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+desktop/</i>. The most important of these are <i>showcaseToolGroup.m</i> and <i>showcaseMPCDesigner.m</i>, both of which use Java-based (Swing) containers and controls. Readers who wish to integrate toolstrips into their app immediately, without waiting for my followup posts in this series, are welcome to dig into the examples&#8217; source-code and replicate it in their programs:</p>
<h5 id="showcaseToolGroup">1. <code>showcaseToolGroup</code></h5>
<pre lang="matlab">h = matlab.ui.internal.desktop.showcaseToolGroup</pre>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/showcaseToolGroup.png" alt="showcaseToolGroup built-in example" title="showcaseToolGroup built-in example" width="80%" style="max-width:1028px; max-height:455px;" /></center><br />
<span id="more-7952"></span></p>
<h5 id="showcaseMPCDesigner">2. <code>showcaseMPCDesigner</code></h5>
<pre lang="matlab">
>> h = matlab.ui.internal.desktop.showcaseMPCDesigner
h =
  showcaseMPCDesigner with properties:
    ToolGroup: [1×1 matlab.ui.internal.desktop.ToolGroup]
       Dialog: [1×1 toolpack.component.TSTearOffPopup]
      Figure1: [1×1 Figure]
      Figure2: [1×1 Figure]
</pre>
<p><center><img decoding="async" src="https://undocumentedmatlab.com/images/showcaseMPCDesigner.png" alt="showcaseMPCDesigner built-in example" title="showcaseMPCDesigner built-in example" width="80%" style="max-width:994px; max-height:552px;" /></center></p>
<h5 id="showcaseHTML">3. <code>showcaseHTML</code> and <code>showcaseCEF</code></h5>
<p>In addition to these showcase examples, the folder also contains a <i>showcaseHTML.m</i> and <i>showcaseCEF.m</i> files, that are supposed to showcase the toolstrip functionality in JavaScript-based containers (browser webpage and uifigure apps, respectively). Unfortunately, on my system running these classes displays blank, although the toolstrip is indeed created, as seen below (if you find out how to make these classes work, please let me know):</p>
<pre lang="matlab">
>> h = matlab.ui.internal.desktop.showcaseHTML
building toolstrip hierarchy...
rendering toolstrip...
h =
  Toolstrip with properties:
               SelectedTab: [1×1 matlab.ui.internal.toolstrip.Tab]
              DisplayState: 'expanded'
    DisplayStateChangedFcn: @PropertyChangedCallback
                       Tag: 'toolstrip'
>> hs = struct(h)
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided.
Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
(Type "warning off MATLAB:structOnObject" to suppress this warning.)
hs =
  struct with fields:
                         SelectedTab: [1×1 matlab.ui.internal.toolstrip.Tab]
                        DisplayState: 'expanded'
              DisplayStateChangedFcn: @PropertyChangedCallback
                 DisplayStatePrivate: 'expanded'
                        QABIdPrivate: '2741bf89'
               QuickAccessBarPrivate: [1×1 matlab.ui.internal.toolstrip.impl.QuickAccessBar]
       DisplayStateChangedFcnPrivate: @PropertyChangedCallback
         SelectedTabChangedListeners: [1×1 event.listener]
                                 Tag: 'toolstrip'
                                Type: 'Toolstrip'
                          TagPrivate: 'toolstrip'
    WidgetPropertyMap_FromMCOSToPeer: [3×1 containers.Map]
    WidgetPropertyMap_FromPeerToMCOS: [3×1 containers.Map]
                              Parent: [0×0 matlab.ui.internal.toolstrip.base.Node]
                            Children: [1×1 matlab.ui.internal.toolstrip.TabGroup]
                             Parent_: []
                           Children_: [1×1 matlab.ui.internal.toolstrip.TabGroup]
                                Peer: [1×1 com.mathworks.peermodel.impl.PeerNodeImpl]
                   PropertySetSource: [1 java.util.HashMap]
                    PeerModelChannel: '/ToolstripShowcaseChannel'
                   PeerEventListener: [1×1 handle.listener]
                 PropertySetListener: [1×1 handle.listener]
>> hs.Peer
ans =
PeerNodeImpl{id='4a1e4b08', type='Toolstrip', properties={displayState=expanded, hostId=ToolStripShowcaseDIV, tag=toolstrip, QABId=2741bf89}, parent=878b0e2b, children=[
    PeerNodeImpl{id='5bb9632c', type='TabGroup', properties={QAGroupId=ea9b628c, tag=, selectedTab=f90db10c}, parent=4a1e4b08, children=[
        PeerNodeImpl{id='f90db10c', type='Tab', properties={mnemonic=, tag=tab_buttons, title=BUTTONS}, parent=5bb9632c, children=[
            PeerNodeImpl{id='1ccc9246', type='Section', properties={collapsePriority=0.0, mnemonic=, tag=sec_push, title=PUSH BUTTON}, parent=f90db10c, children=[
                PeerNodeImpl{id='8323f06e', type='Column', properties={horizontalAlignment=left, width=0.0, tag=}, parent=1ccc9246, children=[
                    PeerNodeImpl{id='af368d7b', type='PushButton', properties={textOverride=, descriptionOverride=, mnemonic=, actionId=230d471b, iconOverride=, tag=pushV, iconPathOverride=}, parent=8323f06e, children=[]}]}
                PeerNodeImpl{id='a557a712', type='Column', properties={horizontalAlignment=left, width=0.0, tag=}, parent=1ccc9246, children=[
                    PeerNodeImpl{id='f0d6a9fc', type='EmptyControl', properties={tag=}, parent=a557a712, children=[]}
                    PeerNodeImpl{id='74bc4cd2', type='PushButton', properties={textOverride=, descriptionOverride=, mnemonic=, actionId=12d6a26a, iconOverride=, tag=pushH, iconPathOverride=}, parent=a557a712, children=[]}
                    PeerNodeImpl{id='bcb5a9d0', type='EmptyControl', properties={tag=}, parent=a557a712, children=[]}]}]}
            PeerNodeImpl{id='0e515319', type='Section', properties={collapsePriority=0.0, mnemonic=, tag=sec_dropdown, title=DROP DOWN BUTTON}, parent=f90db10c, children=[
                PeerNodeImpl{id='80482225', type='Column', properties={horizontalAlignment=left, width=0.0, tag=}, parent=0e515319, children=[
                    PeerNodeImpl{id='469f469a', type='DropDownButton', properties={textOverride=, descriptionOverride=, mnemonic=, actionId=c6ca7335, iconOverride=, tag=dropdownV, iconPathOverride=}, parent=80482225, children=[]}]}
                ...
</pre>
<p>Note: <code>showcaseCEF</code> has been removed in 2018, but is available in older Matlab releases.</p>
<h3 id="levels">Levels of toolstrip encapsulation</h3>
<p>Matlab currently has several levels of encapsulation for toolstrip components:</p>
<ul>
<li>Top-level m-file classes for showcasing the toolstrip functionality and creating toolstrips in Java-based containers and web-based apps &#8211; these are located in <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+desktop/</i></li>
<li>Mid-level m-file classes that contain the toolstrip building blocks (tabs, sections, controls) &#8211; these are located in <i>%matlabroot%/toolbox/matlab/toolstrip/+matlab/+ui/+internal/+toolstrip/</i></li>
<li>Low-level Java classes that implement the underlying user-interface for Java-based UI &#8211; these are located in <i>%matlabroot%/java/jar/toolstrip.jar</i>. I <a href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2#custom" target="_blank">discussed this briefly</a> in a post few years ago.</li>
</ul>
<p>The top- and mid-level m-file classes are provided with full source code that is quite well-documented internally (as m-file source-code comments). However, note that it is not officially documented or supported (i.e., <a href="https://undocumentedmatlab.com/articles/tag/semi-documented-feature" target="_blank">semi-documented</a> in this blog&#8217;s parlance).<br />
The low-level Java classes on the other hand are compiled without available source code &#8211; we can inspect these classes (e.g., using <a href="https://undocumentedmatlab.com/articles/uiinspect" target="_blank">uiinspect</a> or <a href="https://undocumentedmatlab.com/articles/checkclass" target="_blank">checkClass</a>), but we cannot see their original source-code. Luckily, the higher-level m-file classes provide us with plenty of hints and usage examples that we could use to tailor the appearance, functionality and integration of toolstrip components into our app.</p>
<h3 id="Widgets_toolbox">Robyn Jackey&#8217;s Widgets Toolbox</h3>
<p>Users who hesitate to mess around with the built-in toolstrip functionality may find interest in MathWorker Robyn Jackey&#8217;s <a href="https://www.mathworks.com/matlabcentral/fileexchange/66235-widgets-toolbox?focused=a9526a57-50fa-052b-7343-3cbce74b2095&#038;tab=example" rel="nofollow" target="_blank">Toolstrip look-alike</a>, which is part of his open-source <a href="https://www.mathworks.com/matlabcentral/fileexchange/66235-widgets-toolbox" rel="nofollow" target="_blank">Widgets Toolbox</a> on the Matlab File Exchange. Unlike other parts of Robyn&#8217;s toolbox, which use undocumented functionality, his Toolstrip class seems to use documented components (panels, uicontrols etc.), with just a small reliance on undocumented functionality (<code>matlab.ui.*</code> for example). This has a fair chance to continue working well into future releases, even if Matlab&#8217;s built-in toolstrip functionality changes:<br />
<center><img decoding="async" src="https://undocumentedmatlab.com/images/demoToolstrip_05.png" alt="Robyn Jackey's Toolstrip look-alike" title="Robyn Jackey's Toolstrip look-alike" width="80%" style="max-width:835px; max-height:83px;" /></center></p>
<h3 id="caution">Strong caution</h3>
<p>Over the years, Matlab&#8217;s internal toolstrip interface has changed somewhat, but not dramatically. This could change at any time, since the toolstrip uses deeply undocumented functionality. What I will demonstrate over the next few posts might stop working in R2019a, or in R2025b &#8211; nobody really knows, perhaps not even MathWorks at this stage. Something that we do know for a fact is that Matlab is slowly transitioning away from Java-based user interfaces to web-based (HTML/JavaScript/CSS) interfaces, and this could have a drastic effect on the toolstrip functionality/API. It seems reasonable to assume that even if MathWorks would one day open up the toolstrip functionality, this would only be for the new web-based uifigure apps (not legacy Java-based figures), and might well have a different API than the one that I&#8217;ll discuss in this miniseries. Still, users could use the unofficial/undocumented information that I present here in their own Java figures today and quite possibly also in near-term upcoming releases.<br />
Despite the many unknowns regarding future supportability/roadmap of the built-in toolstrip API, I believe that my readers are smart enough to decide for themselves whether they want to take the associated risks to improve their Matlab programs today, or wait until a documented API will possibly be provided sometime in the future. The choice is yours, as it always is when using undocumented tips from my blog.<br />
With this warning stated, let&#8217;s start having fun with Matlab&#8217;s built-in toolstrip!</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1">Matlab toolstrip &#8211; part 1</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-3-basic-customization" rel="bookmark" title="Matlab toolstrip – part 3 (basic customization)">Matlab toolstrip – part 3 (basic customization) </a> <small>Matlab toolstrips can be created and customized in a variety of ways. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-2-toolgroup-app" rel="bookmark" title="Matlab toolstrip – part 2 (ToolGroup App)">Matlab toolstrip – part 2 (ToolGroup App) </a> <small>Matlab users can create custom Apps with toolstrips and docked figures. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-8-galleries" rel="bookmark" title="Matlab toolstrip – part 8 (galleries)">Matlab toolstrip – part 8 (galleries) </a> <small>Matlab toolstrips can contain customizable gallery panels of items. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/matlab-toolstrip-part-1/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
		<item>
		<title>Programmatic shortcuts manipulation &#8211; part 2</title>
		<link>https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=programmatic-shortcuts-manipulation-part-2</link>
					<comments>https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2#comments</comments>
		
		<dc:creator><![CDATA[Yair Altman]]></dc:creator>
		<pubDate>Wed, 30 Dec 2015 16:42:52 +0000</pubDate>
				<category><![CDATA[Desktop]]></category>
		<category><![CDATA[High risk of breaking in future versions]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Undocumented feature]]></category>
		<category><![CDATA[Internal component]]></category>
		<category><![CDATA[Toolstrip]]></category>
		<guid isPermaLink="false">http://undocumentedmatlab.com/?p=6169</guid>

					<description><![CDATA[<p>Non-standard shortcut controls and customizations can easily be added to the Matlab desktop. </p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2">Programmatic shortcuts manipulation &#8211; part 2</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1" rel="bookmark" title="Programmatic shortcuts manipulation &#8211; part 1">Programmatic shortcuts manipulation &#8211; part 1 </a> <small>Matlab Desktop shortcuts can be programmatically accessed and customized. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/command-window-text-manipulation" rel="bookmark" title="Command Window text manipulation">Command Window text manipulation </a> <small>Special control characters can be used to format text output in Matlab's Command Window. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
</ol>
</div>
]]></description>
										<content:encoded><![CDATA[<p>Today I will expand <a target="_blank" href="/articles/programmatic-shortcuts-manipulation-part-1">last week&#8217;s post</a> on customizing Matlab Desktop&#8217;s shortcuts. I will show that we can incorporate non-standard controls, and add tooltips and user callbacks in undocumented ways that are not available using the interactive Desktop GUI.<br />
<center><figure style="width: 584px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Custom shortcut controls" src="https://undocumentedmatlab.com/images/Shortcuts4c.png" title="Custom shortcut controls" width="584" height="182" /><figcaption class="wp-caption-text">Custom shortcut controls</figcaption></figure></center><br />
<span id="more-6169"></span><br />
Today&#8217;s article will focus on the new toolstrip interface of Matlab release R2012b and later; adaptation of the code to R2012a and earlier is relatively easy (in fact, simpler than the toolstrip-based code below).</p>
<h3 id="panel">Displaying the Shortcuts panel</h3>
<p>Before we begin to modify shortcuts in the Toolstrip&#8217;s shortcuts menu, we need to ensure that the Shortcuts panel is visible and active (in current focus), otherwise our customizations will be ignored or cause an error. There is probably a more direct way of doing this, but a simple way that I found was to edit the current Desktop&#8217;s layout to include a directive to display the Shortcuts tab, and then load that layout:</p>
<pre lang='matlab'>
jDesktop = com.mathworks.mde.desk.MLDesktop.getInstance;
hMainFrame = com.mathworks.mde.desk.MLDesktop.getInstance.getMainFrame;
jToolstrip = hMainFrame.getToolstrip;
isOk = jToolstrip.setCurrentTab('shortcuts');
if ~isOk  % i.e., Shortcuts tab is NOT displayed
    % Save the current Desktop layout
    jDesktop.saveLayout('Yair');  pause(0.15);
    % Update the layout file to display the Shortcuts tab
    filename = fullfile(prefdir, 'YairMATLABLayout.xml');
    fid = fopen(filename, 'rt');
    txt = fread(fid, '*char')';
    fclose(fid);
    txt = regexprep(txt,'(ShowShortcutsTab=)"[^"]*"','');
    txt = regexprep(txt,'(<layout [^>]*)>','$1 ShowShortcutsTab="yes">');
    fid = fopen(filename, 'wt');
    fwrite(fid,txt);
    fclose(fid);
    % Load the modified layout
    jDesktop.restoreLayout('Yair');  pause(0.15);
    % The shortcuts tab should now be visible, so transfer focus to that tab
    jToolstrip.setCurrentTab('shortcuts');
end
</pre>
<h3 id="custom">Custom controls</h3>
<p>As I explained in <a target="_blank" href="/articles/programmatic-shortcuts-manipulation-part-1#in-session">last week&#8217;s post</a>, we can use <code>scUtils.addShortcutToBottom</code> to add a simple push-button shortcut to the relevant category panel within the Shortcuts toolstrip tab. To add custom controls, we can simply add the controls to the relevant shortcut category panel container (a <code>com.mathworks.toolstrip.components.TSPanel</code> object). The standard shortcuts are typically placed in the Shortcuts tab&#8217;s second <code>TSPanel</code> (&#8220;general&#8221;), and other categories have <code>TSPanel</code>s of their own.<br />
Now here&#8217;s the tricky part about <code>TSPanel</code>s: we cannot directly add components to the sectino panel (that would be too easy&#8230;): the section panels are composed of an array of internal <code>TSPanel</code>s, and we need to add the new controls to those internal panels. However, these panels only contain 3 empty slots. If we try to add more than 3 components, the 4th+ component(s) will simply not be displayed. In such cases, we need to create a new <code>TSPanel</code> to display the extra components.<br />
Here then is some sample code to add a combo-box (drop-down) control:</p>
<pre lang='matlab'>
% First, get the last internal TSPanel within the Shortcuts tab's "general" section panel
% Note: jToolstrip was defined in the previous section above
jShortcutsTab = jToolstrip.getModel.get('shortcuts').getComponent;
jSectionPanel = jShortcutsTab.getSectionComponent(1).getSection.getComponent;  % the TSPanel object "general"
jContainer = jSectionPanel.getComponent(jSectionPanel.getComponentCount-1);
% If the last internal TSPanel is full, then prepare a new internal TSPanel next to it
if jContainer.getComponentCount >= 3
    % Create a new empty TSPanel and add it to the right of the last internal TSPanel
    jContainer = com.mathworks.toolstrip.components.TSPanel;
    jContainer.setPreferredSize(java.awt.Dimension(100,72));
    jSectionPanel.add(jContainer);
    jSectionPanel.repaint();
    jSectionPanel.revalidate();
end
% Create the new control with a custom tooltip and callback function
optionStrings = {'Project A', 'Project B', 'Project C'};
jCombo = com.mathworks.toolstrip.components.TSComboBox(optionStrings);
jCombo = handle(javaObjectEDT(jCombo), 'callbackproperties'));
set(jCombo, 'ActionPerformedCallback', @myCallbackFunction);
jCombo.setToolTipText('Select the requested project');
% Now add the new control to the internal TSPanel
jContainer.add(jCombo);
jContainer.repaint();
jContainer.revalidate();
</pre>
<p><center><figure style="width: 555px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Custom shortcut controls" src="https://undocumentedmatlab.com/images/Shortcuts4a.png" title="Custom shortcut controls" width="555" height="236" /><figcaption class="wp-caption-text">Custom shortcut controls</figcaption></figure></center><br />
Matlab&#8217;s internal <code>com.mathworks.toolstrip.components</code> package contains many embeddable controls, including the following (I emphasized those that I think are most useful within the context of the Shortcuts panel): <code><b>TSButton</b></code>, <code><b>TSCheckBox</b></code>, <code><b>TSComboBox</b></code>, <code>TSDropDownButton</code> (a custom combo-box component), <code>TSFormattedTextField</code>, <code>TSLabel</code>, <code><b>TSList</b></code>, <code><b>TSRadioButton</b></code>, <code>TSScrollPane</code>, <code><b>TSSlider</b></code>, <code><b>TSSpinner</b></code>, <code>TSSplitButton</code>, <code>TSTextArea</code>, <code><b>TSTextField</b></code>, and <code><b>TSToggleButton</b></code>. These controls are in most cases simple wrappers of the corresponding Java Swing controls. For example, <code>TSSpinner</code> extends the standard Swing <a target="_blank" rel="nofollow" href="https://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html"><code>JSpinner</code> control</a>. In some cases, the controls are more complex: for example, the <code>TSSplitButton</code> is similar to Matlab&#8217;s <a target="_blank" href="/articles/uisplittool-uitogglesplittool"><i><b>uisplittool</b></i> control</a>.<br />
<center><figure style="width: 430px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" alt="Toolstrip controls" src="https://undocumentedmatlab.com/images/Toolstrip_components.png" title="Toolstrip controls" width="430" height="140" /><figcaption class="wp-caption-text">Toolstrip controls</figcaption></figure></center><br />
In fact, these controls can be used even outside the toolstrip, embedded directly in our Matlab figure GUI, using the <a target="_blank" href="/articles/javacomponent"><i><b>javacomponent</b></i> function</a>. For example:</p>
<pre lang='matlab'>
dataModel = javax.swing.SpinnerNumberModel(125, 15, 225, 0.5);  % defaultValue, minValue, maxValue, stepSize
jSpinner = com.mathworks.toolstrip.components.TSSpinner(dataModel);
jSpinner = handle(javaObjectEDT(jSpinner), 'CallbackProperties');
[hjSpinner, hContainer] = javacomponent(jSpinner, [10,10,60,20], gcf);
</pre>
<p>You can find additional interesting components within the <i>%matlabroot%/java/jar/toolstrip.jar</i> file, which can be opened in any zip file utility or Java IDE. In fact, whatever controls that you see Matlab uses in its Desktop toolstrip (including galleries etc.) can be replicated in custom tabs, sections and panels of our own design.<br />
Matlab Desktop&#8217;s interactive GUI only enables creating simple push-button shortcuts having string callbacks (that are <i><b>eval</b></i>&#8216;ed in run-time). Using the undocumented programmatic interface that I just showed, we can include more sophisticated controls, as well as customize those controls in ways that are impossible via the programmatic GUI: add tooltips, set non-string (function-handle) callbacks, enable/disable controls, modify icons in run-time etc.<br />
For example (intentionally showing two separate ways of setting the component properties):</p>
<pre lang='matlab'>
% Toggle-button
jTB = handle(javaObjectEDT(com.mathworks.toolstrip.components.TSToggleButton('Toggle button')), 'CallbackProperties')
jTB.setSelected(true)
jTB.setToolTipText('toggle me!')
jTB.ActionPerformedCallback = @(h,e)doSomething();
jContainer.add(jTB);
% Check-box
jCB = handle(javaObjectEDT(com.mathworks.toolstrip.components.TSCheckBox('selected !')), 'CallbackProperties');
set(jCB, 'Selected', true, 'ToolTipText','Please select me!', 'ActionPerformedCallback',{@myCallbackFunction,extraData});
jContainer.add(jCB);
</pre>
<p>(resulting in the screenshot at the top of this post)<br />
<u><b>Important note</b></u>: none of these customizations is saved to file. Therefore, they need to be redone programmatically for each separate Matlab session. You can easily do that by calling the relevant code in your <i>startup.m</i> file.<br />
If you wish me to assist with any customization of the Desktop shortcuts, or any other Matlab aspect, then <a target="_blank" href="/contact">contact me</a> for a short consultancy.<br />
Happy New Year everybody!</p>
<p>The post <a rel="nofollow" href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2">Programmatic shortcuts manipulation &#8211; part 2</a> appeared first on <a rel="nofollow" href="https://undocumentedmatlab.com">Undocumented Matlab</a>.</p>
<div class='yarpp-related-rss'>
<h3>Related posts:</h3><ol>
<li><a href="https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-1" rel="bookmark" title="Programmatic shortcuts manipulation &#8211; part 1">Programmatic shortcuts manipulation &#8211; part 1 </a> <small>Matlab Desktop shortcuts can be programmatically accessed and customized. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/command-window-text-manipulation" rel="bookmark" title="Command Window text manipulation">Command Window text manipulation </a> <small>Special control characters can be used to format text output in Matlab's Command Window. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-5-icons" rel="bookmark" title="Matlab toolstrip – part 5 (icons)">Matlab toolstrip – part 5 (icons) </a> <small>Icons can be specified in various ways for toolstrip controls and the app window itself. ...</small></li>
<li><a href="https://undocumentedmatlab.com/articles/matlab-toolstrip-part-4-control-customization" rel="bookmark" title="Matlab toolstrip – part 4 (control customization)">Matlab toolstrip – part 4 (control customization) </a> <small>Matlab toolstrip components (controls) can be customized in various ways, including user-defined callbacks. ...</small></li>
</ol>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://undocumentedmatlab.com/articles/programmatic-shortcuts-manipulation-part-2/feed</wfw:commentRss>
			<slash:comments>6</slash:comments>
		
		
			</item>
	</channel>
</rss>
