]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
added cmd line parsing support to wxApp and --theme option to wxUniv
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/appcmn.cpp
3 // Purpose: wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 18.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "appbase.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/app.h"
33 #include "wx/intl.h"
34 #include "wx/list.h"
35 #endif
36
37 #include "wx/cmdline.h"
38 #include "wx/thread.h"
39 #include "wx/confbase.h"
40
41 // ===========================================================================
42 // implementation
43 // ===========================================================================
44
45 // ----------------------------------------------------------------------------
46 // initialization and termination
47 // ----------------------------------------------------------------------------
48
49 wxAppBase::wxAppBase()
50 {
51 wxTheApp = (wxApp *)this;
52
53 // VZ: what's this? is it obsolete?
54 m_wantDebugOutput = FALSE;
55
56 #if wxUSE_GUI
57 m_topWindow = (wxWindow *)NULL;
58 m_useBestVisual = FALSE;
59 m_exitOnFrameDelete = TRUE;
60 m_isActive = TRUE;
61 #endif // wxUSE_GUI
62 }
63
64 #if wxUSE_GUI
65 bool wxAppBase::OnInitGui()
66 {
67 #ifdef __WXUNIVERSAL__
68 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
69 return FALSE;
70 #endif // __WXUNIVERSAL__
71
72 return TRUE;
73 }
74 #endif // wxUSE_GUI
75
76 int wxAppBase::OnExit()
77 {
78 #if wxUSE_CONFIG
79 // delete the config object if any (don't use Get() here, but Set()
80 // because Get() could create a new config object)
81 delete wxConfigBase::Set((wxConfigBase *) NULL);
82 #endif // wxUSE_CONFIG
83
84 #ifdef __WXUNIVERSAL__
85 delete wxTheme::Set(NULL);
86 #endif // __WXUNIVERSAL__
87
88 return 0;
89 }
90
91 // ---------------------------------------------------------------------------
92 // wxAppBase
93 // ----------------------------------------------------------------------------
94
95 void wxAppBase::ProcessPendingEvents()
96 {
97 // ensure that we're the only thread to modify the pending events list
98 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
99
100 if ( !wxPendingEvents )
101 {
102 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
103 return;
104 }
105
106 // iterate until the list becomes empty
107 wxNode *node = wxPendingEvents->First();
108 while (node)
109 {
110 wxEvtHandler *handler = (wxEvtHandler *)node->Data();
111 delete node;
112
113 // In ProcessPendingEvents(), new handlers might be add
114 // and we can safely leave the critical section here.
115 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
116 handler->ProcessPendingEvents();
117 wxENTER_CRIT_SECT( *wxPendingEventsLocker );
118
119 node = wxPendingEvents->First();
120 }
121
122 wxLEAVE_CRIT_SECT( *wxPendingEventsLocker );
123 }
124
125 // ----------------------------------------------------------------------------
126 // misc
127 // ----------------------------------------------------------------------------
128
129 #if wxUSE_GUI
130
131 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
132 {
133 m_isActive = active;
134 }
135
136 #endif // wxUSE_GUI
137
138 // ----------------------------------------------------------------------------
139 // cmd line parsing
140 // ----------------------------------------------------------------------------
141
142 bool wxAppBase::OnInit()
143 {
144 #if wxUSE_CMDLINE_PARSER
145 wxCmdLineParser parser(argc, argv);
146
147 OnInitCmdLine(parser);
148
149 bool cont;
150 switch ( parser.Parse() )
151 {
152 case -1:
153 cont = OnCmdLineHelp(parser);
154 break;
155
156 case 0:
157 cont = OnCmdLineParsed(parser);
158 break;
159
160 default:
161 cont = OnCmdLineError(parser);
162 break;
163 }
164
165 if ( !cont )
166 return FALSE;
167 #endif // wxUSE_CMDLINE_PARSER
168
169 return TRUE;
170 }
171
172 #if wxUSE_CMDLINE_PARSER
173
174 #define OPTION_VERBOSE _T("verbose")
175 #define OPTION_THEME _T("theme")
176
177 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
178 {
179 // the standard command line options
180 static const wxCmdLineEntryDesc cmdLineDesc[] =
181 {
182 {
183 wxCMD_LINE_SWITCH,
184 _T("h"),
185 _T("help"),
186 gettext_noop("show this help message"),
187 wxCMD_LINE_VAL_NONE,
188 wxCMD_LINE_OPTION_HELP
189 },
190
191 #if wxUSE_LOG
192 {
193 wxCMD_LINE_SWITCH,
194 _T(""),
195 OPTION_VERBOSE,
196 gettext_noop("generate verbose log messages")
197 },
198 #endif wxUSE_LOG
199
200 #ifdef __WXUNIVERSAL__
201 {
202 wxCMD_LINE_OPTION,
203 _T(""),
204 OPTION_THEME,
205 gettext_noop("specify the theme to use"),
206 wxCMD_LINE_VAL_STRING
207 },
208 #endif // __WXUNIVERSAL__
209
210 // terminator
211 { wxCMD_LINE_NONE }
212 };
213
214 parser.SetDesc(cmdLineDesc);
215 }
216
217 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
218 {
219 #if wxUSE_LOG
220 if ( parser.Found(OPTION_VERBOSE) )
221 {
222 wxLog::SetVerbose(TRUE);
223 }
224 #endif // wxUSE_LOG
225
226 #ifdef __WXUNIVERSAL__
227 wxString themeName;
228 if ( parser.Found(OPTION_THEME, &themeName) )
229 {
230 wxTheme *theme = wxTheme::Create(themeName);
231 if ( !theme )
232 {
233 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
234
235 return FALSE;
236 }
237
238 wxTheme::Set(theme);
239 }
240 #endif // __WXUNIVERSAL__
241
242 return TRUE;
243 }
244
245 bool wxAppBase::OnCmdLineHelp(wxCmdLineParser& parser)
246 {
247 parser.Usage();
248
249 return FALSE;
250 }
251
252 bool wxAppBase::OnCmdLineError(wxCmdLineParser& parser)
253 {
254 parser.Usage();
255
256 return FALSE;
257 }
258
259 #endif // wxUSE_CMDLINE_PARSER
260