Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Box.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// The Box is a container used to arranges UI components in a linear order.
23     /// </summary>
24     public class Box : Container
25     {
26         private Interop.Elementary.BoxLayoutCallback _layoutCallback;
27
28         /// <summary>
29         /// Creates and initializes a new instance of the Box class.
30         /// </summary>
31         /// <param name="parent">The EvasObject to which the new Box will be attached as a child.</param>
32         public Box(EvasObject parent) : base(parent)
33         {
34         }
35
36         /// <summary>
37         /// Sets or gets IsHorizontal value which describe pack direction, vertical is default.
38         /// </summary>
39         public bool IsHorizontal
40         {
41             get
42             {
43                 return Interop.Elementary.elm_box_horizontal_get(RealHandle);
44             }
45             set
46             {
47                 Interop.Elementary.elm_box_horizontal_set(RealHandle, value);
48             }
49         }
50
51         /// <summary>
52         /// Sets or gets whether the box to arrange its children homogeneously.
53         /// </summary>
54         public bool IsHomogeneous
55         {
56             get
57             {
58                 return Interop.Elementary.elm_box_homogeneous_get(RealHandle);
59             }
60             set
61             {
62                 Interop.Elementary.elm_box_homogeneous_set(RealHandle, value);
63             }
64         }
65
66         /// <summary>
67         /// Adds an object at the end of the pack list.
68         /// </summary>
69         /// <remarks>
70         /// Packs "content" object into the Box, placing it last in the list of children objects.
71         /// The actual position the object will get on screen depends on the layout used.
72         /// If no custom layout is set, it will be at the bottom or right,
73         /// depending if the Box is vertical or horizontal, respectively.
74         /// </remarks>
75         /// <param name="content">The oject be packed</param>
76         public void PackEnd(EvasObject content)
77         {
78             Interop.Elementary.elm_box_pack_end(RealHandle, content);
79             AddChild(content);
80         }
81
82         /// <summary>
83         /// Adds an "content" object to the beginning of the pack list.
84         /// </summary>
85         /// <remarks>
86         /// Pack "content" object into the Box obj, placing it first in the list of children objects.
87         /// The actual position the object will get on screen depends on the layout used.
88         /// If no custom layout is set, it will be at the top or left,
89         /// depending if the Box is vertical or horizontal, respectively.
90         /// </remarks>
91         /// <param name="content">The object to be packed.</param>
92         public void PackStart(EvasObject content)
93         {
94             Interop.Elementary.elm_box_pack_start(RealHandle, content);
95             AddChild(content);
96         }
97
98         /// <summary>
99         /// Adds an "content "object to the Box after the "after" object.
100         /// </summary>
101         /// <remarks>
102         /// This will add the "content" to the Box indicated after the object indicated with "after".
103         /// If "after" is not already in the Box, results are undefined.
104         /// After means either to the right of the "after" object or below it depending on orientation.
105         /// </remarks>
106         /// <param name="content">The object will be added in Box</param>
107         /// <param name="after">The object has been added in Box</param>
108         public void PackAfter(EvasObject content, EvasObject after)
109         {
110             Interop.Elementary.elm_box_pack_after(RealHandle, content, after);
111             AddChild(content);
112         }
113
114         /// <summary>
115         /// Adds an "content "object to the Box before the "before" object.
116         /// </summary>
117         /// <remarks>
118         /// This will add the "content" to the Box indicated before the object indicated with "before".
119         /// If "before" is not already in the Box, results are undefined.
120         /// before means either to the left of the "before" object or below it depending on orientation.
121         /// </remarks>
122         /// <param name="content">The object will be added in Box</param>
123         /// <param name="before">The object has been added in Box</param>
124         public void PackBefore(EvasObject content, EvasObject before)
125         {
126             Interop.Elementary.elm_box_pack_before(RealHandle, content, before);
127             AddChild(content);
128         }
129
130         /// <summary>
131         /// Remove the "content" oject from Box without deleting it.
132         /// </summary>
133         /// <param name="content">The object to unpack</param>
134         public void UnPack(EvasObject content)
135         {
136             Interop.Elementary.elm_box_unpack(RealHandle, content);
137             RemoveChild(content);
138         }
139
140         /// <summary>
141         /// Removes all objects from Box container.
142         /// </summary>
143         public void UnPackAll()
144         {
145             Interop.Elementary.elm_box_unpack_all(RealHandle);
146             ClearChildren();
147         }
148
149         /// <summary>
150         /// Whenever anything changes that requires the Box in obj to recalculate the size and position of its elements,
151         /// the function cb will be called to determine what the layout of the children will be.
152         /// </summary>
153         /// <param name="action">The callback function used for layout </param>
154         public void SetLayoutCallback(Action action)
155         {
156             _layoutCallback = (obj, priv, data) =>
157             {
158                 action();
159             };
160             Interop.Elementary.elm_box_layout_set(RealHandle, _layoutCallback, IntPtr.Zero, null);
161         }
162
163         /// <summary>
164         /// Sets the color of exact part to Box's layout parent.
165         /// </summary>
166         /// <param name="part">The name of part class, it could be 'bg', 'elm.swllow.content'.</param>
167         /// <param name="color">The color value.</param>
168         public override void SetPartColor(string part, Color color)
169         {
170             Interop.Elementary.elm_object_color_class_color_set(Handle, part, color.R * color.A / 255,
171                                                                               color.G * color.A / 255,
172                                                                               color.B * color.A / 255,
173                                                                               color.A);
174         }
175
176         /// <summary>
177         /// Gets the color of exact part of Box's layout parent.
178         /// </summary>
179         /// <param name="part">The name of part class, it could be 'bg', 'elm.swllow.content'.</param>
180         /// <returns></returns>
181         public override Color GetPartColor(string part)
182         {
183             int r, g, b, a;
184             Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
185             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
186         }
187
188         /// <summary>
189         /// Force the box to recalculate its children packing.
190         /// If any children was added or removed, box will not calculate the values immediately rather leaving it to the next main loop iteration.
191         /// While this is great as it would save lots of recalculation, whenever you need to get the position of a just added item you must force recalculate before doing so.
192         /// </summary>
193         public void Recalculate()
194         {
195             Interop.Elementary.elm_box_recalculate(RealHandle);
196         }
197
198         /// <summary>
199         /// Clear the box of all children.
200         /// Remove all the elements contained by the box, deleting the respective objects.
201         /// </summary>
202         public void Clear()
203         {
204             Interop.Elementary.elm_box_clear(RealHandle);
205             ClearChildren();
206         }
207
208         /// <summary>
209         /// Sets or gets the alignment of the whole bounding box of contents.
210         /// </summary>
211         /// <param name="horizontal">Horizontal alignment</param>
212         /// <param name="vertical">Vertical alignment</param>
213         public void SetBoxAlignment(double horizontal, double vertical)
214         {
215             Interop.Elementary.elm_box_align_set(RealHandle, horizontal, vertical);
216         }
217
218         /// <summary>
219         /// Sets or gets the space(padding) between the box's elements.
220         /// </summary>
221         /// <param name="horizontal">Horizontal padding</param>
222         /// <param name="vertical">vertical padding</param>
223         public void SetPadding(int horizontal, int vertical)
224         {
225             Interop.Elementary.elm_box_padding_set(RealHandle, horizontal, vertical);
226         }
227
228         protected override IntPtr CreateHandle(EvasObject parent)
229         {
230             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
231             Interop.Elementary.elm_layout_theme_set(handle, "layout", "background", "default");
232
233             RealHandle = Interop.Elementary.elm_box_add(handle);
234             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
235
236             return handle;
237         }
238     }
239 }