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