Add ScreenDpi API in Window class
[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     [Flags]
23     public enum DisplayRotation
24     {
25         Degree_0 = 1,
26         Degree_90 = 2,
27         Degree_180 = 4,
28         Degree_270 = 8
29     };
30
31     public enum StatusBarMode
32     {
33         /// <summary>
34         /// Opacifies the status bar
35         /// </summary>
36         Opaque = 1,
37
38         /// <summary>
39         /// Be translucent the status bar
40         /// </summary>
41         /// <remarks>
42         /// Not supported.
43         /// </remarks>
44         Translucent = 2,
45
46         /// <summary>
47         /// Transparentizes the status bar
48         /// </summary>
49         Transparent = 3,
50     }
51
52     public class Window : Widget
53     {
54         SmartEvent _deleteRequest;
55         SmartEvent _rotationChanged;
56         HashSet<EvasObject> _referenceHolder = new HashSet<EvasObject>();
57
58         public Window(string name) : this(null, name)
59         {
60         }
61
62         public Window(Window parent, string name)
63         {
64             Name = name;
65             Realize(parent);
66             Interop.Elementary.elm_win_indicator_mode_set(Handle, 2 /* ELM_WIN_INDICATOR_SHOW */);
67
68             _deleteRequest = new SmartEvent(this, "delete,request");
69             _rotationChanged = new SmartEvent(this, "wm,rotation,changed");
70             _deleteRequest.On += (s, e) => CloseRequested?.Invoke(this, EventArgs.Empty);
71             _rotationChanged.On += (s, e) => RotationChanged?.Invoke(this, EventArgs.Empty);
72         }
73
74         protected Window()
75         {
76         }
77
78         public event EventHandler CloseRequested;
79         public event EventHandler RotationChanged;
80
81         public string Name { get; set; }
82
83         public Size ScreenSize
84         {
85             get
86             {
87                 int x, y, w, h;
88                 Interop.Elementary.elm_win_screen_size_get(Handle, out x, out y, out w, out h);
89                 return new Size(w, h);
90             }
91         }
92
93         public Point ScreenDpi
94         {
95             get
96             {
97                 Point point = default(Point);
98                 Interop.Elementary.elm_win_screen_dpi_get(Handle, out point.X, out point.Y);
99                 return point;
100             }
101         }
102
103         public int Rotation
104         {
105             get
106             {
107                 return Interop.Elementary.elm_win_rotation_get(Handle);
108             }
109         }
110
111         public bool IsRotationSupported
112         {
113             get
114             {
115                 return Interop.Elementary.elm_win_wm_rotation_supported_get(Handle);
116             }
117         }
118
119         [Obsolete("Sorry, it's error typo of AvailableRotations, please use AvailableRotations")]
120         public DisplayRotation AavailableRotations { get; set; }
121
122         public DisplayRotation AvailableRotations
123         {
124             get
125             {
126                 int[] rotations;
127                 Interop.Elementary.elm_win_wm_rotation_available_rotations_get(Handle, out rotations);
128                 if (rotations == null)
129                 {
130                     return 0;
131                 }
132                 return ConvertToDisplayRotation(rotations);
133             }
134             set
135             {
136                 Interop.Elementary.elm_win_wm_rotation_available_rotations_set(Handle, ConvertDegreeArray(value));
137             }
138         }
139
140         public bool AutoDeletion
141         {
142             get
143             {
144                 return Interop.Elementary.elm_win_autodel_get(Handle);
145             }
146             set
147             {
148                 Interop.Elementary.elm_win_autodel_set(Handle, value);
149             }
150         }
151
152         public StatusBarMode StatusBarMode
153         {
154             get
155             {
156                 return (StatusBarMode)Interop.Elementary.elm_win_indicator_opacity_get(Handle);
157             }
158             set
159             {
160                 Interop.Elementary.elm_win_indicator_opacity_set(Handle, (int)value);
161             }
162         }
163
164         public void Active()
165         {
166             Interop.Elementary.elm_win_activate(Handle);
167         }
168
169         public void AddResizeObject(EvasObject obj)
170         {
171             Interop.Elementary.elm_win_resize_object_add(Handle, obj);
172         }
173
174         protected override IntPtr CreateHandle(EvasObject parent)
175         {
176             Interop.Elementary.elm_config_accel_preference_set("3d");
177             return Interop.Elementary.elm_win_add(parent != null ? parent.Handle : IntPtr.Zero, Name, 0);
178         }
179
180         internal void AddChild(EvasObject obj)
181         {
182             _referenceHolder.Add(obj);
183         }
184
185         internal void RemoveChild(EvasObject obj)
186         {
187             _referenceHolder.Remove(obj);
188         }
189
190         static int[] ConvertDegreeArray(DisplayRotation value)
191         {
192             List<int> rotations = new List<int>();
193             if (value.HasFlag(DisplayRotation.Degree_0))
194                 rotations.Add(0);
195             if (value.HasFlag(DisplayRotation.Degree_90))
196                 rotations.Add(90);
197             if (value.HasFlag(DisplayRotation.Degree_180))
198                 rotations.Add(180);
199             if (value.HasFlag(DisplayRotation.Degree_270))
200                 rotations.Add(270);
201             return rotations.ToArray();
202         }
203
204         static DisplayRotation ConvertToDisplayRotation(int[] values)
205         {
206             int orientation = 0;
207             foreach (int v in values)
208             {
209                 orientation |= (1 << (v / 90));
210             }
211             return (DisplayRotation)orientation;
212         }
213
214     }
215 }