441f4ba3ec4555ac2b2da248bf71803bc110fcef
[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     /// StyleManager informs applications of 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 an be specified along with any sub folders, e.g 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 (swigCPtr.Handle != global::System.IntPtr.Zero)
67             {
68                 if (swigCMemOwn)
69                 {
70                     swigCMemOwn = false;
71
72                     //Unreference this instance from Registry.
73                     Registry.Unregister(this);
74
75                     NDalicPINVOKE.delete_StyleManager(swigCPtr);
76                 }
77                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
78             }
79
80             base.Dispose(type);
81         }
82
83         /// <summary>
84         /// Style changed event arguments
85         /// </summary>
86         public class StyleChangedEventArgs : EventArgs
87         {
88             private StyleManager _styleManager;
89             private StyleChangeType _styleChange;
90
91             /// <summary>
92             /// StyleManager.
93             /// </summary>
94             public StyleManager StyleManager
95             {
96                 get
97                 {
98                     return _styleManager;
99                 }
100                 set
101                 {
102                     _styleManager = value;
103                 }
104             }
105
106             /// <summary>
107             /// StyleChange - contains Style change information (default font changed or
108             /// default font size changed or theme has changed).<br>
109             /// </summary>
110             public StyleChangeType StyleChange
111             {
112                 get
113                 {
114                     return _styleChange;
115                 }
116                 set
117                 {
118                     _styleChange = value;
119                 }
120             }
121
122         }
123
124         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
125         private delegate void StyleChangedCallbackDelegate(IntPtr styleManager, Tizen.NUI.StyleChangeType styleChange);
126         private EventHandler<StyleChangedEventArgs> _styleManagerStyleChangedEventHandler;
127         private StyleChangedCallbackDelegate _styleManagerStyleChangedCallbackDelegate;
128
129         /// <summary>
130         /// Event for StyleChanged signal which can be used to subscribe/unsubscribe the
131         /// event handler provided by the user.<br>
132         /// StyleChanged signal is is emitted after the style (e.g. theme/font change) has changed
133         /// and the controls have been informed.<br>
134         /// </summary>
135         public event EventHandler<StyleChangedEventArgs> StyleChanged
136         {
137             add
138             {
139                 if (_styleManagerStyleChangedEventHandler == null)
140                 {
141                     _styleManagerStyleChangedCallbackDelegate = (OnStyleChanged);
142                     StyleChangedSignal().Connect(_styleManagerStyleChangedCallbackDelegate);
143                 }
144                 _styleManagerStyleChangedEventHandler += value;
145             }
146             remove
147             {
148                 _styleManagerStyleChangedEventHandler -= value;
149                 if (_styleManagerStyleChangedEventHandler == null && StyleChangedSignal().Empty() == false)
150                 {
151                     StyleChangedSignal().Disconnect(_styleManagerStyleChangedCallbackDelegate);
152                 }
153             }
154         }
155
156         // Callback for StyleManager StyleChangedsignal
157         private void OnStyleChanged(IntPtr styleManager, StyleChangeType styleChange)
158         {
159             StyleChangedEventArgs e = new StyleChangedEventArgs();
160
161             // Populate all members of "e" (StyleChangedEventArgs) with real data
162             e.StyleManager = Registry.GetManagedBaseHandleFromNativePtr(styleManager) as StyleManager;
163             e.StyleChange = styleChange;
164
165             if (_styleManagerStyleChangedEventHandler != null)
166             {
167                 //here we send all data to user event handlers
168                 _styleManagerStyleChangedEventHandler(this, e);
169             }
170         }
171
172         /// <summary>
173         /// Creates a StyleManager handle.<br>
174         /// this can be initialized with StyleManager::Get().<br>
175         /// </summary>
176         public StyleManager() : this(NDalicPINVOKE.new_StyleManager(), true)
177         {
178             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179         }
180
181         /// <summary>
182         /// Gets the singleton of StyleManager object.
183         /// </summary>
184         /// <returns>A handle to the StyleManager control</returns>
185         public static StyleManager Get()
186         {
187             StyleManager ret = new StyleManager(NDalicPINVOKE.StyleManager_Get(), true);
188             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
189             return ret;
190         }
191
192         /// <summary>
193         /// Applies a new theme to the application. <br>
194         /// This will be merged on top of the default Toolkit theme.<br>
195         /// If the application theme file doesn't style all controls that the
196         /// application uses, then the default Toolkit theme will be used
197         /// instead for those controls.<br>
198         /// </summary>
199         /// <param name="themeFile">A relative path is specified for style theme</param>
200         public void ApplyTheme(string themeFile)
201         {
202             NDalicPINVOKE.StyleManager_ApplyTheme(swigCPtr, themeFile);
203             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204         }
205
206         /// <summary>
207         /// Applies the default Toolkit theme.
208         /// </summary>
209         public void ApplyDefaultTheme()
210         {
211             NDalicPINVOKE.StyleManager_ApplyDefaultTheme(swigCPtr);
212             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213         }
214
215         /// <summary>
216         /// Sets a constant for use when building styles.
217         /// </summary>
218         /// <param name="key">The key of the constant</param>
219         /// <param name="value">The value of the constant</param>
220         public void AddConstant(string key, PropertyValue value)
221         {
222             NDalicPINVOKE.StyleManager_SetStyleConstant(swigCPtr, key, PropertyValue.getCPtr(value));
223             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224         }
225
226         /// <summary>
227         /// Returns the style constant set for a specific key.
228         /// </summary>
229         /// <param name="key">The key of the constant</param>
230         /// <param name="valueOut">The value of the constant if it exists</param>
231         /// <returns></returns>
232         public bool GetConstant(string key, PropertyValue valueOut)
233         {
234             bool ret = NDalicPINVOKE.StyleManager_GetStyleConstant(swigCPtr, key, PropertyValue.getCPtr(valueOut));
235             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236             return ret;
237         }
238
239         /// <summary>
240         /// Applies the specified style to the control.
241         /// </summary>
242         /// <param name="control">The control to which to apply the style</param>
243         /// <param name="jsonFileName">The name of the JSON style file to apply</param>
244         /// <param name="styleName">The name of the style within the JSON file to apply</param>
245         public void ApplyStyle(View control, string jsonFileName, string styleName)
246         {
247             NDalicPINVOKE.StyleManager_ApplyStyle(swigCPtr, View.getCPtr(control), jsonFileName, styleName);
248             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
249         }
250
251         internal StyleChangedSignal StyleChangedSignal()
252         {
253             StyleChangedSignal ret = new StyleChangedSignal(NDalicPINVOKE.StyleManager_StyleChangedSignal(swigCPtr), false);
254             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
255             return ret;
256         }
257
258     }
259 }