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