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