06df4db58d3f45364bb458e3b20f3c3a910c745d
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Widget.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     public enum FocusDirection
23     {
24         Previous,
25         Next,
26         Up,
27         Down,
28         Right,
29         Left
30     }
31
32     public abstract class Widget : EvasObject
33     {
34         Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
35
36         SmartEvent _focused;
37         SmartEvent _unfocused;
38
39         internal Color _backgroundColor = Color.Default;
40         internal int _opacity = Color.Default.A;
41
42         protected Widget()
43         {
44         }
45
46         protected Widget(EvasObject parent) : base(parent)
47         {
48             _focused = new SmartEvent(this, "focused");
49             _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
50
51             _unfocused = new SmartEvent(this, "unfocused");
52             _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
53         }
54
55         public event EventHandler Focused;
56
57         public event EventHandler Unfocused;
58
59         public bool IsEnabled
60         {
61             get
62             {
63                 return !Interop.Elementary.elm_object_disabled_get(RealHandle);
64             }
65             set
66             {
67                 Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
68             }
69         }
70
71         public string Style
72         {
73             get
74             {
75                 return Interop.Elementary.elm_object_style_get(RealHandle);
76             }
77             set
78             {
79                 Interop.Elementary.elm_object_style_set(RealHandle, value);
80             }
81         }
82
83         public bool IsFocused
84         {
85             get
86             {
87                 return Interop.Elementary.elm_object_focus_get(RealHandle);
88             }
89         }
90
91         public bool IsFocusAllowed
92         {
93             get
94             {
95                 return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
96             }
97         }
98
99         public virtual string Text
100         {
101             get
102             {
103                 return Interop.Elementary.elm_object_part_text_get(RealHandle);
104             }
105             set
106             {
107                 Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
108             }
109         }
110
111         public virtual Color BackgroundColor
112         {
113             get
114             {
115                 if (!_backgroundColor.IsDefault)
116                 {
117                     _backgroundColor = GetPartColor("bg");
118                 }
119                 return _backgroundColor;
120             }
121             set
122             {
123                 if (value.IsDefault)
124                 {
125                     Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
126                 }
127                 else
128                 {
129                     SetPartColor("bg", value);
130                     _backgroundColor = value;
131                 }
132             }
133         }
134
135         public virtual int Opacity
136         {
137             get
138             {
139                 if (_opacity != Color.Default.A)
140                 {
141                     _opacity = GetPartOpacity("opacity");
142                 }
143                 return _opacity;
144             }
145             set
146             {
147                 SetPartOpacity("opacity", value);
148                 _opacity = value;
149             }
150         }
151
152         public void SetFocus(bool isFocus)
153         {
154             Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
155         }
156
157         public void AllowFocus(bool isAllowFocus)
158         {
159             Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
160         }
161
162         public void FocusNext(FocusDirection direction)
163         {
164             Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
165         }
166
167         public void SetNextFocusObject(EvasObject next, FocusDirection direction)
168         {
169             Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
170         }
171
172         public void SetPartContent(string part, EvasObject content)
173         {
174             SetPartContent(part, content, false);
175         }
176
177         public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
178         {
179             if (preserveOldContent)
180             {
181                 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
182             }
183             Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
184
185             _partContents[part ?? "__default__"] = content;
186         }
187
188         public void SetContent(EvasObject content)
189         {
190             SetContent(content, false);
191         }
192
193         public void SetContent(EvasObject content, bool preserveOldContent)
194         {
195             if (preserveOldContent)
196             {
197                 Interop.Elementary.elm_object_content_unset(RealHandle);
198             }
199
200             Interop.Elementary.elm_object_content_set(RealHandle, content);
201             _partContents["__default__"] = content;
202         }
203
204         public void SetPartText(string part, string text)
205         {
206             Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
207         }
208
209         public string GetPartText(string part)
210         {
211             return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
212         }
213
214         public virtual void SetPartColor(string part, Color color)
215         {
216             Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
217                                                                               color.G * color.A / 255,
218                                                                               color.B * color.A / 255,
219                                                                               color.A);
220         }
221
222         public virtual Color GetPartColor(string part)
223         {
224             int r, g, b, a;
225             Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
226             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
227         }
228
229         public void SetPartOpacity(string part, int opacity)
230         {
231             Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
232         }
233
234         public int GetPartOpacity(string part)
235         {
236             int r, g, b, a;
237             Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
238             return a;
239         }
240
241         internal IntPtr GetPartContent(string part)
242         {
243             return Interop.Elementary.elm_object_part_content_get(RealHandle, part);
244         }
245     }
246 }