]> git.saurik.com Git - wxWidgets.git/blob - src/os2/app.cpp
added correct temp folder path for mac classic
[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 // 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 ERRORID vError = 0L;
330 wxString sError;
331
332 if (!::WinRegisterClass( vHab
333 ,wxFrameClassName
334 ,wxFrameWndProc
335 ,CS_SIZEREDRAW | CS_SYNCPAINT
336 ,sizeof(ULONG)
337 ))
338 {
339 vError = ::WinGetLastError(vHab);
340 sError = wxPMErrorToStr(vError);
341 wxLogLastError(sError);
342 return FALSE;
343 }
344
345 if (!::WinRegisterClass( vHab
346 ,wxFrameClassNameNoRedraw
347 ,wxWndProc
348 ,0
349 ,sizeof(ULONG)
350 ))
351 {
352 vError = ::WinGetLastError(vHab);
353 sError = wxPMErrorToStr(vError);
354 wxLogLastError(sError);
355 return FALSE;
356 }
357
358 if (!::WinRegisterClass( vHab
359 ,wxMDIFrameClassName
360 ,wxWndProc
361 ,CS_SIZEREDRAW | CS_MOVENOTIFY | CS_SYNCPAINT
362 ,sizeof(ULONG)
363 ))
364 {
365 vError = ::WinGetLastError(vHab);
366 sError = wxPMErrorToStr(vError);
367 wxLogLastError(sError);
368 return FALSE;
369 }
370
371 if (!::WinRegisterClass( vHab
372 ,wxMDIFrameClassNameNoRedraw
373 ,wxWndProc
374 ,0
375 ,sizeof(ULONG)
376 ))
377 {
378 vError = ::WinGetLastError(vHab);
379 sError = wxPMErrorToStr(vError);
380 wxLogLastError(sError);
381 return FALSE;
382 }
383
384 if (!::WinRegisterClass( vHab
385 ,wxMDIChildFrameClassName
386 ,wxWndProc
387 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_SYNCPAINT | CS_HITTEST
388 ,sizeof(ULONG)
389 ))
390 {
391 vError = ::WinGetLastError(vHab);
392 sError = wxPMErrorToStr(vError);
393 wxLogLastError(sError);
394 return FALSE;
395 }
396
397 if (!::WinRegisterClass( vHab
398 ,wxMDIChildFrameClassNameNoRedraw
399 ,wxWndProc
400 ,CS_HITTEST
401 ,sizeof(ULONG)
402 ))
403 {
404 vError = ::WinGetLastError(vHab);
405 sError = wxPMErrorToStr(vError);
406 wxLogLastError(sError);
407 return FALSE;
408 }
409
410 if (!::WinRegisterClass( vHab
411 ,wxPanelClassName
412 ,wxWndProc
413 ,CS_MOVENOTIFY | CS_SIZEREDRAW | CS_HITTEST | CS_SAVEBITS | CS_SYNCPAINT
414 ,sizeof(ULONG)
415 ))
416 {
417 vError = ::WinGetLastError(vHab);
418 sError = wxPMErrorToStr(vError);
419 wxLogLastError(sError);
420 return FALSE;
421 }
422
423 if (!::WinRegisterClass( vHab
424 ,wxCanvasClassName
425 ,wxWndProc
426 ,CS_SIZEREDRAW | CS_HITTEST | CS_SYNCPAINT | CS_CLIPCHILDREN
427 ,sizeof(ULONG)
428 ))
429 {
430 vError = ::WinGetLastError(vHab);
431 sError = wxPMErrorToStr(vError);
432 wxLogLastError(sError);
433 return FALSE;
434 }
435 return TRUE;
436 } // end of wxApp::RegisterWindowClasses
437
438 //
439 // Cleans up any wxWindows internal structures left lying around
440 //
441 void wxApp::CleanUp()
442 {
443 //
444 // COMMON CLEANUP
445 //
446
447 #if wxUSE_LOG
448
449 //
450 // Flush the logged messages if any and install a 'safer' log target: the
451 // default one (wxLogGui) can't be used after the resources are freed just
452 // below and the user suppliedo ne might be even more unsafe (using any
453 // wxWindows GUI function is unsafe starting from now)
454 //
455 wxLog::DontCreateOnDemand();
456
457 //
458 // This will flush the old messages if any
459 //
460 delete wxLog::SetActiveTarget(new wxLogStderr);
461 #endif // wxUSE_LOG
462
463 //
464 // One last chance for pending objects to be cleaned up
465 //
466 wxTheApp->DeletePendingObjects();
467
468 wxModule::CleanUpModules();
469
470 #if wxUSE_WX_RESOURCES
471 wxCleanUpResourceSystem();
472 #endif
473
474 wxDeleteStockObjects();
475
476 //
477 // Destroy all GDI lists, etc.
478 //
479 wxDeleteStockLists();
480
481 delete wxTheColourDatabase;
482 wxTheColourDatabase = NULL;
483
484 wxBitmap::CleanUpHandlers();
485
486 delete[] wxBuffer;
487 wxBuffer = NULL;
488
489 //
490 // PM-SPECIFIC CLEANUP
491 //
492
493 // wxSetKeyboardHook(FALSE);
494
495 if (wxSTD_FRAME_ICON)
496 ::WinFreeFileIcon(wxSTD_FRAME_ICON);
497 if (wxSTD_MDICHILDFRAME_ICON)
498 ::WinFreeFileIcon(wxSTD_MDICHILDFRAME_ICON);
499 if (wxSTD_MDIPARENTFRAME_ICON)
500 ::WinFreeFileIcon(wxSTD_MDIPARENTFRAME_ICON);
501
502 if (wxDEFAULT_FRAME_ICON)
503 ::WinFreeFileIcon(wxDEFAULT_FRAME_ICON);
504 if (wxDEFAULT_MDICHILDFRAME_ICON)
505 ::WinFreeFileIcon(wxDEFAULT_MDICHILDFRAME_ICON);
506 if (wxDEFAULT_MDIPARENTFRAME_ICON)
507 ::WinFreeFileIcon(wxDEFAULT_MDIPARENTFRAME_ICON);
508
509 if ( wxDisableButtonBrush )
510 {
511 // TODO: ::DeleteObject( wxDisableButtonBrush );
512 }
513
514 if (wxWinHandleList)
515 delete wxWinHandleList;
516
517 delete wxPendingEvents;
518 #if wxUSE_THREADS
519 delete wxPendingEventsLocker;
520 // If we don't do the following, we get an apparent memory leak.
521 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
522 #endif
523
524 wxClassInfo::CleanUpClasses();
525
526 // Delete Message queue
527 if (wxTheApp->m_hMq)
528 ::WinDestroyMsgQueue(wxTheApp->m_hMq);
529
530 delete wxTheApp;
531 wxTheApp = NULL;
532
533 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
534 // At this point we want to check if there are any memory
535 // blocks that aren't part of the wxDebugContext itself,
536 // as a special case. Then when dumping we need to ignore
537 // wxDebugContext, too.
538 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
539 {
540 wxLogDebug(wxT("There were memory leaks."));
541 wxDebugContext::Dump();
542 wxDebugContext::PrintStatistics();
543 }
544 // wxDebugContext::SetStream(NULL, NULL);
545 #endif
546
547 #if wxUSE_LOG
548 // do it as the very last thing because everything else can log messages
549 delete wxLog::SetActiveTarget(NULL);
550 #endif // wxUSE_LOG
551 } // end of wxApp::CleanUp
552
553 //----------------------------------------------------------------------
554 // Main wxWindows entry point
555 //----------------------------------------------------------------------
556 int wxEntry(
557 int argc
558 , char* argv[]
559 )
560 {
561 HAB vHab = 0;
562
563 if (!wxApp::Initialize(vHab))
564 return 0;
565
566 //
567 // create the application object or ensure that one already exists
568 //
569 if (!wxTheApp)
570 {
571 // The app may have declared a global application object, but we recommend
572 // the IMPLEMENT_APP macro is used instead, which sets an initializer
573 // function for delayed, dynamic app object construction.
574 wxCHECK_MSG( wxApp::GetInitializerFunction(), 0,
575 wxT("No initializer - use IMPLEMENT_APP macro.") );
576 wxTheApp = (*wxApp::GetInitializerFunction()) ();
577 }
578 wxCHECK_MSG( wxTheApp, 0, wxT("You have to define an instance of wxApp!") );
579 wxTheApp->argc = argc;
580
581 #if wxUSE_UNICODE
582 wxTheApp->argv = new wxChar*[argc+1];
583
584 int nArgc = 0;
585
586 while (nArgc < argc)
587 {
588 wxTheApp->argv[nArgc] = wxStrdup(wxConvLibc.cMB2WX(argv[nArgc]));
589 nArgc++;
590 }
591 wxTheApp->argv[nArgc] = (wxChar *)NULL;
592 #else
593 wxTheApp->argv = argv;
594 #endif
595
596 wxString sName(wxFileNameFromPath(argv[0]));
597
598 wxStripExtension(sName);
599 wxTheApp->SetAppName(sName);
600
601 int nRetValue = 0;
602
603 if (!wxTheApp->OnInitGui())
604 nRetValue = -1;
605
606 if (nRetValue == 0)
607 {
608 if (wxTheApp->OnInit())
609 {
610 nRetValue = wxTheApp->OnRun();
611 }
612 // Normal exit
613 wxWindow* pTopWindow = wxTheApp->GetTopWindow();
614 if (pTopWindow)
615 {
616 // Forcibly delete the window.
617 if (pTopWindow->IsKindOf(CLASSINFO(wxFrame)) ||
618 pTopWindow->IsKindOf(CLASSINFO(wxDialog)) )
619 {
620 pTopWindow->Close(TRUE);
621 wxTheApp->DeletePendingObjects();
622 }
623 else
624 {
625 delete pTopWindow;
626 wxTheApp->SetTopWindow(NULL);
627 }
628 }
629 }
630 else // app initialization failed
631 {
632 wxLogLastError(" Gui initialization failed, exitting");
633 }
634 #if wxUSE_CONSOLEDEBUG
635 printf("wxTheApp->OnExit ");
636 fflush(stdout);
637 #endif
638 wxTheApp->OnExit();
639 #if wxUSE_CONSOLEDEBUG
640 printf("wxApp::CleanUp ");
641 fflush(stdout);
642 #endif
643 wxApp::CleanUp();
644 #if wxUSE_CONSOLEDEBUG
645 printf("return %i ", nRetValue);
646 fflush(stdout);
647 #endif
648 return(nRetValue);
649 } // end of wxEntry
650
651 bool wxApp::OnInitGui()
652 {
653 ERRORID vError;
654 wxString sError;
655
656 if (!wxAppBase::OnInitGui())
657 return FALSE;
658
659 m_hMq = ::WinCreateMsgQueue(vHabmain, 0);
660 if (!m_hMq)
661 {
662 vError = ::WinGetLastError(vHabmain);
663 sError = wxPMErrorToStr(vError);
664 wxLogDebug(sError);
665 return FALSE;
666 }
667
668 return TRUE;
669 } // end of wxApp::OnInitGui
670
671 //
672 // Static member initialization
673 //
674 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
675
676 wxApp::wxApp()
677 {
678 m_topWindow = NULL;
679 wxTheApp = this;
680 m_wantDebugOutput = TRUE;
681
682 argc = 0;
683 argv = NULL;
684 m_nPrintMode = wxPRINT_WINDOWS;
685 m_exitOnFrameDelete = TRUE;
686 m_bAuto3D = TRUE;
687 m_hMq = 0;
688 m_maxSocketHandles = 0;
689 m_maxSocketNr = 0;
690 m_sockCallbackInfo = 0;
691 } // end of wxApp::wxApp
692
693 wxApp::~wxApp()
694 {
695 //
696 // Delete command-line args
697 //
698 #if wxUSE_UNICODE
699 int i;
700
701 for (i = 0; i < argc; i++)
702 {
703 delete[] argv[i];
704 }
705 delete[] argv;
706 #endif
707 } // end of wxApp::~wxApp
708
709 bool wxApp::Initialized()
710 {
711 if (GetTopWindow())
712 return TRUE;
713 else
714 return FALSE;
715 } // end of wxApp::Initialized
716
717 //
718 // Get and process a message, returning FALSE if WM_QUIT
719 // received (and also set the flag telling the app to exit the main loop)
720 //
721
722 bool wxApp::DoMessage()
723 {
724 BOOL bRc = ::WinGetMsg(vHabmain, &svCurrentMsg, HWND(NULL), 0, 0);
725
726 if (bRc == 0)
727 {
728 // got WM_QUIT
729 m_bKeepGoing = FALSE;
730 return FALSE;
731 }
732 else if (bRc == -1)
733 {
734 // should never happen, but let's test for it nevertheless
735 wxLogLastError("GetMessage");
736 }
737 else
738 {
739 #if wxUSE_THREADS
740 wxASSERT_MSG( wxThread::IsMain()
741 ,wxT("only the main thread can process Windows messages")
742 );
743
744 static bool sbHadGuiLock = TRUE;
745 static wxMsgArray svSavedMessages;
746
747 //
748 // If a secondary thread owns is doing GUI calls, save all messages for
749 // later processing - we can't process them right now because it will
750 // lead to recursive library calls (and we're not reentrant)
751 //
752 if (!wxGuiOwnedByMainThread())
753 {
754 sbHadGuiLock = FALSE;
755
756 //
757 // Leave out WM_COMMAND messages: too dangerous, sometimes
758 // the message will be processed twice
759 //
760 if ( !wxIsWaitingForThread() ||
761 svCurrentMsg.msg != WM_COMMAND )
762 {
763 svSavedMessages.Add(svCurrentMsg);
764 }
765 return TRUE;
766 }
767 else
768 {
769 //
770 // Have we just regained the GUI lock? if so, post all of the saved
771 // messages
772 //
773 if (!sbHadGuiLock )
774 {
775 sbHadGuiLock = TRUE;
776
777 size_t nCount = svSavedMessages.Count();
778
779 for (size_t n = 0; n < nCount; n++)
780 {
781 QMSG vMsg = svSavedMessages[n];
782
783 if ( !ProcessMessage((WXMSG *)&vMsg) )
784 {
785 ::WinDispatchMsg(vHabmain, &vMsg);
786 }
787 }
788 svSavedMessages.Empty();
789 }
790 }
791 #endif // wxUSE_THREADS
792
793 //
794 // Process the message
795 //
796 DoMessage((WXMSG *)&svCurrentMsg);
797 }
798 return TRUE;
799 } // end of wxApp::DoMessage
800
801 void wxApp::DoMessage(
802 WXMSG* pMsg
803 )
804 {
805 if (!ProcessMessage((WXMSG *)&svCurrentMsg))
806 {
807 ::WinDispatchMsg(vHabmain, (PQMSG)&svCurrentMsg);
808 }
809 } // end of wxApp::DoMessage
810
811 //////////////////////////////////////////////////////////////////////////////
812 //
813 // Keep trying to process messages until WM_QUIT
814 // received.
815 //
816 // If there are messages to be processed, they will all be
817 // processed and OnIdle will not be called.
818 // When there are no more messages, OnIdle is called.
819 // If OnIdle requests more time,
820 // it will be repeatedly called so long as there are no pending messages.
821 // A 'feature' of this is that once OnIdle has decided that no more processing
822 // is required, then it won't get processing time until further messages
823 // are processed (it'll sit in DoMessage).
824 //
825 //////////////////////////////////////////////////////////////////////////////
826 int wxApp::MainLoop()
827 {
828 m_bKeepGoing = TRUE;
829
830 while (m_bKeepGoing)
831 {
832 #if wxUSE_THREADS
833 wxMutexGuiLeaveOrEnter();
834 #endif // wxUSE_THREADS
835 while (!Pending() && ProcessIdle())
836 {
837 HandleSockets();
838 wxUsleep(10000);
839 }
840 HandleSockets();
841 if (Pending())
842 DoMessage();
843 else
844 wxUsleep(10000);
845
846 }
847 return (int)svCurrentMsg.mp1;
848 } // end of wxApp::MainLoop
849
850 //
851 // Returns TRUE if more time is needed.
852 //
853 bool wxApp::ProcessIdle()
854 {
855 wxIdleEvent vEvent;
856
857 vEvent.SetEventObject(this);
858 ProcessEvent(vEvent);
859 return vEvent.MoreRequested();
860 } // end of wxApp::ProcessIdle
861
862 void wxApp::ExitMainLoop()
863 {
864 m_bKeepGoing = FALSE;
865 }
866
867 bool wxApp::Pending()
868 {
869 return (::WinPeekMsg(vHabmain, (PQMSG)&svCurrentMsg, (HWND)NULL, 0, 0, PM_NOREMOVE) != 0);
870 }
871
872 void wxApp::Dispatch()
873 {
874 DoMessage();
875 }
876
877 //////////////////////////////////////////////////////////////////////////////
878 //
879 // Give all windows a chance to preprocess
880 // the message. Some may have accelerator tables, or have
881 // MDI complications.
882 //
883 //////////////////////////////////////////////////////////////////////////////
884 bool wxApp::ProcessMessage(
885 WXMSG* pWxmsg
886 )
887 {
888 QMSG* pMsg = (PQMSG)pWxmsg;
889 HWND hWnd = pMsg->hwnd;
890 wxWindow* pWndThis = wxFindWinFromHandle((WXHWND)hWnd);
891 wxWindow* pWnd;
892
893 #if wxUSE_TOOLTIPS
894 //
895 // We must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
896 // popup the tooltip bubbles
897 //
898 if (pWndThis && (pMsg->msg == WM_MOUSEMOVE))
899 {
900 wxToolTip* pToolTip = pWndThis->GetToolTip();
901 if (pToolTip)
902 {
903 pToolTip->RelayEvent(pWxmsg);
904 }
905 }
906 #endif // wxUSE_TOOLTIPS
907
908 //
909 // We must relay Timer events to wxTimer's processing function
910 //
911 if (pMsg->msg == WM_TIMER)
912 wxTimerProc(NULL, 0, (int)pMsg->mp1, 0);
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