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