summaryrefslogtreecommitdiff
path: root/ElmSharp/ElmSharp/Widget.cs
blob: ccd9ef4ccd6e47dd308e031eb6163d6ffea2404c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using System.Collections.Generic;
using ElmSharp.Accessible;

namespace ElmSharp
{
    /// <summary>
    /// Enumeration for the focus direction.
    /// </summary>
    public enum FocusDirection
    {
        /// <summary>
        /// Previous direction
        /// </summary>
        Previous,
        /// <summary>
        /// Next direction
        /// </summary>
        Next,
        /// <summary>
        /// Up direction
        /// </summary>
        Up,
        /// <summary>
        /// Down direction
        /// </summary>
        Down,
        /// <summary>
        /// Right direction
        /// </summary>
        Right,
        /// <summary>
        /// Left direction
        /// </summary>
        Left
    }

    /// <summary>
    /// The Widget is abstract class, it is the parent of other widgets.
    /// Inherits from <see cref="EvasObject"/>.
    /// </summary>
    public abstract class Widget : AccessibleObject
    {
        Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();

        SmartEvent _focused;
        SmartEvent _unfocused;

        internal Color _backgroundColor = Color.Default;
        internal int _opacity = Color.Default.A;

        protected Widget()
        {
        }

        /// <summary>
        /// Creates and initializes a new instance of the Widget class.
        /// </summary>
        /// <param name="parent">The parent of new Widget instance</param>
        protected Widget(EvasObject parent) : base(parent)
        {
            _focused = new SmartEvent(this, "focused");
            _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);

            _unfocused = new SmartEvent(this, "unfocused");
            _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
        }

        /// <summary>
        /// Focused will be triggered when the widget is focused.
        /// </summary>
        public event EventHandler Focused;

        /// <summary>
        /// Unfocused will be triggered when the widget is unfocused.
        /// </summary>
        public event EventHandler Unfocused;

        /// <summary>
        /// Sets or gets the state of the widget, which might be enabled or disabled.
        /// </summary>
        public bool IsEnabled
        {
            get
            {
                return !Interop.Elementary.elm_object_disabled_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
            }
        }

        /// <summary>
        /// Sets or gets the style of the widget.
        /// </summary>
        public string Style
        {
            get
            {
                return Interop.Elementary.elm_object_style_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_object_style_set(RealHandle, value);
            }
        }

        /// <summary>
        /// Gets whether this widget is focused.
        /// </summary>
        public bool IsFocused
        {
            get
            {
                return Interop.Elementary.elm_object_focus_get(RealHandle);
            }
        }

        /// <summary>
        /// Gets whether a widget is focusable or not.
        /// </summary>
        /// <remarks>Widgets which are meant to be interacted with by input events are created able to be focused, by default</remarks>
        public bool IsFocusAllowed
        {
            get
            {
                return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
            }
        }

        /// <summary>
        /// Sets or gets the text of the widget.
        /// </summary>
        /// <remarks>It could be override by special child class</remarks>
        public virtual string Text
        {
            get
            {
                return Interop.Elementary.elm_object_part_text_get(RealHandle);
            }
            set
            {
                Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
            }
        }

        /// <summary>
        /// Sets or gets the background color of the widget.
        /// </summary>
        /// <remarks>It could be override by special child class</remarks>
        public virtual Color BackgroundColor
        {
            get
            {
                if (!_backgroundColor.IsDefault)
                {
                    _backgroundColor = GetPartColor("bg");
                }
                return _backgroundColor;
            }
            set
            {
                if (value.IsDefault)
                {
                    Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
                }
                else
                {
                    SetPartColor("bg", value);
                    _backgroundColor = value;
                }
            }
        }

        /// <summary>
        /// Sets or gets the opacity of the widget.
        /// </summary>
        /// <remarks>It could be override by special child class</remarks>
        public virtual int Opacity
        {
            get
            {
                if (_opacity != Color.Default.A)
                {
                    _opacity = GetPartOpacity("opacity");
                }
                return _opacity;
            }
            set
            {
                SetPartOpacity("opacity", value);
                _opacity = value;
            }
        }

