NXWidgets  1.19
singletons.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/singletons.cxx
3  *
4  * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18  * me be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************
35  *
36  * Portions of this package derive from Woopsi (http://woopsi.org/) and
37  * portions are original efforts. It is difficult to determine at this
38  * point what parts are original efforts and which parts derive from Woopsi.
39  * However, in any event, the work of Antony Dzeryn will be acknowledged
40  * in most NxWidget files. Thanks Antony!
41  *
42  * Copyright (c) 2007-2011, Antony Dzeryn
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions are met:
47  *
48  * * Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * * Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * * Neither the names "Woopsi", "Simian Zombie" nor the
54  * names of its contributors may be used to endorse or promote products
55  * derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  ****************************************************************************/
69 
70 /****************************************************************************
71  * Included Files
72  ****************************************************************************/
73 
74 #include <nuttx/config.h>
75 
76 #include <sys/types.h>
77 #include <stdint.h>
78 #include <stdbool.h>
79 #include <sched.h>
80 #include <debug.h>
81 
82 #include "nxconfig.hxx"
83 #include "cnxserver.hxx"
84 #include "cnxstring.hxx"
85 #include "cwidgetstyle.hxx"
86 #include "cnxfont.hxx"
87 #include "singletons.hxx"
88 
89 /****************************************************************************
90  * Pre-Processor Definitions
91  ****************************************************************************/
92 
93 /****************************************************************************
94  * Global Static Data
95  ****************************************************************************/
96 
97 using namespace NXWidgets;
98 
99 CWidgetStyle *NXWidgets::g_defaultWidgetStyle; /**< The default widget style */
100 CNxString *NXWidgets::g_nullString; /**< The reusable empty string */
101 TNxArray<CNxTimer*> *NXWidgets::g_nxTimers; /**< An array of all timers */
102 
103 /****************************************************************************
104  * Method Implementations
105  ****************************************************************************/
106 
107 /**
108  * Setup misc singleton instances. Why is there here? Because it needs to be
109  * done one time before any widgets are created. So why isn't it just the
110  * default style just a statically constructed class? Because not all platforms
111  * will support static class constructions.
112  */
113 
115 {
116  // Disable pre-emption. Since we are dealing with global resources here we
117  // are not inherently thread-safe.
118 
119  sched_lock();
120 
121  // Create a global, empty string that may be used whereever a string is
122  // required, but not needed.
123 
124  if (!g_nullString)
125  {
126  g_nullString = new CNxString();
127  }
128 
129  // Setup the default widget style
130 
132  {
133  // Create the singleton
134 
136 
137  // Default colors
138 
139  g_defaultWidgetStyle->colors.background = CONFIG_NXWIDGETS_DEFAULT_BACKGROUNDCOLOR;
140  g_defaultWidgetStyle->colors.selectedBackground = CONFIG_NXWIDGETS_DEFAULT_SELECTEDBACKGROUNDCOLOR;
141  g_defaultWidgetStyle->colors.shineEdge = CONFIG_NXWIDGETS_DEFAULT_SHINEEDGECOLOR;
142  g_defaultWidgetStyle->colors.shadowEdge = CONFIG_NXWIDGETS_DEFAULT_SHADOWEDGECOLOR;
143  g_defaultWidgetStyle->colors.highlight = CONFIG_NXWIDGETS_DEFAULT_HIGHLIGHTCOLOR;
144 
145  g_defaultWidgetStyle->colors.disabledText = CONFIG_NXWIDGETS_DEFAULT_DISABLEDTEXTCOLOR;
146  g_defaultWidgetStyle->colors.enabledText = CONFIG_NXWIDGETS_DEFAULT_ENABLEDTEXTCOLOR;
147  g_defaultWidgetStyle->colors.selectedText = CONFIG_NXWIDGETS_DEFAULT_SELECTEDTEXTCOLOR;
148 
149  // Default font using the default font ID and the default font colors
150 
151  g_defaultWidgetStyle->font = new CNxFont((enum nx_fontid_e)CONFIG_NXWIDGETS_DEFAULT_FONTID,
152  CONFIG_NXWIDGETS_DEFAULT_FONTCOLOR,
153  CONFIG_NXWIDGETS_TRANSPARENT_COLOR);
154  }
155 
156  // Create the timer list
157 
158  if (!g_nxTimers)
159  {
161  }
162 
163  sched_unlock();
164 }
165 
166 /**
167  * Free the singleton instances when the last NX server is destroyed.
168  */
169 
171 {
172  // Delete the null string singleton
173 
174  if (g_nullString)
175  {
176  delete g_nullString;
177  g_nullString = (CNxString *)NULL;
178  }
179 
180  // Delete the default widget style singleton
181 
183  {
185  {
186  delete g_defaultWidgetStyle->font;
187  }
188 
189  delete g_defaultWidgetStyle;
191  }
192 
193  // Free the timer list
194 
195  if (g_nxTimers)
196  {
197  delete g_nxTimers;
199  }
200 
201 }
202 
nxgl_mxpixel_t selectedBackground
nxgl_mxpixel_t enabledText
nxgl_mxpixel_t disabledText
void instantiateSingletons(void)
Definition: singletons.cxx:114
TNxArray< CNxTimer * > * g_nxTimers
Definition: singletons.hxx:109
CWidgetStyle * g_defaultWidgetStyle
Definition: singletons.hxx:101
void freeSingletons(void)
Definition: singletons.cxx:170
nxgl_mxpixel_t selectedText
CNxString * g_nullString
Definition: singletons.hxx:108