Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EvasCanvas.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     /// <summary>
23     /// Low level Evas canvas functions. Sub groups will present more high level ones, though.
24     /// Most of these functions deal with low level Evas actions, like:
25     /// create/destroy raw canvases, not bound to any displaying engine
26     /// tell a canvas i got focused(in a windowing context, for example)
27     /// tell a canvas a region should not be calculated anymore in rendering
28     /// tell a canvas to render its contents, immediately
29     /// Most users will be using Evas by means of the Ecore_Evas wrapper, which deals with all the above mentioned issues automatically for them.Thus, you'll be looking at this section only if you're building low level stuff.
30     /// The groups within present you functions that deal with the canvas directly, too, and not yet with its objects.They are the functions you need to use at a minimum to get a working canvas.
31     /// </summary>
32     public class EvasCanvas
33     {
34         IntPtr _handle = IntPtr.Zero;
35         Dictionary<EventData, Interop.Evas.EvasCallback> _eventDatas = new Dictionary<EventData, Interop.Evas.EvasCallback>();
36
37         internal EvasCanvas(IntPtr evasObject)
38         {
39             _handle = CreateHandle(evasObject);
40         }
41
42         /// <summary>
43         /// Gets or sets the image cache.
44         /// This function returns the image cache size of canvas in bytes.
45         /// </summary>
46         public int ImageCacheSize
47         {
48             get
49             {
50                 return Interop.Evas.evas_image_cache_get(_handle);
51             }
52             set
53             {
54                 Interop.Evas.evas_image_cache_set(_handle, value);
55             }
56         }
57
58         /// <summary>
59         /// Flush the image cache of the canvas.
60         /// </summary>
61         public void FlushImageCache()
62         {
63             Interop.Evas.evas_image_cache_flush(_handle);
64         }
65
66         /// <summary>
67         /// Add a damage rectangle.
68         /// </summary>
69         /// <param name="x">The rectangle's top left corner's horizontal coordinate.</param>
70         /// <param name="y">The rectangle's top left corner's vertical coordinate.</param>
71         /// <param name="width">The rectangle's width.</param>
72         /// <param name="height">The rectangle's height.</param>
73         public void AddDamageRectangle(int x, int y, int width, int height)
74         {
75             Interop.Evas.evas_damage_rectangle_add(_handle, x, y, width, height);
76         }
77
78         /// <summary>
79         /// Add an "obscured region" to an Evas canvas.
80         /// </summary>
81         /// <param name="x">The rectangle's top left corner's horizontal coordinate.</param>
82         /// <param name="y">The rectangle's top left corner's vertical coordinate.</param>
83         /// <param name="width">The rectangle's width.</param>
84         /// <param name="height">The rectangle's height.</param>
85         public void AddObscuredRectangle(int x, int y, int width, int height)
86         {
87             Interop.Evas.evas_obscured_rectangle_add(_handle, x, y, width, height);
88         }
89
90         /// <summary>
91         /// Remove all "obscured regions" from an Evas canvas.
92         /// </summary>
93         public void ClearObscuredRectangle()
94         {
95             Interop.Evas.evas_obscured_clear(_handle);
96         }
97
98         /// <summary>
99         /// Adds or registers a event to a given canvas event.
100         /// </summary>
101         /// <param name="type">The type of event that triggers</param>
102         /// <param name="action">The action to be called when the event is triggered</param>
103         public void AddEventAction(EvasObjectCallbackType type, Action action)
104         {
105             if (action != null)
106             {
107                 var eventData = new EventData(type, action);
108
109                 if (!_eventDatas.ContainsKey(eventData))
110                 {
111                     var evasCallback = new Interop.Evas.EvasCallback((d, o, t) => action());
112                     Interop.Evas.evas_event_callback_add(_handle, (Interop.Evas.ObjectCallbackType)type, evasCallback, IntPtr.Zero);
113                     _eventDatas.Add(eventData, evasCallback);
114                 }
115             }
116         }
117
118         /// <summary>
119         /// Deletes a event to a given canvas event.
120         /// </summary>
121         /// <param name="type">The type of event that triggers</param>
122         /// <param name="action">The action to be called when the event is triggered</param>
123         public void DeleteEventAction(EvasObjectCallbackType type, Action action)
124         {
125             if (action != null)
126             {
127                 var eventData = new EventData(type, action);
128                 Interop.Evas.EvasCallback evasCallback = null;
129                 _eventDatas.TryGetValue(eventData, out evasCallback);
130
131                 if (evasCallback != null)
132                 {
133                     Interop.Evas.evas_event_callback_del(_handle, (Interop.Evas.ObjectCallbackType)type, evasCallback);
134                     _eventDatas.Remove(eventData);
135                 }
136             }
137         }
138
139         IntPtr CreateHandle(IntPtr evasObject)
140         {
141             return Interop.Evas.evas_object_evas_get(evasObject);
142         }
143
144         class EventData
145         {
146             public EvasObjectCallbackType Type { get; set; }
147             public Action Action { get; set; }
148
149             public EventData(EvasObjectCallbackType type, Action action)
150             {
151                 Type = type;
152                 Action = action;
153             }
154
155             public override bool Equals(object obj)
156             {
157                 EventData e = obj as EventData;
158                 if (e == null)
159                 {
160                     return false;
161                 }
162                 return (Type == e.Type) && (Action == e.Action);
163             }
164
165             public override int GetHashCode()
166             {
167                 int hashCode = Type.GetHashCode();
168                 hashCode ^= Action.GetHashCode();
169                 return hashCode;
170             }
171         }
172     }
173 }