More WinCE mods
[wxWidgets.git] / src / msw / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose: wxApp
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "app.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/frame.h"
33 #include "wx/app.h"
34 #include "wx/utils.h"
35 #include "wx/gdicmn.h"
36 #include "wx/pen.h"
37 #include "wx/brush.h"
38 #include "wx/cursor.h"
39 #include "wx/icon.h"
40 #include "wx/palette.h"
41 #include "wx/dc.h"
42 #include "wx/dialog.h"
43 #include "wx/msgdlg.h"
44 #include "wx/intl.h"
45 #include "wx/dynarray.h"
46 #include "wx/wxchar.h"
47 #include "wx/icon.h"
48 #include "wx/log.h"
49 #endif
50
51 #include "wx/apptrait.h"
52 #include "wx/filename.h"
53 #include "wx/module.h"
54
55 #include "wx/msw/private.h"
56
57 #if wxUSE_THREADS
58 #include "wx/thread.h"
59
60 // define the array of MSG strutures
61 WX_DECLARE_OBJARRAY(MSG, wxMsgArray);
62
63 #include "wx/arrimpl.cpp"
64
65 WX_DEFINE_OBJARRAY(wxMsgArray);
66 #endif // wxUSE_THREADS
67
68 #if wxUSE_TOOLTIPS
69 #include "wx/tooltip.h"
70 #endif // wxUSE_TOOLTIPS
71
72 // OLE is used for drag-and-drop, clipboard, OLE Automation..., but some
73 // compilers don't support it (missing headers, libs, ...)
74 #if defined(__GNUWIN32_OLD__) || defined(__SYMANTEC__) || defined(__SALFORDC__)
75 #undef wxUSE_OLE
76
77 #define wxUSE_OLE 0
78 #endif // broken compilers
79
80 #if wxUSE_OLE
81 #include <ole2.h>
82 #endif
83
84 #include <string.h>
85 #include <ctype.h>
86
87 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__WXMICROWIN__)) && !defined(__CYGWIN10__))
88 #include <commctrl.h>
89 #endif
90
91 // ----------------------------------------------------------------------------
92 // conditional compilation
93 // ----------------------------------------------------------------------------
94
95 // The macro _WIN32_IE is defined by commctrl.h (unless it had already been
96 // defined before) and shows us what common control features are available
97 // during the compile time (it doesn't mean that they will be available during
98 // the run-time, use GetComCtl32Version() to test for them!). The possible
99 // values are:
100 //
101 // 0x0200 for comctl32.dll 4.00 shipped with Win95/NT 4.0
102 // 0x0300 4.70 IE 3.x
103 // 0x0400 4.71 IE 4.0
104 // 0x0401 4.72 IE 4.01 and Win98
105 // 0x0500 5.00 IE 5.x and NT 5.0 (Win2000)
106
107 #ifndef _WIN32_IE
108 // minimal set of features by default
109 #define _WIN32_IE 0x0200
110 #endif
111
112 #if _WIN32_IE >= 0x0300 && \
113 (!defined(__MINGW32__) || wxCHECK_W32API_VERSION( 2, 0 )) && \
114 !defined(__CYGWIN__) && !defined(__WXWINCE__)
115 #include <shlwapi.h>
116 #endif
117
118 // ---------------------------------------------------------------------------
119 // global variables
120 // ---------------------------------------------------------------------------
121
122 extern wxList WXDLLEXPORT wxPendingDelete;
123
124 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
125 extern void wxSetKeyboardHook(bool doIt);
126 #endif
127
128 MSG s_currentMsg;
129
130 // NB: all "NoRedraw" classes must have the same names as the "normal" classes
131 // with NR suffix - wxWindow::MSWCreate() supposes this
132 const wxChar *wxCanvasClassName = wxT("wxWindowClass");
133 const wxChar *wxCanvasClassNameNR = wxT("wxWindowClassNR");
134 const wxChar *wxMDIFrameClassName = wxT("wxMDIFrameClass");
135 const wxChar *wxMDIFrameClassNameNoRedraw = wxT("wxMDIFrameClassNR");
136 const wxChar *wxMDIChildFrameClassName = wxT("wxMDIChildFrameClass");
137 const wxChar *wxMDIChildFrameClassNameNoRedraw = wxT("wxMDIChildFrameClassNR");
138
139 HBRUSH wxDisableButtonBrush = (HBRUSH) 0;
140
141 // ----------------------------------------------------------------------------
142 // private functions
143 // ----------------------------------------------------------------------------
144
145 LRESULT WXDLLEXPORT APIENTRY wxWndProc(HWND, UINT, WPARAM, LPARAM);
146
147 // ===========================================================================
148 // wxGUIAppTraits implementation
149 // ===========================================================================
150
151 // private class which we use to pass parameters from BeforeChildWaitLoop() to
152 // AfterChildWaitLoop()
153 struct ChildWaitLoopData
154 {
155 ChildWaitLoopData(wxWindowDisabler *wd_, wxWindow *winActive_)
156 {
157 wd = wd_;
158 winActive = winActive_;
159 }
160
161 wxWindowDisabler *wd;
162 wxWindow *winActive;
163 };
164
165 void *wxGUIAppTraits::BeforeChildWaitLoop()
166 {
167 /*
168 We use a dirty hack here to disable all application windows (which we
169 must do because otherwise the calls to wxYield() could lead to some very
170 unexpected reentrancies in the users code) but to avoid losing
171 focus/activation entirely when the child process terminates which would
172 happen if we simply disabled everything using wxWindowDisabler. Indeed,
173 remember that Windows will never activate a disabled window and when the
174 last childs window is closed and Windows looks for a window to activate
175 all our windows are still disabled. There is no way to enable them in
176 time because we don't know when the childs windows are going to be
177 closed, so the solution we use here is to keep one special tiny frame
178 enabled all the time. Then when the child terminates it will get
179 activated and when we close it below -- after reenabling all the other
180 windows! -- the previously active window becomes activated again and
181 everything is ok.
182 */
183 wxBeginBusyCursor();
184
185 // first disable all existing windows
186 wxWindowDisabler *wd = new wxWindowDisabler;
187
188 // then create an "invisible" frame: it has minimal size, is positioned
189 // (hopefully) outside the screen and doesn't appear on the taskbar
190 wxWindow *winActive = new wxFrame
191 (
192 wxTheApp->GetTopWindow(),
193 wxID_ANY,
194 wxEmptyString,
195 wxPoint(32600, 32600),
196 wxSize(1, 1),
197 wxDEFAULT_FRAME_STYLE | wxFRAME_NO_TASKBAR
198 );
199 winActive->Show();
200
201 return new ChildWaitLoopData(wd, winActive);
202 }
203
204 void wxGUIAppTraits::AlwaysYield()
205 {
206 wxYield();
207 }
208
209 void wxGUIAppTraits::AfterChildWaitLoop(void *dataOrig)
210 {
211 wxEndBusyCursor();
212
213 const ChildWaitLoopData * const data = (ChildWaitLoopData *)dataOrig;
214
215 delete data->wd;
216
217 // finally delete the dummy frame and, as wd has been already destroyed and
218 // the other windows reenabled, the activation is going to return to the
219 // window which had had it before
220 data->winActive->Destroy();
221 }
222
223 bool wxGUIAppTraits::DoMessageFromThreadWait()
224 {
225 return !wxTheApp || wxTheApp->DoMessage();
226 }
227
228 // ===========================================================================
229 // wxApp implementation
230 // ===========================================================================
231
232 int wxApp::m_nCmdShow = SW_SHOWNORMAL;
233
234 // ---------------------------------------------------------------------------
235 // wxWin macros
236 // ---------------------------------------------------------------------------
237
238 IMPLEMENT_DYNAMIC_CLASS(wxApp, wxEvtHandler)
239
240 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
241 EVT_IDLE(wxApp::OnIdle)
242 EVT_END_SESSION(wxApp::OnEndSession)
243 EVT_QUERY_END_SESSION(wxApp::OnQueryEndSession)
244 END_EVENT_TABLE()
245
246 // class to ensure that wxAppBase::CleanUp() is called if our Initialize()
247 // fails
248 class wxCallBaseCleanup
249 {
250 public:
251 wxCallBaseCleanup(wxApp *app) : m_app(app) { }
252 ~wxCallBaseCleanup() { if ( m_app ) m_app->wxAppBase::CleanUp(); }
253
254 void Dismiss() { m_app = NULL; }
255
256 private:
257 wxApp *m_app;
258 };
259
260 //// Initialize
261 bool wxApp::Initialize(int& argc, wxChar **argv)
262 {
263 if ( !wxAppBase::Initialize(argc, argv) )
264 return false;
265
266 // ensure that base cleanup is done if we return too early
267 wxCallBaseCleanup callBaseCleanup(this);
268
269 // the first thing to do is to check if we're trying to run an Unicode
270 // program under Win9x w/o MSLU emulation layer - if so, abort right now
271 // as it has no chance to work
272 #if wxUSE_UNICODE && !wxUSE_UNICODE_MSLU
273 if ( wxGetOsVersion() != wxWINDOWS_NT && wxGetOsVersion() != wxWINDOWS_CE )
274 {
275 // note that we can use MessageBoxW() as it's implemented even under
276 // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs
277 // used by wxLocale are not
278 ::MessageBox
279 (
280 NULL,
281 _T("This program uses Unicode and requires Windows NT/2000/XP/CE.\nProgram aborted."),
282 _T("wxWindows Fatal Error"),
283 MB_ICONERROR | MB_OK
284 );
285
286 return FALSE;
287 }
288 #endif // wxUSE_UNICODE && !wxUSE_UNICODE_MSLU
289
290 #if defined(__WIN95__) && !defined(__WXMICROWIN__)
291 InitCommonControls();
292 #endif // __WIN95__
293
294 #if wxUSE_OLE || wxUSE_DRAG_AND_DROP
295
296 #ifdef __WIN16__
297 // for OLE, enlarge message queue to be as large as possible
298 int iMsg = 96;
299 while (!SetMessageQueue(iMsg) && (iMsg -= 8))
300 ;
301 #endif // Win16
302
303 #if wxUSE_OLE
304 // we need to initialize OLE library
305 #ifdef __WXWINCE__
306 if ( FAILED(::CoInitializeEx(NULL, COINIT_MULTITHREADED)) )
307 wxLogError(_("Cannot initialize OLE"));
308 #else
309 if ( FAILED(::OleInitialize(NULL)) )
310 wxLogError(_("Cannot initialize OLE"));
311 #endif
312 #endif
313
314 #endif // wxUSE_OLE
315
316 #if wxUSE_CTL3D
317 if (!Ctl3dRegister(wxhInstance))
318 wxLogError(wxT("Cannot register CTL3D"));
319
320 Ctl3dAutoSubclass(wxhInstance);
321 #endif // wxUSE_CTL3D
322
323 RegisterWindowClasses();
324
325 #if defined(__WXMICROWIN__) && !defined(__WXWINCE__)
326 // Create the brush for disabling bitmap buttons
327
328 LOGBRUSH lb;
329 lb.lbStyle = BS_PATTERN;
330 lb.lbColor = 0;
331 lb.lbHatch = (int)LoadBitmap( wxhInstance, wxT("wxDISABLE_BUTTON_BITMAP") );
332 if ( lb.lbHatch )
333 {
334 wxDisableButtonBrush = ::CreateBrushIndirect( & lb );
335 ::DeleteObject( (HGDIOBJ)lb.lbHatch );
336 }
337 //else: wxWindows resources are probably not linked in
338 #endif
339
340 #if wxUSE_PENWINDOWS
341 wxRegisterPenWin();
342 #endif
343
344 wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100);
345
346 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
347 // PLEASE DO NOT ALTER THIS.
348 #if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL)
349 extern char wxDummyChar;
350 if (wxDummyChar) wxDummyChar++;
351 #endif
352
353 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
354 wxSetKeyboardHook(TRUE);
355 #endif
356
357 callBaseCleanup.Dismiss();
358
359 return true;
360 }
361
362 // ---------------------------------------------------------------------------
363 // RegisterWindowClasses
364 // ---------------------------------------------------------------------------
365
366 // TODO we should only register classes really used by the app. For this it
367 // would be enough to just delay the class registration until an attempt
368 // to create a window of this class is made.
369 bool wxApp::RegisterWindowClasses()
370 {
371 WNDCLASS wndclass;
372 wxZeroMemory(wndclass);
373
374 // for each class we register one with CS_(V|H)REDRAW style and one
375 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
376 static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
377 static const long styleNoRedraw = CS_DBLCLKS;
378
379 // the fields which are common to all classes
380 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
381 wndclass.hInstance = wxhInstance;
382 wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW);
383
384 // Register the frame window class.
385 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
386 wndclass.lpszClassName = wxCanvasClassName;
387 wndclass.style = styleNormal;
388
389 if ( !RegisterClass(&wndclass) )
390 {
391 wxLogLastError(wxT("RegisterClass(frame)"));
392 }
393
394 // "no redraw" frame
395 wndclass.lpszClassName = wxCanvasClassNameNR;
396 wndclass.style = styleNoRedraw;
397
398 if ( !RegisterClass(&wndclass) )
399 {
400 wxLogLastError(wxT("RegisterClass(no redraw frame)"));
401 }
402
403 // Register the MDI frame window class.
404 wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves
405 wndclass.lpszClassName = wxMDIFrameClassName;
406 wndclass.style = styleNormal;
407
408 if ( !RegisterClass(&wndclass) )
409 {
410 wxLogLastError(wxT("RegisterClass(MDI parent)"));
411 }
412
413 // "no redraw" MDI frame
414 wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw;
415 wndclass.style = styleNoRedraw;
416
417 if ( !RegisterClass(&wndclass) )
418 {
419 wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
420 }
421
422 // Register the MDI child frame window class.
423 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
424 wndclass.lpszClassName = wxMDIChildFrameClassName;
425 wndclass.style = styleNormal;
426
427 if ( !RegisterClass(&wndclass) )
428 {
429 wxLogLastError(wxT("RegisterClass(MDI child)"));
430 }
431
432 // "no redraw" MDI child frame
433 wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw;
434 wndclass.style = styleNoRedraw;
435
436 if ( !RegisterClass(&wndclass) )
437 {
438 wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
439 }
440
441 return TRUE;
442 }
443
444 // ---------------------------------------------------------------------------
445 // UnregisterWindowClasses
446 // ---------------------------------------------------------------------------
447
448 bool wxApp::UnregisterWindowClasses()
449 {
450 bool retval = TRUE;
451
452 #ifndef __WXMICROWIN__
453 // MDI frame window class.
454 if ( !::UnregisterClass(wxMDIFrameClassName, wxhInstance) )
455 {
456 wxLogLastError(wxT("UnregisterClass(MDI parent)"));
457
458 retval = FALSE;
459 }
460
461 // "no redraw" MDI frame
462 if ( !::UnregisterClass(wxMDIFrameClassNameNoRedraw, wxhInstance) )
463 {
464 wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
465
466 retval = FALSE;
467 }
468
469 // MDI child frame window class.
470 if ( !::UnregisterClass(wxMDIChildFrameClassName, wxhInstance) )
471 {
472 wxLogLastError(wxT("UnregisterClass(MDI child)"));
473
474 retval = FALSE;
475 }
476
477 // "no redraw" MDI child frame
478 if ( !::UnregisterClass(wxMDIChildFrameClassNameNoRedraw, wxhInstance) )
479 {
480 wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
481
482 retval = FALSE;
483 }
484
485 // canvas class name
486 if ( !::UnregisterClass(wxCanvasClassName, wxhInstance) )
487 {
488 wxLogLastError(wxT("UnregisterClass(canvas)"));
489
490 retval = FALSE;
491 }
492
493 if ( !::UnregisterClass(wxCanvasClassNameNR, wxhInstance) )
494 {
495 wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
496
497 retval = FALSE;
498 }
499 #endif // __WXMICROWIN__
500
501 return retval;
502 }
503
504 void wxApp::CleanUp()
505 {
506 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
507 wxSetKeyboardHook(FALSE);
508 #endif
509
510 #if wxUSE_PENWINDOWS
511 wxCleanUpPenWin();
512 #endif
513
514 if ( wxDisableButtonBrush )
515 ::DeleteObject( wxDisableButtonBrush );
516
517 #if wxUSE_OLE
518 #ifdef __WXWINCE__
519 ::CoUninitialize();
520 #else
521 ::OleUninitialize();
522 #endif
523 #endif
524
525 // for an EXE the classes are unregistered when it terminates but DLL may
526 // be loaded several times (load/unload/load) into the same process in
527 // which case the registration will fail after the first time if we don't
528 // unregister the classes now
529 UnregisterWindowClasses();
530
531 #if wxUSE_CTL3D
532 Ctl3dUnregister(wxhInstance);
533 #endif
534
535 delete wxWinHandleHash;
536 wxWinHandleHash = NULL;
537
538 wxAppBase::CleanUp();
539 }
540
541 // ----------------------------------------------------------------------------
542 // wxApp ctor/dtor
543 // ----------------------------------------------------------------------------
544
545 wxApp::wxApp()
546 {
547 m_printMode = wxPRINT_WINDOWS;
548 }
549
550 wxApp::~wxApp()
551 {
552 // our cmd line arguments are allocated inside wxEntry(HINSTANCE), they
553 // don't come from main(), so we have to free them
554
555 while ( argc )
556 {
557 // m_argv elements were allocated by wxStrdup()
558 free(argv[--argc]);
559 }
560
561 // but m_argv itself -- using new[]
562 delete [] argv;
563 }
564
565 bool wxApp::Initialized()
566 {
567 #ifndef _WINDLL
568 if (GetTopWindow())
569 return TRUE;
570 else
571 return FALSE;
572 #else // Assume initialized if DLL (no way of telling)
573 return TRUE;
574 #endif
575 }
576
577 /*
578 * Get and process a message, returning FALSE if WM_QUIT
579 * received (and also set the flag telling the app to exit the main loop)
580 *
581 */
582 bool wxApp::DoMessage()
583 {
584 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
585 if ( rc == 0 )
586 {
587 // got WM_QUIT
588 m_keepGoing = FALSE;
589
590 return FALSE;
591 }
592 else if ( rc == -1 )
593 {
594 // should never happen, but let's test for it nevertheless
595 wxLogLastError(wxT("GetMessage"));
596 }
597 else
598 {
599 #if wxUSE_THREADS
600 wxASSERT_MSG( wxThread::IsMain(),
601 wxT("only the main thread can process Windows messages") );
602
603 static bool s_hadGuiLock = TRUE;
604 static wxMsgArray s_aSavedMessages;
605
606 // if a secondary thread owns is doing GUI calls, save all messages for
607 // later processing - we can't process them right now because it will
608 // lead to recursive library calls (and we're not reentrant)
609 if ( !wxGuiOwnedByMainThread() )
610 {
611 s_hadGuiLock = FALSE;
612
613 // leave out WM_COMMAND messages: too dangerous, sometimes
614 // the message will be processed twice
615 if ( !wxIsWaitingForThread() ||
616 s_currentMsg.message != WM_COMMAND )
617 {
618 s_aSavedMessages.Add(s_currentMsg);
619 }
620
621 return TRUE;
622 }
623 else
624 {
625 // have we just regained the GUI lock? if so, post all of the saved
626 // messages
627 //
628 // FIXME of course, it's not _exactly_ the same as processing the
629 // messages normally - expect some things to break...
630 if ( !s_hadGuiLock )
631 {
632 s_hadGuiLock = TRUE;
633
634 size_t count = s_aSavedMessages.GetCount();
635 for ( size_t n = 0; n < count; n++ )
636 {
637 MSG& msg = s_aSavedMessages[n];
638
639 DoMessage((WXMSG *)&msg);
640 }
641
642 s_aSavedMessages.Empty();
643 }
644 }
645 #endif // wxUSE_THREADS
646
647 // Process the message
648 DoMessage((WXMSG *)&s_currentMsg);
649 }
650
651 return TRUE;
652 }
653
654 void wxApp::DoMessage(WXMSG *pMsg)
655 {
656 if ( !ProcessMessage(pMsg) )
657 {
658 ::TranslateMessage((MSG *)pMsg);
659 ::DispatchMessage((MSG *)pMsg);
660 }
661 }
662
663 /*
664 * Keep trying to process messages until WM_QUIT
665 * received.
666 *
667 * If there are messages to be processed, they will all be
668 * processed and OnIdle will not be called.
669 * When there are no more messages, OnIdle is called.
670 * If OnIdle requests more time,
671 * it will be repeatedly called so long as there are no pending messages.
672 * A 'feature' of this is that once OnIdle has decided that no more processing
673 * is required, then it won't get processing time until further messages
674 * are processed (it'll sit in DoMessage).
675 */
676
677 int wxApp::MainLoop()
678 {
679 m_keepGoing = TRUE;
680
681 while ( m_keepGoing )
682 {
683 #if wxUSE_THREADS
684 wxMutexGuiLeaveOrEnter();
685 #endif // wxUSE_THREADS
686
687 while ( !Pending() && ProcessIdle() )
688 ;
689
690 // a message came or no more idle processing to do
691 DoMessage();
692 }
693
694 return s_currentMsg.wParam;
695 }
696
697 void wxApp::ExitMainLoop()
698 {
699 // this will set m_keepGoing to FALSE a bit later
700 ::PostQuitMessage(0);
701 }
702
703 bool wxApp::Pending()
704 {
705 return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0;
706 }
707
708 void wxApp::Dispatch()
709 {
710 DoMessage();
711 }
712
713 /*
714 * Give all windows a chance to preprocess
715 * the message. Some may have accelerator tables, or have
716 * MDI complications.
717 */
718
719 bool wxApp::ProcessMessage(WXMSG *wxmsg)
720 {
721 MSG *msg = (MSG *)wxmsg;
722 HWND hwnd = msg->hwnd;
723 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
724
725 // this may happen if the event occured in a standard modeless dialog (the
726 // only example of which I know of is the find/replace dialog) - then call
727 // IsDialogMessage() to make TAB navigation in it work
728 if ( !wndThis )
729 {
730 // we need to find the dialog containing this control as
731 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
732 // them) if we call it for the control itself
733 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
734 {
735 hwnd = ::GetParent(hwnd);
736 }
737
738 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
739 }
740
741 #if wxUSE_TOOLTIPS
742 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
743 // popup the tooltip bubbles
744 if ( (msg->message == WM_MOUSEMOVE) )
745 {
746 wxToolTip *tt = wndThis->GetToolTip();
747 if ( tt )
748 {
749 tt->RelayEvent(wxmsg);
750 }
751 }
752 #endif // wxUSE_TOOLTIPS
753
754 // allow the window to prevent certain messages from being
755 // translated/processed (this is currently used by wxTextCtrl to always
756 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
757 if ( !wndThis->MSWShouldPreProcessMessage(wxmsg) )
758 {
759 return FALSE;
760 }
761
762 // try translations first: the accelerators override everything
763 wxWindow *wnd;
764
765 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
766 {
767 if ( wnd->MSWTranslateMessage(wxmsg))
768 return TRUE;
769
770 // stop at first top level window, i.e. don't try to process the key
771 // strokes originating in a dialog using the accelerators of the parent
772 // frame - this doesn't make much sense
773 if ( wnd->IsTopLevel() )
774 break;
775 }
776
777 // now try the other hooks (kbd navigation is handled here): we start from
778 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
779 // called above
780 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
781 {
782 if ( wnd->MSWProcessMessage(wxmsg) )
783 return TRUE;
784 }
785
786 // no special preprocessing for this message, dispatch it normally
787 return FALSE;
788 }
789
790 // this is a temporary hack and will be replaced by using wxEventLoop in the
791 // future
792 //
793 // it is needed to allow other event loops (currently only one: the modal
794 // dialog one) to reset the OnIdle() semaphore because otherwise OnIdle()
795 // wouldn't do anything while a modal dialog shown from OnIdle() call is shown.
796 bool wxIsInOnIdleFlag = FALSE;
797
798 void wxApp::OnIdle(wxIdleEvent& event)
799 {
800 // Avoid recursion (via ProcessEvent default case)
801 if ( wxIsInOnIdleFlag )
802 return;
803
804 wxIsInOnIdleFlag = TRUE;
805
806 // If there are pending events, we must process them: pending events
807 // are either events to the threads other than main or events posted
808 // with wxPostEvent() functions
809 // GRG: I have moved this here so that all pending events are processed
810 // before starting to delete any objects. This behaves better (in
811 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
812 // behaviour. Changed Feb/2000 before 2.1.14
813 ProcessPendingEvents();
814
815 // 'Garbage' collection of windows deleted with Close().
816 DeletePendingObjects();
817
818 #if wxUSE_LOG
819 // flush the logged messages if any
820 wxLog::FlushActive();
821 #endif // wxUSE_LOG
822
823 #if wxUSE_DC_CACHEING
824 // automated DC cache management: clear the cached DCs and bitmap
825 // if it's likely that the app has finished with them, that is, we
826 // get an idle event and we're not dragging anything.
827 if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON))
828 wxDC::ClearCache();
829 #endif // wxUSE_DC_CACHEING
830
831 // Send OnIdle events to all windows
832 if ( SendIdleEvents() )
833 {
834 // SendIdleEvents() returns TRUE if at least one window requested more
835 // idle events
836 event.RequestMore(TRUE);
837 }
838
839 wxIsInOnIdleFlag = FALSE;
840 }
841
842 void wxApp::WakeUpIdle()
843 {
844 // Send the top window a dummy message so idle handler processing will
845 // start up again. Doing it this way ensures that the idle handler
846 // wakes up in the right thread (see also wxWakeUpMainThread() which does
847 // the same for the main app thread only)
848 wxWindow *topWindow = wxTheApp->GetTopWindow();
849 if ( topWindow )
850 {
851 if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
852 {
853 // should never happen
854 wxLogLastError(wxT("PostMessage(WM_NULL)"));
855 }
856 }
857 }
858
859 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
860 {
861 if (GetTopWindow())
862 GetTopWindow()->Close(TRUE);
863 }
864
865 // Default behaviour: close the application with prompts. The
866 // user can veto the close, and therefore the end session.
867 void wxApp::OnQueryEndSession(wxCloseEvent& event)
868 {
869 if (GetTopWindow())
870 {
871 if (!GetTopWindow()->Close(!event.CanVeto()))
872 event.Veto(TRUE);
873 }
874 }
875
876 typedef struct _WXADllVersionInfo
877 {
878 DWORD cbSize;
879 DWORD dwMajorVersion; // Major version
880 DWORD dwMinorVersion; // Minor version
881 DWORD dwBuildNumber; // Build number
882 DWORD dwPlatformID; // DLLVER_PLATFORM_*
883 } WXADLLVERSIONINFO;
884
885 typedef HRESULT (CALLBACK* WXADLLGETVERSIONPROC)(WXADLLVERSIONINFO *);
886
887 /* static */
888 int wxApp::GetComCtl32Version()
889 {
890 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
891 return 0;
892 #else
893 // cache the result
894 static int s_verComCtl32 = -1;
895
896 wxCRIT_SECT_DECLARE(csComCtl32);
897 wxCRIT_SECT_LOCKER(lock, csComCtl32);
898
899 if ( s_verComCtl32 == -1 )
900 {
901 // initally assume no comctl32.dll at all
902 s_verComCtl32 = 0;
903
904 // do we have it?
905 HMODULE hModuleComCtl32 = ::GetModuleHandle(wxT("COMCTL32"));
906 BOOL bFreeComCtl32 = FALSE ;
907 if(!hModuleComCtl32)
908 {
909 hModuleComCtl32 = ::LoadLibrary(wxT("COMCTL32.DLL")) ;
910 if(hModuleComCtl32)
911 {
912 bFreeComCtl32 = TRUE ;
913 }
914 }
915
916 // if so, then we can check for the version
917 if ( hModuleComCtl32 )
918 {
919 // try to use DllGetVersion() if available in _headers_
920 WXADLLGETVERSIONPROC pfnDllGetVersion = (WXADLLGETVERSIONPROC)
921 ::GetProcAddress(hModuleComCtl32, "DllGetVersion");
922 if ( pfnDllGetVersion )
923 {
924 WXADLLVERSIONINFO dvi;
925 dvi.cbSize = sizeof(dvi);
926
927 HRESULT hr = (*pfnDllGetVersion)(&dvi);
928 if ( FAILED(hr) )
929 {
930 wxLogApiError(_T("DllGetVersion"), hr);
931 }
932 else
933 {
934 // this is incompatible with _WIN32_IE values, but
935 // compatible with the other values returned by
936 // GetComCtl32Version()
937 s_verComCtl32 = 100*dvi.dwMajorVersion +
938 dvi.dwMinorVersion;
939 }
940 }
941 // DllGetVersion() unavailable either during compile or
942 // run-time, try to guess the version otherwise
943 if ( !s_verComCtl32 )
944 {
945 // InitCommonControlsEx is unique to 4.70 and later
946 FARPROC theProc = ::GetProcAddress
947 (
948 hModuleComCtl32,
949 "InitCommonControlsEx"
950 );
951
952 if ( !theProc )
953 {
954 // not found, must be 4.00
955 s_verComCtl32 = 400;
956 }
957 else
958 {
959 // many symbols appeared in comctl32 4.71, could use
960 // any of them except may be DllInstall
961 theProc = ::GetProcAddress
962 (
963 hModuleComCtl32,
964 "InitializeFlatSB"
965 );
966 if ( !theProc )
967 {
968 // not found, must be 4.70
969 s_verComCtl32 = 470;
970 }
971 else
972 {
973 // found, must be 4.71
974 s_verComCtl32 = 471;
975 }
976 }
977 }
978 }
979
980 if(bFreeComCtl32)
981 {
982 ::FreeLibrary(hModuleComCtl32) ;
983 }
984 }
985
986 return s_verComCtl32;
987 #endif
988 }
989
990 // Yield to incoming messages
991
992 bool wxApp::Yield(bool onlyIfNeeded)
993 {
994 // MT-FIXME
995 static bool s_inYield = FALSE;
996
997 #if wxUSE_LOG
998 // disable log flushing from here because a call to wxYield() shouldn't
999 // normally result in message boxes popping up &c
1000 wxLog::Suspend();
1001 #endif // wxUSE_LOG
1002
1003 if ( s_inYield )
1004 {
1005 if ( !onlyIfNeeded )
1006 {
1007 wxFAIL_MSG( wxT("wxYield called recursively" ) );
1008 }
1009
1010 return FALSE;
1011 }
1012
1013 s_inYield = TRUE;
1014
1015 // we don't want to process WM_QUIT from here - it should be processed in
1016 // the main event loop in order to stop it
1017 MSG msg;
1018 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
1019 msg.message != WM_QUIT )
1020 {
1021 #if wxUSE_THREADS
1022 wxMutexGuiLeaveOrEnter();
1023 #endif // wxUSE_THREADS
1024
1025 if ( !wxTheApp->DoMessage() )
1026 break;
1027 }
1028
1029 // if there are pending events, we must process them.
1030 ProcessPendingEvents();
1031
1032 #if wxUSE_LOG
1033 // let the logs be flashed again
1034 wxLog::Resume();
1035 #endif // wxUSE_LOG
1036
1037 s_inYield = FALSE;
1038
1039 return TRUE;
1040 }
1041