source: MML/trunk/machine/SOLEIL/common/toolbox/GUILayout/+uiextras/BoxPanel.m @ 4

Last change on this file since 4 was 4, checked in by zhangj, 10 years ago

Initial import--MML version from SOLEIL@2013

File size: 17.0 KB
Line 
1classdef BoxPanel < uiextras.CardPanel & uiextras.DecoratedPanel
2    %BoxPanel  Show one element inside a box panel
3    %
4    %   obj = uiextras.BoxPanel() creates a box-styled panel object with
5    %   automatic management of the contained widget or layout. The
6    %   properties available are largely the same as the builtin UIPANEL
7    %   object. Where more than one child is added, the currently visible
8    %   child is determined using the SelectedChild property.
9    %
10    %   obj = uiextras.BoxPanel(param,value,...) also sets one or more
11    %   property values.
12    %
13    %   See the <a href="matlab:doc uiextras.BoxPanel">documentation</a> for more detail and the list of properties.
14    %
15    %   Examples:
16    %   >> f = figure();
17    %   >> p = uiextras.BoxPanel( 'Parent', f, 'Title', 'A BoxPanel', 'Padding', 5 );
18    %   >> uicontrol( 'Style', 'frame', 'Parent', p, 'Background', 'r' )
19    %
20    %   >> f = figure();
21    %   >> p = uiextras.BoxPanel( 'Parent', f, 'Title', 'A BoxPanel', 'Padding', 5 );
22    %   >> b = uiextras.HBox( 'Parent', p, 'Spacing', 5 );
23    %   >> uicontrol( 'Style', 'listbox', 'Parent', b, 'String', {'Item 1','Item 2'} );
24    %   >> uicontrol( 'Style', 'frame', 'Parent', b, 'Background', 'b' );
25    %   >> set( b, 'Sizes', [100 -1] );
26    %   >> p.FontSize = 12;
27    %   >> p.FontWeight = 'bold';
28    %   >> p.HelpFcn = @(x,y) disp('Help me!');
29    %
30    %   See also: uiextras.Panel
31    %             uiextras.TabPanel
32    %             uiextras.HBoxFlex
33   
34    %   Copyright 2009-2010 The MathWorks, Inc.
35    %   1.1
36    %   2012/05/08 08:02:58
37   
38    properties
39        IsMinimized = false
40        IsDocked = true
41    end % public properties
42   
43    properties( Dependent = true )
44        CloseRequestFcn
45        HelpFcn
46        MinimizeFcn
47        DockFcn
48        Title
49        TitleColor
50        TooltipString
51    end % dependent properties
52   
53    properties( SetAccess = private, GetAccess = private, Hidden = true )
54        HGWidgets_ = struct()
55    end % private properties
56   
57    methods
58       
59        function obj = BoxPanel(varargin)
60            % First step is to create the parent class. We pass the
61            % arguments (if any) just incase the parent needs setting
62            obj = obj@uiextras.CardPanel( varargin{:} );
63            obj = obj@uiextras.DecoratedPanel( varargin{:} );
64           
65            % For this container we need the border on
66            set( obj.UIContainer, 'BorderType', 'etchedin' );
67           
68            % Text control for title
69            figh = ancestor( obj.UIContainer, 'figure' );
70            contextmenu = uicontextmenu( 'Parent', figh );
71            titleColor = [0.75 0.9 1.0];
72            obj.HGWidgets_.TitleText = uicontrol('parent', obj.UIContainer, ...
73                'Visible', obj.Visible, ...
74                'units', 'pixels', ...
75                'HitTest', 'off', ...
76                'style', 'text', ...
77                'string', '', ...
78                'horizontalalignment', 'left',...
79                'BackgroundColor', titleColor, ...
80                'ForegroundColor', obj.ForegroundColor, ...
81                'FontSize', obj.FontSize, ...
82                'FontName', obj.FontName, ...
83                'FontAngle', obj.FontAngle, ...
84                'FontWeight', obj.FontWeight, ...
85                'FontUnits', obj.FontUnits, ...
86                'UIContextMenu', contextmenu, ...
87                'HandleVisibility', 'off', ...
88                'Tag', 'uiextras:BoxPanel:TitleText');
89           
90            % Panel for title
91            obj.HGWidgets_.TitlePanel = uipanel('parent', obj.UIContainer, ...
92                'Visible', obj.Visible, ...
93                'units', 'pixels', ...
94                'HitTest', 'off', ...
95                'bordertype', 'etchedin', ...
96                'BackgroundColor', titleColor, ...
97                'Tag', 'uiextras:BoxPanel:TitlePanel', ...
98                'HandleVisibility', 'off', ...
99                'UIContextMenu', contextmenu);
100           
101            % Create the buttons
102            obj.HGWidgets_.HelpButton = uicontrol('parent', obj.UIContainer, ...
103                'style', 'checkbox', ...
104                'cdata', uiextras.loadLayoutIcon( 'panelHelp.png' ), ...
105                'BackgroundColor', titleColor, ...
106                'Visible', 'off', ...
107                'Tag', 'uiextras:BoxPanel:HelpButton', ...
108                'HandleVisibility', 'off', ...
109                'tooltip', 'Get help on this panel' );
110           
111            obj.HGWidgets_.CloseButton = uicontrol('parent', obj.UIContainer, ...
112                'style', 'checkbox', ...
113                'cdata', uiextras.loadLayoutIcon( 'panelClose.png' ), ...
114                'BackgroundColor', titleColor, ...
115                'Visible', 'off', ...
116                'Tag', 'uiextras:BoxPanel:CloseButton', ...
117                'HandleVisibility', 'off', ...
118                'tooltip', 'Close this panel' );
119           
120            obj.HGWidgets_.DockButton = uicontrol('parent', obj.UIContainer, ...
121                'style', 'checkbox', ...
122                'cdata', uiextras.loadLayoutIcon( 'panelUndock.png' ), ...
123                'BackgroundColor', titleColor, ...
124                'Visible', 'off', ...
125                'Tag', 'uiextras:BoxPanel:DockButton', ...
126                'HandleVisibility', 'off', ...
127                'tooltip', 'Undock this panel' );
128           
129            obj.HGWidgets_.MinimizeButton = uicontrol('parent', obj.UIContainer, ...
130                'style', 'checkbox', ...
131                'cdata', uiextras.loadLayoutIcon( 'panelMinimize.png' ), ...
132                'BackgroundColor', titleColor, ...
133                'Visible', 'off', ...
134                'Tag', 'uiextras:BoxPanel:MinimizeButton', ...
135                'HandleVisibility', 'off', ...
136                'tooltip', 'Minimize this panel' );
137           
138            % Get some defaults
139            obj.setPropertyFromDefault( 'TitleColor' );
140           
141            % Parse any input arguments
142            if nargin>0
143                set( obj, varargin{:} );
144            end
145            % Redraw both contents and styling
146            obj.redraw();
147        end % constructor
148    end % public methods
149   
150    methods
151       
152        function set.Title( obj, value )
153            set( obj.HGWidgets_.TitleText, 'String', value );
154            obj.redraw();
155        end % set.Title
156       
157        function value = get.Title( obj )
158            value = get( obj.HGWidgets_.TitleText, 'String' );
159        end % get.Title
160       
161        function set.HelpFcn( obj, value )
162            if isempty( value )
163                set( obj.HGWidgets_.HelpButton, 'Visible', 'off', 'Callback', [] );
164            else
165                set( obj.HGWidgets_.HelpButton, 'Visible', obj.Visible, 'Callback', value );
166            end
167        end % set.HelpFcn
168       
169        function set.CloseRequestFcn( obj, value )
170            if isempty( value )
171                set( obj.HGWidgets_.CloseButton, 'Visible', 'off', 'Callback', [] );
172            else
173                set( obj.HGWidgets_.CloseButton, 'Visible', obj.Visible, 'Callback', value );
174            end
175        end % set.CloseRequestFcn
176       
177        function set.MinimizeFcn( obj, value )
178            if isempty( value )
179                set( obj.HGWidgets_.MinimizeButton, 'Visible', 'off', 'Callback', [] );
180            else
181                set( obj.HGWidgets_.MinimizeButton, 'Visible', obj.Visible, 'Callback', value );
182            end
183        end % set.MinimizeFcn
184       
185        function set.IsMinimized( obj, value )
186            obj.IsMinimized = (value(1) == true);
187            if value( obj.IsMinimized )
188                set( obj.HGWidgets_.MinimizeButton, ...
189                    'cdata', uiextras.loadLayoutIcon( 'panelMaximize.png' ), ...
190                    'tooltip', 'Maximize this panel' ); %#ok<MCSUP>
191            else
192                set( obj.HGWidgets_.MinimizeButton, ...
193                    'cdata', uiextras.loadLayoutIcon( 'panelMinimize.png' ), ...
194                    'tooltip', 'Minimize this panel' ); %#ok<MCSUP>
195            end
196        end % set.IsMinimized
197       
198        function set.DockFcn( obj, value )
199            if isempty( value )
200                set( obj.HGWidgets_.DockButton, 'Visible', 'off', 'Callback', [] );
201            else
202                set( obj.HGWidgets_.DockButton, 'Visible', obj.Visible, 'Callback', value );
203            end
204        end % set.DockFcn
205       
206        function set.IsDocked( obj, value )
207            obj.IsDocked = (value(1) == true);
208            if value( obj.IsDocked )
209                set( obj.HGWidgets_.DockButton, ...
210                    'cdata', uiextras.loadLayoutIcon( 'panelUndock.png' ), ...
211                    'tooltip', 'Undock this panel' ); %#ok<MCSUP>
212            else
213                set( obj.HGWidgets_.DockButton, ...
214                    'cdata', uiextras.loadLayoutIcon( 'panelDock.png' ), ...
215                    'tooltip', 'Dock this panel' ); %#ok<MCSUP>
216            end
217        end % set.IsMinimized
218       
219        function value = get.CloseRequestFcn( obj )
220            value = get( obj.HGWidgets_.CloseButton, 'Callback' );
221        end % get.CloseRequestFcn
222       
223        function value = get.HelpFcn( obj )
224            value = get( obj.HGWidgets_.HelpButton, 'Callback' );
225        end % get.HelpFcn
226       
227        function value = get.MinimizeFcn( obj )
228            value = get( obj.HGWidgets_.MinimizeButton, 'Callback' );
229        end % get.MinimizeFcn
230       
231        function value = get.DockFcn( obj )
232            value = get( obj.HGWidgets_.DockButton, 'Callback' );
233        end % get.DockFcn
234       
235        function set.TitleColor( obj, value )
236            if isfield( obj.HGWidgets_, 'TitleText' )
237                set( obj.HGWidgets_.TitleText, 'BackgroundColor', value );
238            end
239            if isfield( obj.HGWidgets_, 'TitlePanel' )
240                set( obj.HGWidgets_.TitlePanel, 'BackgroundColor', value );
241            end
242            if isfield( obj.HGWidgets_, 'HelpButton' )
243                set( obj.HGWidgets_.HelpButton, 'BackgroundColor', value );
244            end
245            if isfield( obj.HGWidgets_, 'CloseButton' )
246                set( obj.HGWidgets_.CloseButton, 'BackgroundColor', value );
247            end
248        end % set.TitleColor
249       
250        function value = get.TitleColor( obj )
251            value = get( obj.HGWidgets_.TitleText, 'BackgroundColor' );
252        end % get.TitleColor
253       
254        function set.TooltipString( obj, value )
255            set( obj.HGWidgets_.TitleText, 'TooltipString', value );
256        end % set.TooltipString
257       
258        function value = get.TooltipString( obj )
259            value = get( obj.HGWidgets_.TitleText, 'TooltipString' );
260        end % get.TooltipString
261       
262       
263    end % accessor methods
264   
265    methods( Access = protected )
266       
267        function redraw( obj )
268            %redraw  Redraw this widget and its contents
269            pos = getpixelposition( obj.UIContainer );
270            decor = obj.HGWidgets_;
271           
272            % If size is too small, ignore
273            if (pos(3)<12) || (pos(4)<18)
274                return
275            end
276           
277            % If decorations not yet constructed, ignore
278            if ~isstruct( decor ) ...
279                    || isempty(fieldnames(decor)) ...
280                    || ~isfield( decor, 'MinimizeButton' ) % this is the last widget constructed
281                return
282            end
283           
284            % Work out the title height
285            if isempty( obj.Title )
286                titleSize = 14;
287            else
288                % Work out how much extra space to leave for the title
289                oldunits = get( decor.TitleText, 'FontUnits' );
290                set( decor.TitleText, 'FontUnits', 'Pixels' );
291                % Get the height of the title (in pixels) and add a bit to
292                % cope with letters below the baseline (e.g. 'g')
293                titleSize = ceil( get( decor.TitleText, 'FontSize' )*1.2 );
294                % Put the old units back
295                set( decor.TitleText, 'FontUnits', oldunits );
296            end
297           
298            % Set position of close button
299            buttonXPos = pos(3)-2;
300            buttonWidth = titleSize;
301            if ~isempty(decor.CloseButton)
302                set(decor.CloseButton, 'Position', [buttonXPos-buttonWidth, pos(4)-titleSize, buttonWidth-1, buttonWidth-2]);
303                if ~isempty( obj.CloseRequestFcn )
304                    set(decor.CloseButton,'Visible', 'on' );
305                    buttonXPos = buttonXPos - buttonWidth;
306                else
307                    set(decor.CloseButton,'Visible', 'off' );
308                end
309            end
310           
311            % Set position of dock button
312            if ~isempty(decor.DockButton)
313                set(decor.DockButton, 'Position', [buttonXPos-buttonWidth, pos(4)-titleSize, buttonWidth-1, buttonWidth-2]);
314                if ~isempty( obj.DockFcn )
315                    set(decor.DockButton,'Visible', 'on' );
316                    buttonXPos = buttonXPos - buttonWidth;
317                else
318                    set(decor.DockButton,'Visible', 'off' );
319                end
320            end
321           
322            % Set position of minimise button
323            if ~isempty(decor.MinimizeButton)
324                set(decor.MinimizeButton, 'Position', [buttonXPos-buttonWidth, pos(4)-titleSize, buttonWidth-1, buttonWidth-2]);
325                if ~isempty( obj.MinimizeFcn )
326                    set(decor.MinimizeButton,'Visible', 'on' );
327                    buttonXPos = buttonXPos - buttonWidth;
328                else
329                    set(decor.MinimizeButton,'Visible', 'off' );
330                end
331            end
332           
333            % Set position of help button
334            if ~isempty(decor.HelpButton)
335                set(decor.HelpButton, 'Position', [buttonXPos-buttonWidth, pos(4)-titleSize, buttonWidth-1, buttonWidth-2]);
336                if ~isempty( obj.HelpFcn)
337                    set(decor.HelpButton,'Visible', 'on' );
338                    buttonXPos = buttonXPos - buttonWidth;
339                else
340                    set(decor.HelpButton,'Visible', 'off' );
341                end
342            end
343           
344            % Set position of title
345            posText = [2, pos(4)-titleSize-1, buttonXPos-3, titleSize];
346            set(decor.TitleText, 'Position', posText, 'string', obj.Title );
347           
348            % Set position of panel for title
349            posTitle = [-1, pos(4)-titleSize-3, pos(3), titleSize+4];
350            set(decor.TitlePanel, 'Position', posTitle);
351           
352            % Work out where to put the contents
353            panelborder = 2;
354            x0 = obj.Padding+1;
355            y0 = obj.Padding+1;
356            w = pos(3) - 2*panelborder - 2*obj.Padding;
357            h = pos(4) - titleSize - 2*panelborder - 2*obj.Padding;
358            contentPos = [x0 y0 w h];
359           
360            % Use the CardLayout function to put the right child onscreen
361            obj.showSelectedChild( contentPos )
362           
363        end % redraw
364       
365        function onChildAdded( obj, source, eventData ) %#ok<INUSD>
366            %onChildAdded  A child has been added to a container
367            % Select the new addition
368            obj.SelectedChild = numel( obj.Children );
369        end % onChildAdded
370       
371        function onChildRemoved( obj, source, eventData ) %#ok<INUSL>
372            %onChildAdded  A container child has been destroyed or reparented
373            %
374            % If the missing child is the selected one, select something else
375            if eventData.ChildIndex == obj.SelectedChild
376                if isempty( obj.Children )
377                    obj.SelectedChild = [];
378                else
379                    obj.SelectedChild = max( 1, obj.SelectedChild - 1 );
380                end
381            end
382        end % onChildRemoved
383       
384        function onEnable(obj, source, eventData ) %#ok<INUSL>
385            %onEnable  Enable state has been changed, so update
386            set( obj.HGWidgets_.TitleText, 'Enable', eventData );
387        end % onEnable
388       
389        function onPanelColorChanged( obj, source, eventData ) %#ok<INUSD>
390            %onPanelColorChanged  Colors have been changed, so update
391            if isfield( obj.HGWidgets_, 'TitleText' )
392                set( obj.HGWidgets_.TitleText, 'ForegroundColor', obj.ForegroundColor );
393                set( obj.HGWidgets_.TitlePanel, ...
394                    'HighlightColor', obj.HighlightColor, ...
395                    'ShadowColor', obj.ShadowColor );
396                set( obj.UIContainer, ...
397                    'HighlightColor', obj.HighlightColor, ...
398                    'ShadowColor', obj.ShadowColor );
399            end
400        end % onPanelColorChanged
401       
402        function onPanelFontChanged( obj, source, eventData ) %#ok<INUSL>
403            % Font has changed. Since the font size and shape affects the
404            % space available for the contents, we need to redraw.
405            if isfield( obj.HGWidgets_, 'TitleText' )
406                set( obj.HGWidgets_.TitleText, eventData.Property, eventData.Value );
407                obj.redraw();
408            end
409        end % onPanelFontChanged
410       
411    end % protected methods
412   
413end % classdef
Note: See TracBrowser for help on using the repository browser.