]> git.saurik.com Git - wxWidgets.git/blame - src/os2/app.cpp
removed unused variables
[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
272ebf16
SN
12#ifdef __GNUG__
13 #pragma implementation "app.h"
14#endif
15
d88de032
DW
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifndef WX_PRECOMP
20 #include "wx/frame.h"
21 #include "wx/app.h"
22 #include "wx/utils.h"
23 #include "wx/gdicmn.h"
24 #include "wx/pen.h"
25 #include "wx/brush.h"
26 #include "wx/cursor.h"
27 #include "wx/icon.h"
28 #include "wx/palette.h"
29 #include "wx/dc.h"
30 #include "wx/dialog.h"
31 #include "wx/msgdlg.h"
32 #include "wx/intl.h"
33 #include "wx/dynarray.h"
cb21bd1b
SN
34 #include "wx/wxchar.h"
35 #include "wx/icon.h"
36 #include "wx/timer.h"
0e320a79
DW
37#endif
38
0e320a79
DW
39#include "wx/log.h"
40#include "wx/module.h"
d88de032
DW
41
42#include "wx/os2/private.h"
43
29d83fc1
DW
44#ifdef __EMX__
45
19193a2c
KB
46#include <sys/ioctl.h>
47#include <sys/select.h>
29d83fc1
DW
48
49#else
50
65b851bb 51#include <nerrno.h>
19193a2c
KB
52#include <sys/ioctl.h>
53#include <sys/select.h>
54#include <sys/time.h>
29d83fc1 55
19193a2c 56#endif //
29d83fc1 57
3958ae62 58#ifndef __EMX__
29d83fc1 59
3958ae62 60#define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
19193a2c
KB
61extern "C" int _System bsdselect(int,
62 struct fd_set *,
63 struct fd_set *,
64 struct fd_set *,
65 struct timeval *);
3958ae62
SN
66#endif
67
d88de032
DW
68#if wxUSE_THREADS
69 #include "wx/thread.h"
70
9ed0fac8
DW
71 // define the array of QMSG strutures
72 WX_DECLARE_OBJARRAY(QMSG, wxMsgArray);
d88de032
DW
73
74 #include "wx/arrimpl.cpp"
75
9ed0fac8 76 WX_DEFINE_OBJARRAY(wxMsgArray);
d88de032 77#endif // wxUSE_THREADS
0e320a79 78
8df85a61
DW
79#if wxUSE_TOOLTIPS
80 #include "wx/tooltip.h"
81#endif // wxUSE_TOOLTIPS
82
0e320a79 83#include <string.h>
d88de032
DW
84#include <ctype.h>
85
86// ---------------------------------------------------------------------------
87// global variables
88// ---------------------------------------------------------------------------
89
9ed0fac8 90extern wxChar* wxBuffer;
9ed0fac8
DW
91extern wxList* wxWinHandleList;
92extern wxList WXDLLEXPORT wxPendingDelete;
9ed0fac8 93extern wxCursor* g_globalCursor;
0e320a79 94
8df85a61 95HAB vHabmain = NULLHANDLE;
9ed0fac8
DW
96QMSG svCurrentMsg;
97wxApp* wxTheApp = NULL;
d88de032 98
d88de032 99
9ed0fac8
DW
100HICON wxSTD_FRAME_ICON = (HICON) NULL;
101HICON wxSTD_MDICHILDFRAME_ICON = (HICON) NULL;
d88de032
DW
102HICON wxSTD_MDIPARENTFRAME_ICON = (HICON) NULL;
103
9ed0fac8
DW
104HICON wxDEFAULT_FRAME_ICON = (HICON) NULL;
105HICON wxDEFAULT_MDICHILDFRAME_ICON = (HICON) NULL;
106HICON wxDEFAULT_MDIPARENTFRAME_ICON = (HICON) NULL;
d88de032
DW
107
108HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
109
51c1d535
DW
110MRESULT EXPENTRY wxWndProc( HWND hWnd,ULONG message,MPARAM mp1,MPARAM mp2);
111MRESULT EXPENTRY wxFrameWndProc( HWND hWnd,ULONG message,MPARAM mp1,MPARAM mp2);
112
d88de032
DW
113// ===========================================================================
114// implementation
115// ===========================================================================
116
3958ae62
SN
117// ---------------------------------------------------------------------------
118// helper struct and functions for socket handling
119// ---------------------------------------------------------------------------
120
121struct GsocketCallbackInfo{
122 void (*proc)(void *);
123 int type;
124 int handle;
125 void* gsock;
126};
127
128// These defines and wrapper functions are used here and in gsockpm.c
129#define wxSockReadMask 0x01
130#define wxSockWriteMask 0x02
131
29d83fc1 132#ifdef __EMX__
3958ae62
SN
133extern "C"
134int wxAppAddSocketHandler(int handle, int mask,
29d83fc1 135 void (*callback)(void*), void * gsock)
3958ae62
SN
136{
137 return wxTheApp->AddSocketHandler(handle, mask, callback, gsock);
138}
3958ae62
SN
139extern "C"
140void wxAppRemoveSocketHandler(int handle)
141{
142 wxTheApp->RemoveSocketHandler(handle);
143}
29d83fc1
DW
144#else
145// Linkage mode problems using callbacks with extern C in a .cpp module
146int wxAppAddSocketHandler(int handle, int mask,
147 void (*callback)(void*), void * gsock)
148{
149 return wxTheApp->AddSocketHandler(handle, mask, callback, gsock);
150}
151void wxAppRemoveSocketHandler(int handle)
152{
153 wxTheApp->RemoveSocketHandler(handle);
154}
155#endif
3958ae62
SN
156
157void wxApp::HandleSockets()
158{
29d83fc1 159 bool pendingEvent = FALSE;
3958ae62
SN
160
161 // Check whether it's time for Gsocket operation
162 if (m_maxSocketHandles > 0 && m_maxSocketNr > 0)
163 {
164 fd_set readfds = m_readfds;
165 fd_set writefds = m_writefds;
166 struct timeval timeout;
167 int i;
168 struct GsocketCallbackInfo
169 *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
170 int r = 0;
171 timeout.tv_sec = 0;
172 timeout.tv_usec = 0;
173 if ( select(m_maxSocketNr, &readfds, &writefds, 0, &timeout) > 0)
174 {
175 for (i = m_lastUsedHandle + 1; i != m_lastUsedHandle; i++)
176 {
177 if (i == m_maxSocketNr)
178 i = 0;
179 if (FD_ISSET(i, &readfds))
180 {
181 int r;
182 for (r = 0; r < m_maxSocketHandles; r++){
183 if(CallbackInfo[r].handle == i &&
184 CallbackInfo[r].type == wxSockReadMask)
185 break;
186 }
187 if (r < m_maxSocketHandles)
188 {
189 CallbackInfo[r].proc(CallbackInfo[r].gsock);
29d83fc1 190 pendingEvent = TRUE;
3958ae62
SN
191 wxYield();
192 }
193 }
194 if (FD_ISSET(i, &writefds))
195 {
196 int r;
197 for (r = 0; r < m_maxSocketHandles; r++)
198 if(CallbackInfo[r].handle == i &&
199 CallbackInfo[r].type == wxSockWriteMask)
200 break;
201 if (r < m_maxSocketHandles)
202 {
203 CallbackInfo[r].proc(CallbackInfo[r].gsock);
29d83fc1 204 pendingEvent = TRUE;
3958ae62
SN
205 wxYield();
206 }
207 }
208 }
209 m_lastUsedHandle = i;
210 }
211 if (pendingEvent)
212 wxYield();
213 }
214}
d88de032
DW
215// ---------------------------------------------------------------------------
216// wxApp
217// ---------------------------------------------------------------------------
218
d88de032 219 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
0e320a79 220
d88de032
DW
221 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
222 EVT_IDLE(wxApp::OnIdle)
223 EVT_END_SESSION(wxApp::OnEndSession)
224 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
225 END_EVENT_TABLE()
0e320a79 226
8df85a61
DW
227//
228// Initialize
229//
9ed0fac8
DW
230bool wxApp::Initialize(
231 HAB vHab
232)
0e320a79 233{
7e99520b
DW
234#if defined(wxUSE_CONSOLEDEBUG)
235 #if wxUSE_CONSOLEDEBUG
236/***********************************************/
237/* Code for using stdout debug */
238/* To use it you mast link app as "Window" - EK*/
239/***********************************************/
240 {
241 PPIB pib;
242 PTIB tib;
243
244 printf("In console\n");
245
246 DosGetInfoBlocks(&tib, &pib);
247/* Try morphing into a PM application. */
248// if(pib->pib_ultype == 2) /* VIO */
249 pib->pib_ultype = 3;
250 }
251/**********************************************/
252/**********************************************/
253 #endif //wxUSE_CONSOLEDEBUG
254#endif
255
13a4ea8d 256 wxBuffer = new wxChar[1500]; // FIXME; why?
0e320a79 257
d88de032 258 wxClassInfo::InitializeClasses();
0e320a79 259
8df85a61
DW
260#if wxUSE_THREADS
261 wxPendingEventsLocker = new wxCriticalSection;
262#endif
263
d88de032
DW
264 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
265 wxTheColourDatabase->Initialize();
266
267 wxInitializeStockLists();
268 wxInitializeStockObjects();
0e320a79 269
d88de032
DW
270 wxBitmap::InitStandardHandlers();
271
54ffa107
DW
272 //
273 // OS2 has to have an anchorblock
274 //
275 vHab = WinInitialize(0);
276
277 if (!vHab)
278 return FALSE;
279 else
280 vHabmain = vHab;
281
282 // Some people may wish to use this, but
283 // probably it shouldn't be here by default.
284#ifdef __WXDEBUG__
285 // wxRedirectIOToConsole();
286#endif
287
d88de032
DW
288 wxWinHandleList = new wxList(wxKEY_INTEGER);
289
290 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
291 // PLEASE DO NOT ALTER THIS.
1b3d5e55 292#if !defined(WXMAKINGDLL) && defined(__VISAGECPP__)
d88de032
DW
293 extern char wxDummyChar;
294 if (wxDummyChar) wxDummyChar++;
295#endif
296
61243a51 297 // wxSetKeyboardHook(TRUE);
d88de032
DW
298
299 wxModule::RegisterModules();
300 if (!wxModule::InitializeModules())
301 return FALSE;
54ffa107 302 RegisterWindowClasses(vHab);
d88de032 303 return TRUE;
8df85a61 304} // end of wxApp::Initialize
d88de032 305
7e99520b 306const char* CANTREGISTERCLASS = " Can't register Class ";
d88de032
DW
307// ---------------------------------------------------------------------------
308// RegisterWindowClasses
309// ---------------------------------------------------------------------------
310
9ed0fac8
DW
311bool wxApp::RegisterWindowClasses(
312 HAB vHab
313)
d88de032 314{
914589c2
DW
315 ERRORID vError = 0L;
316 wxString sError;
3b9e3455 317
f23208ca
DW
318 if (!::WinRegisterClass( vHab
319 ,wxFrameClassName
51c1d535 320 ,wxFrameWndProc
f9efbe3a 321 ,CS_SIZEREDRAW | CS_SYNCPAINT
a0606634 322 ,sizeof(ULONG)
f23208ca 323 ))
d88de032 324 {
914589c2
DW
325 vError = ::WinGetLastError(vHab);
326 sError = wxPMErrorToStr(vError);
327 wxLogLastError(sError);
d88de032
DW
328 return FALSE;
329 }
330
f23208ca
DW
331 if (!::WinRegisterClass( vHab
332 ,wxFrameClassNameNoRedraw
51c1d535 333 ,wxWndProc
f23208ca 334 ,0
51c1d535 335 ,sizeof(ULONG)
f23208ca 336 ))
d88de032 337 {
914589c2
DW
338 vError = ::WinGetLastError(vHab);
339 sError = wxPMErrorToStr(vError);
340 wxLogLastError(sError);
d88de032
DW
341 return FALSE;
342 }
343
f23208ca
DW
344 if (!::WinRegisterClass( vHab
345 ,wxMDIFrameClassName
51c1d535 346 ,wxWndProc
f6bcfd97 347 ,CS_SIZEREDRAW | CS_MOVENOTIFY | CS_SYNCPAINT
51c1d535 348 ,sizeof(ULONG)
f23208ca 349 ))
d88de032 350 {
914589c2
DW
351 vError = ::WinGetLastError(vHab);
352 sError = wxPMErrorToStr(vError);
353 wxLogLastError(sError);
d88de032
DW
354 return FALSE;
355 }
0e320a79 356
f23208ca
DW
357 if (!::WinRegisterClass( vHab
358 ,wxMDIFrameClassNameNoRedraw
51c1d535 359 ,wxWndProc
f23208ca 360 ,0
51c1d535 361 ,sizeof(ULONG)
f23208ca 362 ))
d88de032 363 {
914589c2
DW
364 vError = ::WinGetLastError(vHab);
365 sError = wxPMErrorToStr(vError);
366 wxLogLastError(sError);
d88de032
DW
367 return FALSE;
368 }
369
f23208ca
DW
370 if (!::WinRegisterClass( vHab
371 ,wxMDIChildFrameClassName
51c1d535 372 ,wxWndProc
f23208ca 373 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST
51c1d535 374 ,sizeof(ULONG)
f23208ca 375 ))
d88de032 376 {
914589c2
DW
377 vError = ::WinGetLastError(vHab);
378 sError = wxPMErrorToStr(vError);
379 wxLogLastError(sError);
d88de032
DW
380 return FALSE;
381 }
382
f23208ca
DW
383 if (!::WinRegisterClass( vHab
384 ,wxMDIChildFrameClassNameNoRedraw
51c1d535 385 ,wxWndProc
f23208ca 386 ,CS_HITTEST
51c1d535 387 ,sizeof(ULONG)
f23208ca 388 ))
d88de032 389 {
914589c2
DW
390 vError = ::WinGetLastError(vHab);
391 sError = wxPMErrorToStr(vError);
392 wxLogLastError(sError);
d88de032
DW
393 return FALSE;
394 }
395
f23208ca
DW
396 if (!::WinRegisterClass( vHab
397 ,wxPanelClassName
51c1d535 398 ,wxWndProc
f23208ca 399 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT
51c1d535 400 ,sizeof(ULONG)
f23208ca 401 ))
d88de032 402 {
914589c2
DW
403 vError = ::WinGetLastError(vHab);
404 sError = wxPMErrorToStr(vError);
405 wxLogLastError(sError);
d88de032
DW
406 return FALSE;
407 }
408
f23208ca
DW
409 if (!::WinRegisterClass( vHab
410 ,wxCanvasClassName
51c1d535 411 ,wxWndProc
54ffa107 412 ,CS_SIZEREDRAW | CS_HITTEST | CS_SYNCPAINT
51c1d535 413 ,sizeof(ULONG)
f23208ca 414 ))
d88de032 415 {
914589c2
DW
416 vError = ::WinGetLastError(vHab);
417 sError = wxPMErrorToStr(vError);
418 wxLogLastError(sError);
d88de032
DW
419 return FALSE;
420 }
0256cfeb
DW
421 if (!::WinRegisterClass( vHab
422 ,wxCanvasClassNameNR
423 ,wxWndProc
424 ,CS_HITTEST | CS_SYNCPAINT
425 ,sizeof(ULONG)
426 ))
427 {
428 vError = ::WinGetLastError(vHab);
429 sError = wxPMErrorToStr(vError);
430 wxLogLastError(sError);
431 return FALSE;
432 }
d88de032 433 return TRUE;
8df85a61 434} // end of wxApp::RegisterWindowClasses
d88de032 435
8df85a61
DW
436//
437// Cleans up any wxWindows internal structures left lying around
438//
0e320a79
DW
439void wxApp::CleanUp()
440{
8df85a61
DW
441 //
442 // COMMON CLEANUP
443 //
d88de032
DW
444
445#if wxUSE_LOG
8df85a61
DW
446
447 //
448 // Flush the logged messages if any and install a 'safer' log target: the
d88de032
DW
449 // default one (wxLogGui) can't be used after the resources are freed just
450 // below and the user suppliedo ne might be even more unsafe (using any
451 // wxWindows GUI function is unsafe starting from now)
8df85a61 452 //
d88de032
DW
453 wxLog::DontCreateOnDemand();
454
8df85a61
DW
455 //
456 // This will flush the old messages if any
457 //
d88de032
DW
458 delete wxLog::SetActiveTarget(new wxLogStderr);
459#endif // wxUSE_LOG
460
8df85a61 461 //
d88de032 462 // One last chance for pending objects to be cleaned up
8df85a61 463 //
d88de032
DW
464 wxTheApp->DeletePendingObjects();
465
466 wxModule::CleanUpModules();
0e320a79 467
d88de032 468 wxDeleteStockObjects();
0e320a79 469
8df85a61 470 //
d88de032 471 // Destroy all GDI lists, etc.
8df85a61 472 //
d88de032 473 wxDeleteStockLists();
0e320a79 474
d88de032
DW
475 delete wxTheColourDatabase;
476 wxTheColourDatabase = NULL;
0e320a79 477
d88de032 478 wxBitmap::CleanUpHandlers();
0e320a79 479
d88de032
DW
480 delete[] wxBuffer;
481 wxBuffer = NULL;
0e320a79 482
8df85a61
DW
483 //
484 // PM-SPECIFIC CLEANUP
485 //
0e320a79 486
61243a51 487 // wxSetKeyboardHook(FALSE);
9ed0fac8 488
d88de032 489 if (wxSTD_FRAME_ICON)
9ed0fac8 490 ::WinFreeFileIcon(wxSTD_FRAME_ICON);
d88de032 491 if (wxSTD_MDICHILDFRAME_ICON)
9ed0fac8 492 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON);
d88de032 493 if (wxSTD_MDIPARENTFRAME_ICON)
9ed0fac8 494 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON);
d88de032
DW
495
496 if (wxDEFAULT_FRAME_ICON)
9ed0fac8 497 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON);
d88de032 498 if (wxDEFAULT_MDICHILDFRAME_ICON)
9ed0fac8 499 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON);
d88de032 500 if (wxDEFAULT_MDIPARENTFRAME_ICON)
9ed0fac8
DW
501 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
502
d88de032
DW
503 if ( wxDisableButtonBrush )
504 {
505// TODO: ::DeleteObject( wxDisableButtonBrush );
506 }
507
508 if (wxWinHandleList)
509 delete wxWinHandleList;
510
d88de032 511 delete wxPendingEvents;
8df85a61 512#if wxUSE_THREADS
d88de032
DW
513 delete wxPendingEventsLocker;
514 // If we don't do the following, we get an apparent memory leak.
515 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
516#endif
0e320a79 517
d88de032 518 wxClassInfo::CleanUpClasses();
0e320a79 519
468e327a
SN
520 // Delete Message queue
521 if (wxTheApp->m_hMq)
522 ::WinDestroyMsgQueue(wxTheApp->m_hMq);
523
d88de032
DW
524 delete wxTheApp;
525 wxTheApp = NULL;
77cd51c3 526
0e320a79 527#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
d88de032
DW
528 // At this point we want to check if there are any memory
529 // blocks that aren't part of the wxDebugContext itself,
530 // as a special case. Then when dumping we need to ignore
531 // wxDebugContext, too.
532 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
533 {
534 wxLogDebug(wxT("There were memory leaks."));
535 wxDebugContext::Dump();
536 wxDebugContext::PrintStatistics();
537 }
538 // wxDebugContext::SetStream(NULL, NULL);
0e320a79 539#endif
77cd51c3 540
d88de032
DW
541#if wxUSE_LOG
542 // do it as the very last thing because everything else can log messages
543 delete wxLog::SetActiveTarget(NULL);
544#endif // wxUSE_LOG
8df85a61 545} // end of wxApp::CleanUp
0e320a79 546
7e99520b
DW
547//----------------------------------------------------------------------
548// Main wxWindows entry point
549//----------------------------------------------------------------------
9ed0fac8
DW
550int wxEntry(
551 int argc
552, char* argv[]
553)
d88de032 554{
9dea36ef 555 HAB vHab = 0;
d88de032 556
9ed0fac8
DW
557 if (!wxApp::Initialize(vHab))
558 return 0;
d88de032 559
9ed0fac8
DW
560 //
561 // create the application object or ensure that one already exists
562 //
563 if (!wxTheApp)
564 {
426d5745
DW
565 // The app may have declared a global application object, but we recommend
566 // the IMPLEMENT_APP macro is used instead, which sets an initializer
567 // function for delayed, dynamic app object construction.
568 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
569 wxT("No initializer - use IMPLEMENT_APP macro.") );
570 wxTheApp = (*wxApp::GetInitializerFunction()) ();
9ed0fac8 571 }
426d5745 572 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
9ed0fac8 573 wxTheApp->argc = argc;
d88de032 574
9ed0fac8
DW
575#if wxUSE_UNICODE
576 wxTheApp->argv = new wxChar*[argc+1];
577
578 int nArgc = 0;
579
580 while (nArgc < argc)
581 {
582 wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc]));
583 nArgc++;
584 }
585 wxTheApp->argv[nArgc] = (wxChar *)NULL;
586#else
587 wxTheApp->argv = argv;
588#endif
d88de032 589
9ed0fac8 590 wxString sName(wxFileNameFromPath(argv[0]));
d88de032 591
9ed0fac8
DW
592 wxStripExtension(sName);
593 wxTheApp->SetAppName(sName);
d88de032 594
9ed0fac8 595 int nRetValue = 0;
d88de032 596
9ed0fac8
DW
597 if (!wxTheApp->OnInitGui())
598 nRetValue = -1;
d88de032 599
9ed0fac8
DW
600 if (nRetValue == 0)
601 {
602 if (wxTheApp->OnInit())
d88de032 603 {
2b5f62a0 604 wxTheApp->OnRun();
7bed7a50 605 }
51c1d535
DW
606 // Normal exit
607 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
608 if (pTopWindow)
d88de032 609 {
51c1d535
DW
610 // Forcibly delete the window.
611 if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) ||
612 pTopWindow->IsKindOf(CLASSINFO(wxDialog)) )
613 {
614 pTopWindow->Close(TRUE);
615 wxTheApp->DeletePendingObjects();
616 }
617 else
618 {
619 delete pTopWindow;
620 wxTheApp->SetTopWindow(NULL);
621 }
d88de032 622 }
9ed0fac8 623 }
7e99520b
DW
624 else // app initialization failed
625 {
626 wxLogLastError(" Gui initialization failed, exitting");
627 }
628#if wxUSE_CONSOLEDEBUG
629 printf("wxTheApp->OnExit ");
630 fflush(stdout);
631#endif
2b5f62a0 632 nRetValue = wxTheApp->OnExit();
7e99520b
DW
633#if wxUSE_CONSOLEDEBUG
634 printf("wxApp::CleanUp ");
635 fflush(stdout);
636#endif
9ed0fac8 637 wxApp::CleanUp();
7e99520b
DW
638#if wxUSE_CONSOLEDEBUG
639 printf("return %i ", nRetValue);
640 fflush(stdout);
641#endif
9ed0fac8 642 return(nRetValue);
8df85a61 643} // end of wxEntry
77cd51c3 644
9ed0fac8 645bool wxApp::OnInitGui()
d88de032 646{
914589c2
DW
647 ERRORID vError;
648 wxString sError;
77cd51c3 649
19193a2c
KB
650 if (!wxAppBase::OnInitGui())
651 return FALSE;
652
f23208ca 653 m_hMq = ::WinCreateMsgQueue(vHabmain, 0);
914589c2
DW
654 if (!m_hMq)
655 {
656 vError = ::WinGetLastError(vHabmain);
657 sError = wxPMErrorToStr(vError);
658 wxLogDebug(sError);
659 return FALSE;
660 }
19193a2c 661
9ed0fac8 662 return TRUE;
8df85a61 663} // end of wxApp::OnInitGui
0e320a79 664
9ed0fac8
DW
665//
666// Static member initialization
667//
d88de032 668wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
0e320a79
DW
669
670wxApp::wxApp()
671{
d88de032
DW
672 m_topWindow = NULL;
673 wxTheApp = this;
d88de032
DW
674
675 argc = 0;
676 argv = NULL;
9ed0fac8 677 m_nPrintMode = wxPRINT_WINDOWS;
9ed0fac8 678 m_bAuto3D = TRUE;
468e327a 679 m_hMq = 0;
3958ae62
SN
680 m_maxSocketHandles = 0;
681 m_maxSocketNr = 0;
682 m_sockCallbackInfo = 0;
8df85a61 683} // end of wxApp::wxApp
d88de032
DW
684
685wxApp::~wxApp()
686{
8df85a61 687 //
d88de032 688 // Delete command-line args
8df85a61 689 //
039bec17 690#if wxUSE_UNICODE
8df85a61
DW
691 int i;
692
d88de032
DW
693 for (i = 0; i < argc; i++)
694 {
695 delete[] argv[i];
696 }
697 delete[] argv;
039bec17 698#endif
8df85a61 699} // end of wxApp::~wxApp
0e320a79
DW
700
701bool wxApp::Initialized()
702{
d88de032
DW
703 if (GetTopWindow())
704 return TRUE;
705 else
706 return FALSE;
8df85a61 707} // end of wxApp::Initialized
0e320a79 708
9ed0fac8
DW
709//
710// Get and process a message, returning FALSE if WM_QUIT
711// received (and also set the flag telling the app to exit the main loop)
712//
3958ae62 713
d88de032
DW
714bool wxApp::DoMessage()
715{
468e327a 716 BOOL bRc = ::WinGetMsg(vHabmain, &svCurrentMsg, HWND(NULL), 0, 0);
9ed0fac8
DW
717
718 if (bRc == 0)
d88de032
DW
719 {
720 // got WM_QUIT
9ed0fac8 721 m_bKeepGoing = FALSE;
d88de032
DW
722 return FALSE;
723 }
9ed0fac8 724 else if (bRc == -1)
d88de032
DW
725 {
726 // should never happen, but let's test for it nevertheless
727 wxLogLastError("GetMessage");
728 }
729 else
730 {
731#if wxUSE_THREADS
9ed0fac8
DW
732 wxASSERT_MSG( wxThread::IsMain()
733 ,wxT("only the main thread can process Windows messages")
734 );
d88de032 735
9ed0fac8
DW
736 static bool sbHadGuiLock = TRUE;
737 static wxMsgArray svSavedMessages;
d88de032 738
9ed0fac8 739 //
8df85a61 740 // If a secondary thread owns is doing GUI calls, save all messages for
d88de032
DW
741 // later processing - we can't process them right now because it will
742 // lead to recursive library calls (and we're not reentrant)
9ed0fac8
DW
743 //
744 if (!wxGuiOwnedByMainThread())
d88de032 745 {
9ed0fac8 746 sbHadGuiLock = FALSE;
d88de032 747
8df85a61
DW
748 //
749 // Leave out WM_COMMAND messages: too dangerous, sometimes
d88de032 750 // the message will be processed twice
8df85a61 751 //
d88de032 752 if ( !wxIsWaitingForThread() ||
9ed0fac8 753 svCurrentMsg.msg != WM_COMMAND )
d88de032 754 {
9ed0fac8 755 svSavedMessages.Add(svCurrentMsg);
d88de032 756 }
d88de032
DW
757 return TRUE;
758 }
759 else
760 {
9ed0fac8 761 //
8df85a61 762 // Have we just regained the GUI lock? if so, post all of the saved
d88de032
DW
763 // messages
764 //
9ed0fac8 765 if (!sbHadGuiLock )
d88de032 766 {
9ed0fac8 767 sbHadGuiLock = TRUE;
d88de032 768
9ed0fac8
DW
769 size_t nCount = svSavedMessages.Count();
770
771 for (size_t n = 0; n < nCount; n++)
d88de032 772 {
9ed0fac8 773 QMSG vMsg = svSavedMessages[n];
d88de032 774
ed2b77fc 775 DoMessage((WXMSG*)&vMsg);
d88de032 776 }
9ed0fac8 777 svSavedMessages.Empty();
d88de032
DW
778 }
779 }
d88de032
DW
780#endif // wxUSE_THREADS
781
3febf684 782 //
d88de032 783 // Process the message
3febf684
DW
784 //
785 DoMessage((WXMSG *)&svCurrentMsg);
d88de032 786 }
d88de032 787 return TRUE;
8df85a61 788} // end of wxApp::DoMessage
d88de032 789
3febf684
DW
790void wxApp::DoMessage(
791 WXMSG* pMsg
792)
793{
794 if (!ProcessMessage((WXMSG *)&svCurrentMsg))
795 {
796 ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg);
797 }
798} // end of wxApp::DoMessage
799
9ed0fac8
DW
800//////////////////////////////////////////////////////////////////////////////
801//
802// Keep trying to process messages until WM_QUIT
803// received.
804//
805// If there are messages to be processed, they will all be
806// processed and OnIdle will not be called.
807// When there are no more messages, OnIdle is called.
808// If OnIdle requests more time,
809// it will be repeatedly called so long as there are no pending messages.
810// A 'feature' of this is that once OnIdle has decided that no more processing
811// is required, then it won't get processing time until further messages
812// are processed (it'll sit in DoMessage).
813//
814//////////////////////////////////////////////////////////////////////////////
0e320a79
DW
815int wxApp::MainLoop()
816{
9ed0fac8 817 m_bKeepGoing = TRUE;
d88de032 818
9ed0fac8 819 while (m_bKeepGoing)
d88de032
DW
820 {
821#if wxUSE_THREADS
822 wxMutexGuiLeaveOrEnter();
823#endif // wxUSE_THREADS
3958ae62 824 while (!Pending() && ProcessIdle())
5b3ed311 825 {
3958ae62
SN
826 HandleSockets();
827 wxUsleep(10000);
5b3ed311 828 }
3958ae62
SN
829 HandleSockets();
830 if (Pending())
831 DoMessage();
832 else
833 wxUsleep(10000);
834
d88de032 835 }
9ed0fac8 836 return (int)svCurrentMsg.mp1;
8df85a61 837} // end of wxApp::MainLoop
0e320a79 838
9ed0fac8 839//
0e320a79 840// Returns TRUE if more time is needed.
9ed0fac8 841//
0e320a79
DW
842bool wxApp::ProcessIdle()
843{
9ed0fac8 844 wxIdleEvent vEvent;
0e320a79 845
9ed0fac8
DW
846 vEvent.SetEventObject(this);
847 ProcessEvent(vEvent);
848 return vEvent.MoreRequested();
8df85a61 849} // end of wxApp::ProcessIdle
d88de032 850
0e320a79
DW
851void wxApp::ExitMainLoop()
852{
a23692f0
DW
853 ::WinPostMsg(NULL, WM_QUIT, 0, 0);
854} // end of wxApp::ExitMainLoop
0e320a79 855
0e320a79
DW
856bool wxApp::Pending()
857{
43543d98 858 return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0);
a23692f0 859} // end of wxApp::Pending
0e320a79 860
0e320a79
DW
861void wxApp::Dispatch()
862{
d88de032 863 DoMessage();
0e320a79
DW
864}
865
9ed0fac8
DW
866//////////////////////////////////////////////////////////////////////////////
867//
868// Give all windows a chance to preprocess
869// the message. Some may have accelerator tables, or have
870// MDI complications.
871//
872//////////////////////////////////////////////////////////////////////////////
873bool wxApp::ProcessMessage(
874 WXMSG* pWxmsg
875)
0e320a79 876{
f6bcfd97
BP
877 QMSG* pMsg = (PQMSG)pWxmsg;
878 HWND hWnd = pMsg->hwnd;
9ed0fac8
DW
879 wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
880 wxWindow* pWnd;
d88de032 881
05a8bfed 882 //
e58dab20 883 // Pass non-system timer messages to the wxTimerProc
05a8bfed 884 //
e58dab20
DW
885 if (pMsg->msg == WM_TIMER &&
886 (SHORT1FROMMP(pMsg->mp1) != TID_CURSOR &&
887 SHORT1FROMMP(pMsg->mp1) != TID_FLASHWINDOW &&
b3260bce
DW
888 SHORT1FROMMP(pMsg->mp1) != TID_SCROLL &&
889 SHORT1FROMMP(pMsg->mp1) != 0x0000
e58dab20 890 ))
496fd9fb 891 wxTimerProc(NULL, 0, (int)pMsg->mp1, 0);
05a8bfed 892
54ffa107
DW
893 //
894 // Allow the window to prevent certain messages from being
895 // translated/processed (this is currently used by wxTextCtrl to always
896 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
897 //
898 if (pWndThis && !pWndThis->OS2ShouldPreProcessMessage(pWxmsg))
899 {
900 return FALSE;
901 }
902
9ed0fac8 903 //
8df85a61 904 // For some composite controls (like a combobox), wndThis might be NULL
d88de032
DW
905 // because the subcontrol is not a wxWindow, but only the control itself
906 // is - try to catch this case
9ed0fac8
DW
907 //
908 while (hWnd && !pWndThis)
d88de032 909 {
9ed0fac8
DW
910 hWnd = ::WinQueryWindow(hWnd, QW_PARENT);
911 pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
d88de032 912 }
0e320a79 913
f6bcfd97
BP
914 //
915 // Try translations first; find the youngest window with
f09d8a3b
DW
916 // a translation table. OS/2 has case sensative accels, so
917 // this block, coded by BK, removes that and helps make them
918 // case insensative.
f6bcfd97 919 //
f09d8a3b 920 if(pMsg->msg == WM_CHAR)
f6bcfd97 921 {
f09d8a3b
DW
922 PBYTE pChmsg = (PBYTE)&(pMsg->msg);
923 USHORT uSch = CHARMSG(pChmsg)->chr;
924 bool bRc;
925
926 //
927 // Do not process keyup events
928 //
929 if(!(CHARMSG(pChmsg)->fs & KC_KEYUP))
930 {
931 if((CHARMSG(pChmsg)->fs & (KC_ALT | KC_CTRL)) && CHARMSG(pChmsg)->chr != 0)
f1f22049 932 CHARMSG(pChmsg)->chr = (USHORT)wxToupper((UCHAR)uSch);
f09d8a3b 933
7e99520b 934
5b3ed311
DW
935 for(pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent() )
936 {
937 if((bRc = pWnd->OS2TranslateMessage(pWxmsg)) == TRUE)
938 break;
939 }
7e99520b 940
f09d8a3b
DW
941 if(!bRc) // untranslated, should restore original value
942 CHARMSG(pChmsg)->chr = uSch;
943 }
f6bcfd97 944 }
8df85a61 945 //
d88de032 946 // Anyone for a non-translation message? Try youngest descendants first.
8df85a61 947 //
e604d44b
DW
948// for (pWnd = pWndThis; pWnd; pWnd = pWnd->GetParent())
949// {
950// if (pWnd->OS2ProcessMessage(pWxmsg))
951// return TRUE;
952// }
d88de032 953 return FALSE;
8df85a61 954} // end of wxApp::ProcessMessage
0e320a79 955
2b5f62a0
VZ
956bool gbInOnIdle = FALSE;
957
9ed0fac8
DW
958void wxApp::OnIdle(
959 wxIdleEvent& rEvent
960)
d88de032 961{
d88de032 962
9ed0fac8 963 //
d88de032 964 // Avoid recursion (via ProcessEvent default case)
9ed0fac8 965 //
2b5f62a0 966 if (gbInOnIdle)
d88de032 967 return;
0e320a79 968
2b5f62a0 969 gbInOnIdle = TRUE;
0e320a79 970
8df85a61
DW
971 //
972 // If there are pending events, we must process them: pending events
973 // are either events to the threads other than main or events posted
974 // with wxPostEvent() functions
975 //
976 ProcessPendingEvents();
977
9ed0fac8 978 //
d88de032 979 // 'Garbage' collection of windows deleted with Close().
9ed0fac8 980 //
d88de032 981 DeletePendingObjects();
0e320a79 982
d88de032 983#if wxUSE_LOG
8df85a61
DW
984 //
985 // Flush the logged messages if any
986 //
987 wxLog::FlushActive();
d88de032 988#endif // wxUSE_LOG
0e320a79 989
893758d5
DW
990#if wxUSE_DC_CACHEING
991 // automated DC cache management: clear the cached DCs and bitmap
992 // if it's likely that the app has finished with them, that is, we
993 // get an idle event and we're not dragging anything.
19193a2c
KB
994 if (!::WinGetKeyState(HWND_DESKTOP, VK_BUTTON1) &&
995 !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON3) &&
996 !::WinGetKeyState(HWND_DESKTOP, VK_BUTTON2))
893758d5
DW
997 wxDC::ClearCache();
998#endif // wxUSE_DC_CACHEING
999
8df85a61 1000 //
d88de032 1001 // Send OnIdle events to all windows
8df85a61 1002 //
9ed0fac8 1003 if (SendIdleEvents())
d88de032 1004 {
9ed0fac8 1005 //
d88de032
DW
1006 // SendIdleEvents() returns TRUE if at least one window requested more
1007 // idle events
9ed0fac8
DW
1008 //
1009 rEvent.RequestMore(TRUE);
d88de032 1010 }
2b5f62a0 1011 gbInOnIdle = FALSE;
8df85a61 1012} // end of wxApp::OnIdle
9779893b 1013
0e320a79
DW
1014// Send idle event to all top-level windows
1015bool wxApp::SendIdleEvents()
1016{
9ed0fac8
DW
1017 bool bNeedMore = FALSE;
1018 wxWindowList::Node* pNode = wxTopLevelWindows.GetFirst();
d88de032 1019
9ed0fac8 1020 while (pNode)
d88de032 1021 {
9ed0fac8 1022 wxWindow* pWin = pNode->GetData();
0e320a79 1023
9ed0fac8
DW
1024 if (SendIdleEvents(pWin))
1025 bNeedMore = TRUE;
1026 pNode = pNode->GetNext();
1027 }
1028 return bNeedMore;
8df85a61 1029} // end of wxApp::SendIdleEvents
0e320a79 1030
9ed0fac8 1031//
0e320a79 1032// Send idle event to window and all subwindows
9ed0fac8
DW
1033//
1034bool wxApp::SendIdleEvents(
1035 wxWindow* pWin
1036)
0e320a79 1037{
9ed0fac8
DW
1038 bool bNeedMore = FALSE;
1039 wxIdleEvent vEvent;
1040
1041 vEvent.SetEventObject(pWin);
1042 pWin->GetEventHandler()->ProcessEvent(vEvent);
0e320a79 1043
9ed0fac8
DW
1044 if (vEvent.MoreRequested())
1045 bNeedMore = TRUE;
0e320a79 1046
9ed0fac8 1047 wxNode* pNode = pWin->GetChildren().First();
0e320a79 1048
9ed0fac8 1049 while (pNode)
d88de032 1050 {
9ed0fac8 1051 wxWindow* pWin = (wxWindow*) pNode->Data();
0e320a79 1052
9ed0fac8
DW
1053 if (SendIdleEvents(pWin))
1054 bNeedMore = TRUE;
1055 pNode = pNode->Next();
d88de032 1056 }
9ed0fac8 1057 return bNeedMore;
8df85a61 1058} // end of wxApp::SendIdleEvents
0e320a79
DW
1059
1060void wxApp::DeletePendingObjects()
1061{
9ed0fac8
DW
1062 wxNode* pNode = wxPendingDelete.First();
1063
1064 while (pNode)
d88de032 1065 {
9ed0fac8 1066 wxObject* pObj = (wxObject *)pNode->Data();
77cd51c3 1067
9ed0fac8 1068 delete pObj;
0e320a79 1069
9ed0fac8
DW
1070 if (wxPendingDelete.Member(pObj))
1071 delete pNode;
0e320a79 1072
9ed0fac8 1073 //
d88de032
DW
1074 // Deleting one object may have deleted other pending
1075 // objects, so start from beginning of list again.
9ed0fac8
DW
1076 //
1077 pNode = wxPendingDelete.First();
d88de032 1078 }
8df85a61 1079} // end of wxApp::DeletePendingObjects
0e320a79 1080
9ed0fac8
DW
1081void wxApp::OnEndSession(
1082 wxCloseEvent& WXUNUSED(rEvent))
0e320a79 1083{
d88de032
DW
1084 if (GetTopWindow())
1085 GetTopWindow()->Close(TRUE);
8df85a61 1086} // end of wxApp::OnEndSession
0e320a79 1087
9ed0fac8 1088//
d88de032
DW
1089// Default behaviour: close the application with prompts. The
1090// user can veto the close, and therefore the end session.
9ed0fac8
DW
1091//
1092void wxApp::OnQueryEndSession(
1093 wxCloseEvent& rEvent
1094)
0e320a79 1095{
d88de032
DW
1096 if (GetTopWindow())
1097 {
9ed0fac8
DW
1098 if (!GetTopWindow()->Close(!rEvent.CanVeto()))
1099 rEvent.Veto(TRUE);
d88de032 1100 }
8df85a61 1101} // end of wxApp::OnQueryEndSession
0e320a79
DW
1102
1103void wxExit()
1104{
d88de032
DW
1105 wxLogError(_("Fatal error: exiting"));
1106
1107 wxApp::CleanUp();
ee453a16 1108} // end of wxExit
0e320a79 1109
8df85a61 1110//
d88de032 1111// Yield to incoming messages
8df85a61 1112//
8461e4c2 1113bool wxApp::Yield(bool onlyIfNeeded)
0e320a79 1114{
8461e4c2
VZ
1115 static bool s_inYield = FALSE;
1116
1117 if ( s_inYield )
1118 {
1119 if ( !onlyIfNeeded )
1120 {
1121 wxFAIL_MSG( _T("wxYield() called recursively") );
1122 }
1123
1124 return FALSE;
1125 }
1126
9dea36ef 1127 HAB vHab = 0;
dde11e60 1128 QMSG vMsg;
ee453a16 1129
8df85a61
DW
1130 //
1131 // Disable log flushing from here because a call to wxYield() shouldn't
1132 // normally result in message boxes popping up &c
1133 //
1134 wxLog::Suspend();
1135
8461e4c2 1136 s_inYield = TRUE;
8b63ae37 1137
8df85a61 1138 //
d88de032
DW
1139 // We want to go back to the main message loop
1140 // if we see a WM_QUIT. (?)
8df85a61 1141 //
dde11e60 1142 while (::WinPeekMsg(vHab, &vMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) && vMsg.msg != WM_QUIT)
d88de032 1143 {
8df85a61
DW
1144#if wxUSE_THREADS
1145 wxMutexGuiLeaveOrEnter();
1146#endif // wxUSE_THREADS
dde11e60 1147 if (!wxTheApp->DoMessage())
d88de032
DW
1148 break;
1149 }
8df85a61 1150 //
d88de032 1151 // If they are pending events, we must process them.
8df85a61
DW
1152 //
1153 if (wxTheApp)
1154 wxTheApp->ProcessPendingEvents();
1155
1156 //
1157 // Let the logs be flashed again
1158 //
1159 wxLog::Resume();
8461e4c2 1160 s_inYield = FALSE;
d88de032 1161 return TRUE;
8df85a61 1162} // end of wxYield
d88de032 1163
3958ae62
SN
1164int wxApp::AddSocketHandler(int handle, int mask,
1165 void (*callback)(void*), void * gsock)
1166{
1167 int find;
1168 struct GsocketCallbackInfo
1169 *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
1170
1171 for (find = 0; find < m_maxSocketHandles; find++)
1172 if (CallbackInfo[find].handle == -1)
1173 break;
1174 if (find == m_maxSocketHandles)
1175 {
1176 // Allocate new memory
1177 m_sockCallbackInfo = realloc(m_sockCallbackInfo,
1178 (m_maxSocketHandles+=10)*
1179 sizeof(struct GsocketCallbackInfo));
1180 CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
1181 for (find = m_maxSocketHandles - 10; find < m_maxSocketHandles; find++)
1182 CallbackInfo[find].handle = -1;
1183 find = m_maxSocketHandles - 10;
1184 }
1185 CallbackInfo[find].proc = callback;
1186 CallbackInfo[find].type = mask;
1187 CallbackInfo[find].handle = handle;
1188 CallbackInfo[find].gsock = gsock;
1189 if (mask & wxSockReadMask)
1190 FD_SET(handle, &m_readfds);
1191 if (mask & wxSockWriteMask)
1192 FD_SET(handle, &m_writefds);
1193 if (handle >= m_maxSocketNr)
1194 m_maxSocketNr = handle + 1;
1195 return find;
1196}
1197
1198void wxApp::RemoveSocketHandler(int handle)
1199{
1200 struct GsocketCallbackInfo
1201 *CallbackInfo = (struct GsocketCallbackInfo *)m_sockCallbackInfo;
1202 if (handle < m_maxSocketHandles)
1203 {
1204 if (CallbackInfo[handle].type & wxSockReadMask)
1205 FD_CLR(CallbackInfo[handle].handle, &m_readfds);
1206 if (CallbackInfo[handle].type & wxSockWriteMask)
1207 FD_CLR(CallbackInfo[handle].handle, &m_writefds);
1208 CallbackInfo[handle].handle = -1;
1209 }
1210}
1211
8df85a61
DW
1212//-----------------------------------------------------------------------------
1213// wxWakeUpIdle
1214//-----------------------------------------------------------------------------
1215
1216void wxWakeUpIdle()
1217{
1218 //
1219 // Send the top window a dummy message so idle handler processing will
1220 // start up again. Doing it this way ensures that the idle handler
1221 // wakes up in the right thread (see also wxWakeUpMainThread() which does
1222 // the same for the main app thread only)
1223 //
1224 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
1225
1226 if (pTopWindow)
1227 {
1228 if ( !::WinPostMsg(GetHwndOf(pTopWindow), WM_NULL, (MPARAM)0, (MPARAM)0))
1229 {
1230 //
1231 // Should never happen
1232 //
1233 wxLogLastError("PostMessage(WM_NULL)");
1234 }
1235 }
1236} // end of wxWakeUpIdle
d88de032 1237
76990f63 1238HAB wxGetInstance()
d88de032 1239{
76990f63 1240 return vHabmain;
d88de032
DW
1241}
1242
76990f63
DW
1243void wxSetInstance(
1244 HAB vHab
1245)
d88de032 1246{
76990f63 1247 vHabmain = vHab;
0e320a79
DW
1248}
1249