]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
added explanatory comment
[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 delete wxPendingEvents;
142 wxPendingEvents = NULL;
143
144 #if wxUSE_THREADS
145 #if wxUSE_VALIDATORS
146 // If we don't do the following, we get an apparent memory leak.
147 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
148 #endif // wxUSE_VALIDATORS
149 #endif // wxUSE_THREADS
150 }
151
152 // ----------------------------------------------------------------------------
153 // various accessors
154 // ----------------------------------------------------------------------------
155
156 wxWindow* wxAppBase::GetTopWindow() const
157 {
158 wxWindow* window = m_topWindow;
159 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
160 window = wxTopLevelWindows.GetFirst()->GetData();
161 return window;
162 }
163
164 wxVideoMode wxAppBase::GetDisplayMode() const
165 {
166 return wxVideoMode();
167 }
168
169 wxLayoutDirection wxAppBase::GetLayoutDirection() const
170 {
171 #if wxUSE_INTL
172 const wxLocale *const locale = wxGetLocale();
173 if ( locale )
174 {
175 const wxLanguageInfo *const
176 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
177
178 if ( info )
179 return info->LayoutDirection;
180 }
181 #endif // wxUSE_INTL
182
183 // we don't know
184 return wxLayout_Default;
185 }
186
187 #if wxUSE_CMDLINE_PARSER
188
189 // ----------------------------------------------------------------------------
190 // GUI-specific command line options handling
191 // ----------------------------------------------------------------------------
192
193 #define OPTION_THEME _T("theme")
194 #define OPTION_MODE _T("mode")
195
196 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
197 {
198 // first add the standard non GUI options
199 wxAppConsole::OnInitCmdLine(parser);
200
201 // the standard command line options
202 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
203 {
204 #ifdef __WXUNIVERSAL__
205 {
206 wxCMD_LINE_OPTION,
207 wxEmptyString,
208 OPTION_THEME,
209 gettext_noop("specify the theme to use"),
210 wxCMD_LINE_VAL_STRING,
211 0x0
212 },
213 #endif // __WXUNIVERSAL__
214
215 #if defined(__WXMGL__)
216 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
217 // should provide this option. That's why it is in common/appcmn.cpp
218 // and not mgl/app.cpp
219 {
220 wxCMD_LINE_OPTION,
221 wxEmptyString,
222 OPTION_MODE,
223 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
224 wxCMD_LINE_VAL_STRING,
225 0x0
226 },
227 #endif // __WXMGL__
228
229 // terminator
230 {
231 wxCMD_LINE_NONE,
232 wxEmptyString,
233 wxEmptyString,
234 wxEmptyString,
235 wxCMD_LINE_VAL_NONE,
236 0x0
237 }
238 };
239
240 parser.SetDesc(cmdLineGUIDesc);
241 }
242
243 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
244 {
245 #ifdef __WXUNIVERSAL__
246 wxString themeName;
247 if ( parser.Found(OPTION_THEME, &themeName) )
248 {
249 wxTheme *theme = wxTheme::Create(themeName);
250 if ( !theme )
251 {
252 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
253 return false;
254 }
255
256 // Delete the defaultly created theme and set the new theme.
257 delete wxTheme::Get();
258 wxTheme::Set(theme);
259 }
260 #endif // __WXUNIVERSAL__
261
262 #if defined(__WXMGL__)
263 wxString modeDesc;
264 if ( parser.Found(OPTION_MODE, &modeDesc) )
265 {
266 unsigned w, h, bpp;
267 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
268 {
269 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
270 return false;
271 }
272
273 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
274 return false;
275 }
276 #endif // __WXMGL__
277
278 return wxAppConsole::OnCmdLineParsed(parser);
279 }
280
281 #endif // wxUSE_CMDLINE_PARSER
282
283 // ----------------------------------------------------------------------------
284 // OnXXX() hooks
285 // ----------------------------------------------------------------------------
286
287 bool wxAppBase::OnInitGui()
288 {
289 #ifdef __WXUNIVERSAL__
290 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
291 return false;
292 #endif // __WXUNIVERSAL__
293
294 return true;
295 }
296
297 int wxAppBase::OnRun()
298 {
299 // see the comment in ctor: if the initial value hasn't been changed, use
300 // the default Yes from now on
301 if ( m_exitOnFrameDelete == Later )
302 {
303 m_exitOnFrameDelete = Yes;
304 }
305 //else: it has been changed, assume the user knows what he is doing
306
307 return wxAppConsole::OnRun();
308 }
309
310 int wxAppBase::OnExit()
311 {
312 #ifdef __WXUNIVERSAL__
313 delete wxTheme::Set(NULL);
314 #endif // __WXUNIVERSAL__
315
316 return wxAppConsole::OnExit();
317 }
318
319 wxAppTraits *wxAppBase::CreateTraits()
320 {
321 return new wxGUIAppTraits;
322 }
323
324 // ----------------------------------------------------------------------------
325 // misc
326 // ----------------------------------------------------------------------------
327
328 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
329 {
330 if ( active == m_isActive )
331 return;
332
333 m_isActive = active;
334
335 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
336 event.SetEventObject(this);
337
338 (void)ProcessEvent(event);
339 }
340
341 // ----------------------------------------------------------------------------
342 // idle handling
343 // ----------------------------------------------------------------------------
344
345 void wxAppBase::DeletePendingObjects()
346 {
347 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
348 while (node)
349 {
350 wxObject *obj = node->GetData();
351
352 // remove it from the list first so that if we get back here somehow
353 // during the object deletion (e.g. wxYield called from its dtor) we
354 // wouldn't try to delete it the second time
355 if ( wxPendingDelete.Member(obj) )
356 wxPendingDelete.Erase(node);
357
358 delete obj;
359
360 // Deleting one object may have deleted other pending
361 // objects, so start from beginning of list again.
362 node = wxPendingDelete.GetFirst();
363 }
364 }
365
366 // Returns true if more time is needed.
367 bool wxAppBase::ProcessIdle()
368 {
369 wxIdleEvent event;
370 bool needMore = false;
371 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
372 while (node)
373 {
374 wxWindow* win = node->GetData();
375 if (SendIdleEvents(win, event))
376 needMore = true;
377 node = node->GetNext();
378 }
379
380 needMore = wxAppConsole::ProcessIdle();
381
382 wxUpdateUIEvent::ResetUpdateTime();
383
384 return needMore;
385 }
386
387 // Send idle event to window and all subwindows
388 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
389 {
390 bool needMore = false;
391
392 win->OnInternalIdle();
393
394 // should we send idle event to this window?
395 if ( wxIdleEvent::GetMode() == wxIDLE_PROCESS_ALL ||
396 win->HasExtraStyle(wxWS_EX_PROCESS_IDLE) )
397 {
398 event.SetEventObject(win);
399 win->GetEventHandler()->ProcessEvent(event);
400
401 if (event.MoreRequested())
402 needMore = true;
403 }
404 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
405 while ( node )
406 {
407 wxWindow *child = node->GetData();
408 if (SendIdleEvents(child, event))
409 needMore = true;
410
411 node = node->GetNext();
412 }
413
414 return needMore;
415 }
416
417 void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
418 {
419 // If there are pending events, we must process them: pending events
420 // are either events to the threads other than main or events posted
421 // with wxPostEvent() functions
422 // GRG: I have moved this here so that all pending events are processed
423 // before starting to delete any objects. This behaves better (in
424 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
425 // behaviour. Changed Feb/2000 before 2.1.14
426 ProcessPendingEvents();
427
428 // 'Garbage' collection of windows deleted with Close().
429 DeletePendingObjects();
430
431 #if wxUSE_LOG
432 // flush the logged messages if any
433 wxLog::FlushActive();
434 #endif // wxUSE_LOG
435
436 }
437
438 // ----------------------------------------------------------------------------
439 // wxGUIAppTraitsBase
440 // ----------------------------------------------------------------------------
441
442 #if wxUSE_LOG
443
444 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
445 {
446 #if wxUSE_LOGGUI
447 return new wxLogGui;
448 #else
449 // we must have something!
450 return new wxLogStderr;
451 #endif
452 }
453
454 #endif // wxUSE_LOG
455
456 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
457 {
458 // The standard way of printing help on command line arguments (app --help)
459 // is (according to common practice):
460 // - console apps: to stderr (on any platform)
461 // - GUI apps: stderr on Unix platforms (!)
462 // message box under Windows and others
463 #ifdef __UNIX__
464 return new wxMessageOutputStderr;
465 #else // !__UNIX__
466 // wxMessageOutputMessageBox doesn't work under Motif
467 #ifdef __WXMOTIF__
468 return new wxMessageOutputLog;
469 #else
470 return new wxMessageOutputMessageBox;
471 #endif
472 #endif // __UNIX__/!__UNIX__
473 }
474
475 #if wxUSE_FONTMAP
476
477 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
478 {
479 return new wxFontMapper;
480 }
481
482 #endif // wxUSE_FONTMAP
483
484 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
485 {
486 // use the default native renderer by default
487 return NULL;
488 }
489
490 #ifdef __WXDEBUG__
491
492 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
493 {
494 #if defined(__WXMSW__) || !wxUSE_MSGDLG
495 // under MSW we prefer to use the base class version using ::MessageBox()
496 // even if wxMessageBox() is available because it has less chances to
497 // double fault our app than our wxMessageBox()
498 return wxAppTraitsBase::ShowAssertDialog(msg);
499 #else // wxUSE_MSGDLG
500 wxString msgDlg = msg;
501
502 #if wxUSE_STACKWALKER
503 // on Unix stack frame generation may take some time, depending on the
504 // size of the executable mainly... warn the user that we are working
505 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
506 fflush(stderr);
507
508 const wxString stackTrace = GetAssertStackTrace();
509 if ( !stackTrace.empty() )
510 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
511 #endif // wxUSE_STACKWALKER
512
513 // this message is intentionally not translated -- it is for
514 // developpers only
515 msgDlg += wxT("\nDo you want to stop the program?\n")
516 wxT("You can also choose [Cancel] to suppress ")
517 wxT("further warnings.");
518
519 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
520 wxYES_NO | wxCANCEL | wxICON_STOP ) )
521 {
522 case wxYES:
523 wxTrap();
524 break;
525
526 case wxCANCEL:
527 // no more asserts
528 return true;
529
530 //case wxNO: nothing to do
531 }
532
533 return false;
534 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
535 }
536
537 #endif // __WXDEBUG__
538
539 bool wxGUIAppTraitsBase::HasStderr()
540 {
541 // we consider that under Unix stderr always goes somewhere, even if the
542 // user doesn't always see it under GUI desktops
543 #ifdef __UNIX__
544 return true;
545 #else
546 return false;
547 #endif
548 }
549
550 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
551 {
552 if ( !wxPendingDelete.Member(object) )
553 wxPendingDelete.Append(object);
554 }
555
556 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
557 {
558 wxPendingDelete.DeleteObject(object);
559 }
560
561 #if wxUSE_SOCKETS
562
563 #if defined(__WINDOWS__)
564 #include "wx/msw/gsockmsw.h"
565 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
566 #include "wx/unix/gsockunx.h"
567 #elif defined(__WXMAC__)
568 #include <MacHeaders.c>
569 #define OTUNIXERRORS 1
570 #include <OpenTransport.h>
571 #include <OpenTransportProviders.h>
572 #include <OpenTptInternet.h>
573
574 #include "wx/mac/gsockmac.h"
575 #else
576 #error "Must include correct GSocket header here"
577 #endif
578
579 GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
580 {
581 #if defined(__WXMAC__) && !defined(__DARWIN__)
582 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
583 // so it doesn't need this table at all
584 return NULL;
585 #else // !__WXMAC__ || __DARWIN__
586 static GSocketGUIFunctionsTableConcrete table;
587 return &table;
588 #endif // !__WXMAC__ || __DARWIN__
589 }
590
591 #endif