Merge "[NUI] Add Extents type Padding in View"
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / StyleManager.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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
18 using System;
19 using System.Runtime.InteropServices;
20 using Tizen.NUI.BaseComponents;
21 using System.ComponentModel;
22
23 namespace Tizen.NUI
24 {
25     /// <summary>
26     /// The StyleManager informs applications of the system theme change, and supports application theme change at runtime.<br />
27     /// Applies various styles to controls using the properties system.<br />
28     /// On theme change, it automatically updates all controls, then raises a event to inform the application.<br />
29     /// If the application wants to customize the theme, RequestThemeChange needs to be called.<br />
30     /// It provides the path to the application resource root folder, from there the filename can be specified along with any subfolders, for example, Images, Models, etc.<br />
31     /// </summary>
32     /// <since_tizen> 3 </since_tizen>
33     public class StyleManager : BaseHandle
34     {
35         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
36
37         internal StyleManager(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.StyleManager_SWIGUpcast(cPtr), cMemoryOwn)
38         {
39             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
40         }
41
42         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(StyleManager obj)
43         {
44             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
45         }
46
47         private static readonly StyleManager instance = StyleManager.Get();
48
49         /// <summary>
50         /// Gets the singleton of the StyleManager object.
51         /// </summary>
52         /// <since_tizen> 5 </since_tizen>
53         public static StyleManager Instance
54         {
55             get
56             {
57                 return instance;
58             }
59         }
60
61         /// <summary>
62         /// Dispose.
63         /// </summary>
64         /// Please DO NOT use! This will be deprecated!
65         /// Dispose() method in Singletone classes (ex: FocusManager, StyleManager, VisualFactory, IMFManager, TtsPlayer, Window) is not required.
66         /// Because it is Sigletone, so it is alive for one thread until the NUI is terminated, so it never be disposed.
67         /// <since_tizen> 3 </since_tizen>
68         [Obsolete("Please do not use! This will be deprecated!")]
69         [EditorBrowsable(EditorBrowsableState.Never)]
70         protected override void Dispose(DisposeTypes type)
71         {
72             if (disposed)
73             {
74                 return;
75             }
76
77             if (type == DisposeTypes.Explicit)
78             {
79                 //Called by User
80                 //Release your own managed resources here.
81                 //You should release all of your own disposable objects here.
82             }
83
84             //Release your own unmanaged resources here.
85             //You should not access any managed member here except static instance.
86             //because the execution order of Finalizes is non-deterministic.
87
88             if (_styleManagerStyleChangedCallbackDelegate != null)
89             {
90                 StyleChangedSignal().Disconnect(_styleManagerStyleChangedCallbackDelegate);
91             }
92
93             if (swigCPtr.Handle != global::System.IntPtr.Zero)
94             {
95                 if (swigCMemOwn)
96                 {
97                     swigCMemOwn = false;
98                     NDalicPINVOKE.delete_StyleManager(swigCPtr);
99                 }
100                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
101             }
102
103             base.Dispose(type);
104         }
105
106         /// <summary>
107         /// Style changed event arguments.
108         /// </summary>
109         /// <since_tizen> 3 </since_tizen>
110         public class StyleChangedEventArgs : EventArgs
111         {
112             private StyleManager _styleManager;
113             private StyleChangeType _styleChange;
114
115             /// <summary>
116             /// StyleManager.
117             /// </summary>
118             /// <since_tizen> 3 </since_tizen>
119             public StyleManager StyleManager
120             {
121                 get
122                 {
123                     return _styleManager;
124                 }
125                 set
126                 {
127                     _styleManager = value;
128                 }
129             }
130
131             /// <summary>
132             /// StyleChange - contains the style change information (default font changed or
133             /// default font size changed or theme has changed).<br />
134             /// </summary>
135             /// <since_tizen> 3 </since_tizen>
136             public StyleChangeType StyleChange
137             {
138                 get
139                 {
140                     return _styleChange;
141                 }
142                 set
143                 {
144                     _styleChange = value;
145                 }
146             }
147
148         }
149
150         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
151         private delegate void StyleChangedCallbackDelegate(IntPtr styleManager, Tizen.NUI.StyleChangeType styleChange);
152         private EventHandler<StyleChangedEventArgs> _styleManagerStyleChangedEventHandler;
153         private StyleChangedCallbackDelegate _styleManagerStyleChangedCallbackDelegate;
154
155         /// <summary>
156         /// An event for the StyleChanged signal which can be used to subscribe or unsubscribe the
157         /// event handler provided by the user.<br />
158         /// The StyleChanged signal is emitted after the style (for example, theme or font change) has changed
159         /// and the controls have been informed.<br />
160         /// </summary>
161         /// <since_tizen> 3 </since_tizen>
162         public event EventHandler<StyleChangedEventArgs> StyleChanged
163         {
164             add
165             {
166                 if (_styleManagerStyleChangedEventHandler == null)
167                 {
168                     _styleManagerStyleChangedCallbackDelegate = (OnStyleChanged);
169                     StyleChangedSignal().Connect(_styleManagerStyleChangedCallbackDelegate);
170                 }
171                 _styleManagerStyleChangedEventHandler += value;
172             }
173             remove
174             {
175                 _styleManagerStyleChangedEventHandler -= value;
176                 if (_styleManagerStyleChangedEventHandler == null && StyleChangedSignal().Empty() == false)
177                 {
178                     StyleChangedSignal().Disconnect(_styleManagerStyleChangedCallbackDelegate);
179                 }
180             }
181         }
182
183         // Callback for StyleManager StyleChangedsignal
184         private void OnStyleChanged(IntPtr styleManager, StyleChangeType styleChange)
185         {
186             StyleChangedEventArgs e = new StyleChangedEventArgs();
187
188             // Populate all members of "e" (StyleChangedEventArgs) with real data
189             e.StyleManager = Registry.GetManagedBaseHandleFromNativePtr(styleManager) as StyleManager;
190             e.StyleChange = styleChange;
191
192             if (_styleManagerStyleChangedEventHandler != null)
193             {
194                 //here we send all data to user event handlers
195                 _styleManagerStyleChangedEventHandler(this, e);
196             }
197         }
198
199         /// <summary>
200         /// Creates a StyleManager handle.<br />
201         /// This can be initialized with StyleManager::Get().<br />
202         /// </summary>
203         /// <since_tizen> 3 </since_tizen>
204         public StyleManager() : this(NDalicPINVOKE.new_StyleManager(), true)
205         {
206             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207         }
208
209         /// <summary>
210         /// Gets the singleton of StyleManager object.
211         /// </summary>
212         /// <returns>A handle to the StyleManager control.</returns>
213         /// <since_tizen> 3 </since_tizen>
214         public static StyleManager Get()
215         {
216             StyleManager ret = new StyleManager(NDalicPINVOKE.StyleManager_Get(), true);
217             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218             return ret;
219         }
220
221         /// <summary>
222         /// Applies a new theme to the application.<br />
223         /// This will be merged on the top of the default Toolkit theme.<br />
224         /// If the application theme file doesn't style all controls that the
225         /// application uses, then the default Toolkit theme will be used
226         /// instead for those controls.<br />
227         /// </summary>
228         /// <param name="themeFile">A relative path is specified for style theme.</param>
229         /// <since_tizen> 3 </since_tizen>
230         public void ApplyTheme(string themeFile)
231         {
232             NDalicPINVOKE.StyleManager_ApplyTheme(swigCPtr, themeFile);
233             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234         }
235
236         /// <summary>
237         /// Applies the default Toolkit theme.
238         /// </summary>
239         /// <since_tizen> 3 </since_tizen>
240         public void ApplyDefaultTheme()
241         {
242             NDalicPINVOKE.StyleManager_ApplyDefaultTheme(swigCPtr);
243             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
244         }
245
246         /// <summary>
247         /// Sets a constant for use when building styles.
248         /// </summary>
249         /// <param name="key">The key of the constant.</param>
250         /// <param name="value">The value of the constant.</param>
251         /// <since_tizen> 3 </since_tizen>
252         public void AddConstant(string key, PropertyValue value)
253         {
254             NDalicPINVOKE.StyleManager_SetStyleConstant(swigCPtr, key, PropertyValue.getCPtr(value));
255             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256         }
257
258         /// <summary>
259         /// Returns the style constant set for a specific key.
260         /// </summary>
261         /// <param name="key">The key of the constant.</param>
262         /// <param name="valueOut">The value of the constant if it exists.</param>
263         /// <returns></returns>
264         /// <since_tizen> 3 </since_tizen>
265         public bool GetConstant(string key, PropertyValue valueOut)
266         {
267             bool ret = NDalicPINVOKE.StyleManager_GetStyleConstant(swigCPtr, key, PropertyValue.getCPtr(valueOut));
268             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
269             return ret;
270         }
271
272         /// <summary>
273         /// Applies the specified style to the control.
274         /// </summary>
275         /// <param name="control">The control to which to apply the style.</param>
276         /// <param name="jsonFileName">The name of the JSON style file to apply.</param>
277         /// <param name="styleName">The name of the style within the JSON file to apply.</param>
278         /// <since_tizen> 3 </since_tizen>
279         public void ApplyStyle(View control, string jsonFileName, string styleName)
280         {
281             NDalicPINVOKE.StyleManager_ApplyStyle(swigCPtr, View.getCPtr(control), jsonFileName, styleName);
282             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
283         }
284
285         internal StyleChangedSignal StyleChangedSignal()
286         {
287             StyleChangedSignal ret = new StyleChangedSignal(NDalicPINVOKE.StyleManager_StyleChangedSignal(swigCPtr), false);
288             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
289             return ret;
290         }
291
292     }
293 }