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