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