1 | classdef Panel < uiextras.CardPanel & uiextras.DecoratedPanel |
---|
2 | %Panel Show one element inside a panel |
---|
3 | % |
---|
4 | % obj = uiextras.Panel() creates a standard UIPANEL object but 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.Panel(param,value,...) also sets one or more |
---|
11 | % property values. |
---|
12 | % |
---|
13 | % See the <a href="matlab:doc uiextras.Panel">documentation</a> for more detail and the list of properties. |
---|
14 | % |
---|
15 | % Examples: |
---|
16 | % >> f = figure(); |
---|
17 | % >> p = uiextras.Panel( 'Parent', f, 'Title', 'A Panel', 'Padding', 5 ); |
---|
18 | % >> uicontrol( 'Parent', p, 'Background', 'r' ) |
---|
19 | % |
---|
20 | % >> f = figure(); |
---|
21 | % >> p = uiextras.Panel( 'Parent', f, 'Title', 'A Panel', 'Padding', 5 ); |
---|
22 | % >> b = uiextras.HBox( 'Parent', p, 'Spacing', 5 ); |
---|
23 | % >> uicontrol( 'Style', 'listbox', 'Parent', b, 'String', {'Item 1','Item 2'} ); |
---|
24 | % >> uicontrol( 'Parent', b, 'Background', 'b' ); |
---|
25 | % >> set( b, 'Sizes', [100 -1] ); |
---|
26 | % |
---|
27 | % See also: uipanel |
---|
28 | % uiextras.BoxPanel |
---|
29 | % uiextras.HBox |
---|
30 | |
---|
31 | % Copyright 2009-2010 The MathWorks, Inc. |
---|
32 | % 1.1 |
---|
33 | % 2012/05/08 08:02:58 |
---|
34 | |
---|
35 | properties( Dependent = true ) |
---|
36 | BorderType % Type of border around the uipanel area [none|etchedin|etchedout|beveledin|beveledout|line] |
---|
37 | BorderWidth % Width of the panel border |
---|
38 | Title % Title string |
---|
39 | TitlePosition % Location of title string in relation to the panel [lefttop|centertop|righttop|leftbottom|centerbottom|rightbottom] |
---|
40 | end % dependent properties |
---|
41 | |
---|
42 | methods |
---|
43 | |
---|
44 | function obj = Panel( varargin ) |
---|
45 | %PANEL constructor |
---|
46 | |
---|
47 | % First step is to create the parent class. We pass the |
---|
48 | % arguments (if any) just incase the parent needs setting |
---|
49 | obj = obj@uiextras.CardPanel( varargin{:} ); |
---|
50 | obj = obj@uiextras.DecoratedPanel( varargin{:} ); |
---|
51 | |
---|
52 | set( obj.UIContainer, 'Title', '' ); |
---|
53 | |
---|
54 | % Override base settings |
---|
55 | obj.BorderType = 'etchedin'; |
---|
56 | obj.BorderWidth = 1; |
---|
57 | obj.TitlePosition = 'lefttop'; |
---|
58 | |
---|
59 | % See if the user has set any defaults |
---|
60 | obj.setPropertyFromDefault( 'BorderType' ); |
---|
61 | obj.setPropertyFromDefault( 'BorderWidth' ); |
---|
62 | obj.setPropertyFromDefault( 'TitlePosition' ); |
---|
63 | |
---|
64 | % Set user-supplied property values |
---|
65 | if nargin > 0 |
---|
66 | set( obj, varargin{:} ); |
---|
67 | end |
---|
68 | end % Panel |
---|
69 | |
---|
70 | end % public methods |
---|
71 | |
---|
72 | methods |
---|
73 | |
---|
74 | function value = get.BorderType( obj ) |
---|
75 | value = get( obj.UIContainer, 'BorderType' ); |
---|
76 | end % get.BorderType |
---|
77 | |
---|
78 | function set.BorderType( obj, value ) |
---|
79 | set( obj.UIContainer, 'BorderType', value ); |
---|
80 | end % set.BorderType |
---|
81 | |
---|
82 | function value = get.BorderWidth( obj ) |
---|
83 | value = get( obj.UIContainer, 'BorderWidth' ); |
---|
84 | end % get.BorderWidth |
---|
85 | |
---|
86 | function set.BorderWidth( obj, value ) |
---|
87 | set( obj.UIContainer, 'BorderWidth', value ); |
---|
88 | obj.redraw(); |
---|
89 | end % set.BorderWidth |
---|
90 | |
---|
91 | function value = get.Title( obj ) |
---|
92 | value = get( obj.UIContainer, 'Title' ); |
---|
93 | end % get.Title |
---|
94 | |
---|
95 | function set.Title( obj, value ) |
---|
96 | % Only need to redraw if changing to/from empty |
---|
97 | oldValue = get( obj.UIContainer, 'Title' ); |
---|
98 | % Unfortunately setting the title creates a uicontrol that |
---|
99 | % isn't tagged, so we have to set a flag so that we know to |
---|
100 | % ignore it for resize purposes |
---|
101 | setappdata( obj.UIContainer, 'PanelTitleCreate', true ); |
---|
102 | set( obj.UIContainer, 'Title', value ); |
---|
103 | rmappdata( obj.UIContainer, 'PanelTitleCreate' ); |
---|
104 | if isempty( value ) && ~isempty( oldValue ) |
---|
105 | obj.redraw(); |
---|
106 | elseif isempty( oldValue ) && ~isempty( value ) |
---|
107 | obj.redraw(); |
---|
108 | end |
---|
109 | end % set.Title |
---|
110 | |
---|
111 | function value = get.TitlePosition( obj ) |
---|
112 | value = get( obj.UIContainer, 'TitlePosition' ); |
---|
113 | end % get.TitlePosition |
---|
114 | |
---|
115 | function set.TitlePosition( obj, value ) |
---|
116 | set( obj.UIContainer, 'TitlePosition', value ); |
---|
117 | obj.redraw(); |
---|
118 | end % set.TitlePosition |
---|
119 | |
---|
120 | end % accessor methods |
---|
121 | |
---|
122 | methods( Access = protected ) |
---|
123 | |
---|
124 | function redraw( obj ) |
---|
125 | %redraw Redraw the layout, positioning the children |
---|
126 | |
---|
127 | if isempty( obj.UIContainer ) || ~ishandle( obj.UIContainer ) |
---|
128 | return |
---|
129 | end |
---|
130 | |
---|
131 | % The selected one inherits the visibility of the layout and |
---|
132 | % fills the available space |
---|
133 | oldUnits = obj.Units; |
---|
134 | obj.Units = 'Pixels'; |
---|
135 | pos = getpixelposition( obj ); |
---|
136 | border = obj.BorderWidth+1+obj.Padding; |
---|
137 | x0 = border; |
---|
138 | y0 = border; |
---|
139 | w = pos(3) - 2*border; |
---|
140 | h = pos(4) - 2*border; |
---|
141 | if ~isempty( obj.Title ) |
---|
142 | % Work out how much extra space to leave for the title |
---|
143 | oldFontUnits = get( obj.UIContainer, 'FontUnits' ); |
---|
144 | set( obj.UIContainer, 'FontUnits', 'Pixels' ); |
---|
145 | % Get the height of the title (in pixels) |
---|
146 | titleSize = get( obj.UIContainer, 'FontSize' ); |
---|
147 | % Put the old units back |
---|
148 | set( obj.UIContainer, 'FontUnits', oldFontUnits ); |
---|
149 | |
---|
150 | % Whether to move top or bottom depends on title |
---|
151 | % position |
---|
152 | if isempty( strfind( get( obj.UIContainer, 'TitlePosition' ), 'top' ) ) |
---|
153 | % Title at the bottom |
---|
154 | h = h - titleSize; |
---|
155 | y0 = y0 + titleSize; |
---|
156 | else |
---|
157 | % Title at the top |
---|
158 | h = h - titleSize; |
---|
159 | end |
---|
160 | |
---|
161 | end |
---|
162 | % Use the CardLayout function to put the right child onscreen |
---|
163 | obj.showSelectedChild( [x0 y0 w h] ); |
---|
164 | obj.Units = oldUnits; |
---|
165 | end % redraw |
---|
166 | |
---|
167 | function onChildAdded( obj, source, eventData ) %#ok<INUSD> |
---|
168 | %onChildAdded: Callback that fires when a child is added to a container. |
---|
169 | % Select the new addition |
---|
170 | obj.SelectedChild = numel( obj.Children ); |
---|
171 | end % onChildAdded |
---|
172 | |
---|
173 | function onChildRemoved( obj, source, eventData ) %#ok<INUSL> |
---|
174 | %onChildAdded: Callback that fires when a container child is destroyed or reparented. |
---|
175 | % If the missing child is the selected one, select something else |
---|
176 | if eventData.ChildIndex == obj.SelectedChild |
---|
177 | if isempty( obj.Children ) |
---|
178 | obj.SelectedChild = []; |
---|
179 | else |
---|
180 | obj.SelectedChild = max( 1, obj.SelectedChild - 1 ); |
---|
181 | end |
---|
182 | end |
---|
183 | end % onChildRemoved |
---|
184 | |
---|
185 | function onEnable( obj, source, eventData ) %#ok<INUSD> |
---|
186 | %onEnable Callback that fires when the enable state is changed |
---|
187 | t = findall( obj.UIContainer, ... |
---|
188 | 'Type', 'uicontrol', ... |
---|
189 | 'HandleVisibility', 'off', ... |
---|
190 | 'Parent', obj.UIContainer ); |
---|
191 | set( t, 'Enable', obj.Enable ); |
---|
192 | end % onChildRemoved |
---|
193 | |
---|
194 | function onBackgroundColorChanged( obj, source, eventData ) %#ok<INUSL> |
---|
195 | %onBackgroundColorChanged Callback that fires when the container background color is changed |
---|
196 | % |
---|
197 | % We need to make the panel match the container background |
---|
198 | set( obj.UIContainer, 'BackgroundColor', eventData ); |
---|
199 | end % onChildRemoved |
---|
200 | |
---|
201 | function onPanelColorChanged( obj, source, eventData ) %#ok<INUSL> |
---|
202 | % Colors have changed. This shouldn't require a redraw. |
---|
203 | set( obj.UIContainer, eventData.Property, eventData.Value ); |
---|
204 | end % onPanelColorChanged |
---|
205 | |
---|
206 | function onPanelFontChanged( obj, source, eventData ) %#ok<INUSL> |
---|
207 | % Font has changed. Since the font size and shape affects the |
---|
208 | % space available for the contents, we need to redraw. |
---|
209 | set( obj.UIContainer, eventData.Property, eventData.Value ); |
---|
210 | obj.redraw(); |
---|
211 | end % onPanelFontChanged |
---|
212 | |
---|
213 | end % protected methods |
---|
214 | |
---|
215 | end % classdef |
---|