]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
compilation fix after argv changes
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppConsole and 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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if defined(__BORLANDC__)
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/window.h"
30 #include "wx/bitmap.h"
31 #include "wx/log.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
34 #include "wx/utils.h"
35 #include "wx/wxcrtvararg.h"
36 #endif
37
38 #include "wx/apptrait.h"
39 #include "wx/cmdline.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
43
44 #ifdef __WXDEBUG__
45 #if wxUSE_STACKWALKER
46 #include "wx/stackwalk.h"
47 #endif // wxUSE_STACKWALKER
48 #endif // __WXDEBUG__
49
50 #if defined(__WXMSW__)
51 #include "wx/msw/private.h" // includes windows.h for LOGFONT
52 #endif
53
54 #if wxUSE_FONTMAP
55 #include "wx/fontmap.h"
56 #endif // wxUSE_FONTMAP
57
58 // DLL options compatibility check:
59 #include "wx/build.h"
60 WX_CHECK_BUILD_OPTIONS("wxCore")
61
62 WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
63
64 // ============================================================================
65 // wxAppBase implementation
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // initialization
70 // ----------------------------------------------------------------------------
71
72 wxAppBase::wxAppBase()
73 {
74 m_topWindow = (wxWindow *)NULL;
75
76 m_useBestVisual = false;
77 m_forceTrueColour = false;
78
79 m_isActive = true;
80
81 // We don't want to exit the app if the user code shows a dialog from its
82 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
83 // to Yes initially as this dialog would be the last top level window.
84 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
85 // when we enter our OnRun() because we do want the default behaviour from
86 // then on. But this would be a problem if the user code calls
87 // SetExitOnFrameDelete(false) from OnInit().
88 //
89 // So we use the special "Later" value which is such that
90 // GetExitOnFrameDelete() returns false for it but which we know we can
91 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
92 // call) overwrite in OnRun()
93 m_exitOnFrameDelete = Later;
94 }
95
96 bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
97 {
98 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
99 return false;
100
101 wxInitializeStockLists();
102
103 wxBitmap::InitStandardHandlers();
104
105 return true;
106 }
107
108 // ----------------------------------------------------------------------------
109 // cleanup
110 // ----------------------------------------------------------------------------
111
112 wxAppBase::~wxAppBase()
113 {
114 // this destructor is required for Darwin
115 }
116
117 void wxAppBase::CleanUp()
118 {
119 // clean up all the pending objects
120 DeletePendingObjects();
121
122 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
123 // when destroyed, so iterate until none are left)
124 while ( !wxTopLevelWindows.empty() )
125 {
126 // do not use Destroy() here as it only puts the TLW in pending list
127 // but we want to delete them now
128 delete wxTopLevelWindows.GetFirst()->GetData();
129 }
130
131 // undo everything we did in Initialize() above
132 wxBitmap::CleanUpHandlers();
133
134 wxStockGDI::DeleteAll();
135
136 wxDeleteStockLists();
137
138 delete wxTheColourDatabase;
139 wxTheColourDatabase = NULL;
140
141 #if wxUSE_THREADS
142 #if wxUSE_VALIDATORS
143 // If we don't do the following, we get an apparent memory leak.
144 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
145 #endif // wxUSE_VALIDATORS
146 #endif // wxUSE_THREADS
147
148 wxAppConsole::CleanUp();
149 }
150
151 // ----------------------------------------------------------------------------
152 // various accessors
153 // ----------------------------------------------------------------------------
154
155 wxWindow* wxAppBase::GetTopWindow() const
156 {
157 wxWindow* window = m_topWindow;
158 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
159 window = wxTopLevelWindows.GetFirst()->GetData();
160 return window;
161 }
162
163 wxVideoMode wxAppBase::GetDisplayMode() const
164 {
165 return wxVideoMode();
166 }
167
168 wxLayoutDirection wxAppBase::GetLayoutDirection() const
169 {
170 #if wxUSE_INTL
171 const wxLocale *const locale = wxGetLocale();
172 if ( locale )
173 {
174 const wxLanguageInfo *const
175 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
176
177 if ( info )
178 return info->LayoutDirection;
179 }
180 #endif // wxUSE_INTL
181
182 // we don't know
183 return wxLayout_Default;
184 }
185
186 #if wxUSE_CMDLINE_PARSER
187
188 // ----------------------------------------------------------------------------
189 // GUI-specific command line options handling
190 // ----------------------------------------------------------------------------
191
192 #define OPTION_THEME "theme"
193 #define OPTION_MODE "mode"
194
195 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
196 {
197 // first add the standard non GUI options
198 wxAppConsole::OnInitCmdLine(parser);
199
200 // the standard command line options
201 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
202 {
203 #ifdef __WXUNIVERSAL__
204 {
205 wxCMD_LINE_OPTION,
206 NULL,
207 OPTION_THEME,
208 gettext_noop("specify the theme to use"),
209 wxCMD_LINE_VAL_STRING,
210 0x0
211 },
212 #endif // __WXUNIVERSAL__
213
214 #if defined(__WXMGL__)
215 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
216 // should provide this option. That's why it is in common/appcmn.cpp
217 // and not mgl/app.cpp
218 {
219 wxCMD_LINE_OPTION,
220 NULL,
221 OPTION_MODE,
222 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
223 wxCMD_LINE_VAL_STRING,
224 0x0
225 },
226 #endif // __WXMGL__
227
228 // terminator
229 wxCMD_LINE_DESC_END
230 };
231
232 parser.SetDesc(cmdLineGUIDesc);
233 }
234
235 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
236 {
237 #ifdef __WXUNIVERSAL__
238 wxString themeName;
239 if ( parser.Found(OPTION_THEME, &themeName) )
240 {
241 wxTheme *theme = wxTheme::Create(themeName);
242 if ( !theme )
243 {
244 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
245 return false;
246 }
247
248 // Delete the defaultly created theme and set the new theme.
249 delete wxTheme::Get();
250 wxTheme::Set(theme);
251 }
252 #endif // __WXUNIVERSAL__
253
254 #if defined(__WXMGL__)
255 wxString modeDesc;
256 if ( parser.Found(OPTION_MODE, &modeDesc) )
257 {
258 unsigned w, h, bpp;
259 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
260 {
261 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
262 return false;
263 }
264
265 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
266 return false;
267 }
268 #endif // __WXMGL__
269
270 return wxAppConsole::OnCmdLineParsed(parser);
271 }
272
273 #endif // wxUSE_CMDLINE_PARSER
274
275 // ----------------------------------------------------------------------------
276 // OnXXX() hooks
277 // ----------------------------------------------------------------------------
278
279 bool wxAppBase::OnInitGui()
280 {
281 #ifdef __WXUNIVERSAL__
282 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
283 return false;
284 #endif // __WXUNIVERSAL__
285
286 return true;
287 }
288
289 int wxAppBase::OnRun()
290 {
291 // see the comment in ctor: if the initial value hasn't been changed, use
292 // the default Yes from now on
293 if ( m_exitOnFrameDelete == Later )
294 {
295 m_exitOnFrameDelete = Yes;
296 }
297 //else: it has been changed, assume the user knows what he is doing
298
299 return wxAppConsole::OnRun();
300 }
301
302 int wxAppBase::OnExit()
303 {
304 #ifdef __WXUNIVERSAL__
305 delete wxTheme::Set(NULL);
306 #endif // __WXUNIVERSAL__
307
308 return wxAppConsole::OnExit();
309 }
310
311 wxAppTraits *wxAppBase::CreateTraits()
312 {
313 return new wxGUIAppTraits;
314 }
315
316 // ----------------------------------------------------------------------------
317 // misc
318 // ----------------------------------------------------------------------------
319
320 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
321 {
322 if ( active == m_isActive )
323 return;
324
325 m_isActive = active;
326
327 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
328 event.SetEventObject(this);
329
330 (void)ProcessEvent(event);
331 }
332
333 // ----------------------------------------------------------------------------
334 // idle handling
335 // ----------------------------------------------------------------------------
336
337 void wxAppBase::DeletePendingObjects()
338 {
339 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
340 while (node)
341 {
342 wxObject *obj = node->GetData();
343
344 // remove it from the list first so that if we get back here somehow
345 // during the object deletion (e.g. wxYield called from its dtor) we
346 // wouldn't try to delete it the second time
347 if ( wxPendingDelete.Member(obj) )
348 wxPendingDelete.Erase(node);
349
350 delete obj;
351
352 // Deleting one object may have deleted other pending
353 // objects, so start from beginning of list again.
354 node = wxPendingDelete.GetFirst();
355 }
356 }
357
358 // Returns true if more time is needed.
359 bool wxAppBase::ProcessIdle()
360 {
361 // process pending wx events before sending idle events
362 ProcessPendingEvents();
363
364 wxIdleEvent event;
365 bool needMore = false;
366 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
367 while (node)
368 {
369 wxWindow* win = node->GetData();
370 if (SendIdleEvents(win, event))
371 needMore = true;
372 node = node->GetNext();
373 }
374
375 if (wxAppConsole::ProcessIdle())
376 needMore = true;
377
378 // 'Garbage' collection of windows deleted with Close().
379 DeletePendingObjects();
380
381 #if wxUSE_LOG
382 // flush the logged messages if any
383 wxLog::FlushActive();
384 #endif
385
386 wxUpdateUIEvent::ResetUpdateTime();
387
388 return needMore;
389 }
390
391 // Send idle event to window and all subwindows
392 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
393 {
394 bool needMore = false;
395
396 win->OnInternalIdle();
397
398 // should we send idle event to this window?
399 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
400 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
401 {
402 event.SetEventObject(win);
403 win->GetEventHandler()->ProcessEvent(event);
404
405 if (event.MoreRequested())
406 needMore = true;
407 }
408 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
409 while ( node )
410 {
411 wxWindow *child = node->GetData();
412 if (SendIdleEvents(child, event))
413 needMore = true;
414
415 node = node->GetNext();
416 }
417
418 return needMore;
419 }
420
421 // ----------------------------------------------------------------------------
422 // wxGUIAppTraitsBase
423 // ----------------------------------------------------------------------------
424
425 #if wxUSE_LOG
426
427 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
428 {
429 // DE: One day I'll remove this but right now the generic dialog used for this
430 // just doesn't work right at all on wxCocoa.
431 #if wxUSE_LOGGUI && !defined(__WXCOCOA__)
432 return new wxLogGui;
433 #else
434 // we must have something!
435 return new wxLogStderr;
436 #endif
437 }
438
439 #endif // wxUSE_LOG
440
441 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
442 {
443 // The standard way of printing help on command line arguments (app --help)
444 // is (according to common practice):
445 // - console apps: to stderr (on any platform)
446 // - GUI apps: stderr on Unix platforms (!)
447 // message box under Windows and others
448 #ifdef __UNIX__
449 return new wxMessageOutputStderr;
450 #else // !__UNIX__
451 // wxMessageOutputMessageBox doesn't work under Motif
452 #ifdef __WXMOTIF__
453 return new wxMessageOutputLog;
454 #elif wxUSE_MSGDLG
455 return new wxMessageOutputMessageBox;
456 #else
457 return new wxMessageOutputStderr;
458 #endif
459 #endif // __UNIX__/!__UNIX__
460 }
461
462 #if wxUSE_FONTMAP
463
464 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
465 {
466 return new wxFontMapper;
467 }
468
469 #endif // wxUSE_FONTMAP
470
471 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
472 {
473 // use the default native renderer by default
474 return NULL;
475 }
476
477 #ifdef __WXDEBUG__
478
479 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
480 {
481 #if defined(__WXMSW__) || !wxUSE_MSGDLG
482 // under MSW we prefer to use the base class version using ::MessageBox()
483 // even if wxMessageBox() is available because it has less chances to
484 // double fault our app than our wxMessageBox()
485 return wxAppTraitsBase::ShowAssertDialog(msg);
486 #else // wxUSE_MSGDLG
487 wxString msgDlg = msg;
488
489 #if wxUSE_STACKWALKER
490 // on Unix stack frame generation may take some time, depending on the
491 // size of the executable mainly... warn the user that we are working
492 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
493 fflush(stderr);
494
495 const wxString stackTrace = GetAssertStackTrace();
496 if ( !stackTrace.empty() )
497 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
498 #endif // wxUSE_STACKWALKER
499
500 // this message is intentionally not translated -- it is for
501 // developpers only
502 msgDlg += wxT("\nDo you want to stop the program?\n")
503 wxT("You can also choose [Cancel] to suppress ")
504 wxT("further warnings.");
505
506 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
507 wxYES_NO | wxCANCEL | wxICON_STOP ) )
508 {
509 case wxYES:
510 wxTrap();
511 break;
512
513 case wxCANCEL:
514 // no more asserts
515 return true;
516
517 //case wxNO: nothing to do
518 }
519
520 return false;
521 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
522 }
523
524 #endif // __WXDEBUG__
525
526 bool wxGUIAppTraitsBase::HasStderr()
527 {
528 // we consider that under Unix stderr always goes somewhere, even if the
529 // user doesn't always see it under GUI desktops
530 #ifdef __UNIX__
531 return true;
532 #else
533 return false;
534 #endif
535 }
536
537 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
538 {
539 if ( !wxPendingDelete.Member(object) )
540 wxPendingDelete.Append(object);
541 }
542
543 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
544 {
545 wxPendingDelete.DeleteObject(object);
546 }
547
548 #if wxUSE_SOCKETS
549
550 #if defined(__WINDOWS__)
551 #include "wx/msw/gsockmsw.h"
552 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
553 #include "wx/unix/gsockunx.h"
554 #elif defined(__WXMAC__)
555 #include <MacHeaders.c>
556 #define OTUNIXERRORS 1
557 #include <OpenTransport.h>
558 #include <OpenTransportProviders.h>
559 #include <OpenTptInternet.h>
560
561 #include "wx/mac/gsockmac.h"
562 #else
563 #error "Must include correct GSocket header here"
564 #endif
565
566 GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
567 {
568 #if defined(__WXMAC__) && !defined(__DARWIN__)
569 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
570 // so it doesn't need this table at all
571 return NULL;
572 #else // !__WXMAC__ || __DARWIN__
573 static GSocketGUIFunctionsTableConcrete table;
574 return &table;
575 #endif // !__WXMAC__ || __DARWIN__
576 }
577
578 #endif