]> git.saurik.com Git - wxWidgets.git/blame - src/os2/app.cpp
Fixed remaining int vs. long warnings.
[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
208 ,0
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
426d5745
DW
324#if (!(defined(__VISAGECPP__) && __IBMCPP__ < 400))
325 // another VA 3.0 memory problem here
d88de032 326 delete wxLog::SetActiveTarget(new wxLogStderr);
426d5745 327#endif
d88de032
DW
328#endif // wxUSE_LOG
329
330 // One last chance for pending objects to be cleaned up
331 wxTheApp->DeletePendingObjects();
332
426d5745
DW
333#if (!(defined(__VISAGECPP__) && __IBMCPP__ < 400))
334 // another VA 3.0 memory problem here
d88de032 335 wxModule::CleanUpModules();
426d5745 336#endif
0e320a79
DW
337
338#if wxUSE_WX_RESOURCES
d88de032 339 wxCleanUpResourceSystem();
0e320a79 340
d88de032
DW
341 // wxDefaultResourceTable->ClearTable();
342#endif
0e320a79 343
d88de032
DW
344 // Indicate that the cursor can be freed, so that cursor won't be deleted
345 // by deleting the bitmap list before g_globalCursor goes out of scope
346 // (double deletion of the cursor).
347 wxSetCursor(wxNullCursor);
426d5745
DW
348#if (!(defined(__VISAGECPP__) && __IBMCPP__ < 400))
349 // another VA 3.0 memory problem here
d88de032 350 delete g_globalCursor;
426d5745 351#endif
d88de032 352 g_globalCursor = NULL;
0e320a79 353
d88de032 354 wxDeleteStockObjects();
0e320a79 355
d88de032
DW
356 // Destroy all GDI lists, etc.
357 wxDeleteStockLists();
0e320a79 358
d88de032
DW
359 delete wxTheColourDatabase;
360 wxTheColourDatabase = NULL;
0e320a79 361
d88de032 362 wxBitmap::CleanUpHandlers();
0e320a79 363
d88de032
DW
364 delete[] wxBuffer;
365 wxBuffer = NULL;
0e320a79 366
d88de032 367 //// WINDOWS-SPECIFIC CLEANUP
0e320a79 368
61243a51 369 // wxSetKeyboardHook(FALSE);
9ed0fac8 370
d88de032 371 if (wxSTD_FRAME_ICON)
9ed0fac8 372 ::WinFreeFileIcon(wxSTD_FRAME_ICON);
d88de032 373 if (wxSTD_MDICHILDFRAME_ICON)
9ed0fac8 374 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON);
d88de032 375 if (wxSTD_MDIPARENTFRAME_ICON)
9ed0fac8 376 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON);
d88de032
DW
377
378 if (wxDEFAULT_FRAME_ICON)
9ed0fac8 379 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON);
d88de032 380 if (wxDEFAULT_MDICHILDFRAME_ICON)
9ed0fac8 381 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON);
d88de032 382 if (wxDEFAULT_MDIPARENTFRAME_ICON)
9ed0fac8
DW
383 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
384
d88de032
DW
385 if ( wxDisableButtonBrush )
386 {
387// TODO: ::DeleteObject( wxDisableButtonBrush );
388 }
389
390 if (wxWinHandleList)
391 delete wxWinHandleList;
392
393 // GL: I'm annoyed ... I don't know where to put this and I don't want to
394 // create a module for that as it's part of the core.
395#if wxUSE_THREADS
396 delete wxPendingEvents;
397 delete wxPendingEventsLocker;
398 // If we don't do the following, we get an apparent memory leak.
399 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
400#endif
0e320a79 401
d88de032 402 wxClassInfo::CleanUpClasses();
0e320a79 403
d88de032
DW
404 delete wxTheApp;
405 wxTheApp = NULL;
77cd51c3 406
0e320a79 407#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
d88de032
DW
408 // At this point we want to check if there are any memory
409 // blocks that aren't part of the wxDebugContext itself,
410 // as a special case. Then when dumping we need to ignore
411 // wxDebugContext, too.
412 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
413 {
414 wxLogDebug(wxT("There were memory leaks."));
415 wxDebugContext::Dump();
416 wxDebugContext::PrintStatistics();
417 }
418 // wxDebugContext::SetStream(NULL, NULL);
0e320a79 419#endif
77cd51c3 420
d88de032
DW
421#if wxUSE_LOG
422 // do it as the very last thing because everything else can log messages
423 delete wxLog::SetActiveTarget(NULL);
424#endif // wxUSE_LOG
0e320a79
DW
425}
426
9ed0fac8
DW
427int wxEntry(
428 int argc
429, char* argv[]
430)
d88de032 431{
9dea36ef 432 HAB vHab = 0;
d88de032 433
9ed0fac8
DW
434 if (!wxApp::Initialize(vHab))
435 return 0;
d88de032 436
9ed0fac8
DW
437 //
438 // create the application object or ensure that one already exists
439 //
440 if (!wxTheApp)
441 {
426d5745
DW
442 // The app may have declared a global application object, but we recommend
443 // the IMPLEMENT_APP macro is used instead, which sets an initializer
444 // function for delayed, dynamic app object construction.
445 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
446 wxT("No initializer - use IMPLEMENT_APP macro.") );
447 wxTheApp = (*wxApp::GetInitializerFunction()) ();
9ed0fac8 448 }
426d5745 449 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
9ed0fac8 450 wxTheApp->argc = argc;
d88de032 451
9ed0fac8
DW
452#if wxUSE_UNICODE
453 wxTheApp->argv = new wxChar*[argc+1];
454
455 int nArgc = 0;
456
457 while (nArgc < argc)
458 {
459 wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc]));
460 nArgc++;
461 }
462 wxTheApp->argv[nArgc] = (wxChar *)NULL;
463#else
464 wxTheApp->argv = argv;
465#endif
d88de032 466
9ed0fac8 467 wxString sName(wxFileNameFromPath(argv[0]));
d88de032 468
9ed0fac8
DW
469 wxStripExtension(sName);
470 wxTheApp->SetAppName(sName);
d88de032 471
9ed0fac8 472 int nRetValue = 0;
d88de032 473
9ed0fac8
DW
474 if (!wxTheApp->OnInitGui())
475 nRetValue = -1;
d88de032 476
9ed0fac8
DW
477 if (nRetValue == 0)
478 {
479 if (wxTheApp->OnInit())
d88de032 480 {
7bed7a50
DW
481 nRetValue = wxTheApp->OnRun();
482// nRetValue = -1;
d88de032 483 }
9ed0fac8
DW
484 }
485
7bed7a50 486 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
d88de032 487
7bed7a50
DW
488 if (pTopWindow)
489 {
490 // Forcibly delete the window.
491 if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) ||
492 pTopWindow->IsKindOf(CLASSINFO(wxDialog)) )
493 {
494 pTopWindow->Close(TRUE);
495 wxTheApp->DeletePendingObjects();
496 }
497 else
d88de032 498 {
7bed7a50
DW
499 delete pTopWindow;
500 wxTheApp->SetTopWindow(NULL);
d88de032 501 }
9ed0fac8
DW
502 }
503 wxTheApp->OnExit();
504 wxApp::CleanUp();
505 return(nRetValue);
d88de032 506}
77cd51c3 507
9ed0fac8 508bool wxApp::OnInitGui()
d88de032 509{
914589c2
DW
510 ERRORID vError;
511 wxString sError;
77cd51c3 512
f23208ca 513 m_hMq = ::WinCreateMsgQueue(vHabmain, 0);
914589c2
DW
514 if (!m_hMq)
515 {
516 vError = ::WinGetLastError(vHabmain);
517 sError = wxPMErrorToStr(vError);
518 wxLogDebug(sError);
519 return FALSE;
520 }
9ed0fac8 521 return TRUE;
d88de032 522}
0e320a79 523
9ed0fac8
DW
524//
525// Static member initialization
526//
d88de032 527wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
0e320a79
DW
528
529wxApp::wxApp()
530{
d88de032
DW
531 m_topWindow = NULL;
532 wxTheApp = this;
533 m_wantDebugOutput = TRUE;
534
535 argc = 0;
536 argv = NULL;
9ed0fac8 537 m_nPrintMode = wxPRINT_WINDOWS;
d88de032 538 m_exitOnFrameDelete = TRUE;
9ed0fac8 539 m_bAuto3D = TRUE;
d88de032
DW
540}
541
542wxApp::~wxApp()
543{
1b3d5e55 544#if wxUSE_UNICODE
d88de032
DW
545 // Delete command-line args
546 int i;
547 for (i = 0; i < argc; i++)
548 {
549 delete[] argv[i];
550 }
551 delete[] argv;
aa2097dd 552#endif
0e320a79
DW
553}
554
555bool wxApp::Initialized()
556{
d88de032
DW
557 if (GetTopWindow())
558 return TRUE;
559 else
560 return FALSE;
0e320a79
DW
561}
562
9ed0fac8
DW
563//
564// Get and process a message, returning FALSE if WM_QUIT
565// received (and also set the flag telling the app to exit the main loop)
566//
d88de032
DW
567bool wxApp::DoMessage()
568{
43543d98 569 BOOL bRc = ::WinGetMsg(vHabmain, &m_vMsg, HWND(NULL), 0, 0);
9ed0fac8
DW
570
571 if (bRc == 0)
d88de032
DW
572 {
573 // got WM_QUIT
9ed0fac8 574 m_bKeepGoing = FALSE;
d88de032
DW
575 return FALSE;
576 }
9ed0fac8 577 else if (bRc == -1)
d88de032
DW
578 {
579 // should never happen, but let's test for it nevertheless
580 wxLogLastError("GetMessage");
581 }
582 else
583 {
584#if wxUSE_THREADS
9ed0fac8
DW
585 wxASSERT_MSG( wxThread::IsMain()
586 ,wxT("only the main thread can process Windows messages")
587 );
d88de032 588
9ed0fac8
DW
589 static bool sbHadGuiLock = TRUE;
590 static wxMsgArray svSavedMessages;
d88de032 591
9ed0fac8 592 //
d88de032
DW
593 // if a secondary thread owns is doing GUI calls, save all messages for
594 // later processing - we can't process them right now because it will
595 // lead to recursive library calls (and we're not reentrant)
9ed0fac8
DW
596 //
597 if (!wxGuiOwnedByMainThread())
d88de032 598 {
9ed0fac8 599 sbHadGuiLock = FALSE;
d88de032
DW
600
601 // leave out WM_COMMAND messages: too dangerous, sometimes
602 // the message will be processed twice
603 if ( !wxIsWaitingForThread() ||
9ed0fac8 604 svCurrentMsg.msg != WM_COMMAND )
d88de032 605 {
9ed0fac8 606 svSavedMessages.Add(svCurrentMsg);
d88de032 607 }
d88de032
DW
608 return TRUE;
609 }
610 else
611 {
9ed0fac8 612 //
d88de032
DW
613 // have we just regained the GUI lock? if so, post all of the saved
614 // messages
615 //
616 // FIXME of course, it's not _exactly_ the same as processing the
617 // messages normally - expect some things to break...
9ed0fac8
DW
618 //
619 if (!sbHadGuiLock )
d88de032 620 {
9ed0fac8 621 sbHadGuiLock = TRUE;
d88de032 622
9ed0fac8
DW
623 size_t nCount = svSavedMessages.Count();
624
625 for (size_t n = 0; n < nCount; n++)
d88de032 626 {
9ed0fac8 627 QMSG vMsg = svSavedMessages[n];
d88de032 628
9ed0fac8 629 if ( !ProcessMessage((WXMSG *)&vMsg) )
d88de032 630 {
43543d98 631 ::WinDispatchMsg(vHabmain, &vMsg);
d88de032
DW
632 }
633 }
9ed0fac8 634 svSavedMessages.Empty();
d88de032
DW
635 }
636 }
d88de032
DW
637#endif // wxUSE_THREADS
638
639 // Process the message
9ed0fac8 640 if (!ProcessMessage((WXMSG *)&svCurrentMsg) )
d88de032 641 {
43543d98 642 ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg);
d88de032 643 }
d88de032 644 }
d88de032
DW
645 return TRUE;
646}
647
9ed0fac8
DW
648//////////////////////////////////////////////////////////////////////////////
649//
650// Keep trying to process messages until WM_QUIT
651// received.
652//
653// If there are messages to be processed, they will all be
654// processed and OnIdle will not be called.
655// When there are no more messages, OnIdle is called.
656// If OnIdle requests more time,
657// it will be repeatedly called so long as there are no pending messages.
658// A 'feature' of this is that once OnIdle has decided that no more processing
659// is required, then it won't get processing time until further messages
660// are processed (it'll sit in DoMessage).
661//
662//////////////////////////////////////////////////////////////////////////////
0e320a79
DW
663int wxApp::MainLoop()
664{
9ed0fac8 665 m_bKeepGoing = TRUE;
d88de032 666
9ed0fac8 667 while (m_bKeepGoing)
d88de032
DW
668 {
669#if wxUSE_THREADS
670 wxMutexGuiLeaveOrEnter();
671#endif // wxUSE_THREADS
43543d98 672 while (!::WinPeekMsg(vHabmain, &svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) &&
d88de032
DW
673 ProcessIdle() )
674 {
675 }
d88de032
DW
676 DoMessage();
677 }
9ed0fac8 678 return (int)svCurrentMsg.mp1;
0e320a79
DW
679}
680
9ed0fac8 681//
0e320a79 682// Returns TRUE if more time is needed.
9ed0fac8 683//
0e320a79
DW
684bool wxApp::ProcessIdle()
685{
9ed0fac8 686 wxIdleEvent vEvent;
0e320a79 687
9ed0fac8
DW
688 vEvent.SetEventObject(this);
689 ProcessEvent(vEvent);
690 return vEvent.MoreRequested();
0e320a79
DW
691}
692
d88de032
DW
693#if wxUSE_THREADS
694void wxApp::ProcessPendingEvents()
695{
9ed0fac8
DW
696 wxNode* pNode = wxPendingEvents->First();
697 wxCriticalSectionLocker vLocker(*wxPendingEventsLocker);
d88de032 698
9ed0fac8 699 while (pNode)
d88de032 700 {
9ed0fac8
DW
701 wxEvtHandler* pHandler = (wxEvtHandler *)pNode->Data();
702 pHandler->ProcessPendingEvents();
d88de032 703
9ed0fac8
DW
704 delete pNode;
705 pNode = wxPendingEvents->First();
d88de032
DW
706 }
707}
708#endif
709
0e320a79
DW
710void wxApp::ExitMainLoop()
711{
9ed0fac8 712 m_bKeepGoing = FALSE;
0e320a79
DW
713}
714
0e320a79
DW
715bool wxApp::Pending()
716{
43543d98 717 return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0);
0e320a79
DW
718}
719
0e320a79
DW
720void wxApp::Dispatch()
721{
d88de032 722 DoMessage();
0e320a79
DW
723}
724
9ed0fac8
DW
725//////////////////////////////////////////////////////////////////////////////
726//
727// Give all windows a chance to preprocess
728// the message. Some may have accelerator tables, or have
729// MDI complications.
730//
731//////////////////////////////////////////////////////////////////////////////
732bool wxApp::ProcessMessage(
733 WXMSG* pWxmsg
734)
0e320a79 735{
9ed0fac8
DW
736 QMSG* vMsg = (PQMSG)pWxmsg;
737 HWND hWnd = vMsg->hwnd;
738 wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
739 wxWindow* pWnd;
d88de032 740
9ed0fac8 741 //
d88de032
DW
742 // for some composite controls (like a combobox), wndThis might be NULL
743 // because the subcontrol is not a wxWindow, but only the control itself
744 // is - try to catch this case
9ed0fac8
DW
745 //
746 while (hWnd && !pWndThis)
d88de032 747 {
9ed0fac8
DW
748 hWnd = ::WinQueryWindow(hWnd, QW_PARENT);
749 pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
d88de032 750 }
0e320a79 751
d88de032 752 // Anyone for a non-translation message? Try youngest descendants first.
9ed0fac8 753 for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent())
d88de032 754 {
9ed0fac8 755 if (pWnd->OS2ProcessMessage(pWxmsg))
d88de032
DW
756 return TRUE;
757 }
d88de032
DW
758 return FALSE;
759}
0e320a79 760
9ed0fac8
DW
761void wxApp::OnIdle(
762 wxIdleEvent& rEvent
763)
d88de032 764{
9ed0fac8 765 static bool sbInOnIdle = FALSE;
d88de032 766
9ed0fac8 767 //
d88de032 768 // Avoid recursion (via ProcessEvent default case)
9ed0fac8
DW
769 //
770 if (sbInOnIdle )
d88de032 771 return;
0e320a79 772
9ed0fac8 773 sbInOnIdle = TRUE;
0e320a79 774
9ed0fac8 775 //
d88de032 776 // 'Garbage' collection of windows deleted with Close().
9ed0fac8 777 //
d88de032 778 DeletePendingObjects();
0e320a79 779
d88de032
DW
780#if wxUSE_LOG
781 // flush the logged messages if any
9ed0fac8
DW
782 wxLog* pLog = wxLog::GetActiveTarget();
783
784 if (pLog != NULL && pLog->HasPendingMessages())
d88de032
DW
785 pLog->Flush();
786#endif // wxUSE_LOG
0e320a79 787
d88de032 788 // Send OnIdle events to all windows
9ed0fac8 789 if (SendIdleEvents())
d88de032 790 {
9ed0fac8 791 //
d88de032
DW
792 // SendIdleEvents() returns TRUE if at least one window requested more
793 // idle events
9ed0fac8
DW
794 //
795 rEvent.RequestMore(TRUE);
d88de032
DW
796 }
797
9ed0fac8 798 //
d88de032 799 // If they are pending events, we must process them.
9ed0fac8 800 //
d88de032
DW
801#if wxUSE_THREADS
802 ProcessPendingEvents();
803#endif
9ed0fac8 804 sbInOnIdle = FALSE;
0e320a79
DW
805}
806
9779893b
RD
807void wxWakeUpIdle()
808{
809 // **** please implement me! ****
810 // Wake up the idle handler processor, even if it is in another thread...
811}
812
0e320a79
DW
813// Send idle event to all top-level windows
814bool wxApp::SendIdleEvents()
815{
9ed0fac8
DW
816 bool bNeedMore = FALSE;
817 wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst();
d88de032 818
9ed0fac8 819 while (pNode)
d88de032 820 {
9ed0fac8 821 wxWindow* pWin = pNode->GetData();
0e320a79 822
9ed0fac8
DW
823 if (SendIdleEvents(pWin))
824 bNeedMore = TRUE;
825 pNode = pNode->GetNext();
826 }
827 return bNeedMore;
0e320a79
DW
828}
829
9ed0fac8 830//
0e320a79 831// Send idle event to window and all subwindows
9ed0fac8
DW
832//
833bool wxApp::SendIdleEvents(
834 wxWindow* pWin
835)
0e320a79 836{
9ed0fac8
DW
837 bool bNeedMore = FALSE;
838 wxIdleEvent vEvent;
839
840 vEvent.SetEventObject(pWin);
841 pWin->GetEventHandler()->ProcessEvent(vEvent);
0e320a79 842
9ed0fac8
DW
843 if (vEvent.MoreRequested())
844 bNeedMore = TRUE;
0e320a79 845
9ed0fac8 846 wxNode* pNode = pWin->GetChildren().First();
0e320a79 847
9ed0fac8 848 while (pNode)
d88de032 849 {
9ed0fac8 850 wxWindow* pWin = (wxWindow*) pNode->Data();
0e320a79 851
9ed0fac8
DW
852 if (SendIdleEvents(pWin))
853 bNeedMore = TRUE;
854 pNode = pNode->Next();
d88de032 855 }
9ed0fac8 856 return bNeedMore;
0e320a79
DW
857}
858
859void wxApp::DeletePendingObjects()
860{
9ed0fac8
DW
861 wxNode* pNode = wxPendingDelete.First();
862
863 while (pNode)
d88de032 864 {
9ed0fac8 865 wxObject* pObj = (wxObject *)pNode->Data();
77cd51c3 866
9ed0fac8 867 delete pObj;
0e320a79 868
9ed0fac8
DW
869 if (wxPendingDelete.Member(pObj))
870 delete pNode;
0e320a79 871
9ed0fac8 872 //
d88de032
DW
873 // Deleting one object may have deleted other pending
874 // objects, so start from beginning of list again.
9ed0fac8
DW
875 //
876 pNode = wxPendingDelete.First();
d88de032 877 }
0e320a79
DW
878}
879
9ed0fac8
DW
880void wxApp::OnEndSession(
881 wxCloseEvent& WXUNUSED(rEvent))
0e320a79 882{
d88de032
DW
883 if (GetTopWindow())
884 GetTopWindow()->Close(TRUE);
0e320a79
DW
885}
886
9ed0fac8 887//
d88de032
DW
888// Default behaviour: close the application with prompts. The
889// user can veto the close, and therefore the end session.
9ed0fac8
DW
890//
891void wxApp::OnQueryEndSession(
892 wxCloseEvent& rEvent
893)
0e320a79 894{
d88de032
DW
895 if (GetTopWindow())
896 {
9ed0fac8
DW
897 if (!GetTopWindow()->Close(!rEvent.CanVeto()))
898 rEvent.Veto(TRUE);
d88de032 899 }
0e320a79
DW
900}
901
902void wxExit()
903{
d88de032
DW
904 wxLogError(_("Fatal error: exiting"));
905
906 wxApp::CleanUp();
0e320a79
DW
907}
908
d88de032 909// Yield to incoming messages
0e320a79
DW
910bool wxYield()
911{
9dea36ef 912 HAB vHab = 0;
dde11e60 913 QMSG vMsg;
d88de032
DW
914 // We want to go back to the main message loop
915 // if we see a WM_QUIT. (?)
dde11e60 916 while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT)
d88de032 917 {
dde11e60 918 if (!wxTheApp->DoMessage())
d88de032
DW
919 break;
920 }
921 // If they are pending events, we must process them.
922#if wxUSE_THREADS
923 wxTheApp->ProcessPendingEvents();
924#endif
d88de032
DW
925 return TRUE;
926}
927
9ed0fac8
DW
928wxIcon wxApp::GetStdIcon(
929 int nWhich
930) const
d88de032 931{
9ed0fac8 932 switch(nWhich)
d88de032
DW
933 {
934 case wxICON_INFORMATION:
935 return wxIcon("wxICON_INFO");
936
937 case wxICON_QUESTION:
938 return wxIcon("wxICON_QUESTION");
939
940 case wxICON_EXCLAMATION:
941 return wxIcon("wxICON_WARNING");
942
943 default:
944 wxFAIL_MSG(wxT("requested non existent standard icon"));
945 // still fall through
946
947 case wxICON_HAND:
948 return wxIcon("wxICON_ERROR");
949 }
d88de032
DW
950 return wxIcon("wxICON_ERROR");
951}
952
d88de032
DW
953HINSTANCE wxGetInstance()
954{
955 return wxhInstance;
956}
957
958void wxSetInstance(HINSTANCE hInst)
959{
960 wxhInstance = hInst;
0e320a79
DW
961}
962