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