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