Merge "Use orientation from Window not application" into tizen
[platform/core/uifw/dali-toolkit.git] / docs / content / programming-guide / size-negotiation.h
1 /*! \page size-negotiation Size Negotiation
2  *
3 <h2 class="pg">Overview</h2>
4
5 Size negotiation, also known as layout management, is responsible for allocating sizes to all actors on the stage based on rules of dependency between
6 the actors. Requests for relayout on actors are collected during the frame with the actual relayout performed at the end of the frame.
7
8 This document details how to use the size negotiation API and is intended for application writters.
9
10 The topics covered are:
11 - Dimensions
12 - Resize policies
13 - Actor
14 - Debugging
15
16 <h2 class="pg">Dimensions</h2>
17
18 The notion of width and height is generalised into the concept of a Dimension. Several methods take a Dimension parameter.
19
20 The Dimension enum specifies the available dimensions as bitfields:
21 - Dimension::WIDTH
22 - Dimension::HEIGHT
23
24 If a method can process width and height at the same time then the Dimension::ALL_DIMENSIONS mask can be specified.
25
26 <h2 class="pg">Resize Policies</h2>
27
28 <h3>Policies</h3>
29 The ResizePolicy enum specifies a range of options for controlling the way actors resize. These are powerful rules that enable much automatic
30 resizing behaviour. They are as following:
31
32 - ResizePolicy::FIXED: This is the option to use when you want the specific definite size as set by SetSize (This is the default for all actors)
33 - ResizePolicy::USE_NATURAL_SIZE: Use this option for objects such as images or text to get their natural size e.g. The dimensions of the image, or the size of the text without wrapping. Also use this on TableViews when the size of the table is dependent on its children.
34 - ResizePolicy::FILL_TO_PARENT: Size will fill up to the size of its parent's size, taking a size factor into account to allow proportionate filling
35 - ResizePolicy::SIZE_RELATIVE_TO_PARENT: Fill up the parent with a relative scale. Use SetSizeModeFactor to specify the ratio to fill up to the parent.
36 - ResizePolicy::SIZE_FIXED_OFFSET_FROM_PARENT: Fill up the parent and add a fixed offset using SetSizeModeFactor.
37 - ResizePolicy::FIT_TO_CHILDREN: Size will scale around the size of the actor's children. E.g. A popup's height may resize itself around it's contents.
38 - ResizePolicy::DIMENSION_DEPENDENCY: This covers rules such as width-for-height and height-for-width. You specify that one dimension depends on another.
39
40 \image html size-negotiation/ResizePolicies.png
41
42 <h2 class="pg">Actor</h2>
43
44 This section details how an actor may be used with size negotiation.
45
46 <h3>Enabling Size Negotiation</h3>
47
48 The first thing to do is to specify whether you want an actor to be included or excluded from the relayout process. The following method is used to enable or disable the relayout
49 for an individual actor. Make sure this is the first thing that is called after the actor is created otherwise the actor may still be negotiated.
50 @code void SetRelayoutEnabled( bool enabled ) @endcode
51 Text and image actors have relayout enabled by default, while a plain Actor is disabled. Be aware that if desiring to use an Actor in relayout
52 then relayout needs to be explicitly enabled first.
53
54 <h3>Specifying Size Policies</h3>
55
56 Actors have different size policies by default. For example ImageActor is set to USE_NATURAL_SIZE. This ensures that when an image actor is
57 placed on the stage it will use its natural size by default. However if the user calls SetSize with non-zero sizes on the image actor then the current
58 size policy is overridden by the FIXED size policy and the actor will take on the size specified.
59
60 The next step is to specify how an actor will be size negotiated. The resize policies for an actor may be specified by the following method:
61 @code void SetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) @endcode
62 It is common to specifiy different policies for the different dimensions of width and height to achive different layouts. Different actors have
63 different resize policies specified by default. For example ImageActors are set to use USE_NATURAL_SIZE.
64
65 The following example code snippet shows rootActor having its width policy set to ResizePolicy::FILL_TO_PARENT and its height policy set to ResizePolicy::FIT_TO_CHILDREN.
66 It has an ImageActor added to it with an explicit call to USE_NATURAL_SIZE in both dimensions called on it. This will make an actor that will
67 fill up the space of its parent in the width dimension and fit to its child in the height dimension. As the image actor child is using natural size
68 the height of the root actor will fit to the height of the child image.
69
70 @code
71 Actor rootActor = Actor::New();
72 rootActor.SetRelayoutEnabled( true );
73 rootActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
74 rootActor.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::HEIGHT );
75 ImageActor image = ImageActor::New( Image::New( MY_IMAGE_PATH ) );
76 image.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
77 rootActor.Add( image );
78 @endcode
79
80 The following images show the before and after layouts for this code example.
81
82 Before:
83 \image html size-negotiation/SizeNegotiationExample_Before.png
84 After:
85 \image html size-negotiation/SizeNegotiationExample_After.png
86
87 This example shows an actor rootActor set to expand to its parent's width and contract/expand around its child's height. The child image actor
88 is set to natural size which means it will display at the acutal size of the image.
89
90 To specify that a dimension has a dependency on another dimension use ResizePolicy::DIMENSION_DEPENDENCY. For example if dimension is Dimension::HEIGHT and dependency is
91 Dimension::WIDTH then there is a height-for-width dependency in effect. The classic use case for this
92 is a text view that wraps its text. The following example snippet shows a text view that expands its width to the size of its parent, wraps its
93 contents and then determines its height based on the width.
94 @code
95 TextLabel text = TextLabel::New( "Example" );
96 text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
97 text.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
98 @endcode
99
100 <h3>Specifying Sizes and Size Limits</h3>
101
102 When wanting a specific fixed size for an actor then specify the resize policy to be FIXED and set the desired, or preferred size using SetSize.
103 If only one dimension is FIXED then the other value in the size parameter will be ignored, so it is safe to set it to zero.
104
105 To constrain the final negotiated size of an actor, set the following for minimum and maximum sizes respectively.
106 @code
107 void SetMinimumSize( const Vector2& size )
108 void SetMaximumSize( const Vector2& size )
109 @endcode
110
111 <h3>Altering Negotiated Size</h3>
112
113 When an actor is required to maintain the aspect ratio of its natural size the following method can be used. This is useful for size negotiating images
114 to ensure they maintain their aspect ratio while still fitting within the bounds they have been allocated. This can be one of SizeScalePolicy::USE_SIZE_SET, SizeScalePolicy::FIT_WITH_ASPECT_RATIO
115 or SizeScalePolicy::FILL_WITH_ASPECT_RATIO. The first is the default. The second will fit the actor within the bounds it has been allocated while maintaining aspect ratio. The
116 third will fill all available space, potentially overflowing its bounds, while maintaining apsect ratio.
117 @code void SetSizeScalePolicy( SizeScalePolicy::Type policy ) @endcode
118
119 <h3>Using Actors in Containers</h3>
120
121 When laying out actors in containers such as TableView it is useful to be able to specify padding that surrounds the actor. E.g. You may
122 want some white space around an image actor placed in a table cell. The padding specifies the left, right, bottom and top padding values.
123 @code void SetPadding( const Padding& padding ) @endcode
124
125 <h2 class="pg">An Example</h2>
126
127 This section shows a more complex example of how to configure size negotiation. It creates a popup and adds a table view to it with a text view,
128 an image and a sub-table. The sub-table contains a checkbox and another text view.
129 @code
130 mPopup = CreatePopup();
131 mPopup.SetTitle( "Warning" );
132
133 // Content
134 Toolkit::TableView content = Toolkit::TableView::New( 2, 2 );
135 content.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
136 content.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
137 content.SetFitHeight( 0 );
138 content.SetFitHeight( 1 );
139 content.SetPadding( Padding( 20.0f, 20.0f, 20.0f, 0.0f ) );
140
141 // Text
142 Toolkit::TextLabel text = Toolkit::TextLabel::New( "Do you really want to quit?" );
143 text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
144 text.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
145
146 content.AddChild( text, Toolkit::TableView::CellPosition( 0, 0 ) );
147
148 // Image
149 ImageActor image = ImageActor::New( ResourceImage::New( IMAGE1 ) );
150 image.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
151 image.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
152 image.SetPadding( Padding( 20.0f, 0.0f, 0.0f, 0.0f ) );
153 content.AddChild( image, Toolkit::TableView::CellPosition( 0, 1 ) );
154
155 // Checkbox and text
156 Toolkit::TableView root = Toolkit::TableView::New( 1, 2 );
157 root.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
158 root.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
159 root.SetFitHeight( 0 );
160 root.SetFitWidth( 0 );
161 root.SetPadding( Padding( 0.0f, 0.0f, 0.0f, 20.0f ) );
162
163 Dali::Image unchecked = Dali::ResourceImage::New( CHECKBOX_UNCHECKED_IMAGE );
164 Dali::Image checked = Dali::ResourceImage::New( CHECKBOX_CHECKED_IMAGE );
165 Toolkit::CheckBoxButton checkBox = Toolkit::CheckBoxButton::New();
166 checkBox.SetBackgroundImage( unchecked );
167 checkBox.SetSelectedImage( checked );
168 checkBox.SetSize( Vector2( 48, 48 ) );
169
170 root.AddChild( checkBox, Toolkit::TableView::CellPosition( 0, 0 ) );
171
172 Toolkit::TextLabel text2 = Toolkit::TextLabel::New( "Don't show again" );
173 text2.SetPadding( Padding( 20.0f, 0.0f, 0.0f, 10.0f ) );
174
175 root.AddChild( text2, Toolkit::TableView::CellPosition( 0, 1 ) );
176
177 content.AddChild( root, Toolkit::TableView::CellPosition( 1, 0, 0, 2 ) );  // Column span 2
178
179 mPopup.Add( content );
180 @endcode
181
182 The resulting popup with additional buttons added is shown below.
183 \image html size-negotiation/Popup.png
184
185 The key things to pick out from this example are the use of the size negotiation API.
186 The content table view is set to ResizePolicy::FILL_TO_PARENT for its width and USE_NATURAL_SIZE for its height. This will result
187 in the table view expanding its width to fit the available space in the popup while also expanding/contracting its
188 height based on the size of the contents in its cells.
189 @code
190 content.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
191 content.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
192 @endcode
193 To add a little space around the left, right and bottom of the table view, some padding is added.
194 @code
195 content.SetPadding( Padding( 20.0f, 20.0f, 20.0f, 0.0f ) );
196 @endcode
197 The first text view has its width set to ResizePolicy::FILL_TO_PARENT and its height has a dimension dependency on its width. This
198 will result in a text view that fills up its width to available space in the table cell and then then calculates its
199 height based on its new width. The table view will then fit its height taking the height of the text view into account.
200 @code
201 text.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
202 text.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
203 @endcode
204 The image view performs a similar relayout. It fits its width to the size of the cell and calculates its height based on the new
205 width. Some padding is added to the left of it as well to center it more.
206 @code
207 image.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
208 image.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
209 image.SetPadding( Padding( 20.0f, 0.0f, 0.0f, 0.0f ) );
210 @endcode
211 The sub table view is similar as well in that it expands its width to the size of its cell. When it is added to the table view it
212 will span two columns. Its height is set to natural size so that it will grow or shrink based on its children cells. Note that for
213 a container like table view, USE_NATURAL_SIZE acts in a similar manner to ResizePolicy::FIT_TO_CHILDREN in that the size of the container could
214 grow or shrink based on the sizes of the child actors.
215 @code
216 root.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
217 root.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
218 @endcode
219 The checkbox is set to have a fixed size.
220 @code
221 checkBox.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
222 @endcode
223 The second text view has not specified a resize policy so will use its default of USE_NATURAL_SIZE.
224
225 <h2 class="pg">Debugging</h2>
226
227 When constructing large scenes using interacting resize policies it is useful to be able to debug the relayout process. The following sections
228 describe a pitfall to avoid when creating scenes and a way to print debug output about the actors.
229
230 <h3>Infinite Dependency Loops</h3>
231 Despite the power of the resize rules there is one pitfall to be aware of: infinite dependency loops. The most simplest form of this is
232 shown by a parent actor with resize policy set to ResizePolicy::FIT_TO_CHILDREN with a child that has a resize policy of ResizePolicy::FILL_TO_PARENT. Who should
233 determine the size in this case? A more complex loop occurs when ResizePolicy::DIMENSION_DEPENDENCY comes into play. Say a parent has a width policy
234 of ResizePolicy::DIMENSION_DEPENDENCY with height and a height policy of ResizePolicy::FIT_TO_CHILDREN. The parent has a single child with a height policy ResizePolicy::DIMENSION_DEPENDENCY
235 with width. If the child's width policy is ResizePolicy::FILL_TO_PARENT then a loop will occur. These are two simple examples but the loops could occur
236 over larger spreads of parent child relationships. These loops are detected by the relayout algorithm with the result being that actors will
237 receive zero sizes. These loops are not common but are still something to watch out for.
238
239 <h3 class="pg">Inspecting Actor Relayout Properties</h3>
240 To get a print out of the stage hierarchy before and after negotiation, with a list of actors that were negotiated set the LOG_RELAYOUT_CONTROLLER environment variable to 3,true.
241
242 E.g. On desktop run:
243
244 $  LOG_RELAYOUT_CONTROLLER=3,true dali-demo
245
246 Example output from the logging is as follows:
247
248 PushButton, OKAY_BUTTON - Pos: [185, 0, 0.1] Size: [165, 76, 76], Dirty: (FALSE,FALSE), Negotiated: (TRUE,TRUE), Enabled: TRUE, (0x1649850)
249
250 The format is as follows:
251
252 [Actor type], [Actor name] – Pos:[X, Y, Z] Size[Dimension::WIDTH, Dimension::HEIGHT, DEPTH], Dirty:(Dimension::WIDTH, Dimension::HEIGHT), Negotiated: (Dimension::WIDTH, Dimension::HEIGHT), Enabled: BOOLEAN, (Object address)
253 - <i>Actor type</i>: The type name of the actor E.g. PushButton
254 - <i>Actor name</i>: The name set on the actor with SetName(). Useful for debugging.
255 - <i>Pos</i>: The position of the actor
256 - <i>Size</i>: The current size of the actor. Check this to see if the actor has been negotiated correctly.
257 - <i>Dirty</i>: Booleans to say if the width or height has been marked as dirty by the relayout dirty flag propagation algorithm
258 - <i>Negotiated</i>: Booleans to say if the width or height has been negotiated by the size negotiation algorithm
259 - <i>Enabled</i>: Boolean to say if the actor is enabled for size negotitation
260 - <i>Object address</i>: The address of the actor object in memory
261 *
262 */