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