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