19a5a9ba3a6f9d215e9590c0ed8a78db658b4df1
[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     /// <since_tizen> preview </since_tizen>
25     public class Box : Container
26     {
27         private Interop.Elementary.BoxLayoutCallback _layoutCallback;
28
29         /// <summary>
30         /// Creates and initializes a new instance of the Box class.
31         /// </summary>
32         /// <param name="parent">The EvasObject to which the new Box will be attached as a child.</param>
33         /// <since_tizen> preview </since_tizen>
34         public Box(EvasObject parent) : base(parent)
35         {
36         }
37
38         /// <summary>
39         /// Sets or gets IsHorizontal value which describe pack direction, vertical is default.
40         /// </summary>
41         /// <since_tizen> preview </since_tizen>
42         public bool IsHorizontal
43         {
44             get
45             {
46                 return Interop.Elementary.elm_box_horizontal_get(RealHandle);
47             }
48             set
49             {
50                 Interop.Elementary.elm_box_horizontal_set(RealHandle, value);
51             }
52         }
53
54         /// <summary>
55         /// Sets or gets whether the box to arrange its children homogeneously.
56         /// </summary>
57         /// <since_tizen> preview </since_tizen>
58         public bool IsHomogeneous
59         {
60             get
61             {
62                 return Interop.Elementary.elm_box_homogeneous_get(RealHandle);
63             }
64             set
65             {
66                 Interop.Elementary.elm_box_homogeneous_set(RealHandle, value);
67             }
68         }
69
70         /// <summary>
71         /// Adds an object at the end of the pack list.
72         /// </summary>
73         /// <remarks>
74         /// Packs "content" object into the Box, placing it last in the list of children objects.
75         /// The actual position the object will get on screen depends on the layout used.
76         /// If no custom layout is set, it will be at the bottom or right,
77         /// depending if the Box is vertical or horizontal, respectively.
78         /// </remarks>
79         /// <param name="content">The oject be packed</param>
80         /// <since_tizen> preview </since_tizen>
81         public void PackEnd(EvasObject content)
82         {
83             Interop.Elementary.elm_box_pack_end(RealHandle, content);
84             AddChild(content);
85         }
86
87         /// <summary>
88         /// Adds an "content" object to the beginning of the pack list.
89         /// </summary>
90         /// <remarks>
91         /// Pack "content" object into the Box obj, placing it first in the list of children objects.
92         /// The actual position the object will get on screen depends on the layout used.
93         /// If no custom layout is set, it will be at the top or left,
94         /// depending if the Box is vertical or horizontal, respectively.
95         /// </remarks>
96         /// <param name="content">The object to be packed.</param>
97         /// <since_tizen> preview </since_tizen>
98         public void PackStart(EvasObject content)
99         {
100             Interop.Elementary.elm_box_pack_start(RealHandle, content);
101             AddChild(content);
102         }
103
104         /// <summary>
105         /// Adds an "content "object to the Box after the "after" object.
106         /// </summary>
107         /// <remarks>
108         /// This will add the "content" to the Box indicated after the object indicated with "after".
109         /// If "after" is not already in the Box, results are undefined.
110         /// After means either to the right of the "after" object or below it depending on orientation.
111         /// </remarks>
112         /// <param name="content">The object will be added in Box</param>
113         /// <param name="after">The object has been added in Box</param>
114         /// <since_tizen> preview </since_tizen>
115         public void PackAfter(EvasObject content, EvasObject after)
116         {
117             Interop.Elementary.elm_box_pack_after(RealHandle, content, after);
118             AddChild(content);
119         }
120
121         /// <summary>
122         /// Adds an "content "object to the Box before the "before" object.
123         /// </summary>
124         /// <remarks>
125         /// This will add the "content" to the Box indicated before the object indicated with "before".
126         /// If "before" is not already in the Box, results are undefined.
127         /// before means either to the left of the "before" object or below it depending on orientation.
128         /// </remarks>
129         /// <param name="content">The object will be added in Box</param>
130         /// <param name="before">The object has been added in Box</param>
131         /// <since_tizen> preview </since_tizen>
132         public void PackBefore(EvasObject content, EvasObject before)
133         {
134             Interop.Elementary.elm_box_pack_before(RealHandle, content, before);
135             AddChild(content);
136         }
137
138         /// <summary>
139         /// Remove the "content" oject from Box without deleting it.
140         /// </summary>
141         /// <param name="content">The object to unpack</param>
142         /// <since_tizen> preview </since_tizen>
143         public void UnPack(EvasObject content)
144         {
145             Interop.Elementary.elm_box_unpack(RealHandle, content);
146             RemoveChild(content);
147         }
148
149         /// <summary>
150         /// Removes all objects from Box container.
151         /// </summary>
152         /// <since_tizen> preview </since_tizen>
153         public void UnPackAll()
154         {
155             Interop.Elementary.elm_box_unpack_all(RealHandle);
156             ClearChildren();
157         }
158
159         /// <summary>
160         /// Whenever anything changes that requires the Box in obj to recalculate the size and position of its elements,
161         /// the function cb will be called to determine what the layout of the children will be.
162         /// </summary>
163         /// <param name="action">The callback function used for layout </param>
164         /// <since_tizen> preview </since_tizen>
165         public void SetLayoutCallback(Action action)
166         {
167             _layoutCallback = (obj, priv, data) =>
168             {
169                 action();
170             };
171             Interop.Elementary.elm_box_layout_set(RealHandle, _layoutCallback, IntPtr.Zero, null);
172         }
173
174         /// <summary>
175         /// Sets the color of exact part to Box's layout parent.
176         /// </summary>
177         /// <param name="part">The name of part class, it could be 'bg', 'elm.swllow.content'.</param>
178         /// <param name="color">The color value.</param>
179         /// <since_tizen> preview </since_tizen>
180         public override void SetPartColor(string part, Color color)
181         {
182             Interop.Elementary.elm_object_color_class_color_set(Handle, part, color.R * color.A / 255,
183                                                                               color.G * color.A / 255,
184                                                                               color.B * color.A / 255,
185                                                                               color.A);
186         }
187
188         /// <summary>
189         /// Gets the color of exact part of Box's layout parent.
190         /// </summary>
191         /// <param name="part">The name of part class, it could be 'bg', 'elm.swllow.content'.</param>
192         /// <returns></returns>
193         /// <since_tizen> preview </since_tizen>
194         public override Color GetPartColor(string part)
195         {
196             int r, g, b, a;
197             Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
198             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
199         }
200
201         /// <summary>
202         /// Force the box to recalculate its children packing.
203         /// If any children was added or removed, box will not calculate the values immediately rather leaving it to the next main loop iteration.
204         /// 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.
205         /// </summary>
206         /// <since_tizen> preview </since_tizen>
207         public void Recalculate()
208         {
209             Interop.Elementary.elm_box_recalculate(RealHandle);
210         }
211
212         /// <summary>
213         /// Clear the box of all children.
214         /// Remove all the elements contained by the box, deleting the respective objects.
215         /// </summary>
216         /// <since_tizen> preview </since_tizen>
217         public void Clear()
218         {
219             Interop.Elementary.elm_box_clear(RealHandle);
220             ClearChildren();
221         }
222
223         /// <summary>
224         /// Sets or gets the alignment of the whole bounding box of contents.
225         /// </summary>
226         /// <param name="horizontal">Horizontal alignment</param>
227         /// <param name="vertical">Vertical alignment</param>
228         /// <since_tizen> preview </since_tizen>
229         public void SetBoxAlignment(double horizontal, double vertical)
230         {
231             Interop.Elementary.elm_box_align_set(RealHandle, horizontal, vertical);
232         }
233
234         /// <summary>
235         /// Sets or gets the space(padding) between the box's elements.
236         /// </summary>
237         /// <param name="horizontal">Horizontal padding</param>
238         /// <param name="vertical">vertical padding</param>
239         /// <since_tizen> preview </since_tizen>
240         public void SetPadding(int horizontal, int vertical)
241         {
242             Interop.Elementary.elm_box_padding_set(RealHandle, horizontal, vertical);
243         }
244
245         /// <summary>
246         /// Creates a widget handle.
247         /// </summary>
248         /// <param name="parent">Parent EvasObject</param>
249         /// <returns>Handle IntPtr</returns>
250         /// <since_tizen> preview </since_tizen>
251         protected override IntPtr CreateHandle(EvasObject parent)
252         {
253             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
254             Interop.Elementary.elm_layout_theme_set(handle, "layout", "background", "default");
255
256             RealHandle = Interop.Elementary.elm_box_add(handle);
257             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
258
259             return handle;
260         }
261     }
262 }