Release 4.0.0-preview1-00235
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / StyleManager.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     using System;
21     using System.Runtime.InteropServices;
22     using Tizen.NUI.BaseComponents;
23
24     /// <summary>
25     /// The StyleManager informs applications of the system theme change, and supports application theme change at runtime.<br>
26     /// Applies various styles to controls using the properties system.<br>
27     /// On theme change, it automatically updates all controls, then raises a event to inform the application.<br>
28     /// If the application wants to customize the theme, RequestThemeChange needs to be called.<br>
29     /// 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>
30     /// </summary>
31     public class StyleManager : BaseHandle
32     {
33         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
34
35         internal StyleManager(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.StyleManager_SWIGUpcast(cPtr), cMemoryOwn)
36         {
37             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
38         }
39
40         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(StyleManager obj)
41         {
42             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
43         }
44
45         /// <summary>
46         /// Dispose.
47         /// </summary>
48         protected override void Dispose(DisposeTypes type)
49         {
50             if(disposed)
51             {
52                 return;
53             }
54
55             if(type == DisposeTypes.Explicit)
56             {
57                 //Called by User
58                 //Release your own managed resources here.
59                 //You should release all of your own disposable objects here.
60             }
61
62             //Release your own unmanaged resources here.
63             //You should not access any managed member here except static instance.
64             //because the execution order of Finalizes is non-deterministic.
65
66             if (_styleManagerStyleChangedCallbackDelegate != null)
67             {
68                 StyleChangedSignal().Disconnect(_styleManagerStyleChangedCallbackDelegate);
69             }
70
71             if (swigCPtr.Handle != global::System.IntPtr.Zero)
72             {
73                 if (swigCMemOwn)
74                 {
75                     swigCMemOwn = false;
76                     NDalicPINVOKE.delete_StyleManager(swigCPtr);
77                 }
78                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
79             }
80
81             base.Dispose(type);
82         }
83
84         /// <summary>
85         /// Style changed event arguments.
86         /// </summary>
87         public class StyleChangedEventArgs : EventArgs
88         {
89             private StyleManager _styleManager;
90             private StyleChangeType _styleChange;
91
92             /// <summary>
93             /// StyleManager.
94             /// </summary>
95             public StyleManager StyleManager
96             {
97                 get
98                 {
99                     return _styleManager;
100                 }
101                 set
102                 {
103                     _styleManager = value;
104                 }
105             }
106
107             /// <summary>
108             /// StyleChange - contains the style change information (default font changed or
109             /// default font size changed or theme has changed).<br>
110             /// </summary>
111             public StyleChangeType StyleChange
112             {
113                 get
114                 {
115                     return _styleChange;
116                 }
117                 set
118                 {
119                     _styleChange = value;
120                 }
121             }
122
123         }
124
125         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
126         private delegate void StyleChangedCallbackDelegate(IntPtr styleManager, Tizen.NUI.StyleChangeType styleChange);
127         private EventHandler<StyleChangedEventArgs> _styleManagerStyleChangedEventHandler;
128         private StyleChangedCallbackDelegate _styleManagerStyleChangedCallbackDelegate;
129
130         /// <summary>
131         /// An event for the StyleChanged signal which can be used to subscribe or unsubscribe the
132         /// event handler provided by the user.<br>
133         /// The StyleChanged signal is emitted after the style (for example, theme or font change) has changed
134         /// and the controls have been informed.<br>
135         /// </summary>
136         public event EventHandler<StyleChangedEventArgs> StyleChanged
137         {
138             add
139             {
140                 if (_styleManagerStyleChangedEventHandler == null)
141                 {
142                     _styleManagerStyleChangedCallbackDelegate = (OnStyleChanged);
143                     StyleChangedSignal().Connect(_styleManagerStyleChangedCallbackDelegate);
144                 }
145                 _styleManagerStyleChangedEventHandler += value;
146             }
147             remove
148             {
149                 _styleManagerStyleChangedEventHandler -= value;
150                 if (_styleManagerStyleChangedEventHandler == null && StyleChangedSignal().Empty() == false)
151                 {
152                     StyleChangedSignal().Disconnect(_styleManagerStyleChangedCallbackDelegate);
153                 }
154             }
155         }
156
157         // Callback for StyleManager StyleChangedsignal
158         private void OnStyleChanged(IntPtr styleManager, StyleChangeType styleChange)
159         {
160             StyleChangedEventArgs e = new StyleChangedEventArgs();
161
162             // Populate all members of "e" (StyleChangedEventArgs) with real data
163             e.StyleManager = Registry.GetManagedBaseHandleFromNativePtr(styleManager) as StyleManager;
164             e.StyleChange = styleChange;
165
166             if (_styleManagerStyleChangedEventHandler != null)
167             {
168                 //here we send all data to user event handlers
169                 _styleManagerStyleChangedEventHandler(this, e);
170             }
171         }
172
173         /// <summary>
174         /// Creates a StyleManager handle.<br>
175         /// This can be initialized with StyleManager::Get().<br>
176         /// </summary>
177         public StyleManager() : this(NDalicPINVOKE.new_StyleManager(), true)
178         {
179             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
180         }
181
182         /// <summary>
183         /// Gets the singleton of StyleManager object.
184         /// </summary>
185         /// <returns>A handle to the StyleManager control.</returns>
186         public static StyleManager Get()
187         {
188             StyleManager ret = new StyleManager(NDalicPINVOKE.StyleManager_Get(), true);
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190             return ret;
191         }
192
193         /// <summary>
194         /// Applies a new theme to the application.<br>
195         /// This will be merged on the top of the default Toolkit theme.<br>
196         /// If the application theme file doesn't style all controls that the
197         /// application uses, then the default Toolkit theme will be used
198         /// instead for those controls.<br>
199         /// </summary>
200         /// <param name="themeFile">A relative path is specified for style theme.</param>
201         public void ApplyTheme(string themeFile)
202         {
203             NDalicPINVOKE.StyleManager_ApplyTheme(swigCPtr, themeFile);
204             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205         }
206
207         /// <summary>
208         /// Applies the default Toolkit theme.
209         /// </summary>
210         public void ApplyDefaultTheme()
211         {
212             NDalicPINVOKE.StyleManager_ApplyDefaultTheme(swigCPtr);
213             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214         }
215
216         /// <summary>
217         /// Sets a constant for use when building styles.
218         /// </summary>
219         /// <param name="key">The key of the constant.</param>
220         /// <param name="value">The value of the constant.</param>
221         public void AddConstant(string key, PropertyValue value)
222         {
223             NDalicPINVOKE.StyleManager_SetStyleConstant(swigCPtr, key, PropertyValue.getCPtr(value));
224             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225         }
226
227         /// <summary>
228         /// Returns the style constant set for a specific key.
229         /// </summary>
230         /// <param name="key">The key of the constant.</param>
231         /// <param name="valueOut">The value of the constant if it exists.</param>
232         /// <returns></returns>
233         public bool GetConstant(string key, PropertyValue valueOut)
234         {
235             bool ret = NDalicPINVOKE.StyleManager_GetStyleConstant(swigCPtr, key, PropertyValue.getCPtr(valueOut));
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return ret;
238         }
239
240         /// <summary>
241         /// Applies the specified style to the control.
242         /// </summary>
243         /// <param name="control">The control to which to apply the style.</param>
244         /// <param name="jsonFileName">The name of the JSON style file to apply.</param>
245         /// <param name="styleName">The name of the style within the JSON file to apply.</param>
246         public void ApplyStyle(View control, string jsonFileName, string styleName)
247         {
248             NDalicPINVOKE.StyleManager_ApplyStyle(swigCPtr, View.getCPtr(control), jsonFileName, styleName);
249             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
250         }
251
252         internal StyleChangedSignal StyleChangedSignal()
253         {
254             StyleChangedSignal ret = new StyleChangedSignal(NDalicPINVOKE.StyleManager_StyleChangedSignal(swigCPtr), false);
255             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
256             return ret;
257         }
258
259     }
260 }