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