1 | classdef HButtonBox < uiextras.ButtonBox |
---|
2 | %HButtonBox Arrange buttons horizontally in a single row |
---|
3 | % |
---|
4 | % obj = uiextras.HButtonBox() is a type of HBox specialised for |
---|
5 | % arranging a row of buttons, check-boxes or similar graphical |
---|
6 | % elements. All buttons are given equal size and by default are |
---|
7 | % centered in the drawing area. The justification can be changed as |
---|
8 | % required. |
---|
9 | % |
---|
10 | % obj = uiextras.HButtonBox(param,value,...) also sets one or more |
---|
11 | % parameter values. |
---|
12 | % |
---|
13 | % See the <a href="matlab:doc uiextras.HButtonBox">documentation</a> for more detail and the list of properties. |
---|
14 | % |
---|
15 | % Examples: |
---|
16 | % >> f = figure(); |
---|
17 | % >> b = uiextras.HButtonBox( 'Parent', f ); |
---|
18 | % >> uicontrol( 'Parent', b, 'String', 'One' ); |
---|
19 | % >> uicontrol( 'Parent', b, 'String', 'Two' ); |
---|
20 | % >> uicontrol( 'Parent', b, 'String', 'Three' ); |
---|
21 | % >> set( b, 'ButtonSize', [130 35], 'Spacing', 5 ); |
---|
22 | % |
---|
23 | % See also: uiextras.VButtonBox |
---|
24 | % uiextras.HBox |
---|
25 | |
---|
26 | % Copyright 2009-2010 The MathWorks, Inc. |
---|
27 | % 1.1 |
---|
28 | % 2012/05/08 08:02:59 |
---|
29 | |
---|
30 | methods |
---|
31 | |
---|
32 | function obj = HButtonBox( varargin ) |
---|
33 | %HButtonBox Create a new horizontal button box |
---|
34 | |
---|
35 | % First step is to create the parent class. We pass the |
---|
36 | % arguments (if any) just incase the parent needs setting |
---|
37 | obj@uiextras.ButtonBox( varargin{:} ); |
---|
38 | |
---|
39 | % Set properties |
---|
40 | if nargin > 0 |
---|
41 | set( obj, varargin{:} ); |
---|
42 | end |
---|
43 | end % constructor |
---|
44 | |
---|
45 | end % public methods |
---|
46 | |
---|
47 | methods( Access = protected ) |
---|
48 | |
---|
49 | function redraw( obj ) |
---|
50 | % First we need to work out how much space is available - if |
---|
51 | % there's not enough for the desired size then the buttons may |
---|
52 | % have to shrink |
---|
53 | children = obj.getValidChildren(); |
---|
54 | nChildren = numel( children ); |
---|
55 | pos = ceil( getpixelposition( obj.UIContainer ) ); |
---|
56 | availableWidth = pos(3) - 2*obj.Padding - (nChildren-1)*obj.Spacing; |
---|
57 | availableHeight = pos(4) - 2*obj.Padding; |
---|
58 | |
---|
59 | buttonWidth = min( obj.ButtonSize(1), availableWidth/nChildren ); |
---|
60 | buttonHeight = min( obj.ButtonSize(2), availableHeight ); |
---|
61 | |
---|
62 | % The positioning depends on the alignment |
---|
63 | switch upper( obj.VerticalAlignment ) |
---|
64 | case 'TOP' |
---|
65 | y0 = pos(4) - obj.Padding - buttonHeight; |
---|
66 | |
---|
67 | case 'MIDDLE' |
---|
68 | y0 = 1 + round( ( pos(4) - buttonHeight ) / 2 ); |
---|
69 | |
---|
70 | case 'BOTTOM' |
---|
71 | y0 = 1 + obj.Padding; |
---|
72 | |
---|
73 | otherwise |
---|
74 | error( 'GUILayout:InvalidState', ... |
---|
75 | 'Invalid vertical alignment ''%s''.', obj.VerticalAlignment ) |
---|
76 | end |
---|
77 | |
---|
78 | buttonsWidth = buttonWidth*nChildren + (nChildren-1)*obj.Spacing; |
---|
79 | switch upper( obj.HorizontalAlignment ) |
---|
80 | case 'LEFT' |
---|
81 | x0 = 1+ obj.Padding; |
---|
82 | |
---|
83 | case 'CENTER' |
---|
84 | x0 = 1 + round( (pos(3) - buttonsWidth ) / 2 ); |
---|
85 | |
---|
86 | case 'RIGHT' |
---|
87 | x0 = pos(3) - obj.Padding - buttonsWidth; |
---|
88 | |
---|
89 | otherwise |
---|
90 | error( 'GUILayout:InvalidState', ... |
---|
91 | 'Invalid horizontal alignment ''%s''.', obj.HorizontalAlignment ) |
---|
92 | |
---|
93 | end |
---|
94 | |
---|
95 | % OK, we now know the start coords, so position each child |
---|
96 | children = obj.Children; |
---|
97 | for ii=1:nChildren |
---|
98 | contentPos = [x0 + (ii-1)*(obj.Spacing+buttonWidth), y0, buttonWidth, buttonHeight]; |
---|
99 | obj.repositionChild( children(ii), contentPos ) |
---|
100 | end |
---|
101 | |
---|
102 | end % redraw |
---|
103 | |
---|
104 | end % protected methods |
---|
105 | |
---|
106 | end % classdef |
---|