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 )
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.\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 if ( FAILED(::OleInitialize(NULL)) )
306 wxLogError(_("Cannot initialize OLE"));
307 #endif
308
309 #endif // wxUSE_OLE
310
311 #if wxUSE_CTL3D
312 if (!Ctl3dRegister(wxhInstance))
313 wxLogError(wxT("Cannot register CTL3D"));
314
315 Ctl3dAutoSubclass(wxhInstance);
316 #endif // wxUSE_CTL3D
317
318 RegisterWindowClasses();
319
320 #if defined(__WXMICROWIN__) && !defined(__WXWINCE__)
321 // Create the brush for disabling bitmap buttons
322
323 LOGBRUSH lb;
324 lb.lbStyle = BS_PATTERN;
325 lb.lbColor = 0;
326 lb.lbHatch = (int)LoadBitmap( wxhInstance, wxT("wxDISABLE_BUTTON_BITMAP") );
327 if ( lb.lbHatch )
328 {
329 wxDisableButtonBrush = ::CreateBrushIndirect( & lb );
330 ::DeleteObject( (HGDIOBJ)lb.lbHatch );
331 }
332 //else: wxWindows resources are probably not linked in
333 #endif
334
335 #if wxUSE_PENWINDOWS
336 wxRegisterPenWin();
337 #endif
338
339 wxWinHandleHash = new wxWinHashTable(wxKEY_INTEGER, 100);
340
341 // This is to foil optimizations in Visual C++ that throw out dummy.obj.
342 // PLEASE DO NOT ALTER THIS.
343 #if defined(__VISUALC__) && defined(__WIN16__) && !defined(WXMAKINGDLL)
344 extern char wxDummyChar;
345 if (wxDummyChar) wxDummyChar++;
346 #endif
347
348 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
349 wxSetKeyboardHook(TRUE);
350 #endif
351
352 callBaseCleanup.Dismiss();
353
354 return true;
355 }
356
357 // ---------------------------------------------------------------------------
358 // RegisterWindowClasses
359 // ---------------------------------------------------------------------------
360
361 // TODO we should only register classes really used by the app. For this it
362 // would be enough to just delay the class registration until an attempt
363 // to create a window of this class is made.
364 bool wxApp::RegisterWindowClasses()
365 {
366 WNDCLASS wndclass;
367 wxZeroMemory(wndclass);
368
369 // for each class we register one with CS_(V|H)REDRAW style and one
370 // without for windows created with wxNO_FULL_REDRAW_ON_REPAINT flag
371 static const long styleNormal = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
372 static const long styleNoRedraw = CS_DBLCLKS;
373
374 // the fields which are common to all classes
375 wndclass.lpfnWndProc = (WNDPROC)wxWndProc;
376 wndclass.hInstance = wxhInstance;
377 wndclass.hCursor = ::LoadCursor((HINSTANCE)NULL, IDC_ARROW);
378
379 // Register the frame window class.
380 wndclass.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE + 1);
381 wndclass.lpszClassName = wxCanvasClassName;
382 wndclass.style = styleNormal;
383
384 if ( !RegisterClass(&wndclass) )
385 {
386 wxLogLastError(wxT("RegisterClass(frame)"));
387 }
388
389 // "no redraw" frame
390 wndclass.lpszClassName = wxCanvasClassNameNR;
391 wndclass.style = styleNoRedraw;
392
393 if ( !RegisterClass(&wndclass) )
394 {
395 wxLogLastError(wxT("RegisterClass(no redraw frame)"));
396 }
397
398 // Register the MDI frame window class.
399 wndclass.hbrBackground = (HBRUSH)NULL; // paint MDI frame ourselves
400 wndclass.lpszClassName = wxMDIFrameClassName;
401 wndclass.style = styleNormal;
402
403 if ( !RegisterClass(&wndclass) )
404 {
405 wxLogLastError(wxT("RegisterClass(MDI parent)"));
406 }
407
408 // "no redraw" MDI frame
409 wndclass.lpszClassName = wxMDIFrameClassNameNoRedraw;
410 wndclass.style = styleNoRedraw;
411
412 if ( !RegisterClass(&wndclass) )
413 {
414 wxLogLastError(wxT("RegisterClass(no redraw MDI parent frame)"));
415 }
416
417 // Register the MDI child frame window class.
418 wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
419 wndclass.lpszClassName = wxMDIChildFrameClassName;
420 wndclass.style = styleNormal;
421
422 if ( !RegisterClass(&wndclass) )
423 {
424 wxLogLastError(wxT("RegisterClass(MDI child)"));
425 }
426
427 // "no redraw" MDI child frame
428 wndclass.lpszClassName = wxMDIChildFrameClassNameNoRedraw;
429 wndclass.style = styleNoRedraw;
430
431 if ( !RegisterClass(&wndclass) )
432 {
433 wxLogLastError(wxT("RegisterClass(no redraw MDI child)"));
434 }
435
436 return TRUE;
437 }
438
439 // ---------------------------------------------------------------------------
440 // UnregisterWindowClasses
441 // ---------------------------------------------------------------------------
442
443 bool wxApp::UnregisterWindowClasses()
444 {
445 bool retval = TRUE;
446
447 #ifndef __WXMICROWIN__
448 // MDI frame window class.
449 if ( !::UnregisterClass(wxMDIFrameClassName, wxhInstance) )
450 {
451 wxLogLastError(wxT("UnregisterClass(MDI parent)"));
452
453 retval = FALSE;
454 }
455
456 // "no redraw" MDI frame
457 if ( !::UnregisterClass(wxMDIFrameClassNameNoRedraw, wxhInstance) )
458 {
459 wxLogLastError(wxT("UnregisterClass(no redraw MDI parent frame)"));
460
461 retval = FALSE;
462 }
463
464 // MDI child frame window class.
465 if ( !::UnregisterClass(wxMDIChildFrameClassName, wxhInstance) )
466 {
467 wxLogLastError(wxT("UnregisterClass(MDI child)"));
468
469 retval = FALSE;
470 }
471
472 // "no redraw" MDI child frame
473 if ( !::UnregisterClass(wxMDIChildFrameClassNameNoRedraw, wxhInstance) )
474 {
475 wxLogLastError(wxT("UnregisterClass(no redraw MDI child)"));
476
477 retval = FALSE;
478 }
479
480 // canvas class name
481 if ( !::UnregisterClass(wxCanvasClassName, wxhInstance) )
482 {
483 wxLogLastError(wxT("UnregisterClass(canvas)"));
484
485 retval = FALSE;
486 }
487
488 if ( !::UnregisterClass(wxCanvasClassNameNR, wxhInstance) )
489 {
490 wxLogLastError(wxT("UnregisterClass(no redraw canvas)"));
491
492 retval = FALSE;
493 }
494 #endif // __WXMICROWIN__
495
496 return retval;
497 }
498
499 void wxApp::CleanUp()
500 {
501 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
502 wxSetKeyboardHook(FALSE);
503 #endif
504
505 #if wxUSE_PENWINDOWS
506 wxCleanUpPenWin();
507 #endif
508
509 if ( wxDisableButtonBrush )
510 ::DeleteObject( wxDisableButtonBrush );
511
512 #if wxUSE_OLE
513 ::OleUninitialize();
514 #endif
515
516 // for an EXE the classes are unregistered when it terminates but DLL may
517 // be loaded several times (load/unload/load) into the same process in
518 // which case the registration will fail after the first time if we don't
519 // unregister the classes now
520 UnregisterWindowClasses();
521
522 #if wxUSE_CTL3D
523 Ctl3dUnregister(wxhInstance);
524 #endif
525
526 delete wxWinHandleHash;
527 wxWinHandleHash = NULL;
528
529 wxAppBase::CleanUp();
530 }
531
532 // ----------------------------------------------------------------------------
533 // wxApp ctor/dtor
534 // ----------------------------------------------------------------------------
535
536 wxApp::wxApp()
537 {
538 m_printMode = wxPRINT_WINDOWS;
539 }
540
541 wxApp::~wxApp()
542 {
543 // our cmd line arguments are allocated inside wxEntry(HINSTANCE), they
544 // don't come from main(), so we have to free them
545
546 while ( argc )
547 {
548 // m_argv elements were allocated by wxStrdup()
549 free(argv[--argc]);
550 }
551
552 // but m_argv itself -- using new[]
553 delete [] argv;
554 }
555
556 bool wxApp::Initialized()
557 {
558 #ifndef _WINDLL
559 if (GetTopWindow())
560 return TRUE;
561 else
562 return FALSE;
563 #else // Assume initialized if DLL (no way of telling)
564 return TRUE;
565 #endif
566 }
567
568 /*
569 * Get and process a message, returning FALSE if WM_QUIT
570 * received (and also set the flag telling the app to exit the main loop)
571 *
572 */
573 bool wxApp::DoMessage()
574 {
575 BOOL rc = ::GetMessage(&s_currentMsg, (HWND) NULL, 0, 0);
576 if ( rc == 0 )
577 {
578 // got WM_QUIT
579 m_keepGoing = FALSE;
580
581 return FALSE;
582 }
583 else if ( rc == -1 )
584 {
585 // should never happen, but let's test for it nevertheless
586 wxLogLastError(wxT("GetMessage"));
587 }
588 else
589 {
590 #if wxUSE_THREADS
591 wxASSERT_MSG( wxThread::IsMain(),
592 wxT("only the main thread can process Windows messages") );
593
594 static bool s_hadGuiLock = TRUE;
595 static wxMsgArray s_aSavedMessages;
596
597 // if a secondary thread owns is doing GUI calls, save all messages for
598 // later processing - we can't process them right now because it will
599 // lead to recursive library calls (and we're not reentrant)
600 if ( !wxGuiOwnedByMainThread() )
601 {
602 s_hadGuiLock = FALSE;
603
604 // leave out WM_COMMAND messages: too dangerous, sometimes
605 // the message will be processed twice
606 if ( !wxIsWaitingForThread() ||
607 s_currentMsg.message != WM_COMMAND )
608 {
609 s_aSavedMessages.Add(s_currentMsg);
610 }
611
612 return TRUE;
613 }
614 else
615 {
616 // have we just regained the GUI lock? if so, post all of the saved
617 // messages
618 //
619 // FIXME of course, it's not _exactly_ the same as processing the
620 // messages normally - expect some things to break...
621 if ( !s_hadGuiLock )
622 {
623 s_hadGuiLock = TRUE;
624
625 size_t count = s_aSavedMessages.GetCount();
626 for ( size_t n = 0; n < count; n++ )
627 {
628 MSG& msg = s_aSavedMessages[n];
629
630 DoMessage((WXMSG *)&msg);
631 }
632
633 s_aSavedMessages.Empty();
634 }
635 }
636 #endif // wxUSE_THREADS
637
638 // Process the message
639 DoMessage((WXMSG *)&s_currentMsg);
640 }
641
642 return TRUE;
643 }
644
645 void wxApp::DoMessage(WXMSG *pMsg)
646 {
647 if ( !ProcessMessage(pMsg) )
648 {
649 ::TranslateMessage((MSG *)pMsg);
650 ::DispatchMessage((MSG *)pMsg);
651 }
652 }
653
654 /*
655 * Keep trying to process messages until WM_QUIT
656 * received.
657 *
658 * If there are messages to be processed, they will all be
659 * processed and OnIdle will not be called.
660 * When there are no more messages, OnIdle is called.
661 * If OnIdle requests more time,
662 * it will be repeatedly called so long as there are no pending messages.
663 * A 'feature' of this is that once OnIdle has decided that no more processing
664 * is required, then it won't get processing time until further messages
665 * are processed (it'll sit in DoMessage).
666 */
667
668 int wxApp::MainLoop()
669 {
670 m_keepGoing = TRUE;
671
672 while ( m_keepGoing )
673 {
674 #if wxUSE_THREADS
675 wxMutexGuiLeaveOrEnter();
676 #endif // wxUSE_THREADS
677
678 while ( !Pending() && ProcessIdle() )
679 ;
680
681 // a message came or no more idle processing to do
682 DoMessage();
683 }
684
685 return s_currentMsg.wParam;
686 }
687
688 void wxApp::ExitMainLoop()
689 {
690 // this will set m_keepGoing to FALSE a bit later
691 ::PostQuitMessage(0);
692 }
693
694 bool wxApp::Pending()
695 {
696 return ::PeekMessage(&s_currentMsg, 0, 0, 0, PM_NOREMOVE) != 0;
697 }
698
699 void wxApp::Dispatch()
700 {
701 DoMessage();
702 }
703
704 /*
705 * Give all windows a chance to preprocess
706 * the message. Some may have accelerator tables, or have
707 * MDI complications.
708 */
709
710 bool wxApp::ProcessMessage(WXMSG *wxmsg)
711 {
712 MSG *msg = (MSG *)wxmsg;
713 HWND hwnd = msg->hwnd;
714 wxWindow *wndThis = wxGetWindowFromHWND((WXHWND)hwnd);
715
716 // this may happen if the event occured in a standard modeless dialog (the
717 // only example of which I know of is the find/replace dialog) - then call
718 // IsDialogMessage() to make TAB navigation in it work
719 if ( !wndThis )
720 {
721 // we need to find the dialog containing this control as
722 // IsDialogMessage() just eats all the messages (i.e. returns TRUE for
723 // them) if we call it for the control itself
724 while ( hwnd && ::GetWindowLong(hwnd, GWL_STYLE) & WS_CHILD )
725 {
726 hwnd = ::GetParent(hwnd);
727 }
728
729 return hwnd && ::IsDialogMessage(hwnd, msg) != 0;
730 }
731
732 #if wxUSE_TOOLTIPS
733 // we must relay WM_MOUSEMOVE events to the tooltip ctrl if we want it to
734 // popup the tooltip bubbles
735 if ( (msg->message == WM_MOUSEMOVE) )
736 {
737 wxToolTip *tt = wndThis->GetToolTip();
738 if ( tt )
739 {
740 tt->RelayEvent(wxmsg);
741 }
742 }
743 #endif // wxUSE_TOOLTIPS
744
745 // allow the window to prevent certain messages from being
746 // translated/processed (this is currently used by wxTextCtrl to always
747 // grab Ctrl-C/V/X, even if they are also accelerators in some parent)
748 if ( !wndThis->MSWShouldPreProcessMessage(wxmsg) )
749 {
750 return FALSE;
751 }
752
753 // try translations first: the accelerators override everything
754 wxWindow *wnd;
755
756 for ( wnd = wndThis; wnd; wnd = wnd->GetParent() )
757 {
758 if ( wnd->MSWTranslateMessage(wxmsg))
759 return TRUE;
760
761 // stop at first top level window, i.e. don't try to process the key
762 // strokes originating in a dialog using the accelerators of the parent
763 // frame - this doesn't make much sense
764 if ( wnd->IsTopLevel() )
765 break;
766 }
767
768 // now try the other hooks (kbd navigation is handled here): we start from
769 // wndThis->GetParent() because wndThis->MSWProcessMessage() was already
770 // called above
771 for ( wnd = wndThis->GetParent(); wnd; wnd = wnd->GetParent() )
772 {
773 if ( wnd->MSWProcessMessage(wxmsg) )
774 return TRUE;
775 }
776
777 // no special preprocessing for this message, dispatch it normally
778 return FALSE;
779 }
780
781 // this is a temporary hack and will be replaced by using wxEventLoop in the
782 // future
783 //
784 // it is needed to allow other event loops (currently only one: the modal
785 // dialog one) to reset the OnIdle() semaphore because otherwise OnIdle()
786 // wouldn't do anything while a modal dialog shown from OnIdle() call is shown.
787 bool wxIsInOnIdleFlag = FALSE;
788
789 void wxApp::OnIdle(wxIdleEvent& event)
790 {
791 // Avoid recursion (via ProcessEvent default case)
792 if ( wxIsInOnIdleFlag )
793 return;
794
795 wxIsInOnIdleFlag = TRUE;
796
797 // If there are pending events, we must process them: pending events
798 // are either events to the threads other than main or events posted
799 // with wxPostEvent() functions
800 // GRG: I have moved this here so that all pending events are processed
801 // before starting to delete any objects. This behaves better (in
802 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
803 // behaviour. Changed Feb/2000 before 2.1.14
804 ProcessPendingEvents();
805
806 // 'Garbage' collection of windows deleted with Close().
807 DeletePendingObjects();
808
809 #if wxUSE_LOG
810 // flush the logged messages if any
811 wxLog::FlushActive();
812 #endif // wxUSE_LOG
813
814 #if wxUSE_DC_CACHEING
815 // automated DC cache management: clear the cached DCs and bitmap
816 // if it's likely that the app has finished with them, that is, we
817 // get an idle event and we're not dragging anything.
818 if (!::GetKeyState(MK_LBUTTON) && !::GetKeyState(MK_MBUTTON) && !::GetKeyState(MK_RBUTTON))
819 wxDC::ClearCache();
820 #endif // wxUSE_DC_CACHEING
821
822 // Send OnIdle events to all windows
823 if ( SendIdleEvents() )
824 {
825 // SendIdleEvents() returns TRUE if at least one window requested more
826 // idle events
827 event.RequestMore(TRUE);
828 }
829
830 wxIsInOnIdleFlag = FALSE;
831 }
832
833 void wxApp::WakeUpIdle()
834 {
835 // Send the top window a dummy message so idle handler processing will
836 // start up again. Doing it this way ensures that the idle handler
837 // wakes up in the right thread (see also wxWakeUpMainThread() which does
838 // the same for the main app thread only)
839 wxWindow *topWindow = wxTheApp->GetTopWindow();
840 if ( topWindow )
841 {
842 if ( !::PostMessage(GetHwndOf(topWindow), WM_NULL, 0, 0) )
843 {
844 // should never happen
845 wxLogLastError(wxT("PostMessage(WM_NULL)"));
846 }
847 }
848 }
849
850 void wxApp::OnEndSession(wxCloseEvent& WXUNUSED(event))
851 {
852 if (GetTopWindow())
853 GetTopWindow()->Close(TRUE);
854 }
855
856 // Default behaviour: close the application with prompts. The
857 // user can veto the close, and therefore the end session.
858 void wxApp::OnQueryEndSession(wxCloseEvent& event)
859 {
860 if (GetTopWindow())
861 {
862 if (!GetTopWindow()->Close(!event.CanVeto()))
863 event.Veto(TRUE);
864 }
865 }
866
867 typedef struct _WXADllVersionInfo
868 {
869 DWORD cbSize;
870 DWORD dwMajorVersion; // Major version
871 DWORD dwMinorVersion; // Minor version
872 DWORD dwBuildNumber; // Build number
873 DWORD dwPlatformID; // DLLVER_PLATFORM_*
874 } WXADLLVERSIONINFO;
875
876 typedef HRESULT (CALLBACK* WXADLLGETVERSIONPROC)(WXADLLVERSIONINFO *);
877
878 /* static */
879 int wxApp::GetComCtl32Version()
880 {
881 #if defined(__WXMICROWIN__) || defined(__WXWINCE__)
882 return 0;
883 #else
884 // cache the result
885 static int s_verComCtl32 = -1;
886
887 wxCRIT_SECT_DECLARE(csComCtl32);
888 wxCRIT_SECT_LOCKER(lock, csComCtl32);
889
890 if ( s_verComCtl32 == -1 )
891 {
892 // initally assume no comctl32.dll at all
893 s_verComCtl32 = 0;
894
895 // do we have it?
896 HMODULE hModuleComCtl32 = ::GetModuleHandle(wxT("COMCTL32"));
897 BOOL bFreeComCtl32 = FALSE ;
898 if(!hModuleComCtl32)
899 {
900 hModuleComCtl32 = ::LoadLibrary(wxT("COMCTL32.DLL")) ;
901 if(hModuleComCtl32)
902 {
903 bFreeComCtl32 = TRUE ;
904 }
905 }
906
907 // if so, then we can check for the version
908 if ( hModuleComCtl32 )
909 {
910 // try to use DllGetVersion() if available in _headers_
911 WXADLLGETVERSIONPROC pfnDllGetVersion = (WXADLLGETVERSIONPROC)
912 ::GetProcAddress(hModuleComCtl32, "DllGetVersion");
913 if ( pfnDllGetVersion )
914 {
915 WXADLLVERSIONINFO dvi;
916 dvi.cbSize = sizeof(dvi);
917
918 HRESULT hr = (*pfnDllGetVersion)(&dvi);
919 if ( FAILED(hr) )
920 {
921 wxLogApiError(_T("DllGetVersion"), hr);
922 }
923 else
924 {
925 // this is incompatible with _WIN32_IE values, but
926 // compatible with the other values returned by
927 // GetComCtl32Version()
928 s_verComCtl32 = 100*dvi.dwMajorVersion +
929 dvi.dwMinorVersion;
930 }
931 }
932 // DllGetVersion() unavailable either during compile or
933 // run-time, try to guess the version otherwise
934 if ( !s_verComCtl32 )
935 {
936 // InitCommonControlsEx is unique to 4.70 and later
937 FARPROC theProc = ::GetProcAddress
938 (
939 hModuleComCtl32,
940 "InitCommonControlsEx"
941 );
942
943 if ( !theProc )
944 {
945 // not found, must be 4.00
946 s_verComCtl32 = 400;
947 }
948 else
949 {
950 // many symbols appeared in comctl32 4.71, could use
951 // any of them except may be DllInstall
952 theProc = ::GetProcAddress
953 (
954 hModuleComCtl32,
955 "InitializeFlatSB"
956 );
957 if ( !theProc )
958 {
959 // not found, must be 4.70
960 s_verComCtl32 = 470;
961 }
962 else
963 {
964 // found, must be 4.71
965 s_verComCtl32 = 471;
966 }
967 }
968 }
969 }
970
971 if(bFreeComCtl32)
972 {
973 ::FreeLibrary(hModuleComCtl32) ;
974 }
975 }
976
977 return s_verComCtl32;
978 #endif
979 }
980
981 // Yield to incoming messages
982
983 bool wxApp::Yield(bool onlyIfNeeded)
984 {
985 // MT-FIXME
986 static bool s_inYield = FALSE;
987
988 #if wxUSE_LOG
989 // disable log flushing from here because a call to wxYield() shouldn't
990 // normally result in message boxes popping up &c
991 wxLog::Suspend();
992 #endif // wxUSE_LOG
993
994 if ( s_inYield )
995 {
996 if ( !onlyIfNeeded )
997 {
998 wxFAIL_MSG( wxT("wxYield called recursively" ) );
999 }
1000
1001 return FALSE;
1002 }
1003
1004 s_inYield = TRUE;
1005
1006 // we don't want to process WM_QUIT from here - it should be processed in
1007 // the main event loop in order to stop it
1008 MSG msg;
1009 while ( PeekMessage(&msg, (HWND)0, 0, 0, PM_NOREMOVE) &&
1010 msg.message != WM_QUIT )
1011 {
1012 #if wxUSE_THREADS
1013 wxMutexGuiLeaveOrEnter();
1014 #endif // wxUSE_THREADS
1015
1016 if ( !wxTheApp->DoMessage() )
1017 break;
1018 }
1019
1020 // if there are pending events, we must process them.
1021 ProcessPendingEvents();
1022
1023 #if wxUSE_LOG
1024 // let the logs be flashed again
1025 wxLog::Resume();
1026 #endif // wxUSE_LOG
1027
1028 s_inYield = FALSE;
1029
1030 return TRUE;
1031 }
1032