        /// <summary>
        /// Sets the widget to be focused or not.
        /// </summary>
        /// <param name="isFocus">Weather be focused</param>
        public void SetFocus(bool isFocus)
        {
            Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
        }

        /// <summary>
        /// Sets the ability for a widget to be focused.
        /// </summary>
        /// <param name="isAllowFocus">True if the object can be focused, false if not(and on errors)</param>
        public void AllowFocus(bool isAllowFocus)
        {
            Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
        }

        /// <summary>
        /// Gives focus to next widget in widget tree.
        /// </summary>
        /// <param name="direction">Direction to move the focus</param>
        public void FocusNext(FocusDirection direction)
        {
            Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
        }

        /// <summary>
        /// Set next widget with specific focus direction.
        /// </summary>
        /// <param name="next">Focus next widget</param>
        /// <param name="direction">Focus direction</param>
        public void SetNextFocusObject(EvasObject next, FocusDirection direction)
        {
            Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
        }

        /// <summary>
        /// Sets content to particular part of the widget, and the preserve old content will not be unset.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <param name="content">The content</param>
        /// <seealso cref="SetPartContent(string, EvasObject, bool)"/>
        public void SetPartContent(string part, EvasObject content)
        {
            SetPartContent(part, content, false);
        }

        /// <summary>
        /// Sets content to particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <param name="content">The content</param>
        /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
        /// <seealso cref="SetPartContent(string, EvasObject)"/>
        public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
        {
            if (preserveOldContent)
            {
                Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
            }
            Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);

            _partContents[part ?? "__default__"] = content;
        }

        /// <summary>
        /// Sets content to the widget, and the preserve old content will not be unset.
        /// </summary>
        /// <param name="content">The content</param>
        /// <seealso cref="SetContent(EvasObject, bool)"/>
        public void SetContent(EvasObject content)
        {
            SetContent(content, false);
        }

        /// <summary>
        /// Sets content the widget.
        /// </summary>
        /// <param name="content">The content</param>
        /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
        /// <seealso cref="SetContent(EvasObject)"/>
        public void SetContent(EvasObject content, bool preserveOldContent)
        {
            if (preserveOldContent)
            {
                Interop.Elementary.elm_object_content_unset(RealHandle);
            }

            Interop.Elementary.elm_object_content_set(RealHandle, content);
            _partContents["__default__"] = content;
        }

        /// <summary>
        /// Sets text to particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <param name="text">The text</param>
        public void SetPartText(string part, string text)
        {
            Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
        }

        /// <summary>
        /// Gets text of a particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <returns>Text of the particular part of the widget</returns>
        public string GetPartText(string part)
        {
            return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
        }

        /// <summary>
        /// Sets color of a particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <param name="color">The color be set to widget</param>
        /// <remarks>This method is a virtual method, it could be override by special child class</remarks>
        public virtual void SetPartColor(string part, Color color)
        {
            Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
                                                                              color.G * color.A / 255,
                                                                              color.B * color.A / 255,
                                                                              color.A);
        }

        /// <summary>
        /// Gets color of the particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <returns>The color of the particular part</returns>
        /// <remarks>This method is a virtual method, it could be override by special child class</remarks>
        public virtual Color GetPartColor(string part)
        {
            int r, g, b, a;
            Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
            return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
        }

        /// <summary>
        /// Sets opacity of the particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <param name="opacity">The opacity of the particular part</param>
        public void SetPartOpacity(string part, int opacity)
        {
            Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
        }

        /// <summary>
        /// Gets opacity of the particular part of the widget.
        /// </summary>
        /// <param name="part">The name of particular part</param>
        /// <returns>Opacity value of the particular part</returns>
        public int GetPartOpacity(string part)
        {
            int r, g, b, a;
            Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
            return a;
        }

        internal IntPtr GetPartContent(string part)
        {
            return Interop.Elementary.elm_object_part_content_get(RealHandle, part);
        }
    }
}