/
ItemObject.cs
409 lines (368 loc) · 14.5 KB
/
ItemObject.cs
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
* 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;
namespace ElmSharp
{
/// <summary>
/// The ItemObject is used to manage the item object.
/// </summary>
/// <since_tizen> preview </since_tizen>
public class ItemObject
{
private static Dictionary<int, ItemObject> s_IdToItemTable = new Dictionary<int, ItemObject>();
private static Dictionary<IntPtr, ItemObject> s_HandleToItemTable = new Dictionary<IntPtr, ItemObject>();
private static int s_globalId = 0;
readonly Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
Interop.Evas.SmartCallback _deleteCallback;
IntPtr _handle = IntPtr.Zero;
Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb> _signalDatas = new Dictionary<SignalData, Interop.Elementary.Elm_Object_Item_Signal_Cb>();
EvasObject _trackObject = null;
/// <summary>
/// Creates and initializes a new instance of the ItemObject class.
/// </summary>
/// <param name="handle">IntPtr</param>
/// <since_tizen> preview </since_tizen>
protected ItemObject(IntPtr handle)
{
_deleteCallback = DeleteCallbackHandler;
Id = GetNextId();
s_IdToItemTable[Id] = this;
Handle = handle;
}
// C# Finalizer was called on GC thread
// So, We can't access to EFL object
// And When Finalizer was called, Field can be already released.
//~ItemObject()
//{
// if (Handle != IntPtr.Zero)
// Interop.Elementary.elm_object_item_del(Handle);
//}
/// <summary>
/// Gets the ID of the item object.
/// </summary>
/// <since_tizen> preview </since_tizen>
public int Id { get; private set; }
/// <summary>
/// Sets or gets whether the item object is enabled.
/// </summary>
/// <since_tizen> preview </since_tizen>
public bool IsEnabled
{
get { return !Interop.Elementary.elm_object_item_disabled_get(Handle); }
set { Interop.Elementary.elm_object_item_disabled_set(Handle, !value); }
}
/// <summary>
/// Gets the track object of the item.
/// </summary>
/// <since_tizen> preview </since_tizen>
public EvasObject TrackObject
{
get
{
if (_trackObject == null || Interop.Elementary.elm_object_item_track_get(Handle) == 0)
{
_trackObject = new ItemEvasObject(Handle);
}
return _trackObject;
}
}
/// <summary>
/// Sets or gets the style of the item.
/// </summary>
/// <since_tizen> preview </since_tizen>
public virtual string Style
{
get
{
return Interop.Elementary.elm_object_item_style_get(Handle);
}
set
{
Interop.Elementary.elm_object_item_style_set(Handle, value);
}
}
internal IntPtr Handle
{
get
{
return _handle;
}
set
{
if (_handle == value)
return;
if (_handle != IntPtr.Zero)
{
UnsetDeleteCallback();
}
_handle = value;
SetDeleteCallback();
s_HandleToItemTable[Handle] = this;
}
}
/// <summary>
/// Deleted will be triggered when the item object is deleted.
/// </summary>
/// <since_tizen> preview </since_tizen>
public event EventHandler Deleted;
/// <summary>
/// Deletes the item object.
/// </summary>
/// <since_tizen> preview </since_tizen>
public void Delete()
{
Interop.Elementary.elm_object_item_del(Handle);
_handle = IntPtr.Zero;
}
/// <summary>
/// Sets a content of an object item and deletes the old content.
/// </summary>
/// <param name="part">The content part name (null for the default content).</param>
/// <param name="content">The content of the object item.</param>
/// <since_tizen> preview </since_tizen>
public void SetPartContent(string part, EvasObject content)
{
SetPartContent(part, content, false);
}
/// <summary>.
/// Sets a content of the object item.
/// </summary>
/// <param name="part">The content part name (null for the default content)</param>
/// <param name="content">The content of the object item.</param>
/// <param name="preserveOldContent">Judge whether to delete the old content.</param>
/// <since_tizen> preview </since_tizen>
public void SetPartContent(string part, EvasObject content, bool preserveOldContent)
{
IntPtr oldContent = Interop.Elementary.elm_object_item_part_content_unset(Handle, part);
if (oldContent != IntPtr.Zero && !preserveOldContent)
{
Interop.Evas.evas_object_del(oldContent);
}
Interop.Elementary.elm_object_item_part_content_set(Handle, part, content);
_partContents[part ?? "__default__"] = content;
}
/// <summary>
/// Sets the label of the object item.
/// </summary>
/// <param name="part">The text part name (null for the default label).</param>
/// <param name="text">Text of the label.</param>
/// <since_tizen> preview </since_tizen>
public void SetPartText(string part, string text)
{
Interop.Elementary.elm_object_item_part_text_set(Handle, part, text);
}
/// <summary>
/// Gets the label of the object item.
/// </summary>
/// <param name="part">The text part name (null for the default label).</param>
/// <returns></returns>
/// <since_tizen> preview </since_tizen>
public string GetPartText(string part)
{
return Interop.Elementary.elm_object_item_part_text_get(Handle, part);
}
/// <summary>
/// Sets the color of the object item.
/// </summary>
/// <param name="part">The text part name (null for the default label).</param>
/// <param name="color">The color.</param>
/// <since_tizen> preview </since_tizen>
public void SetPartColor(string part, Color color)
{
Interop.Elementary.elm_object_item_color_class_color_set(Handle, part, color.R * color.A / 255,
color.G * color.A / 255,
color.B * color.A / 255,
color.A);
}
/// <summary>
/// Gets the color of the object item.
/// </summary>
/// <param name="part">The text part name (null for the default label).</param>
/// <returns>The color of an object item.</returns>
/// <since_tizen> preview </since_tizen>
public Color GetPartColor(string part)
{
int r, g, b, a;
Interop.Elementary.elm_object_item_color_class_color_get(Handle, 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>
/// Deletes the color of the object item.
/// </summary>
/// <param name="part">The text part name.</param>
/// <since_tizen> preview </since_tizen>
public void DeletePartColor(string part)
{
Interop.Elementary.elm_object_item_color_class_del(Handle, part);
}
/// <summary>
/// Adds a function for a signal emitted by the object item edje.
/// </summary>
/// <param name="emission">The signal's name.</param>
/// <param name="source">The signal's source.</param>
/// <param name="func">The function to be executed when the signal is emitted.</param>
/// <since_tizen> preview </since_tizen>
public void AddSignalHandler(string emission, string source, Func<string, string, bool> func)
{
if (emission != null && source != null && func != null)
{
var signalData = new SignalData(emission, source, func);
if (!_signalDatas.ContainsKey(signalData))
{
var signalCallback = new Interop.Elementary.Elm_Object_Item_Signal_Cb((d, o, e, s) =>
{
return func(e, s);
});
Interop.Elementary.elm_object_item_signal_callback_add(Handle, emission, source, signalCallback, IntPtr.Zero);
}
}
}
/// <summary>
/// Removes a signal-triggered function from the object item edje object.
/// </summary>
/// <param name="emission">The signal's name.</param>
/// <param name="source">The signal's source.</param>
/// <param name="func">The function to be executed when the signal is emitted.</param>
/// <since_tizen> preview </since_tizen>
public void RemoveSignalHandler(string emission, string source, Func<string, string, bool> func)
{
if (emission != null && source != null && func != null)
{
var signalData = new SignalData(emission, source, func);
Interop.Elementary.Elm_Object_Item_Signal_Cb signalCallback = null;
_signalDatas.TryGetValue(signalData, out signalCallback);
if (signalCallback != null)
{
Interop.Elementary.elm_object_item_signal_callback_del(Handle, emission, source, signalCallback);
_signalDatas.Remove(signalData);
}
}
}
/// <summary>
/// Send a signal to the edje object of the widget item.
/// </summary>
/// <param name="emission">The signal's name.</param>
/// <param name="source">The signal's source.</param>
/// <since_tizen> preview </since_tizen>
public void EmitSignal(string emission, string source)
{
Interop.Elementary.elm_object_item_signal_emit(Handle, emission, source);
}
/// <summary>
/// Gets the handle of the object item.
/// </summary>
/// <param name="obj">ItemObject</param>
/// <since_tizen> preview </since_tizen>
public static implicit operator IntPtr(ItemObject obj)
{
if (obj == null)
return IntPtr.Zero;
return obj.Handle;
}
/// <summary>
/// OnInvalidate of the object item.
/// </summary>
/// <since_tizen> preview </since_tizen>
protected virtual void OnInvalidate() { }
internal static ItemObject GetItemById(int id)
{
ItemObject value;
s_IdToItemTable.TryGetValue(id, out value);
return value;
}
internal static ItemObject GetItemByHandle(IntPtr handle)
{
ItemObject value;
s_HandleToItemTable.TryGetValue(handle, out value);
return value;
}
void DeleteCallbackHandler(IntPtr data, IntPtr obj, IntPtr info)
{
Deleted?.Invoke(this, EventArgs.Empty);
OnInvalidate();
if (s_IdToItemTable.ContainsKey(Id))
{
s_IdToItemTable.Remove(Id);
}
if (s_HandleToItemTable.ContainsKey(_handle))
{
s_HandleToItemTable.Remove(_handle);
}
_partContents.Clear();
_handle = IntPtr.Zero;
}
void UnsetDeleteCallback()
{
Interop.Elementary.elm_object_item_del_cb_set(Handle, null);
}
void SetDeleteCallback()
{
if (Handle != IntPtr.Zero)
Interop.Elementary.elm_object_item_del_cb_set(Handle, _deleteCallback);
}
static int GetNextId()
{
return s_globalId++;
}
class SignalData : IEquatable<SignalData>
{
public string Emission { get; set; }
public string Source { get; set; }
public Func<string, string, bool> Func { get; set; }
public SignalData(string emission, string source, Func<string, string, bool> func)
{
Emission = emission;
Source = source;
Func = func;
}
public override bool Equals(object other)
{
return Equals(other as SignalData);
}
public bool Equals(SignalData other) {
if (other == null)
{
return false;
}
return (Emission == other.Emission) && (Source == other.Source) && (Func == other.Func);
}
public override int GetHashCode()
{
int hashCode = Emission.GetHashCode();
hashCode ^= Source.GetHashCode();
hashCode ^= Func.GetHashCode();
return hashCode;
}
}
class ItemEvasObject : EvasObject
{
IntPtr _trackHandle = IntPtr.Zero;
public ItemEvasObject(IntPtr parent)
{
_trackHandle = Interop.Elementary.elm_object_item_track(parent);
if (_trackHandle != IntPtr.Zero)
{
Realize(null);
}
}
protected override IntPtr CreateHandle(EvasObject parent)
{
return _trackHandle;
}
}
}
}