]> git.saurik.com Git - wxWidgets.git/blame - src/common/appcmn.cpp
Improved size handling.
[wxWidgets.git] / src / common / appcmn.cpp
CommitLineData
72cdf4c9 1/////////////////////////////////////////////////////////////////////////////
127eab18 2// Name: src/common/appcmn.cpp
e2478fde 3// Purpose: wxAppConsole and wxAppBase methods common to all platforms
72cdf4c9
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 18.10.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
65571936 9// Licence: wxWindows licence
72cdf4c9
VZ
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
72cdf4c9
VZ
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"
b2e972ec 29 #include "wx/bitmap.h"
bf188f1a 30 #include "wx/intl.h"
e87271f3 31 #include "wx/list.h"
46446cc2 32 #include "wx/log.h"
e2478fde 33 #include "wx/msgdlg.h"
b3dfbbc9
MB
34 #include "wx/bitmap.h"
35 #include "wx/confbase.h"
72cdf4c9
VZ
36#endif
37
e2478fde 38#include "wx/apptrait.h"
b913d3ed 39#include "wx/cmdline.h"
1bf77ee5 40#include "wx/evtloop.h"
e2478fde 41#include "wx/msgout.h"
72cdf4c9 42#include "wx/thread.h"
bebc39e3 43#include "wx/utils.h"
1bf77ee5 44#include "wx/ptr_scpd.h"
a5f1fd3e 45
82ef81ed
WS
46#if defined(__WXMSW__)
47 #include "wx/msw/private.h" // includes windows.h for LOGFONT
1c193821
JS
48#endif
49
50#if wxUSE_FONTMAP
51 #include "wx/fontmap.h"
52#endif // wxUSE_FONTMAP
53
34fdf762
VS
54// DLL options compatibility check:
55#include "wx/build.h"
56WX_CHECK_BUILD_OPTIONS("wxCore")
57
1bf77ee5
VZ
58
59// ----------------------------------------------------------------------------
60// wxEventLoopPtr
61// ----------------------------------------------------------------------------
62
63// this defines wxEventLoopPtr
259c43f6 64wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop)
1bf77ee5 65
e2478fde
VZ
66// ============================================================================
67// wxAppBase implementation
68// ============================================================================
d54598dd 69
bf188f1a 70// ----------------------------------------------------------------------------
94826170 71// initialization
bf188f1a
VZ
72// ----------------------------------------------------------------------------
73
090a6d7a 74wxAppBase::wxAppBase()
697c5f51 75{
1e6feb95 76 m_topWindow = (wxWindow *)NULL;
4629016d
WS
77 m_useBestVisual = false;
78 m_isActive = true;
1cbee0b4 79
fb761cd5 80#if wxUSE_EVTLOOP_IN_APP
1bf77ee5 81 m_mainLoop = NULL;
fb761cd5 82#endif // wxUSE_EVTLOOP_IN_APP
1bf77ee5 83
1cbee0b4
VZ
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
4629016d 90 // SetExitOnFrameDelete(false) from OnInit().
1cbee0b4
VZ
91 //
92 // So we use the special "Later" value which is such that
4629016d 93 // GetExitOnFrameDelete() returns false for it but which we know we can
1cbee0b4
VZ
94 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
95 // call) overwrite in OnRun()
96 m_exitOnFrameDelete = Later;
1e6feb95
VZ
97}
98
a54930e0 99bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
94826170 100{
a54930e0 101 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
94826170
VZ
102 return false;
103
94826170
VZ
104#if wxUSE_THREADS
105 wxPendingEventsLocker = new wxCriticalSection;
106#endif
107
94826170
VZ
108 wxInitializeStockLists();
109 wxInitializeStockObjects();
110
111 wxBitmap::InitStandardHandlers();
112
113 return true;
114}
115
116// ----------------------------------------------------------------------------
117// cleanup
118// ----------------------------------------------------------------------------
119
799ea011
GD
120wxAppBase::~wxAppBase()
121{
122 // this destructor is required for Darwin
123}
124
94826170
VZ
125void wxAppBase::CleanUp()
126{
07460370 127 // clean up all the pending objects
94826170
VZ
128 DeletePendingObjects();
129
07460370
VZ
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 }
4055ed82 138
07460370 139 // undo everything we did in Initialize() above
94826170
VZ
140 wxBitmap::CleanUpHandlers();
141
142 wxDeleteStockObjects();
143
144 wxDeleteStockLists();
145
146 delete wxTheColourDatabase;
147 wxTheColourDatabase = NULL;
148
94826170
VZ
149 delete wxPendingEvents;
150 wxPendingEvents = NULL;
151
c8b1e804 152#if wxUSE_THREADS
94826170
VZ
153 delete wxPendingEventsLocker;
154 wxPendingEventsLocker = NULL;
155
b913d3ed
VZ
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
94826170
VZ
160#endif // wxUSE_THREADS
161}
162
b913d3ed
VZ
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
172void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
173{
9c13e5ef
VZ
174 // first add the standard non GUI options
175 wxAppConsole::OnInitCmdLine(parser);
176
b913d3ed
VZ
177 // the standard command line options
178 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
179 {
180#ifdef __WXUNIVERSAL__
181 {
182 wxCMD_LINE_OPTION,
b494c48b 183 wxEmptyString,
b913d3ed
VZ
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,
b494c48b 197 wxEmptyString,
b913d3ed
VZ
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,
b494c48b
WS
208 wxEmptyString,
209 wxEmptyString,
210 wxEmptyString,
b913d3ed
VZ
211 wxCMD_LINE_VAL_NONE,
212 0x0
213 }
214 };
215
216 parser.SetDesc(cmdLineGUIDesc);
217}
218
219bool 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());
4629016d 229 return false;
b913d3ed
VZ
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());
4629016d 246 return false;
b913d3ed
VZ
247 }
248
1c53456f 249 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
4629016d 250 return false;
b913d3ed
VZ
251 }
252#endif // __WXMGL__
253
254 return wxAppConsole::OnCmdLineParsed(parser);
255}
256
257#endif // wxUSE_CMDLINE_PARSER
258
1bf77ee5
VZ
259// ----------------------------------------------------------------------------
260// main event loop implementation
261// ----------------------------------------------------------------------------
262
263int wxAppBase::MainLoop()
264{
fb761cd5 265#if wxUSE_EVTLOOP_IN_APP
efbfda9d 266 wxEventLoopTiedPtr mainLoop(&m_mainLoop, new wxEventLoop);
1bf77ee5
VZ
267
268 return m_mainLoop->Run();
fb761cd5
VZ
269#else // !wxUSE_EVTLOOP_IN_APP
270 return 0;
271#endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
272}
273
274void wxAppBase::ExitMainLoop()
275{
fb761cd5 276#if wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
277 // we should exit from the main event loop, not just any currently active
278 // (e.g. modal dialog) event loop
dd435a79 279 if ( m_mainLoop && m_mainLoop->IsRunning() )
1bf77ee5
VZ
280 {
281 m_mainLoop->Exit(0);
282 }
fb761cd5 283#endif // wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
284}
285
286bool wxAppBase::Pending()
287{
fb761cd5 288#if wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
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();
fb761cd5
VZ
295#else // wxUSE_EVTLOOP_IN_APP
296 return false;
297#endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
298}
299
300bool wxAppBase::Dispatch()
301{
fb761cd5 302#if wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
303 // see comment in Pending()
304 wxEventLoop * const loop = wxEventLoop::GetActive();
305
d1fc6f06 306 return loop && loop->Dispatch();
fb761cd5
VZ
307#else // wxUSE_EVTLOOP_IN_APP
308 return true;
309#endif // wxUSE_EVTLOOP_IN_APP/!wxUSE_EVTLOOP_IN_APP
1bf77ee5
VZ
310}
311
94826170
VZ
312// ----------------------------------------------------------------------------
313// OnXXX() hooks
314// ----------------------------------------------------------------------------
315
1e6feb95
VZ
316bool wxAppBase::OnInitGui()
317{
318#ifdef __WXUNIVERSAL__
bf188f1a 319 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
4629016d 320 return false;
1e6feb95
VZ
321#endif // __WXUNIVERSAL__
322
4629016d 323 return true;
1e6feb95 324}
1e6feb95 325
1cbee0b4
VZ
326int 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
b913d3ed
VZ
339int wxAppBase::OnExit()
340{
341#ifdef __WXUNIVERSAL__
342 delete wxTheme::Set(NULL);
343#endif // __WXUNIVERSAL__
344
345 return wxAppConsole::OnExit();
346}
347
e2478fde 348void wxAppBase::Exit()
1e6feb95 349{
e2478fde 350 ExitMainLoop();
1e6feb95
VZ
351}
352
e2478fde 353wxAppTraits *wxAppBase::CreateTraits()
a69be60b 354{
7843d11b 355 return new wxGUIAppTraits;
72cdf4c9
VZ
356}
357
1e6feb95
VZ
358// ----------------------------------------------------------------------------
359// misc
360// ----------------------------------------------------------------------------
361
6e169cf3 362void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
7beba2fc 363{
66dfed9b
VZ
364 if ( active == m_isActive )
365 return;
366
1e6feb95 367 m_isActive = active;
66dfed9b
VZ
368
369 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
370 event.SetEventObject(this);
371
372 (void)ProcessEvent(event);
7beba2fc 373}
1e6feb95 374
94826170
VZ
375void wxAppBase::DeletePendingObjects()
376{
222ed1d6 377 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
94826170
VZ
378 while (node)
379 {
380 wxObject *obj = node->GetData();
381
382 delete obj;
383
384 if (wxPendingDelete.Member(obj))
222ed1d6 385 wxPendingDelete.Erase(node);
94826170
VZ
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
4629016d 393// Returns true if more time is needed.
e39af974
JS
394bool wxAppBase::ProcessIdle()
395{
5109ae5d 396 wxIdleEvent event;
4629016d 397 bool needMore = false;
222ed1d6 398 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
e39af974
JS
399 while (node)
400 {
401 wxWindow* win = node->GetData();
5109ae5d 402 if (SendIdleEvents(win, event))
4629016d 403 needMore = true;
e39af974
JS
404 node = node->GetNext();
405 }
406
e39af974 407 event.SetEventObject(this);
5109ae5d
JS
408 (void) ProcessEvent(event);
409 if (event.MoreRequested())
4629016d 410 needMore = true;
e39af974
JS
411
412 wxUpdateUIEvent::ResetUpdateTime();
4629016d 413
5109ae5d 414 return needMore;
e39af974
JS
415}
416
e39af974 417// Send idle event to window and all subwindows
5109ae5d 418bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
e39af974 419{
4629016d 420 bool needMore = false;
42d11c8e 421
5109ae5d 422 win->OnInternalIdle();
42d11c8e 423
e39af974
JS
424 if (wxIdleEvent::CanSend(win))
425 {
e39af974
JS
426 event.SetEventObject(win);
427 win->GetEventHandler()->ProcessEvent(event);
428
5109ae5d 429 if (event.MoreRequested())
4629016d 430 needMore = true;
e39af974 431 }
222ed1d6 432 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
e39af974
JS
433 while ( node )
434 {
529b7f71
JS
435 wxWindow *child = node->GetData();
436 if (SendIdleEvents(child, event))
4629016d 437 needMore = true;
e39af974
JS
438
439 node = node->GetNext();
440 }
441
442 return needMore;
443}
444
fc7a2a60 445void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
955a9197
JS
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}
e39af974 465
bf188f1a 466// ----------------------------------------------------------------------------
e2478fde 467// wxGUIAppTraitsBase
bf188f1a
VZ
468// ----------------------------------------------------------------------------
469
bf188f1a 470#if wxUSE_LOG
bf188f1a 471
e2478fde
VZ
472wxLog *wxGUIAppTraitsBase::CreateLogTarget()
473{
461dae94 474#if wxUSE_LOGGUI
e2478fde 475 return new wxLogGui;
461dae94 476#else
fa6416df 477 // we must have something!
461dae94
VZ
478 return new wxLogStderr;
479#endif
bf188f1a
VZ
480}
481
bf188f1a
VZ
482#endif // wxUSE_LOG
483
e2478fde 484wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
bf188f1a 485{
e2478fde
VZ
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__
bf188f1a
VZ
501}
502
e2478fde 503#if wxUSE_FONTMAP
bf188f1a 504
e2478fde
VZ
505wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
506{
507 return new wxFontMapper;
bf188f1a
VZ
508}
509
e2478fde 510#endif // wxUSE_FONTMAP
bf188f1a 511
f0244295
VZ
512wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
513{
514 // use the default native renderer by default
515 return NULL;
516}
517
090a6d7a 518#ifdef __WXDEBUG__
e6e6fcc9 519
e2478fde
VZ
520bool 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
77ffb593 535 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
e2478fde
VZ
536 wxYES_NO | wxCANCEL | wxICON_STOP ) )
537 {
538 case wxYES:
539 wxTrap();
540 break;
090a6d7a 541
e2478fde
VZ
542 case wxCANCEL:
543 // no more asserts
544 return true;
a5f1fd3e 545
e2478fde 546 //case wxNO: nothing to do
090a6d7a 547 }
090a6d7a 548
e2478fde
VZ
549 return false;
550#endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
a5f1fd3e
VZ
551}
552
e2478fde
VZ
553#endif // __WXDEBUG__
554
555bool wxGUIAppTraitsBase::HasStderr()
a5f1fd3e 556{
e2478fde
VZ
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;
a5f1fd3e 561#else
e2478fde 562 return false;
a5f1fd3e 563#endif
a5f1fd3e
VZ
564}
565
e2478fde 566void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
a5f1fd3e 567{
e2478fde
VZ
568 if ( !wxPendingDelete.Member(object) )
569 wxPendingDelete.Append(object);
a5f1fd3e
VZ
570}
571
e2478fde 572void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
a5f1fd3e 573{
e2478fde 574 wxPendingDelete.DeleteObject(object);
a5f1fd3e
VZ
575}
576
38bb138f
VS
577#if wxUSE_SOCKETS
578
ae3036a2 579#if defined(__WINDOWS__)
38bb138f 580 #include "wx/msw/gsockmsw.h"
ae3036a2
RN
581#elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
582 #include "wx/unix/gsockunx.h"
69aa21ac 583#elif defined(__WXMAC__)
127eab18
WS
584 #include <MacHeaders.c>
585 #define OTUNIXERRORS 1
586 #include <OpenTransport.h>
587 #include <OpenTransportProviders.h>
588 #include <OpenTptInternet.h>
69aa21ac 589
127eab18 590 #include "wx/mac/gsockmac.h"
38bb138f
VS
591#else
592 #error "Must include correct GSocket header here"
593#endif
594
595GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
596{
27a52ff9
GD
597#if defined(__WXMAC__) && !defined(__DARWIN__)
598 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
14a39351
VS
599 // so it doesn't need this table at all
600 return NULL;
27a52ff9 601#else // !__WXMAC__ || __DARWIN__
1d2ec8e5
DE
602 static GSocketGUIFunctionsTableConcrete table;
603 return &table;
1d2ec8e5 604#endif // !__WXMAC__ || __DARWIN__
38bb138f
VS
605}
606
607#endif