Add comments for ElmSharp APIs.
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Window.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 using System.Collections.Generic;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// Enumeration for the display rotation of window.
24     /// </summary>
25     [Flags]
26     public enum DisplayRotation
27     {
28         /// <summary>
29         /// Rotation value of window is 0 degree
30         /// </summary>
31         Degree_0 = 1,
32         /// <summary>
33         /// Rotation value of window is 90 degree
34         /// </summary>
35         Degree_90 = 2,
36         /// <summary>
37         /// Rotation value of window is 180 degree
38         /// </summary>
39         Degree_180 = 4,
40         /// <summary>
41         /// Rotation value of window is 270 degree
42         /// </summary>
43         Degree_270 = 8
44     };
45
46     /// <summary>
47     /// Enum indicator opacity
48     /// </summary>
49     public enum StatusBarMode
50     {
51         /// <summary>
52         /// Opacifies the status bar
53         /// </summary>
54         Opaque = 1,
55
56         /// <summary>
57         /// Be translucent the status bar
58         /// </summary>
59         /// <remarks>
60         /// Not supported.
61         /// </remarks>
62         Translucent = 2,
63
64         /// <summary>
65         /// Transparentizes the status bar
66         /// </summary>
67         Transparent = 3,
68     }
69
70     /// <summary>
71     /// The Window is container that contain the graphical user interface of a program.
72     /// </summary>
73     public class Window : Widget
74     {
75         SmartEvent _deleteRequest;
76         SmartEvent _rotationChanged;
77         HashSet<EvasObject> _referenceHolder = new HashSet<EvasObject>();
78
79         /// <summary>
80         /// Creates and initializes a new instance of the Window class.
81         /// </summary>
82         /// <param name="name">Window name.</param>
83         public Window(string name) : this(null, name)
84         {
85         }
86
87         /// <summary>
88         /// Creates and initializes a new instance of the Window class.
89         /// </summary>
90         /// <param name="parent">
91         /// Parent widget which this widow created on.
92         /// </param>
93         /// <param name="name">
94         /// Window name.
95         /// </param>
96         /// <remarks>
97         /// Window constructor.show window indicator,set callback
98         /// When closing the window in any way outside the program control,
99         /// and set callback when window rotation changed.
100         /// </remarks>
101         public Window(Window parent, string name)
102         {
103             Name = name;
104             Realize(parent);
105             Interop.Elementary.elm_win_indicator_mode_set(Handle, 2 /* ELM_WIN_INDICATOR_SHOW */);
106
107             _deleteRequest = new SmartEvent(this, "delete,request");
108             _rotationChanged = new SmartEvent(this, "wm,rotation,changed");
109             _deleteRequest.On += (s, e) => CloseRequested?.Invoke(this, EventArgs.Empty);
110             _rotationChanged.On += (s, e) => RotationChanged?.Invoke(this, EventArgs.Empty);
111         }
112
113         protected Window()
114         {
115         }
116
117         /// <summary>
118         /// CloseRequested will be triggered when Window close.
119         /// </summary>
120         public event EventHandler CloseRequested;
121
122         /// <summary>
123         /// RotationChanged will be triggered when Window do rotation.
124         /// </summary>
125         public event EventHandler RotationChanged;
126
127         /// <summary>
128         /// Sets or gets Window name.
129         /// </summary>
130         public string Name { get; set; }
131
132         /// <summary>
133         /// Gets Window size with Size value(w,h)
134         /// </summary>
135         public Size ScreenSize
136         {
137             get
138             {
139                 int x, y, w, h;
140                 Interop.Elementary.elm_win_screen_size_get(Handle, out x, out y, out w, out h);
141                 return new Size(w, h);
142             }
143         }
144
145         /// <summary>
146         /// Gets the screen dpi for the screen that a Window is on.
147         /// </summary>
148         public Point ScreenDpi
149         {
150             get
151             {
152                 Point point = default(Point);
153                 Interop.Elementary.elm_win_screen_dpi_get(Handle, out point.X, out point.Y);
154                 return point;
155             }
156         }
157
158         /// <summary>
159         /// Gets the rotation of the Window.The rotation of the window in degrees (0-360).
160         /// </summary>
161         public int Rotation
162         {
163             get
164             {
165                 return Interop.Elementary.elm_win_rotation_get(Handle);
166             }
167         }
168
169         /// <summary>
170         /// Gets whether window manager supports window rotation or not.
171         /// </summary>
172         public bool IsRotationSupported
173         {
174             get
175             {
176                 return Interop.Elementary.elm_win_wm_rotation_supported_get(Handle);
177             }
178         }
179
180         [Obsolete("Sorry, it's error typo of AvailableRotations, please use AvailableRotations")]
181         public DisplayRotation AavailableRotations { get; set; }
182
183
184         /// <summary>
185         /// Sets or gets available rotation degree.
186         /// </summary>
187         public DisplayRotation AvailableRotations
188         {
189             get
190             {
191                 int[] rotations;
192                 Interop.Elementary.elm_win_wm_rotation_available_rotations_get(Handle, out rotations);
193                 if (rotations == null)
194                 {
195                     return 0;
196                 }
197                 return ConvertToDisplayRotation(rotations);
198             }
199             set
200             {
201                 Interop.Elementary.elm_win_wm_rotation_available_rotations_set(Handle, ConvertDegreeArray(value));
202             }
203         }
204
205         /// <summary>
206         /// Sets or gets whether auto deletion function is enable.
207         /// </summary>
208         /// <remarks>
209         /// If you enable auto deletion, the window is automatically destroyed after the signal is emitted.
210         /// If auto deletion is disabled, the window is not destroyed and the program has to handle it.
211         /// </remarks>
212         public bool AutoDeletion
213         {
214             get
215             {
216                 return Interop.Elementary.elm_win_autodel_get(Handle);
217             }
218             set
219             {
220                 Interop.Elementary.elm_win_autodel_set(Handle, value);
221             }
222         }
223
224         /// <summary>
225         /// Sets or gets the alpha channel state of a window.
226         /// </summary>
227         /// <remarks>
228         /// True if the window alpha channel is enabled, false otherwise.
229         /// If alpha is true, the alpha channel of the canvas will be enabled possibly making parts of the window completely or partially transparent.
230         /// </remarks>
231         public bool Alpha
232         {
233             get
234             {
235                 return Interop.Elementary.elm_win_alpha_get(Handle);
236             }
237             set
238             {
239                 Interop.Elementary.elm_win_alpha_set(Handle, value);
240             }
241         }
242
243         /// <summary>
244         /// Sets or gets the role of the window.
245         /// </summary>
246         /// <remarks>
247         /// The Role will be invalid if a new role is set or if the window is destroyed.
248         /// </remarks>
249         public string Role
250         {
251             get
252             {
253                 return Interop.Elementary.elm_win_role_get(Handle);
254             }
255             set
256             {
257                 Interop.Elementary.elm_win_role_set(Handle, value);
258             }
259         }
260
261         /// <summary>
262         /// Sets or gets the mode of status bar.
263         /// </summary>
264         public StatusBarMode StatusBarMode
265         {
266             get
267             {
268                 return (StatusBarMode)Interop.Elementary.elm_win_indicator_opacity_get(Handle);
269             }
270             set
271             {
272                 Interop.Elementary.elm_win_indicator_opacity_set(Handle, (int)value);
273             }
274         }
275
276         /// <summary>
277         /// This function sends a request to the Windows Manager to activate the Window.
278         /// If honored by the WM, the window receives the keyboard focus.
279         /// </summary>
280         /// <remarks>
281         /// This is just a request that a Window Manager may ignore, so calling this function does not ensure
282         /// in any way that the window is going to be the active one after it.
283         /// </remarks>
284         public void Active()
285         {
286             Interop.Elementary.elm_win_activate(Handle);
287         }
288
289         /// <summary>
290         /// Adds obj as a resize object of the Window.
291         /// </summary>
292         /// <remarks>
293         /// Setting an object as a resize object of the window means that the obj child's size and
294         /// position is controlled by the window directly. That is, the obj is resized to match the window size
295         /// and should never be moved or resized manually by the developer.In addition,
296         /// resize objects of the window control the minimum size of it as well as whether it can or cannot be resized by the user.
297         /// </remarks>
298         /// <param name="obj">
299         /// Resize object.
300         /// </param>
301         public void AddResizeObject(EvasObject obj)
302         {
303             Interop.Elementary.elm_win_resize_object_add(Handle, obj);
304         }
305
306
307         /// <summary>
308         /// Set the keygrab of the window.
309         /// </summary>
310         /// <param name="keyname">keyname string to set keygrab</param>
311         public void KeyGrabEx(string keyname)
312         {
313             Interop.Elementary.eext_win_keygrab_set(RealHandle, keyname);
314         }
315
316         /// <summary>
317         /// Unset the keygrab of the window.
318         /// </summary>
319         /// <param name="keyname">keyname string to unset keygrab</param>
320         public void KeyUngrabEx(string keyname)
321         {
322             Interop.Elementary.eext_win_keygrab_unset(RealHandle, keyname);
323         }
324
325         protected override IntPtr CreateHandle(EvasObject parent)
326         {
327             Interop.Elementary.elm_config_accel_preference_set("3d");
328             return Interop.Elementary.elm_win_add(parent != null ? parent.Handle : IntPtr.Zero, Name, 0);
329         }
330
331         internal void AddChild(EvasObject obj)
332         {
333             _referenceHolder.Add(obj);
334         }
335
336         internal void RemoveChild(EvasObject obj)
337         {
338             _referenceHolder.Remove(obj);
339         }
340
341         static int[] ConvertDegreeArray(DisplayRotation value)
342         {
343             List<int> rotations = new List<int>();
344             if (value.HasFlag(DisplayRotation.Degree_0))
345                 rotations.Add(0);
346             if (value.HasFlag(DisplayRotation.Degree_90))
347                 rotations.Add(90);
348             if (value.HasFlag(DisplayRotation.Degree_180))
349                 rotations.Add(180);
350             if (value.HasFlag(DisplayRotation.Degree_270))
351                 rotations.Add(270);
352             return rotations.ToArray();
353         }
354
355         static DisplayRotation ConvertToDisplayRotation(int[] values)
356         {
357             int orientation = 0;
358             foreach (int v in values)
359             {
360                 orientation |= (1 << (v / 90));
361             }
362             return (DisplayRotation)orientation;
363         }
364
365     }
366 }