Merge "[NUI] Fix WidgetView dispose issue"
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Layout.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     /// This is a container widget that takes a standard Edje design file and wraps it very thinly in a widget.
23     /// Inherits Widget
24     /// </summary>
25     public class Layout : Container
26     {
27         SmartEvent _languageChanged;
28         SmartEvent _themeChanged;
29
30         IntPtr _edjeHandle;
31
32         /// <summary>
33         /// Creates and initializes a new instance of Layout class.
34         /// </summary>
35         /// <param name="parent">The parent is a given container which will be attached by Layout as a child. It's <see cref="EvasObject"/> type.</param>
36         public Layout(EvasObject parent) : base(parent)
37         {
38         }
39
40         /// <summary>
41         /// Creates and initializes a new instance of Layout class.
42         /// </summary>
43         protected Layout() : base()
44         {
45         }
46
47         /// <summary>
48         /// LanguageChanged will be triggered when the program's language is changed.
49         /// </summary>
50         public event EventHandler LanguageChanged;
51
52         /// <summary>
53         /// ThemeChanged will be triggered when the theme is changed.
54         /// </summary>
55         public event EventHandler ThemeChanged;
56
57         /// <summary>
58         /// Gets the edje layout.
59         /// </summary>
60         public EdjeObject EdjeObject
61         {
62             get
63             {
64                 if (_edjeHandle == IntPtr.Zero)
65                     _edjeHandle = Interop.Elementary.elm_layout_edje_get(RealHandle);
66                 return new EdjeObject(_edjeHandle);
67             }
68         }
69
70         /// <summary>
71         /// Gets or sets accessibility state of texblock(text) parts in the layout object.
72         /// </summary>
73         public bool TextBlockAccessibility
74         {
75             get
76             {
77                 return Interop.Elementary.elm_layout_edje_object_can_access_get(RealHandle);
78             }
79             set
80             {
81                 Interop.Elementary.elm_layout_edje_object_can_access_set(RealHandle, value);
82             }
83         }
84
85         /// <summary>
86         /// Freezes the Elementary layout object.
87         /// This function puts all changes on hold.
88         /// Successive freezes will nest, requiring an equal number of thaws.
89         /// </summary>
90         /// <returns>The frozen state or 0 if the object is not frozen or on error.</returns>
91         public int Freeze()
92         {
93             return Interop.Elementary.elm_layout_freeze(RealHandle);
94         }
95
96         /// <summary>
97         /// Thaws the Elementary object.
98         /// If sucessives freezes were done, an equal number of thaws will be required.
99         /// </summary>
100         /// <returns>The frozen state or 0 if the object is not frozen or on error.</returns>
101         public int Thaw()
102         {
103             return Interop.Elementary.elm_layout_thaw(RealHandle);
104         }
105
106         /// <summary>
107         /// Eval sizing.
108         /// Manually forces a sizing re-evaluation.
109         /// This is useful when the minimum size required by the edje theme of this layout has changed.
110         /// The change on the minimum size required by the edje theme is not immediately reported to the elementary layout, so one needs to call this function in order to tell the widget (layout) that it needs to reevaluate its own size.
111         /// The minimum size of the theme is calculated based on minimum size of parts, the size of elements inside containers like box and table, etc.
112         /// All of this can change due to state changes, and that's when this function should be called.
113         /// </summary>
114         public void Resizing()
115         {
116             Interop.Elementary.elm_layout_sizing_eval(RealHandle);
117         }
118
119         /// <summary>
120         /// Request sizing reevaluation, restricted to current width and/or height.
121         /// Useful mostly when there are TEXTBLOCK parts defining the height of the object and nothing else restricting it to a minimum width.Calling this function will restrict the minimum size in the Edje calculation to whatever size it the layout has at the moment.
122         /// </summary>
123         /// <param name="width">Restrict minimum size ot the current width.</param>
124         /// <param name="height">Restrict minimum size ot the current height.</param>
125         public void Resizing(bool width, bool height)
126         {
127             Interop.Elementary.elm_layout_sizing_restricted_eval(RealHandle, width, height);
128         }
129
130         /// <summary>
131         /// Get the edje data from the given layout.
132         /// This function fetches data specified inside the edje theme of this layout.
133         /// This function return NULL if data is not found.
134         /// </summary>
135         /// <param name="key">The data key</param>
136         /// <returns>The data</returns>
137         public string GetEdjeData(string key)
138         {
139             return Interop.Elementary.elm_layout_data_get(RealHandle, key);
140         }
141
142         /// <summary>
143         /// Gets the text set in the given part.
144         /// </summary>
145         /// <param name="part">The TEXT part to retrieve the text off.</param>
146         /// <returns></returns>
147         public override string GetPartText(string part)
148         {
149             return Interop.Elementary.elm_layout_text_get(RealHandle, part);
150         }
151
152         /// <summary>
153         /// Sets the text set in the given part.
154         /// </summary>
155         /// <param name="part">The TEXT part to retrieve the text off.</param>
156         /// <param name="text">The text to set.</param>
157         /// <returns></returns>
158         public override bool SetPartText(string part, string text)
159         {
160             return Interop.Elementary.elm_layout_text_set(RealHandle, part, text);
161         }
162
163         /// <summary>
164         /// Append child to layout box part.
165         /// Once the object is appended, it will become child of the layout.
166         /// Its lifetime will be bound to the layout, whenever the layout dies the child will be deleted automatically.
167         /// </summary>
168         /// <param name="part">The part</param>
169         /// <param name="child">The Object to append</param>
170         /// <returns>Sucess is true</returns>
171         public bool BoxAppend(string part, EvasObject child)
172         {
173             AddChild(child);
174             return Interop.Elementary.elm_layout_box_append(RealHandle, part, child.Handle);
175         }
176
177         /// <summary>
178         /// Prepend child to layout box part.
179         /// Once the object is prepended, it will become child of the layout.
180         /// Its lifetime will be bound to the layout, whenever the layout dies the child will be deleted automatically.
181         /// </summary>
182         /// <param name="part">The part</param>
183         /// <param name="child">The Object to prepend</param>
184         /// <returns>Sucess is true</returns>
185         public bool BoxPrepend(string part, EvasObject child)
186         {
187             AddChild(child);
188             return Interop.Elementary.elm_layout_box_prepend(RealHandle, part, child.Handle);
189         }
190
191         /// <summary>
192         /// Remove a child of the given part box.
193         /// The object will be removed from the box part and its lifetime will not be handled by the layout anymore.
194         /// </summary>
195         /// <param name="part">The part</param>
196         /// <param name="child">The Object to remove</param>
197         /// <returns>Sucess is true</returns>
198         public bool BoxRemove(string part, EvasObject child)
199         {
200             RemoveChild(child);
201             return Interop.Elementary.elm_layout_box_remove(RealHandle, part, child.Handle) != null;
202         }
203
204         /// <summary>
205         /// Remove all children of the given part box.
206         /// The objects will be removed from the box part and their lifetime will not be handled by the layout anymore.
207         /// </summary>
208         /// <param name="part">The part</param>
209         /// <param name="clear">If true, then all objects will be deleted as well, otherwise they will just be removed and will be dangling on the canvas.</param>
210         /// <returns>Sucess is true</returns>
211         public bool BoxRemoveAll(string part, bool clear)
212         {
213             ClearChildren();
214             return Interop.Elementary.elm_layout_box_remove_all(RealHandle, part, clear);
215         }
216
217         /// <summary>
218         /// Insert child to layout box part at a given position.
219         /// Once the object is inserted, it will become child of the layout.
220         /// Its lifetime will be bound to the layout, whenever the layout dies the child will be deleted automatically.
221         /// </summary>
222         /// <param name="part">The part</param>
223         /// <param name="child">The child object to insert into box.</param>
224         /// <param name="position">The numeric position >=0 to insert the child.</param>
225         /// <returns>Sucess is true</returns>
226         public bool BoxInsertAt(string part, EvasObject child, uint position)
227         {
228             AddChild(child);
229             return Interop.Elementary.elm_layout_box_insert_at(RealHandle, part, child.Handle, position);
230         }
231
232         /// <summary>
233         /// Insert child to layout box part before a reference object.
234         /// Once the object is inserted, it will become child of the layout.
235         /// Its lifetime will be bound to the layout, whenever the layout dies the child will be deleted automatically.
236         /// </summary>
237         /// <param name="part"></param>
238         /// <param name="child">The child object to insert into box.</param>
239         /// <param name="reference">Another reference object to insert before in box.</param>
240         /// <returns>Sucess is true</returns>
241         public bool BoxInsertBefore(string part, EvasObject child, EvasObject reference)
242         {
243             AddChild(child);
244             return Interop.Elementary.elm_layout_box_insert_before(RealHandle, part, child.Handle, reference.Handle);
245         }
246
247         /// <summary>
248         /// Sets the layout content.
249         /// </summary>
250         /// <param name="part">The swallow part name in the edje file</param>
251         /// <param name="content">The child that will be added in this layout object.</param>
252         /// <returns>TRUE on success, FALSE otherwise</returns>
253         public override bool SetPartContent(string part, EvasObject content)
254         {
255             return SetPartContent(part, content, false);
256         }
257
258         /// <summary>
259         /// Sets the layout content.
260         /// </summary>
261         /// <param name="part">The name of particular part</param>
262         /// <param name="content">The content</param>
263         /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
264         /// <returns>TRUE on success, FALSE otherwise</returns>
265         public override bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
266         {
267             if (preserveOldContent)
268             {
269                 Interop.Elementary.elm_layout_content_unset(RealHandle, part);
270             }
271             UpdatePartContents(content, part);
272             return Interop.Elementary.elm_layout_content_set(RealHandle, part, content);
273         }
274
275         /// <summary>
276         /// Sets the edje group from the elementary theme that is used as a layout.
277         /// </summary>
278         /// <param name="klass">The class of the group</param>
279         /// <param name="group">The group</param>
280         /// <param name="style">The style to use</param>
281         public void SetTheme(string klass, string group, string style)
282         {
283             Interop.Elementary.elm_layout_theme_set(RealHandle, klass, group, style);
284         }
285
286         /// <summary>
287         /// Sets the file that is used as a layout.
288         /// </summary>
289         /// <param name="file">The path to the file (edj) that is used as a layout</param>
290         /// <param name="group">The group that the layout belongs to in the edje file</param>
291         public void SetFile(string file, string group)
292         {
293             Interop.Elementary.elm_layout_file_set(RealHandle, file, group);
294         }
295
296         /// <summary>
297         /// Sets the back ground color of layout
298         /// </summary>
299         public override Color BackgroundColor
300         {
301             set
302             {
303                 if (value.IsDefault && ClassName != null)
304                 {
305                     string part = ClassName.ToLower().Replace("elm_", "") + "/" + "bg";
306                     EdjeObject.DeleteColorClass(part);
307                 }
308                 else
309                 {
310                     SetPartColor("bg", value);
311                 }
312                 _backgroundColor = value;
313             }
314         }
315
316         /// <summary>
317         /// Sets the vertical text alignment of layout's text part
318         /// </summary>
319         /// <remarks>
320         /// API, elm_layout_text_valign_set, is an internal API only in Tizen. Avalilable since Tizen_4.0.
321         /// </remarks>
322         public virtual void SetVerticalTextAlignment(string part, double valign)
323         {
324             Interop.Elementary.elm_layout_text_valign_set(RealHandle, part, valign);
325         }
326
327         /// <summary>
328         /// Gets the vertical text alignment of layout's text part
329         /// </summary>
330         /// <remarks>
331         /// API, elm_layout_text_valign_get, is internal API only in Tizen. Avalilable since Tizen_4.0.
332         /// </remarks>
333         public virtual double GetVerticalTextAlignment(string part)
334         {
335             return Interop.Elementary.elm_layout_text_valign_get(RealHandle, part);
336         }
337
338         /// <summary>
339         /// The callback of Realized Event
340         /// </summary>
341         protected override void OnRealized()
342         {
343             base.OnRealized();
344             _languageChanged = new SmartEvent(this, this.RealHandle, "language,changed");
345             _languageChanged.On += (s, e) =>
346             {
347                 LanguageChanged?.Invoke(this, EventArgs.Empty);
348             };
349
350             _themeChanged = new SmartEvent(this, this.RealHandle, "theme,changed");
351             _themeChanged.On += (s, e) =>
352             {
353                 ThemeChanged?.Invoke(this, EventArgs.Empty);
354             };
355         }
356
357         /// <summary>
358         /// Sets the content at a part of a given container widget.
359         /// </summary>
360         /// <param name="parent">The parent is a given container which will be attached by Layout as a child. It's <see cref="EvasObject"/> type.</param>
361         /// <returns>The new object, otherwise null if it cannot be created</returns>
362         protected override IntPtr CreateHandle(EvasObject parent)
363         {
364             return Interop.Elementary.elm_layout_add(parent.Handle);
365         }
366     }
367 }