]> git.saurik.com Git - wxWidgets.git/blame - src/os2/app.cpp
Use system default font for all platforms
[wxWidgets.git] / src / os2 / app.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose: wxApp
d88de032 4// Author: David Webster
0e320a79 5// Modified by:
d88de032 6// Created: 10/13/99
0e320a79 7// RCS-ID: $Id$
d88de032
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
d88de032
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/frame.h"
17 #include "wx/app.h"
18 #include "wx/utils.h"
19 #include "wx/gdicmn.h"
20 #include "wx/pen.h"
21 #include "wx/brush.h"
22 #include "wx/cursor.h"
23 #include "wx/icon.h"
24 #include "wx/palette.h"
25 #include "wx/dc.h"
26 #include "wx/dialog.h"
27 #include "wx/msgdlg.h"
28 #include "wx/intl.h"
29 #include "wx/dynarray.h"
30# include "wx/wxchar.h"
31# include "wx/icon.h"
0e320a79
DW
32#endif
33
0e320a79
DW
34#include "wx/log.h"
35#include "wx/module.h"
d88de032
DW
36
37#include "wx/os2/private.h"
38
39#if wxUSE_THREADS
40 #include "wx/thread.h"
41
9ed0fac8
DW
42 // define the array of QMSG strutures
43 WX_DECLARE_OBJARRAY(QMSG, wxMsgArray);
d88de032
DW
44
45 #include "wx/arrimpl.cpp"
46
9ed0fac8 47 WX_DEFINE_OBJARRAY(wxMsgArray);
d88de032 48#endif // wxUSE_THREADS
0e320a79
DW
49
50#if wxUSE_WX_RESOURCES
d88de032 51 #include "wx/resource.h"
0e320a79
DW
52#endif
53
54#include <string.h>
d88de032
DW
55#include <ctype.h>
56
57// ---------------------------------------------------------------------------
58// global variables
59// ---------------------------------------------------------------------------
60
9ed0fac8
DW
61extern wxChar* wxBuffer;
62extern wxChar* wxOsVersion;
63extern wxList* wxWinHandleList;
64extern wxList WXDLLEXPORT wxPendingDelete;
9ed0fac8 65extern wxCursor* g_globalCursor;
0e320a79 66
9ed0fac8
DW
67HINSTANCE wxhInstance = 0;
68QMSG svCurrentMsg;
69wxApp* wxTheApp = NULL;
43543d98 70HAB vHabmain = NULL;
0e320a79 71
d88de032
DW
72// FIXME why not const? and not static?
73
74// NB: all "NoRedraw" classes must have the same names as the "normal" classes
13a4ea8d 75// with NR suffix - wxWindow::OS2Create() supposes this
9ed0fac8
DW
76wxChar wxFrameClassName[] = wxT("wxFrameClass");
77wxChar wxFrameClassNameNoRedraw[] = wxT("wxFrameClassNR");
78wxChar wxMDIFrameClassName[] = wxT("wxMDIFrameClass");
79wxChar wxMDIFrameClassNameNoRedraw[] = wxT("wxMDIFrameClassNR");
80wxChar wxMDIChildFrameClassName[] = wxT("wxMDIChildFrameClass");
d88de032 81wxChar wxMDIChildFrameClassNameNoRedraw[] = wxT("wxMDIChildFrameClassNR");
9ed0fac8
DW
82wxChar wxPanelClassName[] = wxT("wxPanelClass");
83wxChar wxCanvasClassName[] = wxT("wxCanvasClass");
d88de032 84
9ed0fac8
DW
85HICON wxSTD_FRAME_ICON = (HICON) NULL;
86HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL;
d88de032
DW
87HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL;
88
9ed0fac8
DW
89HICON wxDEFAULT_FRAME_ICON = (HICON) NULL;
90HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL;
91HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL;
d88de032
DW
92
93HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
94
f23208ca
DW
95MRESULT EXPENTRY wxWndProc( HWND
96 ,ULONG
97 ,MPARAM
98 ,MPARAM
99 );
d88de032
DW
100
101// ===========================================================================
102// implementation
103// ===========================================================================
104
105// ---------------------------------------------------------------------------
106// wxApp
107// ---------------------------------------------------------------------------
108
d88de032 109 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
0e320a79 110
d88de032
DW
111 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
112 EVT_IDLE(wxApp::OnIdle)
113 EVT_END_SESSION(wxApp::OnEndSession)
114 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
115 END_EVENT_TABLE()
0e320a79 116
d88de032 117//// Initialize
9ed0fac8
DW
118bool wxApp::Initialize(
119 HAB vHab
120)
0e320a79 121{
914589c2
DW
122 //
123 // OS2 has to have an anchorblock
124 //
125 vHab = WinInitialize(0);
126
127 if (!vHab)
128 return FALSE;
129 else
130 vHabmain = vHab;
131
d88de032
DW
132 // Some people may wish to use this, but
133 // probably it shouldn't be here by default.
134#ifdef __WXDEBUG__
135 // wxRedirectIOToConsole();
0e320a79
DW
136#endif
137
13a4ea8d 138 wxBuffer = new wxChar[1500]; // FIXME; why?
0e320a79 139
d88de032 140 wxClassInfo::InitializeClasses();
0e320a79 141
d88de032
DW
142#if wxUSE_RESOURCES
143 wxGetResource(wxT("wxWindows"), wxT("OsVersion"), &wxOsVersion);
144#endif
145
146 // I'm annoyed ... I don't know where to put this and I don't want to
147 // create a module for that as it's part of the core.
d88de032
DW
148 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
149 wxTheColourDatabase->Initialize();
150
151 wxInitializeStockLists();
152 wxInitializeStockObjects();
0e320a79
DW
153
154#if wxUSE_WX_RESOURCES
d88de032 155 wxInitializeResourceSystem();
0e320a79
DW
156#endif
157
d88de032
DW
158 wxBitmap::InitStandardHandlers();
159
160 g_globalCursor = new wxCursor;
161
9ed0fac8
DW
162#if 0
163 wxSTD_FRAME_ICON = ::WinLoadFileIcon(wxT("wxSTD_FRAME"), TRUE);
164 wxSTD_MDIPARENTFRAME_ICON = ::WinLoadFileIcon(wxT("wxSTD_MDIPARENTFRAME"), TRUE);
165 wxSTD_MDICHILDFRAME_ICON = ::WinLoadFileIcon(wxT("wxSTD_MDICHILDFRAME"), TRUE);
d88de032 166
9ed0fac8
DW
167 wxDEFAULT_FRAME_ICON = ::WinLoadFileIcon(wxT("wxDEFAULT_FRAME"), TRUE);
168 wxDEFAULT_MDIPARENTFRAME_ICON = ::WinLoadFileIcon(wxT("wxDEFAULT_MDIPARENTFRAME"), TRUE);
169 wxDEFAULT_MDICHILDFRAME_ICON = ::WinLoadFileIcon(wxT("wxDEFAULT_MDICHILDFRAME"), TRUE);
170#endif
171 RegisterWindowClasses(vHab);
d88de032
DW
172 wxWinHandleList = new wxList(wxKEY_INTEGER);
173
174 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
175 // PLEASE DO NOT ALTER THIS.
1b3d5e55 176#if !defined(WXMAKINGDLL) && defined(__VISAGECPP__)
d88de032
DW
177 extern char wxDummyChar;
178 if (wxDummyChar) wxDummyChar++;
179#endif
180
61243a51 181 // wxSetKeyboardHook(TRUE);
d88de032
DW
182
183 wxModule::RegisterModules();
184 if (!wxModule::InitializeModules())
185 return FALSE;
186 return TRUE;
187}
188
189// ---------------------------------------------------------------------------
190// RegisterWindowClasses
191// ---------------------------------------------------------------------------
192
193// TODO we should only register classes really used by the app. For this it
194// would be enough to just delay the class registration until an attempt
195// to create a window of this class is made.
9ed0fac8
DW
196bool wxApp::RegisterWindowClasses(
197 HAB vHab
198)
d88de032 199{
3b9e3455 200 APIRET rc;
914589c2
DW
201 ERRORID vError = 0L;
202 wxString sError;
3b9e3455 203
f23208ca
DW
204 if (!::WinRegisterClass( vHab
205 ,wxFrameClassName
206 ,(PFNWP)wxWndProc
207 ,CS_SIZEREDRAW | CS_SYNCPAINT
a0606634 208 ,sizeof(ULONG)
f23208ca 209 ))
d88de032 210 {
914589c2
DW
211 vError = ::WinGetLastError(vHab);
212 sError = wxPMErrorToStr(vError);
213 wxLogLastError(sError);
d88de032
DW
214 return FALSE;
215 }
216
f23208ca
DW
217 if (!::WinRegisterClass( vHab
218 ,wxFrameClassNameNoRedraw
219 ,(PFNWP)wxWndProc
220 ,0
221 ,0
222 ))
d88de032 223 {
914589c2
DW
224 vError = ::WinGetLastError(vHab);
225 sError = wxPMErrorToStr(vError);
226 wxLogLastError(sError);
d88de032
DW
227 return FALSE;
228 }
229
f23208ca
DW
230 if (!::WinRegisterClass( vHab
231 ,wxMDIFrameClassName
232 ,(PFNWP)wxWndProc
233 ,CS_SIZEREDRAW | CS_SYNCPAINT
234 ,0
235 ))
d88de032 236 {
914589c2
DW
237 vError = ::WinGetLastError(vHab);
238 sError = wxPMErrorToStr(vError);
239 wxLogLastError(sError);
d88de032
DW
240 return FALSE;
241 }
0e320a79 242
f23208ca
DW
243 if (!::WinRegisterClass( vHab
244 ,wxMDIFrameClassNameNoRedraw
245 ,(PFNWP)wxWndProc
246 ,0
247 ,0
248 ))
d88de032 249 {
914589c2
DW
250 vError = ::WinGetLastError(vHab);
251 sError = wxPMErrorToStr(vError);
252 wxLogLastError(sError);
d88de032
DW
253 return FALSE;
254 }
255
f23208ca
DW
256 if (!::WinRegisterClass( vHab
257 ,wxMDIChildFrameClassName
258 ,(PFNWP)wxWndProc
259 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST
260 ,0
261 ))
d88de032 262 {
914589c2
DW
263 vError = ::WinGetLastError(vHab);
264 sError = wxPMErrorToStr(vError);
265 wxLogLastError(sError);
d88de032
DW
266 return FALSE;
267 }
268
f23208ca
DW
269 if (!::WinRegisterClass( vHab
270 ,wxMDIChildFrameClassNameNoRedraw
271 ,(PFNWP)wxWndProc
272 ,CS_HITTEST
273 ,0
274 ))
d88de032 275 {
914589c2
DW
276 vError = ::WinGetLastError(vHab);
277 sError = wxPMErrorToStr(vError);
278 wxLogLastError(sError);
d88de032
DW
279 return FALSE;
280 }
281
f23208ca
DW
282 if (!::WinRegisterClass( vHab
283 ,wxPanelClassName
284 ,(PFNWP)wxWndProc
285 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT
286 ,0
287 ))
d88de032 288 {
914589c2
DW
289 vError = ::WinGetLastError(vHab);
290 sError = wxPMErrorToStr(vError);
291 wxLogLastError(sError);
d88de032
DW
292 return FALSE;
293 }
294
f23208ca
DW
295 if (!::WinRegisterClass( vHab
296 ,wxCanvasClassName
297 ,(PFNWP)wxWndProc
298 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT
299 ,0
300 ))
d88de032 301 {
914589c2
DW
302 vError = ::WinGetLastError(vHab);
303 sError = wxPMErrorToStr(vError);
304 wxLogLastError(sError);
d88de032
DW
305 return FALSE;
306 }
d88de032 307 return TRUE;
0e320a79
DW
308}
309
d88de032
DW
310//// Cleans up any wxWindows internal structures left lying around
311
0e320a79
DW
312void wxApp::CleanUp()
313{
d88de032
DW
314 //// COMMON CLEANUP
315
316#if wxUSE_LOG
317 // flush the logged messages if any and install a 'safer' log target: the
318 // default one (wxLogGui) can't be used after the resources are freed just
319 // below and the user suppliedo ne might be even more unsafe (using any
320 // wxWindows GUI function is unsafe starting from now)
321 wxLog::DontCreateOnDemand();
322
323 // this will flush the old messages if any
324 delete wxLog::SetActiveTarget(new wxLogStderr);
325#endif // wxUSE_LOG
326
327 // One last chance for pending objects to be cleaned up
328 wxTheApp->DeletePendingObjects();
329
330 wxModule::CleanUpModules();
0e320a79
DW
331
332#if wxUSE_WX_RESOURCES
d88de032 333 wxCleanUpResourceSystem();
0e320a79 334
d88de032
DW
335 // wxDefaultResourceTable->ClearTable();
336#endif
0e320a79 337
d88de032
DW
338 // Indicate that the cursor can be freed, so that cursor won't be deleted
339 // by deleting the bitmap list before g_globalCursor goes out of scope
340 // (double deletion of the cursor).
341 wxSetCursor(wxNullCursor);
342 delete g_globalCursor;
343 g_globalCursor = NULL;
0e320a79 344
d88de032 345 wxDeleteStockObjects();
0e320a79 346
d88de032
DW
347 // Destroy all GDI lists, etc.
348 wxDeleteStockLists();
0e320a79 349
d88de032
DW
350 delete wxTheColourDatabase;
351 wxTheColourDatabase = NULL;
0e320a79 352
d88de032 353 wxBitmap::CleanUpHandlers();
0e320a79 354
d88de032
DW
355 delete[] wxBuffer;
356 wxBuffer = NULL;
0e320a79 357
a0606634 358 //// PM-SPECIFIC CLEANUP
0e320a79 359
61243a51 360 // wxSetKeyboardHook(FALSE);
9ed0fac8 361
d88de032 362 if (wxSTD_FRAME_ICON)
9ed0fac8 363 ::WinFreeFileIcon(wxSTD_FRAME_ICON);
d88de032 364 if (wxSTD_MDICHILDFRAME_ICON)
9ed0fac8 365 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON);
d88de032 366 if (wxSTD_MDIPARENTFRAME_ICON)
9ed0fac8 367 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON);
d88de032
DW
368
369 if (wxDEFAULT_FRAME_ICON)
9ed0fac8 370 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON);
d88de032 371 if (wxDEFAULT_MDICHILDFRAME_ICON)
9ed0fac8 372 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON);
d88de032 373 if (wxDEFAULT_MDIPARENTFRAME_ICON)
9ed0fac8
DW
374 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
375
d88de032
DW
376 if ( wxDisableButtonBrush )
377 {
378// TODO: ::DeleteObject( wxDisableButtonBrush );
379 }
380
381 if (wxWinHandleList)
382 delete wxWinHandleList;
383
384 // GL: I'm annoyed ... I don't know where to put this and I don't want to
385 // create a module for that as it's part of the core.
386#if wxUSE_THREADS
387 delete wxPendingEvents;
388 delete wxPendingEventsLocker;
389 // If we don't do the following, we get an apparent memory leak.
390 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
391#endif
0e320a79 392
d88de032 393 wxClassInfo::CleanUpClasses();
0e320a79 394
d88de032
DW
395 delete wxTheApp;
396 wxTheApp = NULL;
77cd51c3 397
0e320a79 398#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
d88de032
DW
399 // At this point we want to check if there are any memory
400 // blocks that aren't part of the wxDebugContext itself,
401 // as a special case. Then when dumping we need to ignore
402 // wxDebugContext, too.
403 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
404 {
405 wxLogDebug(wxT("There were memory leaks."));
406 wxDebugContext::Dump();
407 wxDebugContext::PrintStatistics();
408 }
409 // wxDebugContext::SetStream(NULL, NULL);
0e320a79 410#endif
77cd51c3 411
d88de032
DW
412#if wxUSE_LOG
413 // do it as the very last thing because everything else can log messages
414 delete wxLog::SetActiveTarget(NULL);
415#endif // wxUSE_LOG
0e320a79
DW
416}
417
9ed0fac8
DW
418int wxEntry(
419 int argc
420, char* argv[]
421)
d88de032 422{
9dea36ef 423 HAB vHab = 0;
d88de032 424
9ed0fac8
DW
425 if (!wxApp::Initialize(vHab))
426 return 0;
d88de032 427
9ed0fac8
DW
428 //
429 // create the application object or ensure that one already exists
430 //
431 if (!wxTheApp)
432 {
426d5745
DW
433 // The app may have declared a global application object, but we recommend
434 // the IMPLEMENT_APP macro is used instead, which sets an initializer
435 // function for delayed, dynamic app object construction.
436 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
437 wxT("No initializer - use IMPLEMENT_APP macro.") );
438 wxTheApp = (*wxApp::GetInitializerFunction()) ();
9ed0fac8 439 }
426d5745 440 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
9ed0fac8 441 wxTheApp->argc = argc;
d88de032 442
9ed0fac8
DW
443#if wxUSE_UNICODE
444 wxTheApp->argv = new wxChar*[argc+1];
445
446 int nArgc = 0;
447
448 while (nArgc < argc)
449 {
450 wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc]));
451 nArgc++;
452 }
453 wxTheApp->argv[nArgc] = (wxChar *)NULL;
454#else
455 wxTheApp->argv = argv;
456#endif
d88de032 457
9ed0fac8 458 wxString sName(wxFileNameFromPath(argv[0]));
d88de032 459
9ed0fac8
DW
460 wxStripExtension(sName);
461 wxTheApp->SetAppName(sName);
d88de032 462
9ed0fac8 463 int nRetValue = 0;
d88de032 464
9ed0fac8
DW
465 if (!wxTheApp->OnInitGui())
466 nRetValue = -1;
d88de032 467
9ed0fac8
DW
468 if (nRetValue == 0)
469 {
470 if (wxTheApp->OnInit())
d88de032 471 {
7bed7a50
DW
472 nRetValue = wxTheApp->OnRun();
473// nRetValue = -1;
d88de032 474 }
9ed0fac8
DW
475 }
476
7bed7a50 477 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
d88de032 478
7bed7a50
DW
479 if (pTopWindow)
480 {
481 // Forcibly delete the window.
482 if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) ||
483 pTopWindow->IsKindOf(CLASSINFO(wxDialog)) )
484 {
485 pTopWindow->Close(TRUE);
486 wxTheApp->DeletePendingObjects();
487 }
488 else
d88de032 489 {
7bed7a50
DW
490 delete pTopWindow;
491 wxTheApp->SetTopWindow(NULL);
d88de032 492 }
9ed0fac8
DW
493 }
494 wxTheApp->OnExit();
495 wxApp::CleanUp();
496 return(nRetValue);
d88de032 497}
77cd51c3 498
9ed0fac8 499bool wxApp::OnInitGui()
d88de032 500{
914589c2
DW
501 ERRORID vError;
502 wxString sError;
77cd51c3 503
f23208ca 504 m_hMq = ::WinCreateMsgQueue(vHabmain, 0);
914589c2
DW
505 if (!m_hMq)
506 {
507 vError = ::WinGetLastError(vHabmain);
508 sError = wxPMErrorToStr(vError);
509 wxLogDebug(sError);
510 return FALSE;
511 }
9ed0fac8 512 return TRUE;
d88de032 513}
0e320a79 514
9ed0fac8
DW
515//
516// Static member initialization
517//
d88de032 518wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
0e320a79
DW
519
520wxApp::wxApp()
521{
d88de032
DW
522 m_topWindow = NULL;
523 wxTheApp = this;
524 m_wantDebugOutput = TRUE;
525
526 argc = 0;
527 argv = NULL;
9ed0fac8 528 m_nPrintMode = wxPRINT_WINDOWS;
d88de032 529 m_exitOnFrameDelete = TRUE;
9ed0fac8 530 m_bAuto3D = TRUE;
d88de032
DW
531}
532
533wxApp::~wxApp()
534{
1b3d5e55 535#if wxUSE_UNICODE
d88de032
DW
536 // Delete command-line args
537 int i;
538 for (i = 0; i < argc; i++)
539 {
540 delete[] argv[i];
541 }
542 delete[] argv;
aa2097dd 543#endif
0e320a79
DW
544}
545
546bool wxApp::Initialized()
547{
d88de032
DW
548 if (GetTopWindow())
549 return TRUE;
550 else
551 return FALSE;
0e320a79
DW
552}
553
9ed0fac8
DW
554//
555// Get and process a message, returning FALSE if WM_QUIT
556// received (and also set the flag telling the app to exit the main loop)
557//
d88de032
DW
558bool wxApp::DoMessage()
559{
43543d98 560 BOOL bRc = ::WinGetMsg(vHabmain, &m_vMsg, HWND(NULL), 0, 0);
9ed0fac8
DW
561
562 if (bRc == 0)
d88de032
DW
563 {
564 // got WM_QUIT
9ed0fac8 565 m_bKeepGoing = FALSE;
d88de032
DW
566 return FALSE;
567 }
9ed0fac8 568 else if (bRc == -1)
d88de032
DW
569 {
570 // should never happen, but let's test for it nevertheless
571 wxLogLastError("GetMessage");
572 }
573 else
574 {
575#if wxUSE_THREADS
9ed0fac8
DW
576 wxASSERT_MSG( wxThread::IsMain()
577 ,wxT("only the main thread can process Windows messages")
578 );
d88de032 579
9ed0fac8
DW
580 static bool sbHadGuiLock = TRUE;
581 static wxMsgArray svSavedMessages;
d88de032 582
9ed0fac8 583 //
d88de032
DW
584 // if a secondary thread owns is doing GUI calls, save all messages for
585 // later processing - we can't process them right now because it will
586 // lead to recursive library calls (and we're not reentrant)
9ed0fac8
DW
587 //
588 if (!wxGuiOwnedByMainThread())
d88de032 589 {
9ed0fac8 590 sbHadGuiLock = FALSE;
d88de032
DW
591
592 // leave out WM_COMMAND messages: too dangerous, sometimes
593 // the message will be processed twice
594 if ( !wxIsWaitingForThread() ||
9ed0fac8 595 svCurrentMsg.msg != WM_COMMAND )
d88de032 596 {
9ed0fac8 597 svSavedMessages.Add(svCurrentMsg);
d88de032 598 }
d88de032
DW
599 return TRUE;
600 }
601 else
602 {
9ed0fac8 603 //
d88de032
DW
604 // have we just regained the GUI lock? if so, post all of the saved
605 // messages
606 //
607 // FIXME of course, it's not _exactly_ the same as processing the
608 // messages normally - expect some things to break...
9ed0fac8
DW
609 //
610 if (!sbHadGuiLock )
d88de032 611 {
9ed0fac8 612 sbHadGuiLock = TRUE;
d88de032 613
9ed0fac8
DW
614 size_t nCount = svSavedMessages.Count();
615
616 for (size_t n = 0; n < nCount; n++)
d88de032 617 {
9ed0fac8 618 QMSG vMsg = svSavedMessages[n];
d88de032 619
9ed0fac8 620 if ( !ProcessMessage((WXMSG *)&vMsg) )
d88de032 621 {
43543d98 622 ::WinDispatchMsg(vHabmain, &vMsg);
d88de032
DW
623 }
624 }
9ed0fac8 625 svSavedMessages.Empty();
d88de032
DW
626 }
627 }
d88de032
DW
628#endif // wxUSE_THREADS
629
630 // Process the message
9ed0fac8 631 if (!ProcessMessage((WXMSG *)&svCurrentMsg) )
d88de032 632 {
43543d98 633 ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg);
d88de032 634 }
d88de032 635 }
d88de032
DW
636 return TRUE;
637}
638
9ed0fac8
DW
639//////////////////////////////////////////////////////////////////////////////
640//
641// Keep trying to process messages until WM_QUIT
642// received.
643//
644// If there are messages to be processed, they will all be
645// processed and OnIdle will not be called.
646// When there are no more messages, OnIdle is called.
647// If OnIdle requests more time,
648// it will be repeatedly called so long as there are no pending messages.
649// A 'feature' of this is that once OnIdle has decided that no more processing
650// is required, then it won't get processing time until further messages
651// are processed (it'll sit in DoMessage).
652//
653//////////////////////////////////////////////////////////////////////////////
0e320a79
DW
654int wxApp::MainLoop()
655{
9ed0fac8 656 m_bKeepGoing = TRUE;
d88de032 657
9ed0fac8 658 while (m_bKeepGoing)
d88de032
DW
659 {
660#if wxUSE_THREADS
661 wxMutexGuiLeaveOrEnter();
662#endif // wxUSE_THREADS
43543d98 663 while (!::WinPeekMsg(vHabmain, &svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) &&
d88de032
DW
664 ProcessIdle() )
665 {
666 }
d88de032
DW
667 DoMessage();
668 }
9ed0fac8 669 return (int)svCurrentMsg.mp1;
0e320a79
DW
670}
671
9ed0fac8 672//
0e320a79 673// Returns TRUE if more time is needed.
9ed0fac8 674//
0e320a79
DW
675bool wxApp::ProcessIdle()
676{
9ed0fac8 677 wxIdleEvent vEvent;
0e320a79 678
9ed0fac8
DW
679 vEvent.SetEventObject(this);
680 ProcessEvent(vEvent);
681 return vEvent.MoreRequested();
0e320a79
DW
682}
683
d88de032
DW
684#if wxUSE_THREADS
685void wxApp::ProcessPendingEvents()
686{
9ed0fac8
DW
687 wxNode* pNode = wxPendingEvents->First();
688 wxCriticalSectionLocker vLocker(*wxPendingEventsLocker);
d88de032 689
9ed0fac8 690 while (pNode)
d88de032 691 {
9ed0fac8
DW
692 wxEvtHandler* pHandler = (wxEvtHandler *)pNode->Data();
693 pHandler->ProcessPendingEvents();
d88de032 694
9ed0fac8
DW
695 delete pNode;
696 pNode = wxPendingEvents->First();
d88de032
DW
697 }
698}
699#endif
700
0e320a79
DW
701void wxApp::ExitMainLoop()
702{
9ed0fac8 703 m_bKeepGoing = FALSE;
0e320a79
DW
704}
705
0e320a79
DW
706bool wxApp::Pending()
707{
43543d98 708 return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0);
0e320a79
DW
709}
710
0e320a79
DW
711void wxApp::Dispatch()
712{
d88de032 713 DoMessage();
0e320a79
DW
714}
715
9ed0fac8
DW
716//////////////////////////////////////////////////////////////////////////////
717//
718// Give all windows a chance to preprocess
719// the message. Some may have accelerator tables, or have
720// MDI complications.
721//
722//////////////////////////////////////////////////////////////////////////////
723bool wxApp::ProcessMessage(
724 WXMSG* pWxmsg
725)
0e320a79 726{
9ed0fac8
DW
727 QMSG* vMsg = (PQMSG)pWxmsg;
728 HWND hWnd = vMsg->hwnd;
729 wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
730 wxWindow* pWnd;
d88de032 731
9ed0fac8 732 //
d88de032
DW
733 // for some composite controls (like a combobox), wndThis might be NULL
734 // because the subcontrol is not a wxWindow, but only the control itself
735 // is - try to catch this case
9ed0fac8
DW
736 //
737 while (hWnd && !pWndThis)
d88de032 738 {
9ed0fac8
DW
739 hWnd = ::WinQueryWindow(hWnd, QW_PARENT);
740 pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
d88de032 741 }
0e320a79 742
d88de032 743 // Anyone for a non-translation message? Try youngest descendants first.
9ed0fac8 744 for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent())
d88de032 745 {
9ed0fac8 746 if (pWnd->OS2ProcessMessage(pWxmsg))
d88de032
DW
747 return TRUE;
748 }
d88de032
DW
749 return FALSE;
750}
0e320a79 751
9ed0fac8
DW
752void wxApp::OnIdle(
753 wxIdleEvent& rEvent
754)
d88de032 755{
9ed0fac8 756 static bool sbInOnIdle = FALSE;
d88de032 757
9ed0fac8 758 //
d88de032 759 // Avoid recursion (via ProcessEvent default case)
9ed0fac8
DW
760 //
761 if (sbInOnIdle )
d88de032 762 return;
0e320a79 763
9ed0fac8 764 sbInOnIdle = TRUE;
0e320a79 765
9ed0fac8 766 //
d88de032 767 // 'Garbage' collection of windows deleted with Close().
9ed0fac8 768 //
d88de032 769 DeletePendingObjects();
0e320a79 770
d88de032
DW
771#if wxUSE_LOG
772 // flush the logged messages if any
9ed0fac8
DW
773 wxLog* pLog = wxLog::GetActiveTarget();
774
775 if (pLog != NULL && pLog->HasPendingMessages())
d88de032
DW
776 pLog->Flush();
777#endif // wxUSE_LOG
0e320a79 778
d88de032 779 // Send OnIdle events to all windows
9ed0fac8 780 if (SendIdleEvents())
d88de032 781 {
9ed0fac8 782 //
d88de032
DW
783 // SendIdleEvents() returns TRUE if at least one window requested more
784 // idle events
9ed0fac8
DW
785 //
786 rEvent.RequestMore(TRUE);
d88de032
DW
787 }
788
9ed0fac8 789 //
d88de032 790 // If they are pending events, we must process them.
9ed0fac8 791 //
d88de032
DW
792#if wxUSE_THREADS
793 ProcessPendingEvents();
794#endif
9ed0fac8 795 sbInOnIdle = FALSE;
0e320a79
DW
796}
797
9779893b
RD
798void wxWakeUpIdle()
799{
800 // **** please implement me! ****
801 // Wake up the idle handler processor, even if it is in another thread...
802}
803
0e320a79
DW
804// Send idle event to all top-level windows
805bool wxApp::SendIdleEvents()
806{
9ed0fac8
DW
807 bool bNeedMore = FALSE;
808 wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst();
d88de032 809
9ed0fac8 810 while (pNode)
d88de032 811 {
9ed0fac8 812 wxWindow* pWin = pNode->GetData();
0e320a79 813
9ed0fac8
DW
814 if (SendIdleEvents(pWin))
815 bNeedMore = TRUE;
816 pNode = pNode->GetNext();
817 }
818 return bNeedMore;
0e320a79
DW
819}
820
9ed0fac8 821//
0e320a79 822// Send idle event to window and all subwindows
9ed0fac8
DW
823//
824bool wxApp::SendIdleEvents(
825 wxWindow* pWin
826)
0e320a79 827{
9ed0fac8
DW
828 bool bNeedMore = FALSE;
829 wxIdleEvent vEvent;
830
831 vEvent.SetEventObject(pWin);
832 pWin->GetEventHandler()->ProcessEvent(vEvent);
0e320a79 833
9ed0fac8
DW
834 if (vEvent.MoreRequested())
835 bNeedMore = TRUE;
0e320a79 836
9ed0fac8 837 wxNode* pNode = pWin->GetChildren().First();
0e320a79 838
9ed0fac8 839 while (pNode)
d88de032 840 {
9ed0fac8 841 wxWindow* pWin = (wxWindow*) pNode->Data();
0e320a79 842
9ed0fac8
DW
843 if (SendIdleEvents(pWin))
844 bNeedMore = TRUE;
845 pNode = pNode->Next();
d88de032 846 }
9ed0fac8 847 return bNeedMore;
0e320a79
DW
848}
849
850void wxApp::DeletePendingObjects()
851{
9ed0fac8
DW
852 wxNode* pNode = wxPendingDelete.First();
853
854 while (pNode)
d88de032 855 {
9ed0fac8 856 wxObject* pObj = (wxObject *)pNode->Data();
77cd51c3 857
9ed0fac8 858 delete pObj;
0e320a79 859
9ed0fac8
DW
860 if (wxPendingDelete.Member(pObj))
861 delete pNode;
0e320a79 862
9ed0fac8 863 //
d88de032
DW
864 // Deleting one object may have deleted other pending
865 // objects, so start from beginning of list again.
9ed0fac8
DW
866 //
867 pNode = wxPendingDelete.First();
d88de032 868 }
0e320a79
DW
869}
870
9ed0fac8
DW
871void wxApp::OnEndSession(
872 wxCloseEvent& WXUNUSED(rEvent))
0e320a79 873{
d88de032
DW
874 if (GetTopWindow())
875 GetTopWindow()->Close(TRUE);
0e320a79
DW
876}
877
9ed0fac8 878//
d88de032
DW
879// Default behaviour: close the application with prompts. The
880// user can veto the close, and therefore the end session.
9ed0fac8
DW
881//
882void wxApp::OnQueryEndSession(
883 wxCloseEvent& rEvent
884)
0e320a79 885{
d88de032
DW
886 if (GetTopWindow())
887 {
9ed0fac8
DW
888 if (!GetTopWindow()->Close(!rEvent.CanVeto()))
889 rEvent.Veto(TRUE);
d88de032 890 }
0e320a79
DW
891}
892
893void wxExit()
894{
d88de032
DW
895 wxLogError(_("Fatal error: exiting"));
896
897 wxApp::CleanUp();
0e320a79
DW
898}
899
d88de032 900// Yield to incoming messages
0e320a79
DW
901bool wxYield()
902{
9dea36ef 903 HAB vHab = 0;
dde11e60 904 QMSG vMsg;
d88de032
DW
905 // We want to go back to the main message loop
906 // if we see a WM_QUIT. (?)
dde11e60 907 while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT)
d88de032 908 {
dde11e60 909 if (!wxTheApp->DoMessage())
d88de032
DW
910 break;
911 }
912 // If they are pending events, we must process them.
913#if wxUSE_THREADS
914 wxTheApp->ProcessPendingEvents();
915#endif
d88de032
DW
916 return TRUE;
917}
918
9ed0fac8
DW
919wxIcon wxApp::GetStdIcon(
920 int nWhich
921) const
d88de032 922{
9ed0fac8 923 switch(nWhich)
d88de032
DW
924 {
925 case wxICON_INFORMATION:
926 return wxIcon("wxICON_INFO");
927
928 case wxICON_QUESTION:
929 return wxIcon("wxICON_QUESTION");
930
931 case wxICON_EXCLAMATION:
932 return wxIcon("wxICON_WARNING");
933
934 default:
935 wxFAIL_MSG(wxT("requested non existent standard icon"));
936 // still fall through
937
938 case wxICON_HAND:
939 return wxIcon("wxICON_ERROR");
940 }
d88de032
DW
941 return wxIcon("wxICON_ERROR");
942}
943
d88de032
DW
944HINSTANCE wxGetInstance()
945{
946 return wxhInstance;
947}
948
949void wxSetInstance(HINSTANCE hInst)
950{
951 wxhInstance = hInst;
0e320a79
DW
952}
953