2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using System.Diagnostics;
23 public abstract class EvasObject
25 private IntPtr _realHandle = IntPtr.Zero;
26 internal IntPtr Handle { get; set; }
27 internal EvasObject Parent { get; set; }
28 internal IntPtr RealHandle
32 return _realHandle == IntPtr.Zero ? Handle : _realHandle;
40 EvasObjectEvent _deleted;
41 EvasObjectEvent<EvasKeyEventArgs> _keyup;
42 EvasObjectEvent<EvasKeyEventArgs> _keydown;
43 EvasObjectEvent _moved;
44 EvasObjectEvent _resized;
45 EvasObjectEvent _renderPost;
47 readonly HashSet<IInvalidatable> _eventStore = new HashSet<IInvalidatable>();
49 protected EvasObject(EvasObject parent) : this()
51 Debug.Assert(parent == null || parent.IsRealized);
55 protected EvasObject()
60 // C# Finalizer was called on GC thread
61 // So, We can't access to EFL object
62 // And When Finalizer was called, Field can be already released.
68 public event EventHandler Deleted;
69 public event EventHandler<EvasKeyEventArgs> KeyUp;
70 public event EventHandler<EvasKeyEventArgs> KeyDown;
71 public event EventHandler Moved
73 add { _moved.On += value; }
74 remove { _moved.On -= value; }
76 public event EventHandler Resized
78 add { _resized.On += value; }
79 remove { _resized.On -= value; }
82 public event EventHandler RenderPost
84 add { _renderPost.On += value; }
85 remove { _renderPost.On -= value; }
88 public bool IsRealized { get { return Handle != IntPtr.Zero; } }
90 public string ClassName
94 return Interop.Eo.eo_class_name_get(Interop.Eo.eo_class_get(RealHandle));
102 return Interop.Evas.GetWeightX(Handle);
106 Interop.Evas.SetWeightX(Handle, value);
110 public double WeightY
114 return Interop.Evas.GetWeightY(Handle);
118 Interop.Evas.SetWeightY(Handle, value);
122 public virtual double AlignmentX
126 return Interop.Evas.GetAlignX(Handle);
130 Interop.Evas.SetAlignX(Handle, value);
134 public virtual double AlignmentY
138 return Interop.Evas.GetAlignY(Handle);
142 Interop.Evas.SetAlignY(Handle, value);
146 public int MinimumWidth
151 Interop.Evas.evas_object_size_hint_min_get(RealHandle, out w, out h);
156 int h = MinimumHeight;
157 Interop.Evas.evas_object_size_hint_min_set(RealHandle, value, h);
161 public int MinimumHeight
166 Interop.Evas.evas_object_size_hint_min_get(RealHandle, out w, out h);
171 int w = MinimumWidth;
172 Interop.Evas.evas_object_size_hint_min_set(RealHandle, w, value);
176 public bool IsVisible
180 return Interop.Evas.evas_object_visible_get(Handle);
189 Interop.Evas.evas_object_geometry_get(Handle, out x, out y, out w, out h);
190 Rect rect = new Rect(x, y, w, h);
195 Interop.Evas.evas_object_geometry_set(Handle, value.X, value.Y, value.Width, value.Height);
199 public virtual Color Color
204 Interop.Evas.evas_object_color_get(RealHandle, out r, out g, out b, out a);
205 return Color.FromRgba(r, g, b, a);
209 Interop.Evas.SetPremultipliedColor(RealHandle, value.R, value.G, value.B, value.A);
213 public bool IsMapEnabled
217 return Interop.Evas.evas_object_map_enable_get(Handle);
221 Interop.Evas.evas_object_map_enable_set(Handle, value);
225 public EvasMap EvasMap
229 IntPtr evasMap = Interop.Evas.evas_object_map_get(Handle);
230 return new EvasMap(evasMap);
234 Interop.Evas.evas_object_map_set(Handle, value.Handle);
238 public bool RepeatEvents
242 return Interop.Evas.evas_object_repeat_events_get(RealHandle);
246 Interop.Evas.evas_object_repeat_events_set(RealHandle, value);
250 public bool PropagateEvents
254 return Interop.Evas.evas_object_propagate_events_get(RealHandle);
258 Interop.Evas.evas_object_propagate_events_set(RealHandle, value);
262 public bool PassEvents
266 return Interop.Evas.evas_object_pass_events_get(RealHandle);
270 Interop.Evas.evas_object_pass_events_set(RealHandle, value);
274 public void SetClip(EvasObject clip)
276 Interop.Evas.evas_object_clip_set(Handle, clip);
279 public void SetAlignment(double x, double y)
281 Interop.Evas.evas_object_size_hint_align_set(Handle, x, y);
284 public void SetWeight(double x, double y)
286 Interop.Evas.evas_object_size_hint_weight_set(Handle, x, y);
291 Interop.Evas.evas_object_show(Handle);
296 Interop.Evas.evas_object_hide(Handle);
299 public void Resize(int w, int h)
301 Interop.Evas.evas_object_resize(Handle, w, h);
304 public void Move(int x, int y)
306 Interop.Evas.evas_object_move(Handle, x, y);
311 Interop.Evas.evas_object_lower(Handle);
314 public static implicit operator IntPtr(EvasObject obj)
321 public bool KeyGrab(string keyname, bool exclusive)
323 return Interop.Evas.evas_object_key_grab(Handle, keyname, 0, 0, exclusive);
326 public void KeyUngrab(string keyname)
328 Interop.Evas.evas_object_key_ungrab(Handle, keyname, 0, 0);
331 public void MarkChanged()
333 Interop.Evas.evas_object_smart_changed(RealHandle);
336 protected virtual void OnInvalidate()
340 protected virtual void OnInstantiated()
344 protected virtual void OnRealized()
348 protected virtual void OnUnrealize()
352 protected abstract IntPtr CreateHandle(EvasObject parent);
354 public void Realize(EvasObject parent)
359 Handle = CreateHandle(parent);
360 Debug.Assert(Handle != IntPtr.Zero);
362 (parent as Window)?.AddChild(this);
365 _deleted = new EvasObjectEvent(this, EvasObjectCallbackType.Del);
366 _keydown = new EvasObjectEvent<EvasKeyEventArgs>(this, EvasObjectCallbackType.KeyDown, EvasKeyEventArgs.Create);
367 _keyup = new EvasObjectEvent<EvasKeyEventArgs>(this, EvasObjectCallbackType.KeyUp, EvasKeyEventArgs.Create);
368 _moved = new EvasObjectEvent(this, EvasObjectCallbackType.Move);
369 _resized = new EvasObjectEvent(this, EvasObjectCallbackType.Resize);
370 _renderPost = new EvasObjectEvent(this, Interop.Evas.evas_object_evas_get(Handle), EvasObjectCallbackType.RenderPost);
372 _deleted.On += (s, e) => MakeInvalidate();
373 _keydown.On += (s, e) => KeyDown?.Invoke(this, e);
374 _keyup.On += (s, e) => KeyUp?.Invoke(this, e);
378 public void Unrealize()
383 IntPtr toBeDeleted = Handle;
384 Handle = IntPtr.Zero;
388 (Parent as Window)?.RemoveChild(this);
390 Interop.Evas.evas_object_del(toBeDeleted);
395 private void MakeInvalidate()
397 Deleted?.Invoke(this, EventArgs.Empty);
399 Handle = IntPtr.Zero;
401 MakeInvalidateEvent();
403 (Parent as Window)?.RemoveChild(this);
408 private void DisposeEvent()
410 foreach (var evt in _eventStore)
416 private void MakeInvalidateEvent()
418 foreach (var evt in _eventStore)
420 evt.MakeInvalidate();
425 internal void AddToEventLifeTracker(IInvalidatable item)
427 _eventStore.Add(item);