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