26e840041a9886fad6e8dc5ca94886bc9ac989da
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WidgetApplication / Tizen.Applications / WidgetBase.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 ElmSharp;
18 using System;
19
20 namespace Tizen.Applications
21 {
22     /// <summary>
23     /// The abstract class for widget instances.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public abstract class WidgetBase
27     {
28         internal IntPtr Handle;
29         internal string Id;
30         private const string LogTag = "Tizen.Applications.WidgetBase";
31
32         /// <summary>
33         /// Window object for this widget instance.
34         /// It will be created after OnCreate method is invoked.
35         /// </summary>
36         /// <since_tizen> 3 </since_tizen>
37         protected Window Window;
38
39         /// <summary>
40         /// Delete type.
41         /// </summary>
42         /// <since_tizen> 3 </since_tizen>
43         public enum WidgetDestroyType
44         {
45             /// <summary>
46             /// User deleted this widget from the viewer.
47             /// </summary>
48             Permanent = 0,
49
50             /// <summary>
51             /// Widget is deleted because of other reasons. (For example, widget process is terminated temporarily by the system)
52             /// </summary>
53             Temporary
54         }
55
56         /// <summary>
57         /// Constructor.
58         /// </summary>
59         /// <since_tizen> 3 </since_tizen>
60         public WidgetBase()
61         {
62         }
63
64         /// <summary>
65         /// Sets the content information to the widget.
66         /// </summary>
67         /// <param name="info">The data set to save.</param>
68         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
69         /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
70         /// <exception cref="InvalidOperationException">Thrown in case of an unrecoverable error.</exception>
71         /// <since_tizen> 3 </since_tizen>
72         public void SetContent(Bundle info)
73         {
74             Interop.Widget.ErrorCode err = Interop.Widget.SetContent(Handle, info.SafeBundleHandle);
75
76             if (err != Interop.Widget.ErrorCode.None)
77             {
78                 Log.Error(LogTag, "Failed to set content. Err = " + err);
79
80                 switch(err)
81                 {
82                     case Interop.Widget.ErrorCode.InvalidParameter:
83                         throw new ArgumentException("Invalid parameter of SetContent()");
84
85                     case Interop.Widget.ErrorCode.NotSupported:
86                         throw new NotSupportedException();
87
88                     case Interop.Widget.ErrorCode.OutOfMemory:
89                     case Interop.Widget.ErrorCode.Fault:
90                         throw new InvalidOperationException();
91                 }
92
93             }
94         }
95
96         /// <summary>
97         /// Sends the title to the widget.
98         /// </summary>
99         /// <param name="title">When an accessibility mode is turned on, this string will be read.</param>
100         /// <exception cref="ArgumentException">Thrown when failed because of an invalid argument.</exception>
101         /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
102         /// <exception cref="InvalidOperationException">Thrown in case of an unrecoverable error.</exception>
103         /// <since_tizen> 3 </since_tizen>
104         public void SetTitle(string title)
105         {
106             Interop.Widget.ErrorCode err = Interop.Widget.SetTitle(Handle, title);
107
108             if (err != Interop.Widget.ErrorCode.None)
109             {
110                 Log.Error(LogTag, "Failed to set title. Err = " + err);
111
112                 switch (err)
113                 {
114                     case Interop.Widget.ErrorCode.InvalidParameter:
115                         throw new ArgumentException("Invalid parameter of SetContent()");
116
117                     case Interop.Widget.ErrorCode.NotSupported:
118                         throw new NotSupportedException();
119
120                     case Interop.Widget.ErrorCode.OutOfMemory:
121                     case Interop.Widget.ErrorCode.Fault:
122                         throw new InvalidOperationException();
123                 }
124             }
125         }
126
127         /// <summary>
128         /// Finishes the context for the widget instance.
129         /// </summary>
130         /// <exception cref="NotSupportedException">Thrown when the API is not supported in this device.</exception>
131         /// <exception cref="InvalidOperationException">Thrown in case of an unrecoverable error.</exception>
132         /// <since_tizen> 3 </since_tizen>
133         public void Exit()
134         {
135             Interop.Widget.ErrorCode err = Interop.Widget.TerminateContext(Handle);
136
137             if (err != Interop.Widget.ErrorCode.None)
138             {
139                 Log.Error(LogTag, "Failed to terminate context. Err = " + err);
140
141                 switch (err)
142                 {
143                     case Interop.Widget.ErrorCode.NotSupported:
144                         throw new NotSupportedException();
145
146                     case Interop.Widget.ErrorCode.InvalidParameter:
147                     case Interop.Widget.ErrorCode.Fault:
148                         throw new InvalidOperationException();
149                 }
150             }
151         }
152
153         internal void Bind(IntPtr handle, string id)
154         {
155             Handle = handle;
156             Id = id;
157         }
158
159         /// <summary>
160         /// Overrides this method if want to handle the behavior when the widget instance is started.
161         /// </summary>
162         /// <param name="content">The data set for the previous status.</param>
163         /// <param name="w">The pixel value for the widget width.</param>
164         /// <param name="h">The pixel value for the widget height.</param>
165         /// <since_tizen> 3 </since_tizen>
166         public virtual void OnCreate(Bundle content, int w, int h)
167         {
168             IntPtr win;
169
170             Interop.Widget.GetWin(Handle, out win);
171             Window = new WidgetWindow(win);
172             Window.Resize(w, h);
173             Window.Show();
174         }
175
176         /// <summary>
177         /// Overrides this method if want to handle the behavior when the widget instance is destroyed.
178         /// </summary>
179         /// <param name="reason">The reason for destruction.</param>
180         /// <param name="content">The data set to save.</param>
181         /// <since_tizen> 3 </since_tizen>
182         public virtual void OnDestroy(WidgetDestroyType reason, Bundle content)
183         {
184         }
185
186         /// <summary>
187         /// Overrides this method if want to handle the behavior when the widget instance is paused.
188         /// </summary>
189         /// <since_tizen> 3 </since_tizen>
190         public virtual void OnPause()
191         {
192         }
193
194         /// <summary>
195         /// Overrides this method if want to handle the behavior when the widget instance is resumed.
196         /// </summary>
197         /// <since_tizen> 3 </since_tizen>
198         public virtual void OnResume()
199         {
200         }
201
202         /// <summary>
203         /// Overrides this method if want to handle the behavior when the widget instance is resized.
204         /// </summary>
205         /// <param name="w">Widget width.</param>
206         /// <param name="h">Widget height.</param>
207         /// <since_tizen> 3 </since_tizen>
208         public virtual void OnResize(int w, int h)
209         {
210         }
211
212         /// <summary>
213         /// Overrides this method if want to handle the behavior when the widget instance is updated.
214         /// </summary>
215         /// <param name="content">The data set for updating this widget will be provided by the requester.</param>
216         /// <param name="isForce">Although the widget is paused, if it is true, the widget can be updated.</param>
217         /// <since_tizen> 3 </since_tizen>
218         public virtual void OnUpdate(Bundle content, bool isForce)
219         {
220         }
221
222     }
223 }