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