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