No changes, just refactor wxMSW background brush methods.
[wxWidgets.git] / src / msw / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/window.cpp
3 // Purpose: wxWindowMSW
4 // Author: Julian Smart
5 // Modified by: VZ on 13.05.99: no more Default(), MSWOnXXX() reorganisation
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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/window.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/msw/wrapwin.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/msw/missing.h"
33 #include "wx/accel.h"
34 #include "wx/menu.h"
35 #include "wx/dc.h"
36 #include "wx/dcclient.h"
37 #include "wx/dcmemory.h"
38 #include "wx/utils.h"
39 #include "wx/app.h"
40 #include "wx/layout.h"
41 #include "wx/dialog.h"
42 #include "wx/frame.h"
43 #include "wx/listbox.h"
44 #include "wx/button.h"
45 #include "wx/msgdlg.h"
46 #include "wx/settings.h"
47 #include "wx/statbox.h"
48 #include "wx/sizer.h"
49 #include "wx/intl.h"
50 #include "wx/log.h"
51 #include "wx/textctrl.h"
52 #include "wx/menuitem.h"
53 #include "wx/module.h"
54 #endif
55
56 #if wxUSE_OWNER_DRAWN && !defined(__WXUNIVERSAL__)
57 #include "wx/ownerdrw.h"
58 #endif
59
60 #include "wx/hashmap.h"
61 #include "wx/evtloop.h"
62 #include "wx/power.h"
63 #include "wx/sysopt.h"
64
65 #if wxUSE_DRAG_AND_DROP
66 #include "wx/dnd.h"
67 #endif
68
69 #if wxUSE_ACCESSIBILITY
70 #include "wx/access.h"
71 #include <ole2.h>
72 #include <oleacc.h>
73 #ifndef WM_GETOBJECT
74 #define WM_GETOBJECT 0x003D
75 #endif
76 #ifndef OBJID_CLIENT
77 #define OBJID_CLIENT 0xFFFFFFFC
78 #endif
79 #endif
80
81 #include "wx/msw/private.h"
82 #include "wx/msw/private/keyboard.h"
83 #include "wx/msw/dcclient.h"
84
85 #if wxUSE_TOOLTIPS
86 #include "wx/tooltip.h"
87 #endif
88
89 #if wxUSE_CARET
90 #include "wx/caret.h"
91 #endif // wxUSE_CARET
92
93 #if wxUSE_RADIOBOX
94 #include "wx/radiobox.h"
95 #endif // wxUSE_RADIOBOX
96
97 #if wxUSE_SPINCTRL
98 #include "wx/spinctrl.h"
99 #endif // wxUSE_SPINCTRL
100
101 #include "wx/notebook.h"
102 #include "wx/listctrl.h"
103 #include "wx/dynlib.h"
104
105 #include <string.h>
106
107 #if (!defined(__GNUWIN32_OLD__) && !defined(__WXMICROWIN__) /* && !defined(__WXWINCE__) */ ) || defined(__CYGWIN10__)
108 #include <shellapi.h>
109 #include <mmsystem.h>
110 #endif
111
112 #ifdef __WIN32__
113 #include <windowsx.h>
114 #endif
115
116 #if !defined __WXWINCE__ && !defined NEED_PBT_H
117 #include <pbt.h>
118 #endif
119
120 #if defined(__WXWINCE__)
121 #include "wx/msw/wince/missing.h"
122 #ifdef __POCKETPC__
123 #include <windows.h>
124 #include <shellapi.h>
125 #include <ole2.h>
126 #include <aygshell.h>
127 #endif
128 #endif
129
130 #if wxUSE_UXTHEME
131 #include "wx/msw/uxtheme.h"
132 #define EP_EDITTEXT 1
133 #define ETS_NORMAL 1
134 #define ETS_HOT 2
135 #define ETS_SELECTED 3
136 #define ETS_DISABLED 4
137 #define ETS_FOCUSED 5
138 #define ETS_READONLY 6
139 #define ETS_ASSIST 7
140 #endif
141
142 // define the constants used by AnimateWindow() if our SDK doesn't have them
143 #ifndef AW_CENTER
144 #define AW_HOR_POSITIVE 0x00000001
145 #define AW_HOR_NEGATIVE 0x00000002
146 #define AW_VER_POSITIVE 0x00000004
147 #define AW_VER_NEGATIVE 0x00000008
148 #define AW_CENTER 0x00000010
149 #define AW_HIDE 0x00010000
150 #define AW_ACTIVATE 0x00020000
151 #define AW_SLIDE 0x00040000
152 #define AW_BLEND 0x00080000
153 #endif
154
155 #if defined(TME_LEAVE) && defined(WM_MOUSELEAVE) && wxUSE_DYNLIB_CLASS
156 #define HAVE_TRACKMOUSEEVENT
157 #endif // everything needed for TrackMouseEvent()
158
159 // set this to 1 to filter out duplicate mouse events, e.g. mouse move events
160 // when mouse position didnd't change
161 #ifdef __WXWINCE__
162 #define wxUSE_MOUSEEVENT_HACK 0
163 #else
164 #define wxUSE_MOUSEEVENT_HACK 1
165 #endif
166
167 // not all compilers/platforms have X button related declarations (notably
168 // Windows CE doesn't, and probably some old SDKs don't neither)
169 #ifdef WM_XBUTTONDOWN
170 #define wxHAS_XBUTTON
171 #endif
172
173 #ifndef MAPVK_VK_TO_CHAR
174 #define MAPVK_VK_TO_CHAR 2
175 #endif
176
177 // ---------------------------------------------------------------------------
178 // global variables
179 // ---------------------------------------------------------------------------
180
181 #if wxUSE_MENUS_NATIVE
182 extern wxMenu *wxCurrentPopupMenu;
183 #endif
184
185 namespace
186 {
187
188 // true if we had already created the std colour map, used by
189 // wxGetStdColourMap() and wxWindow::OnSysColourChanged() (FIXME-MT)
190 bool gs_hasStdCmap = false;
191
192 // last mouse event information we need to filter out the duplicates
193 #if wxUSE_MOUSEEVENT_HACK
194 struct MouseEventInfoDummy
195 {
196 // mouse position (in screen coordinates)
197 wxPoint pos;
198
199 // last mouse event type
200 wxEventType type;
201 } gs_lastMouseEvent;
202 #endif // wxUSE_MOUSEEVENT_HACK
203
204 // hash containing the registered handlers for the custom messages
205 WX_DECLARE_HASH_MAP(int, wxWindow::MSWMessageHandler,
206 wxIntegerHash, wxIntegerEqual,
207 MSWMessageHandlers);
208
209 MSWMessageHandlers gs_messageHandlers;
210
211 // hash containing all our windows, it uses HWND keys and wxWindow* values
212 WX_DECLARE_HASH_MAP(HWND, wxWindow *,
213 wxPointerHash, wxPointerEqual,
214 WindowHandles);
215
216 WindowHandles gs_windowHandles;
217
218 #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
219
220 // temporary override for WM_ERASEBKGND processing: we don't store this in
221 // wxWindow itself as we don't need it during most of the time so don't
222 // increase the size of all window objects unnecessarily
223 WX_DECLARE_HASH_MAP(wxWindow *, wxWindow *,
224 wxPointerHash, wxPointerEqual,
225 EraseBgHooks);
226
227 EraseBgHooks gs_eraseBgHooks;
228
229 #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
230
231 // If this variable is strictly positive, EVT_CHAR_HOOK is not generated for
232 // Escape key presses as it can't be intercepted because it's needed by some
233 // currently shown window, e.g. IME entry.
234 //
235 // This is currently global as we allow using UI from the main thread only
236 // anyhow but could be replaced with a thread-specific value in the future if
237 // needed.
238 int gs_modalEntryWindowCount = 0;
239
240 } // anonymous namespace
241
242 // ---------------------------------------------------------------------------
243 // private functions
244 // ---------------------------------------------------------------------------
245
246 // the window proc for all our windows
247 LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message,
248 WPARAM wParam, LPARAM lParam);
249
250
251 #if wxDEBUG_LEVEL >= 2
252 const wxChar *wxGetMessageName(int message);
253 #endif // wxDEBUG_LEVEL >= 2
254
255 void wxRemoveHandleAssociation(wxWindowMSW *win);
256 extern void wxAssociateWinWithHandle(HWND hWnd, wxWindowMSW *win);
257
258 // get the text metrics for the current font
259 static TEXTMETRIC wxGetTextMetrics(const wxWindowMSW *win);
260
261 #ifdef __WXWINCE__
262 // find the window for the mouse event at the specified position
263 static wxWindowMSW *FindWindowForMouseEvent(wxWindowMSW *win, int *x, int *y);
264 #endif // __WXWINCE__
265
266 // wrapper around BringWindowToTop() API
267 static inline void wxBringWindowToTop(HWND hwnd)
268 {
269 #ifdef __WXMICROWIN__
270 // It seems that MicroWindows brings the _parent_ of the window to the top,
271 // which can be the wrong one.
272
273 // activate (set focus to) specified window
274 ::SetFocus(hwnd);
275 #endif
276
277 // raise top level parent to top of z order
278 if (!::SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE))
279 {
280 wxLogLastError(wxT("SetWindowPos"));
281 }
282 }
283
284 #ifndef __WXWINCE__
285
286 // ensure that all our parent windows have WS_EX_CONTROLPARENT style
287 static void EnsureParentHasControlParentStyle(wxWindow *parent)
288 {
289 /*
290 If we have WS_EX_CONTROLPARENT flag we absolutely *must* set it for our
291 parent as well as otherwise several Win32 functions using
292 GetNextDlgTabItem() to iterate over all controls such as
293 IsDialogMessage() or DefDlgProc() would enter an infinite loop: indeed,
294 all of them iterate over all the controls starting from the currently
295 focused one and stop iterating when they get back to the focus but
296 unless all parents have WS_EX_CONTROLPARENT bit set, they would never
297 get back to the initial (focused) window: as we do have this style,
298 GetNextDlgTabItem() will leave this window and continue in its parent,
299 but if the parent doesn't have it, it wouldn't recurse inside it later
300 on and so wouldn't have a chance of getting back to this window either.
301 */
302 while ( parent && !parent->IsTopLevel() )
303 {
304 LONG exStyle = wxGetWindowExStyle(parent);
305 if ( !(exStyle & WS_EX_CONTROLPARENT) )
306 {
307 // force the parent to have this style
308 wxSetWindowExStyle(parent, exStyle | WS_EX_CONTROLPARENT);
309 }
310
311 parent = parent->GetParent();
312 }
313 }
314
315 #endif // !__WXWINCE__
316
317 #ifdef __WXWINCE__
318 // On Windows CE, GetCursorPos can return an error, so use this function
319 // instead
320 bool GetCursorPosWinCE(POINT* pt)
321 {
322 if (!GetCursorPos(pt))
323 {
324 DWORD pos = GetMessagePos();
325 pt->x = LOWORD(pos);
326 pt->y = HIWORD(pos);
327 }
328 return true;
329 }
330 #endif
331
332 // ---------------------------------------------------------------------------
333 // event tables
334 // ---------------------------------------------------------------------------
335
336 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
337 // method
338 #ifdef __WXUNIVERSAL__
339 IMPLEMENT_ABSTRACT_CLASS(wxWindowMSW, wxWindowBase)
340 #endif // __WXUNIVERSAL__
341
342 BEGIN_EVENT_TABLE(wxWindowMSW, wxWindowBase)
343 EVT_SYS_COLOUR_CHANGED(wxWindowMSW::OnSysColourChanged)
344 #ifdef __WXWINCE__
345 EVT_INIT_DIALOG(wxWindowMSW::OnInitDialog)
346 #endif
347 END_EVENT_TABLE()
348
349 // ===========================================================================
350 // implementation
351 // ===========================================================================
352
353 // ---------------------------------------------------------------------------
354 // wxWindow utility functions
355 // ---------------------------------------------------------------------------
356
357 // Find an item given the MS Windows id
358 wxWindow *wxWindowMSW::FindItem(long id) const
359 {
360 #if wxUSE_CONTROLS
361 wxControl *item = wxDynamicCastThis(wxControl);
362 if ( item )
363 {
364 // is it us or one of our "internal" children?
365 if ( item->GetId() == id
366 #ifndef __WXUNIVERSAL__
367 || (item->GetSubcontrols().Index(id) != wxNOT_FOUND)
368 #endif // __WXUNIVERSAL__
369 )
370 {
371 return item;
372 }
373 }
374 #endif // wxUSE_CONTROLS
375
376 wxWindowList::compatibility_iterator current = GetChildren().GetFirst();
377 while (current)
378 {
379 wxWindow *childWin = current->GetData();
380
381 wxWindow *wnd = childWin->FindItem(id);
382 if ( wnd )
383 return wnd;
384
385 current = current->GetNext();
386 }
387
388 return NULL;
389 }
390
391 // Find an item given the MS Windows handle
392 wxWindow *wxWindowMSW::FindItemByHWND(WXHWND hWnd, bool controlOnly) const
393 {
394 wxWindowList::compatibility_iterator current = GetChildren().GetFirst();
395 while (current)
396 {
397 wxWindow *parent = current->GetData();
398
399 // Do a recursive search.
400 wxWindow *wnd = parent->FindItemByHWND(hWnd);
401 if ( wnd )
402 return wnd;
403
404 if ( !controlOnly
405 #if wxUSE_CONTROLS
406 || parent->IsKindOf(CLASSINFO(wxControl))
407 #endif // wxUSE_CONTROLS
408 )
409 {
410 wxWindow *item = current->GetData();
411 if ( item->GetHWND() == hWnd )
412 return item;
413 else
414 {
415 if ( item->ContainsHWND(hWnd) )
416 return item;
417 }
418 }
419
420 current = current->GetNext();
421 }
422 return NULL;
423 }
424
425 // Default command handler
426 bool wxWindowMSW::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
427 {
428 return false;
429 }
430
431 // ----------------------------------------------------------------------------
432 // constructors and such
433 // ----------------------------------------------------------------------------
434
435 void wxWindowMSW::Init()
436 {
437 // MSW specific
438 m_oldWndProc = NULL;
439 m_mouseInWindow = false;
440 m_lastKeydownProcessed = false;
441
442 m_hWnd = 0;
443
444 m_xThumbSize = 0;
445 m_yThumbSize = 0;
446
447 #if wxUSE_DEFERRED_SIZING
448 m_hDWP = 0;
449 m_pendingPosition = wxDefaultPosition;
450 m_pendingSize = wxDefaultSize;
451 #endif // wxUSE_DEFERRED_SIZING
452
453 #ifdef __POCKETPC__
454 m_contextMenuEnabled = false;
455 #endif
456 }
457
458 // Destructor
459 wxWindowMSW::~wxWindowMSW()
460 {
461 SendDestroyEvent();
462
463 #ifndef __WXUNIVERSAL__
464 // VS: make sure there's no wxFrame with last focus set to us:
465 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
466 {
467 wxTopLevelWindow *frame = wxDynamicCast(win, wxTopLevelWindow);
468 if ( frame )
469 {
470 if ( frame->GetLastFocus() == this )
471 {
472 frame->SetLastFocus(NULL);
473 }
474
475 // apparently sometimes we can end up with our grand parent
476 // pointing to us as well: this is surely a bug in focus handling
477 // code but it's not clear where it happens so for now just try to
478 // fix it here by not breaking out of the loop
479 //break;
480 }
481 }
482 #endif // __WXUNIVERSAL__
483
484 // VS: destroy children first and _then_ detach *this from its parent.
485 // If we did it the other way around, children wouldn't be able
486 // find their parent frame (see above).
487 DestroyChildren();
488
489 if ( m_hWnd )
490 {
491 // VZ: test temp removed to understand what really happens here
492 //if (::IsWindow(GetHwnd()))
493 {
494 if ( !::DestroyWindow(GetHwnd()) )
495 {
496 wxLogLastError(wxT("DestroyWindow"));
497 }
498 }
499
500 // remove hWnd <-> wxWindow association
501 wxRemoveHandleAssociation(this);
502 }
503
504 }
505
506 /* static */
507 const wxChar *wxWindowMSW::MSWGetRegisteredClassName()
508 {
509 return wxApp::GetRegisteredClassName(wxT("wxWindow"), COLOR_BTNFACE);
510 }
511
512 // real construction (Init() must have been called before!)
513 bool wxWindowMSW::Create(wxWindow *parent,
514 wxWindowID id,
515 const wxPoint& pos,
516 const wxSize& size,
517 long style,
518 const wxString& name)
519 {
520 wxCHECK_MSG( parent, false, wxT("can't create wxWindow without parent") );
521
522 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
523 return false;
524
525 parent->AddChild(this);
526
527 WXDWORD exstyle;
528 DWORD msflags = MSWGetCreateWindowFlags(&exstyle);
529
530 #ifdef __WXUNIVERSAL__
531 // no borders, we draw them ourselves
532 exstyle &= ~(WS_EX_DLGMODALFRAME |
533 WS_EX_STATICEDGE |
534 WS_EX_CLIENTEDGE |
535 WS_EX_WINDOWEDGE);
536 msflags &= ~WS_BORDER;
537 #endif // wxUniversal
538
539 if ( IsShown() )
540 {
541 msflags |= WS_VISIBLE;
542 }
543
544 if ( !MSWCreate(MSWGetRegisteredClassName(),
545 NULL, pos, size, msflags, exstyle) )
546 return false;
547
548 InheritAttributes();
549
550 return true;
551 }
552
553 // ---------------------------------------------------------------------------
554 // basic operations
555 // ---------------------------------------------------------------------------
556
557 void wxWindowMSW::SetFocus()
558 {
559 HWND hWnd = GetHwnd();
560 wxCHECK_RET( hWnd, wxT("can't set focus to invalid window") );
561
562 #if !defined(__WXWINCE__)
563 ::SetLastError(0);
564 #endif
565
566 if ( !::SetFocus(hWnd) )
567 {
568 // was there really an error?
569 DWORD dwRes = ::GetLastError();
570 if ( dwRes )
571 {
572 HWND hwndFocus = ::GetFocus();
573 if ( hwndFocus != hWnd )
574 {
575 wxLogApiError(wxT("SetFocus"), dwRes);
576 }
577 }
578 }
579 }
580
581 void wxWindowMSW::SetFocusFromKbd()
582 {
583 // when the focus is given to the control with DLGC_HASSETSEL style from
584 // keyboard its contents should be entirely selected: this is what
585 // ::IsDialogMessage() does and so we should do it as well to provide the
586 // same LNF as the native programs
587 if ( ::SendMessage(GetHwnd(), WM_GETDLGCODE, 0, 0) & DLGC_HASSETSEL )
588 {
589 ::SendMessage(GetHwnd(), EM_SETSEL, 0, -1);
590 }
591
592 // do this after (maybe) setting the selection as like this when
593 // wxEVT_SET_FOCUS handler is called, the selection would have been already
594 // set correctly -- this may be important
595 wxWindowBase::SetFocusFromKbd();
596 }
597
598 // Get the window with the focus
599 wxWindow *wxWindowBase::DoFindFocus()
600 {
601 HWND hWnd = ::GetFocus();
602 if ( hWnd )
603 {
604 return wxGetWindowFromHWND((WXHWND)hWnd);
605 }
606
607 return NULL;
608 }
609
610 void wxWindowMSW::DoEnable( bool enable )
611 {
612 MSWEnableHWND(GetHwnd(), enable);
613 }
614
615 bool wxWindowMSW::MSWEnableHWND(WXHWND hWnd, bool enable)
616 {
617 if ( !hWnd )
618 return false;
619
620 // If disabling focused control, we move focus to the next one, as if the
621 // user pressed Tab. That's because we can't keep focus on a disabled
622 // control, Tab-navigation would stop working then.
623 if ( !enable && ::GetFocus() == hWnd )
624 Navigate();
625
626 return ::EnableWindow(hWnd, (BOOL)enable) != 0;
627 }
628
629 bool wxWindowMSW::Show(bool show)
630 {
631 if ( !wxWindowBase::Show(show) )
632 return false;
633
634 HWND hWnd = GetHwnd();
635
636 // we could be called before the underlying window is created (this is
637 // actually useful to prevent it from being initially shown), e.g.
638 //
639 // wxFoo *foo = new wxFoo;
640 // foo->Hide();
641 // foo->Create(parent, ...);
642 //
643 // should work without errors
644 if ( hWnd )
645 {
646 ::ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
647 }
648
649 if ( IsFrozen() )
650 {
651 // DoFreeze/DoThaw don't do anything if the window is not shown, so
652 // we have to call them from here now
653 if ( show )
654 DoFreeze();
655 else
656 DoThaw();
657 }
658
659 return true;
660 }
661
662 bool
663 wxWindowMSW::MSWShowWithEffect(bool show,
664 wxShowEffect effect,
665 unsigned timeout)
666 {
667 if ( effect == wxSHOW_EFFECT_NONE )
668 return Show(show);
669
670 if ( !wxWindowBase::Show(show) )
671 return false;
672
673 typedef BOOL (WINAPI *AnimateWindow_t)(HWND, DWORD, DWORD);
674
675 static AnimateWindow_t s_pfnAnimateWindow = NULL;
676 static bool s_initDone = false;
677 if ( !s_initDone )
678 {
679 wxDynamicLibrary dllUser32(wxT("user32.dll"), wxDL_VERBATIM | wxDL_QUIET);
680 wxDL_INIT_FUNC(s_pfn, AnimateWindow, dllUser32);
681
682 s_initDone = true;
683
684 // notice that it's ok to unload user32.dll here as it won't be really
685 // unloaded, being still in use because we link to it statically too
686 }
687
688 if ( !s_pfnAnimateWindow )
689 return Show(show);
690
691 // Show() has a side effect of sending a WM_SIZE to the window, which helps
692 // ensuring that it's laid out correctly, but AnimateWindow() doesn't do
693 // this so send the event ourselves
694 SendSizeEvent();
695
696 // prepare to use AnimateWindow()
697
698 if ( !timeout )
699 timeout = 200; // this is the default animation timeout, per MSDN
700
701 DWORD dwFlags = show ? 0 : AW_HIDE;
702
703 switch ( effect )
704 {
705 case wxSHOW_EFFECT_ROLL_TO_LEFT:
706 dwFlags |= AW_HOR_NEGATIVE;
707 break;
708
709 case wxSHOW_EFFECT_ROLL_TO_RIGHT:
710 dwFlags |= AW_HOR_POSITIVE;
711 break;
712
713 case wxSHOW_EFFECT_ROLL_TO_TOP:
714 dwFlags |= AW_VER_NEGATIVE;
715 break;
716
717 case wxSHOW_EFFECT_ROLL_TO_BOTTOM:
718 dwFlags |= AW_VER_POSITIVE;
719 break;
720
721 case wxSHOW_EFFECT_SLIDE_TO_LEFT:
722 dwFlags |= AW_SLIDE | AW_HOR_NEGATIVE;
723 break;
724
725 case wxSHOW_EFFECT_SLIDE_TO_RIGHT:
726 dwFlags |= AW_SLIDE | AW_HOR_POSITIVE;
727 break;
728
729 case wxSHOW_EFFECT_SLIDE_TO_TOP:
730 dwFlags |= AW_SLIDE | AW_VER_NEGATIVE;
731 break;
732
733 case wxSHOW_EFFECT_SLIDE_TO_BOTTOM:
734 dwFlags |= AW_SLIDE | AW_VER_POSITIVE;
735 break;
736
737 case wxSHOW_EFFECT_BLEND:
738 dwFlags |= AW_BLEND;
739 break;
740
741 case wxSHOW_EFFECT_EXPAND:
742 dwFlags |= AW_CENTER;
743 break;
744
745
746 case wxSHOW_EFFECT_MAX:
747 wxFAIL_MSG( wxT("invalid window show effect") );
748 return false;
749
750 default:
751 wxFAIL_MSG( wxT("unknown window show effect") );
752 return false;
753 }
754
755 if ( !(*s_pfnAnimateWindow)(GetHwnd(), timeout, dwFlags) )
756 {
757 wxLogLastError(wxT("AnimateWindow"));
758
759 return false;
760 }
761
762 return true;
763 }
764
765 // Raise the window to the top of the Z order
766 void wxWindowMSW::Raise()
767 {
768 wxBringWindowToTop(GetHwnd());
769 }
770
771 // Lower the window to the bottom of the Z order
772 void wxWindowMSW::Lower()
773 {
774 ::SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0,
775 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
776 }
777
778 void wxWindowMSW::DoCaptureMouse()
779 {
780 HWND hWnd = GetHwnd();
781 if ( hWnd )
782 {
783 ::SetCapture(hWnd);
784 }
785 }
786
787 void wxWindowMSW::DoReleaseMouse()
788 {
789 if ( !::ReleaseCapture() )
790 {
791 wxLogLastError(wxT("ReleaseCapture"));
792 }
793 }
794
795 /* static */ wxWindow *wxWindowBase::GetCapture()
796 {
797 HWND hwnd = ::GetCapture();
798 return hwnd ? wxFindWinFromHandle(hwnd) : NULL;
799 }
800
801 bool wxWindowMSW::SetFont(const wxFont& font)
802 {
803 if ( !wxWindowBase::SetFont(font) )
804 {
805 // nothing to do
806 return false;
807 }
808
809 HWND hWnd = GetHwnd();
810 if ( hWnd != 0 )
811 {
812 // note the use of GetFont() instead of m_font: our own font could have
813 // just been reset and in this case we need to change the font used by
814 // the native window to the default for this class, i.e. exactly what
815 // GetFont() returns
816 WXHANDLE hFont = GetFont().GetResourceHandle();
817
818 wxASSERT_MSG( hFont, wxT("should have valid font") );
819
820 ::SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
821 }
822
823 return true;
824 }
825 bool wxWindowMSW::SetCursor(const wxCursor& cursor)
826 {
827 if ( !wxWindowBase::SetCursor(cursor) )
828 {
829 // no change
830 return false;
831 }
832
833 // don't "overwrite" busy cursor
834 if ( m_cursor.Ok() && !wxIsBusy() )
835 {
836 // normally we should change the cursor only if it's over this window
837 // but we should do it always if we capture the mouse currently
838 bool set = HasCapture();
839 if ( !set )
840 {
841 HWND hWnd = GetHwnd();
842
843 POINT point;
844 #ifdef __WXWINCE__
845 ::GetCursorPosWinCE(&point);
846 #else
847 ::GetCursorPos(&point);
848 #endif
849
850 RECT rect = wxGetWindowRect(hWnd);
851
852 set = ::PtInRect(&rect, point) != 0;
853 }
854
855 if ( set )
856 {
857 ::SetCursor(GetHcursorOf(m_cursor));
858 }
859 //else: will be set later when the mouse enters this window
860 }
861
862 return true;
863 }
864
865 void wxWindowMSW::WarpPointer(int x, int y)
866 {
867 ClientToScreen(&x, &y);
868
869 if ( !::SetCursorPos(x, y) )
870 {
871 wxLogLastError(wxT("SetCursorPos"));
872 }
873 }
874
875 void wxWindowMSW::MSWUpdateUIState(int action, int state)
876 {
877 // WM_CHANGEUISTATE only appeared in Windows 2000 so it can do us no good
878 // to use it on older systems -- and could possibly do some harm
879 static int s_needToUpdate = -1;
880 if ( s_needToUpdate == -1 )
881 {
882 int verMaj, verMin;
883 s_needToUpdate = wxGetOsVersion(&verMaj, &verMin) == wxOS_WINDOWS_NT &&
884 verMaj >= 5;
885 }
886
887 if ( s_needToUpdate )
888 {
889 // we send WM_CHANGEUISTATE so if nothing needs changing then the system
890 // won't send WM_UPDATEUISTATE
891 ::SendMessage(GetHwnd(), WM_CHANGEUISTATE, MAKEWPARAM(action, state), 0);
892 }
893 }
894
895 // ---------------------------------------------------------------------------
896 // scrolling stuff
897 // ---------------------------------------------------------------------------
898
899 namespace
900 {
901
902 inline int GetScrollPosition(HWND hWnd, int wOrient)
903 {
904 #ifdef __WXMICROWIN__
905 return ::GetScrollPosWX(hWnd, wOrient);
906 #else
907 WinStruct<SCROLLINFO> scrollInfo;
908 scrollInfo.cbSize = sizeof(SCROLLINFO);
909 scrollInfo.fMask = SIF_POS;
910 ::GetScrollInfo(hWnd, wOrient, &scrollInfo );
911
912 return scrollInfo.nPos;
913
914 #endif
915 }
916
917 inline UINT WXOrientToSB(int orient)
918 {
919 return orient == wxHORIZONTAL ? SB_HORZ : SB_VERT;
920 }
921
922 } // anonymous namespace
923
924 int wxWindowMSW::GetScrollPos(int orient) const
925 {
926 HWND hWnd = GetHwnd();
927 wxCHECK_MSG( hWnd, 0, wxT("no HWND in GetScrollPos") );
928
929 return GetScrollPosition(hWnd, WXOrientToSB(orient));
930 }
931
932 // This now returns the whole range, not just the number
933 // of positions that we can scroll.
934 int wxWindowMSW::GetScrollRange(int orient) const
935 {
936 int maxPos;
937 HWND hWnd = GetHwnd();
938 if ( !hWnd )
939 return 0;
940 WinStruct<SCROLLINFO> scrollInfo;
941 scrollInfo.fMask = SIF_RANGE;
942 if ( !::GetScrollInfo(hWnd, WXOrientToSB(orient), &scrollInfo) )
943 {
944 // Most of the time this is not really an error, since the return
945 // value can also be zero when there is no scrollbar yet.
946 // wxLogLastError(wxT("GetScrollInfo"));
947 }
948 maxPos = scrollInfo.nMax;
949
950 // undo "range - 1" done in SetScrollbar()
951 return maxPos + 1;
952 }
953
954 int wxWindowMSW::GetScrollThumb(int orient) const
955 {
956 return orient == wxHORIZONTAL ? m_xThumbSize : m_yThumbSize;
957 }
958
959 void wxWindowMSW::SetScrollPos(int orient, int pos, bool refresh)
960 {
961 HWND hWnd = GetHwnd();
962 wxCHECK_RET( hWnd, wxT("SetScrollPos: no HWND") );
963
964 WinStruct<SCROLLINFO> info;
965 info.nPage = 0;
966 info.nMin = 0;
967 info.nPos = pos;
968 info.fMask = SIF_POS;
969 if ( HasFlag(wxALWAYS_SHOW_SB) )
970 {
971 // disable scrollbar instead of removing it then
972 info.fMask |= SIF_DISABLENOSCROLL;
973 }
974
975 ::SetScrollInfo(hWnd, WXOrientToSB(orient), &info, refresh);
976 }
977
978 // New function that will replace some of the above.
979 void wxWindowMSW::SetScrollbar(int orient,
980 int pos,
981 int pageSize,
982 int range,
983 bool refresh)
984 {
985 // We have to set the variables here to make them valid in events
986 // triggered by ::SetScrollInfo()
987 *(orient == wxHORIZONTAL ? &m_xThumbSize : &m_yThumbSize) = pageSize;
988
989 HWND hwnd = GetHwnd();
990 if ( !hwnd )
991 return;
992
993 WinStruct<SCROLLINFO> info;
994 if ( range != -1 )
995 {
996 info.nPage = pageSize;
997 info.nMin = 0; // range is nMax - nMin + 1
998 info.nMax = range - 1; // as both nMax and nMax are inclusive
999 info.nPos = pos;
1000
1001 // We normally also reenable scrollbar in case it had been previously
1002 // disabled by specifying SIF_DISABLENOSCROLL below but we should only
1003 // do this if it has valid range, otherwise it would be enabled but not
1004 // do anything.
1005 if ( range >= pageSize )
1006 {
1007 ::EnableScrollBar(hwnd, WXOrientToSB(orient), ESB_ENABLE_BOTH);
1008 }
1009 }
1010 //else: leave all the fields to be 0
1011
1012 info.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1013 if ( HasFlag(wxALWAYS_SHOW_SB) || range == -1 )
1014 {
1015 // disable scrollbar instead of removing it then
1016 info.fMask |= SIF_DISABLENOSCROLL;
1017 }
1018
1019 ::SetScrollInfo(hwnd, WXOrientToSB(orient), &info, refresh);
1020 }
1021
1022 void wxWindowMSW::ScrollWindow(int dx, int dy, const wxRect *prect)
1023 {
1024 RECT rect;
1025 RECT *pr;
1026 if ( prect )
1027 {
1028 wxCopyRectToRECT(*prect, rect);
1029 pr = &rect;
1030 }
1031 else
1032 {
1033 pr = NULL;
1034
1035 }
1036
1037 #ifdef __WXWINCE__
1038 // FIXME: is this the exact equivalent of the line below?
1039 ::ScrollWindowEx(GetHwnd(), dx, dy, pr, pr, 0, 0, SW_SCROLLCHILDREN|SW_ERASE|SW_INVALIDATE);
1040 #else
1041 ::ScrollWindow(GetHwnd(), dx, dy, pr, pr);
1042 #endif
1043 }
1044
1045 static bool ScrollVertically(HWND hwnd, int kind, int count)
1046 {
1047 int posStart = GetScrollPosition(hwnd, SB_VERT);
1048
1049 int pos = posStart;
1050 for ( int n = 0; n < count; n++ )
1051 {
1052 ::SendMessage(hwnd, WM_VSCROLL, kind, 0);
1053
1054 int posNew = GetScrollPosition(hwnd, SB_VERT);
1055 if ( posNew == pos )
1056 {
1057 // don't bother to continue, we're already at top/bottom
1058 break;
1059 }
1060
1061 pos = posNew;
1062 }
1063
1064 return pos != posStart;
1065 }
1066
1067 bool wxWindowMSW::ScrollLines(int lines)
1068 {
1069 bool down = lines > 0;
1070
1071 return ScrollVertically(GetHwnd(),
1072 down ? SB_LINEDOWN : SB_LINEUP,
1073 down ? lines : -lines);
1074 }
1075
1076 bool wxWindowMSW::ScrollPages(int pages)
1077 {
1078 bool down = pages > 0;
1079
1080 return ScrollVertically(GetHwnd(),
1081 down ? SB_PAGEDOWN : SB_PAGEUP,
1082 down ? pages : -pages);
1083 }
1084
1085 // ----------------------------------------------------------------------------
1086 // RTL support
1087 // ----------------------------------------------------------------------------
1088
1089 void wxWindowMSW::SetLayoutDirection(wxLayoutDirection dir)
1090 {
1091 #ifdef __WXWINCE__
1092 wxUnusedVar(dir);
1093 #else
1094 wxCHECK_RET( GetHwnd(),
1095 wxT("layout direction must be set after window creation") );
1096
1097 LONG styleOld = wxGetWindowExStyle(this);
1098
1099 LONG styleNew = styleOld;
1100 switch ( dir )
1101 {
1102 case wxLayout_LeftToRight:
1103 styleNew &= ~WS_EX_LAYOUTRTL;
1104 break;
1105
1106 case wxLayout_RightToLeft:
1107 styleNew |= WS_EX_LAYOUTRTL;
1108 break;
1109
1110 default:
1111 wxFAIL_MSG(wxT("unsupported layout direction"));
1112 break;
1113 }
1114
1115 if ( styleNew != styleOld )
1116 {
1117 wxSetWindowExStyle(this, styleNew);
1118 }
1119 #endif
1120 }
1121
1122 wxLayoutDirection wxWindowMSW::GetLayoutDirection() const
1123 {
1124 #ifdef __WXWINCE__
1125 return wxLayout_Default;
1126 #else
1127 wxCHECK_MSG( GetHwnd(), wxLayout_Default, wxT("invalid window") );
1128
1129 return wxHasWindowExStyle(this, WS_EX_LAYOUTRTL) ? wxLayout_RightToLeft
1130 : wxLayout_LeftToRight;
1131 #endif
1132 }
1133
1134 wxCoord
1135 wxWindowMSW::AdjustForLayoutDirection(wxCoord x,
1136 wxCoord WXUNUSED(width),
1137 wxCoord WXUNUSED(widthTotal)) const
1138 {
1139 // Win32 mirrors the coordinates of RTL windows automatically, so don't
1140 // redo it ourselves
1141 return x;
1142 }
1143
1144 // ---------------------------------------------------------------------------
1145 // subclassing
1146 // ---------------------------------------------------------------------------
1147
1148 void wxWindowMSW::SubclassWin(WXHWND hWnd)
1149 {
1150 wxASSERT_MSG( !m_oldWndProc, wxT("subclassing window twice?") );
1151
1152 HWND hwnd = (HWND)hWnd;
1153 wxCHECK_RET( ::IsWindow(hwnd), wxT("invalid HWND in SubclassWin") );
1154
1155 SetHWND(hWnd);
1156
1157 wxAssociateWinWithHandle(hwnd, this);
1158
1159 m_oldWndProc = (WXFARPROC)wxGetWindowProc((HWND)hWnd);
1160
1161 // we don't need to subclass the window of our own class (in the Windows
1162 // sense of the word)
1163 if ( !wxCheckWindowWndProc(hWnd, (WXFARPROC)wxWndProc) )
1164 {
1165 wxSetWindowProc(hwnd, wxWndProc);
1166 }
1167 else
1168 {
1169 // don't bother restoring it either: this also makes it easy to
1170 // implement IsOfStandardClass() method which returns true for the
1171 // standard controls and false for the wxWidgets own windows as it can
1172 // simply check m_oldWndProc
1173 m_oldWndProc = NULL;
1174 }
1175
1176 // we're officially created now, send the event
1177 wxWindowCreateEvent event((wxWindow *)this);
1178 (void)HandleWindowEvent(event);
1179 }
1180
1181 void wxWindowMSW::UnsubclassWin()
1182 {
1183 wxRemoveHandleAssociation(this);
1184
1185 // Restore old Window proc
1186 HWND hwnd = GetHwnd();
1187 if ( hwnd )
1188 {
1189 SetHWND(0);
1190
1191 wxCHECK_RET( ::IsWindow(hwnd), wxT("invalid HWND in UnsubclassWin") );
1192
1193 if ( m_oldWndProc )
1194 {
1195 if ( !wxCheckWindowWndProc((WXHWND)hwnd, m_oldWndProc) )
1196 {
1197 wxSetWindowProc(hwnd, (WNDPROC)m_oldWndProc);
1198 }
1199
1200 m_oldWndProc = NULL;
1201 }
1202 }
1203 }
1204
1205 void wxWindowMSW::AssociateHandle(WXWidget handle)
1206 {
1207 if ( m_hWnd )
1208 {
1209 if ( !::DestroyWindow(GetHwnd()) )
1210 {
1211 wxLogLastError(wxT("DestroyWindow"));
1212 }
1213 }
1214
1215 WXHWND wxhwnd = (WXHWND)handle;
1216
1217 // this also calls SetHWND(wxhwnd)
1218 SubclassWin(wxhwnd);
1219 }
1220
1221 void wxWindowMSW::DissociateHandle()
1222 {
1223 // this also calls SetHWND(0) for us
1224 UnsubclassWin();
1225 }
1226
1227
1228 bool wxCheckWindowWndProc(WXHWND hWnd,
1229 WXFARPROC WXUNUSED(wndProc))
1230 {
1231 const wxString str(wxGetWindowClass(hWnd));
1232
1233 // TODO: get rid of wxTLWHiddenParent special case (currently it's not
1234 // registered by wxApp but using ad hoc code in msw/toplevel.cpp);
1235 // there is also a hidden window class used by sockets &c
1236 return wxApp::IsRegisteredClassName(str) || str == wxT("wxTLWHiddenParent");
1237 }
1238
1239 // ----------------------------------------------------------------------------
1240 // Style handling
1241 // ----------------------------------------------------------------------------
1242
1243 void wxWindowMSW::SetWindowStyleFlag(long flags)
1244 {
1245 long flagsOld = GetWindowStyleFlag();
1246 if ( flags == flagsOld )
1247 return;
1248
1249 // update the internal variable
1250 wxWindowBase::SetWindowStyleFlag(flags);
1251
1252 // and the real window flags
1253 MSWUpdateStyle(flagsOld, GetExtraStyle());
1254 }
1255
1256 void wxWindowMSW::SetExtraStyle(long exflags)
1257 {
1258 long exflagsOld = GetExtraStyle();
1259 if ( exflags == exflagsOld )
1260 return;
1261
1262 // update the internal variable
1263 wxWindowBase::SetExtraStyle(exflags);
1264
1265 // and the real window flags
1266 MSWUpdateStyle(GetWindowStyleFlag(), exflagsOld);
1267 }
1268
1269 void wxWindowMSW::MSWUpdateStyle(long flagsOld, long exflagsOld)
1270 {
1271 // now update the Windows style as well if needed - and if the window had
1272 // been already created
1273 if ( !GetHwnd() )
1274 return;
1275
1276 // we may need to call SetWindowPos() when we change some styles
1277 bool callSWP = false;
1278
1279 WXDWORD exstyle;
1280 long style = MSWGetStyle(GetWindowStyleFlag(), &exstyle);
1281
1282 // this is quite a horrible hack but we need it because MSWGetStyle()
1283 // doesn't take exflags as parameter but uses GetExtraStyle() internally
1284 // and so we have to modify the window exflags temporarily to get the
1285 // correct exstyleOld
1286 long exflagsNew = GetExtraStyle();
1287 wxWindowBase::SetExtraStyle(exflagsOld);
1288
1289 WXDWORD exstyleOld;
1290 long styleOld = MSWGetStyle(flagsOld, &exstyleOld);
1291
1292 wxWindowBase::SetExtraStyle(exflagsNew);
1293
1294
1295 if ( style != styleOld )
1296 {
1297 // some flags (e.g. WS_VISIBLE or WS_DISABLED) should not be changed by
1298 // this function so instead of simply setting the style to the new
1299 // value we clear the bits which were set in styleOld but are set in
1300 // the new one and set the ones which were not set before
1301 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1302 styleReal &= ~styleOld;
1303 styleReal |= style;
1304
1305 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
1306
1307 // we need to call SetWindowPos() if any of the styles affecting the
1308 // frame appearance have changed
1309 callSWP = ((styleOld ^ style ) & (WS_BORDER |
1310 WS_THICKFRAME |
1311 WS_CAPTION |
1312 WS_DLGFRAME |
1313 WS_MAXIMIZEBOX |
1314 WS_MINIMIZEBOX |
1315 WS_SYSMENU) ) != 0;
1316 }
1317
1318 // and the extended style
1319 long exstyleReal = wxGetWindowExStyle(this);
1320
1321 if ( exstyle != exstyleOld )
1322 {
1323 exstyleReal &= ~exstyleOld;
1324 exstyleReal |= exstyle;
1325
1326 wxSetWindowExStyle(this, exstyleReal);
1327
1328 // ex style changes don't take effect without calling SetWindowPos
1329 callSWP = true;
1330 }
1331
1332 if ( callSWP )
1333 {
1334 // we must call SetWindowPos() to flush the cached extended style and
1335 // also to make the change to wxSTAY_ON_TOP style take effect: just
1336 // setting the style simply doesn't work
1337 if ( !::SetWindowPos(GetHwnd(),
1338 exstyleReal & WS_EX_TOPMOST ? HWND_TOPMOST
1339 : HWND_NOTOPMOST,
1340 0, 0, 0, 0,
1341 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
1342 SWP_FRAMECHANGED) )
1343 {
1344 wxLogLastError(wxT("SetWindowPos"));
1345 }
1346 }
1347 }
1348
1349 wxBorder wxWindowMSW::GetDefaultBorderForControl() const
1350 {
1351 return wxBORDER_THEME;
1352 }
1353
1354 wxBorder wxWindowMSW::GetDefaultBorder() const
1355 {
1356 return wxWindowBase::GetDefaultBorder();
1357 }
1358
1359 // Translate wxBORDER_THEME (and other border styles if necessary) to the value
1360 // that makes most sense for this Windows environment
1361 wxBorder wxWindowMSW::TranslateBorder(wxBorder border) const
1362 {
1363 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
1364 if (border == wxBORDER_THEME || border == wxBORDER_SUNKEN || border == wxBORDER_SIMPLE)
1365 return wxBORDER_SIMPLE;
1366 else
1367 return wxBORDER_NONE;
1368 #else
1369 #if wxUSE_UXTHEME
1370 if (border == wxBORDER_THEME)
1371 {
1372 if (CanApplyThemeBorder())
1373 {
1374 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
1375 if (theme)
1376 return wxBORDER_THEME;
1377 }
1378 return wxBORDER_SUNKEN;
1379 }
1380 #endif
1381 return border;
1382 #endif
1383 }
1384
1385
1386 WXDWORD wxWindowMSW::MSWGetStyle(long flags, WXDWORD *exstyle) const
1387 {
1388 // translate common wxWidgets styles to Windows ones
1389
1390 // most of windows are child ones, those which are not (such as
1391 // wxTopLevelWindow) should remove WS_CHILD in their MSWGetStyle()
1392 WXDWORD style = WS_CHILD;
1393
1394 // using this flag results in very significant reduction in flicker,
1395 // especially with controls inside the static boxes (as the interior of the
1396 // box is not redrawn twice), but sometimes results in redraw problems, so
1397 // optionally allow the old code to continue to use it provided a special
1398 // system option is turned on
1399 if ( !wxSystemOptions::GetOptionInt(wxT("msw.window.no-clip-children"))
1400 || (flags & wxCLIP_CHILDREN) )
1401 style |= WS_CLIPCHILDREN;
1402
1403 // it doesn't seem useful to use WS_CLIPSIBLINGS here as we officially
1404 // don't support overlapping windows and it only makes sense for them and,
1405 // presumably, gives the system some extra work (to manage more clipping
1406 // regions), so avoid it alltogether
1407
1408
1409 if ( flags & wxVSCROLL )
1410 style |= WS_VSCROLL;
1411
1412 if ( flags & wxHSCROLL )
1413 style |= WS_HSCROLL;
1414
1415 const wxBorder border = TranslateBorder(GetBorder(flags));
1416
1417 // After translation, border is now optimized for the specific version of Windows
1418 // and theme engine presence.
1419
1420 // WS_BORDER is only required for wxBORDER_SIMPLE
1421 if ( border == wxBORDER_SIMPLE )
1422 style |= WS_BORDER;
1423
1424 // now deal with ext style if the caller wants it
1425 if ( exstyle )
1426 {
1427 *exstyle = 0;
1428
1429 #ifndef __WXWINCE__
1430 if ( flags & wxTRANSPARENT_WINDOW )
1431 *exstyle |= WS_EX_TRANSPARENT;
1432 #endif
1433
1434 switch ( border )
1435 {
1436 default:
1437 case wxBORDER_DEFAULT:
1438 wxFAIL_MSG( wxT("unknown border style") );
1439 // fall through
1440
1441 case wxBORDER_NONE:
1442 case wxBORDER_SIMPLE:
1443 case wxBORDER_THEME:
1444 break;
1445
1446 case wxBORDER_STATIC:
1447 *exstyle |= WS_EX_STATICEDGE;
1448 break;
1449
1450 case wxBORDER_RAISED:
1451 *exstyle |= WS_EX_DLGMODALFRAME;
1452 break;
1453
1454 case wxBORDER_SUNKEN:
1455 *exstyle |= WS_EX_CLIENTEDGE;
1456 style &= ~WS_BORDER;
1457 break;
1458
1459 // case wxBORDER_DOUBLE:
1460 // *exstyle |= WS_EX_DLGMODALFRAME;
1461 // break;
1462 }
1463
1464 // wxUniv doesn't use Windows dialog navigation functions at all
1465 #if !defined(__WXUNIVERSAL__) && !defined(__WXWINCE__)
1466 // to make the dialog navigation work with the nested panels we must
1467 // use this style (top level windows such as dialogs don't need it)
1468 if ( (flags & wxTAB_TRAVERSAL) && !IsTopLevel() )
1469 {
1470 *exstyle |= WS_EX_CONTROLPARENT;
1471 }
1472 #endif // __WXUNIVERSAL__
1473 }
1474
1475 return style;
1476 }
1477
1478 // Setup background and foreground colours correctly
1479 void wxWindowMSW::SetupColours()
1480 {
1481 if ( GetParent() )
1482 SetBackgroundColour(GetParent()->GetBackgroundColour());
1483 }
1484
1485 bool wxWindowMSW::IsMouseInWindow() const
1486 {
1487 // get the mouse position
1488 POINT pt;
1489 #ifdef __WXWINCE__
1490 ::GetCursorPosWinCE(&pt);
1491 #else
1492 ::GetCursorPos(&pt);
1493 #endif
1494
1495 // find the window which currently has the cursor and go up the window
1496 // chain until we find this window - or exhaust it
1497 HWND hwnd = ::WindowFromPoint(pt);
1498 while ( hwnd && (hwnd != GetHwnd()) )
1499 hwnd = ::GetParent(hwnd);
1500
1501 return hwnd != NULL;
1502 }
1503
1504 void wxWindowMSW::OnInternalIdle()
1505 {
1506 #ifndef HAVE_TRACKMOUSEEVENT
1507 // Check if we need to send a LEAVE event
1508 if ( m_mouseInWindow )
1509 {
1510 // note that we should generate the leave event whether the window has
1511 // or doesn't have mouse capture
1512 if ( !IsMouseInWindow() )
1513 {
1514 GenerateMouseLeave();
1515 }
1516 }
1517 #endif // !HAVE_TRACKMOUSEEVENT
1518
1519 wxWindowBase::OnInternalIdle();
1520 }
1521
1522 // Set this window to be the child of 'parent'.
1523 bool wxWindowMSW::Reparent(wxWindowBase *parent)
1524 {
1525 if ( !wxWindowBase::Reparent(parent) )
1526 return false;
1527
1528 HWND hWndChild = GetHwnd();
1529 HWND hWndParent = GetParent() ? GetWinHwnd(GetParent()) : (HWND)0;
1530
1531 ::SetParent(hWndChild, hWndParent);
1532
1533 #ifndef __WXWINCE__
1534 if ( wxHasWindowExStyle(this, WS_EX_CONTROLPARENT) )
1535 {
1536 EnsureParentHasControlParentStyle(GetParent());
1537 }
1538 #endif // !__WXWINCE__
1539
1540 return true;
1541 }
1542
1543 static inline void SendSetRedraw(HWND hwnd, bool on)
1544 {
1545 #ifndef __WXMICROWIN__
1546 ::SendMessage(hwnd, WM_SETREDRAW, (WPARAM)on, 0);
1547 #endif
1548 }
1549
1550 void wxWindowMSW::DoFreeze()
1551 {
1552 if ( !IsShown() )
1553 return; // no point in freezing hidden window
1554
1555 SendSetRedraw(GetHwnd(), false);
1556 }
1557
1558 void wxWindowMSW::DoThaw()
1559 {
1560 if ( !IsShown() )
1561 return; // hidden windows aren't frozen by DoFreeze
1562
1563 SendSetRedraw(GetHwnd(), true);
1564
1565 // we need to refresh everything or otherwise the invalidated area
1566 // is not going to be repainted
1567 Refresh();
1568 }
1569
1570 void wxWindowMSW::Refresh(bool eraseBack, const wxRect *rect)
1571 {
1572 HWND hWnd = GetHwnd();
1573 if ( hWnd )
1574 {
1575 RECT mswRect;
1576 const RECT *pRect;
1577 if ( rect )
1578 {
1579 wxCopyRectToRECT(*rect, mswRect);
1580 pRect = &mswRect;
1581 }
1582 else
1583 {
1584 pRect = NULL;
1585 }
1586
1587 // RedrawWindow not available on SmartPhone or eVC++ 3
1588 #if !defined(__SMARTPHONE__) && !(defined(_WIN32_WCE) && _WIN32_WCE < 400)
1589 UINT flags = RDW_INVALIDATE | RDW_ALLCHILDREN;
1590 if ( eraseBack )
1591 flags |= RDW_ERASE;
1592
1593 ::RedrawWindow(hWnd, pRect, NULL, flags);
1594 #else
1595 ::InvalidateRect(hWnd, pRect, eraseBack);
1596 #endif
1597 }
1598 }
1599
1600 void wxWindowMSW::Update()
1601 {
1602 if ( !::UpdateWindow(GetHwnd()) )
1603 {
1604 wxLogLastError(wxT("UpdateWindow"));
1605 }
1606
1607 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1608 // just calling UpdateWindow() is not enough, what we did in our WM_PAINT
1609 // handler needs to be really drawn right now
1610 (void)::GdiFlush();
1611 #endif // __WIN32__
1612 }
1613
1614 // ---------------------------------------------------------------------------
1615 // drag and drop
1616 // ---------------------------------------------------------------------------
1617
1618 #if wxUSE_DRAG_AND_DROP || !defined(__WXWINCE__)
1619
1620 #if wxUSE_STATBOX
1621
1622 // we need to lower the sibling static boxes so controls contained within can be
1623 // a drop target
1624 static void AdjustStaticBoxZOrder(wxWindow *parent)
1625 {
1626 // no sibling static boxes if we have no parent (ie TLW)
1627 if ( !parent )
1628 return;
1629
1630 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1631 node;
1632 node = node->GetNext() )
1633 {
1634 wxStaticBox *statbox = wxDynamicCast(node->GetData(), wxStaticBox);
1635 if ( statbox )
1636 {
1637 ::SetWindowPos(GetHwndOf(statbox), HWND_BOTTOM, 0, 0, 0, 0,
1638 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
1639 }
1640 }
1641 }
1642
1643 #else // !wxUSE_STATBOX
1644
1645 static inline void AdjustStaticBoxZOrder(wxWindow * WXUNUSED(parent))
1646 {
1647 }
1648
1649 #endif // wxUSE_STATBOX/!wxUSE_STATBOX
1650
1651 #endif // drag and drop is used
1652
1653 #if wxUSE_DRAG_AND_DROP
1654 void wxWindowMSW::SetDropTarget(wxDropTarget *pDropTarget)
1655 {
1656 if ( m_dropTarget != 0 ) {
1657 m_dropTarget->Revoke(m_hWnd);
1658 delete m_dropTarget;
1659 }
1660
1661 m_dropTarget = pDropTarget;
1662 if ( m_dropTarget != 0 )
1663 {
1664 AdjustStaticBoxZOrder(GetParent());
1665 m_dropTarget->Register(m_hWnd);
1666 }
1667 }
1668 #endif // wxUSE_DRAG_AND_DROP
1669
1670 // old-style file manager drag&drop support: we retain the old-style
1671 // DragAcceptFiles in parallel with SetDropTarget.
1672 void wxWindowMSW::DragAcceptFiles(bool WXUNUSED_IN_WINCE(accept))
1673 {
1674 #ifndef __WXWINCE__
1675 HWND hWnd = GetHwnd();
1676 if ( hWnd )
1677 {
1678 AdjustStaticBoxZOrder(GetParent());
1679 ::DragAcceptFiles(hWnd, (BOOL)accept);
1680 }
1681 #endif
1682 }
1683
1684 // ----------------------------------------------------------------------------
1685 // tooltips
1686 // ----------------------------------------------------------------------------
1687
1688 #if wxUSE_TOOLTIPS
1689
1690 void wxWindowMSW::DoSetToolTip(wxToolTip *tooltip)
1691 {
1692 wxWindowBase::DoSetToolTip(tooltip);
1693
1694 if ( m_tooltip )
1695 m_tooltip->SetWindow((wxWindow *)this);
1696 }
1697
1698 #endif // wxUSE_TOOLTIPS
1699
1700 // ---------------------------------------------------------------------------
1701 // moving and resizing
1702 // ---------------------------------------------------------------------------
1703
1704 bool wxWindowMSW::IsSizeDeferred() const
1705 {
1706 #if wxUSE_DEFERRED_SIZING
1707 if ( m_pendingPosition != wxDefaultPosition ||
1708 m_pendingSize != wxDefaultSize )
1709 return true;
1710 #endif // wxUSE_DEFERRED_SIZING
1711
1712 return false;
1713 }
1714
1715 // Get total size
1716 void wxWindowMSW::DoGetSize(int *x, int *y) const
1717 {
1718 #if wxUSE_DEFERRED_SIZING
1719 // if SetSize() had been called at wx level but not realized at Windows
1720 // level yet (i.e. EndDeferWindowPos() not called), we still should return
1721 // the new and not the old position to the other wx code
1722 if ( m_pendingSize != wxDefaultSize )
1723 {
1724 if ( x )
1725 *x = m_pendingSize.x;
1726 if ( y )
1727 *y = m_pendingSize.y;
1728 }
1729 else // use current size
1730 #endif // wxUSE_DEFERRED_SIZING
1731 {
1732 RECT rect = wxGetWindowRect(GetHwnd());
1733
1734 if ( x )
1735 *x = rect.right - rect.left;
1736 if ( y )
1737 *y = rect.bottom - rect.top;
1738 }
1739 }
1740
1741 // Get size *available for subwindows* i.e. excluding menu bar etc.
1742 void wxWindowMSW::DoGetClientSize(int *x, int *y) const
1743 {
1744 #if wxUSE_DEFERRED_SIZING
1745 if ( m_pendingSize != wxDefaultSize )
1746 {
1747 // we need to calculate the client size corresponding to pending size
1748 //
1749 // FIXME: Unfortunately this doesn't work correctly for the maximized
1750 // top level windows, the returned values are too small (e.g.
1751 // under Windows 7 on a 1600*1200 screen with task bar on the
1752 // right the pending size for a maximized window is 1538*1200
1753 // and WM_NCCALCSIZE returns 1528*1172 even though the correct
1754 // client size of such window is 1538*1182). No idea how to fix
1755 // it though, setting WS_MAXIMIZE in GWL_STYLE before calling
1756 // WM_NCCALCSIZE doesn't help and AdjustWindowRectEx() doesn't
1757 // work in this direction neither. So we just have to live with
1758 // the slightly wrong results and relayout the window when it
1759 // gets finally shown in its maximized state (see #11762).
1760 RECT rect;
1761 rect.left = m_pendingPosition.x;
1762 rect.top = m_pendingPosition.y;
1763 rect.right = rect.left + m_pendingSize.x;
1764 rect.bottom = rect.top + m_pendingSize.y;
1765
1766 ::SendMessage(GetHwnd(), WM_NCCALCSIZE, FALSE, (LPARAM)&rect);
1767
1768 if ( x )
1769 *x = rect.right - rect.left;
1770 if ( y )
1771 *y = rect.bottom - rect.top;
1772 }
1773 else
1774 #endif // wxUSE_DEFERRED_SIZING
1775 {
1776 RECT rect = wxGetClientRect(GetHwnd());
1777
1778 if ( x )
1779 *x = rect.right;
1780 if ( y )
1781 *y = rect.bottom;
1782 }
1783 }
1784
1785 void wxWindowMSW::DoGetPosition(int *x, int *y) const
1786 {
1787 wxWindow * const parent = GetParent();
1788
1789 wxPoint pos;
1790 #if wxUSE_DEFERRED_SIZING
1791 if ( m_pendingPosition != wxDefaultPosition )
1792 {
1793 pos = m_pendingPosition;
1794 }
1795 else // use current position
1796 #endif // wxUSE_DEFERRED_SIZING
1797 {
1798 RECT rect = wxGetWindowRect(GetHwnd());
1799
1800 POINT point;
1801 point.x = rect.left;
1802 point.y = rect.top;
1803
1804 // we do the adjustments with respect to the parent only for the "real"
1805 // children, not for the dialogs/frames
1806 if ( !IsTopLevel() )
1807 {
1808 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
1809 {
1810 // In RTL mode, we want the logical left x-coordinate,
1811 // which would be the physical right x-coordinate.
1812 point.x = rect.right;
1813 }
1814
1815 // Since we now have the absolute screen coords, if there's a
1816 // parent we must subtract its top left corner
1817 if ( parent )
1818 {
1819 ::ScreenToClient(GetHwndOf(parent), &point);
1820 }
1821 }
1822
1823 pos.x = point.x;
1824 pos.y = point.y;
1825 }
1826
1827 // we also must adjust by the client area offset: a control which is just
1828 // under a toolbar could be at (0, 30) in Windows but at (0, 0) in wx
1829 if ( parent && !IsTopLevel() )
1830 {
1831 const wxPoint pt(parent->GetClientAreaOrigin());
1832 pos.x -= pt.x;
1833 pos.y -= pt.y;
1834 }
1835
1836 if ( x )
1837 *x = pos.x;
1838 if ( y )
1839 *y = pos.y;
1840 }
1841
1842 void wxWindowMSW::DoScreenToClient(int *x, int *y) const
1843 {
1844 POINT pt;
1845 if ( x )
1846 pt.x = *x;
1847 if ( y )
1848 pt.y = *y;
1849
1850 ::ScreenToClient(GetHwnd(), &pt);
1851
1852 if ( x )
1853 *x = pt.x;
1854 if ( y )
1855 *y = pt.y;
1856 }
1857
1858 void wxWindowMSW::DoClientToScreen(int *x, int *y) const
1859 {
1860 POINT pt;
1861 if ( x )
1862 pt.x = *x;
1863 if ( y )
1864 pt.y = *y;
1865
1866 ::ClientToScreen(GetHwnd(), &pt);
1867
1868 if ( x )
1869 *x = pt.x;
1870 if ( y )
1871 *y = pt.y;
1872 }
1873
1874 bool
1875 wxWindowMSW::DoMoveSibling(WXHWND hwnd, int x, int y, int width, int height)
1876 {
1877 #if wxUSE_DEFERRED_SIZING
1878 // if our parent had prepared a defer window handle for us, use it (unless
1879 // we are a top level window)
1880 wxWindowMSW * const parent = IsTopLevel() ? NULL : GetParent();
1881
1882 HDWP hdwp = parent ? (HDWP)parent->m_hDWP : NULL;
1883 if ( hdwp )
1884 {
1885 hdwp = ::DeferWindowPos(hdwp, (HWND)hwnd, NULL, x, y, width, height,
1886 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
1887 if ( !hdwp )
1888 {
1889 wxLogLastError(wxT("DeferWindowPos"));
1890 }
1891 }
1892
1893 if ( parent )
1894 {
1895 // hdwp must be updated as it may have been changed
1896 parent->m_hDWP = (WXHANDLE)hdwp;
1897 }
1898
1899 if ( hdwp )
1900 {
1901 // did deferred move, remember new coordinates of the window as they're
1902 // different from what Windows would return for it
1903 return true;
1904 }
1905
1906 // otherwise (or if deferring failed) move the window in place immediately
1907 #endif // wxUSE_DEFERRED_SIZING
1908 if ( !::MoveWindow((HWND)hwnd, x, y, width, height, IsShown()) )
1909 {
1910 wxLogLastError(wxT("MoveWindow"));
1911 }
1912
1913 // if wxUSE_DEFERRED_SIZING, indicates that we didn't use deferred move,
1914 // ignored otherwise
1915 return false;
1916 }
1917
1918 void wxWindowMSW::DoMoveWindow(int x, int y, int width, int height)
1919 {
1920 // TODO: is this consistent with other platforms?
1921 // Still, negative width or height shouldn't be allowed
1922 if (width < 0)
1923 width = 0;
1924 if (height < 0)
1925 height = 0;
1926
1927 if ( DoMoveSibling(m_hWnd, x, y, width, height) )
1928 {
1929 #if wxUSE_DEFERRED_SIZING
1930 m_pendingPosition = wxPoint(x, y);
1931 m_pendingSize = wxSize(width, height);
1932 }
1933 else // window was moved immediately, without deferring it
1934 {
1935 m_pendingPosition = wxDefaultPosition;
1936 m_pendingSize = wxDefaultSize;
1937 #endif // wxUSE_DEFERRED_SIZING
1938 }
1939 }
1940
1941 // set the size of the window: if the dimensions are positive, just use them,
1942 // but if any of them is equal to -1, it means that we must find the value for
1943 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1944 // which case -1 is a valid value for x and y)
1945 //
1946 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1947 // the width/height to best suit our contents, otherwise we reuse the current
1948 // width/height
1949 void wxWindowMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1950 {
1951 // get the current size and position...
1952 int currentX, currentY;
1953 int currentW, currentH;
1954
1955 GetPosition(&currentX, &currentY);
1956 GetSize(&currentW, &currentH);
1957
1958 // ... and don't do anything (avoiding flicker) if it's already ok unless
1959 // we're forced to resize the window
1960 if ( x == currentX && y == currentY &&
1961 width == currentW && height == currentH &&
1962 !(sizeFlags & wxSIZE_FORCE) )
1963 {
1964 if (sizeFlags & wxSIZE_FORCE_EVENT)
1965 {
1966 wxSizeEvent event( wxSize(width,height), GetId() );
1967 event.SetEventObject( this );
1968 HandleWindowEvent( event );
1969 }
1970 return;
1971 }
1972
1973 if ( x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1974 x = currentX;
1975 if ( y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1976 y = currentY;
1977
1978 AdjustForParentClientOrigin(x, y, sizeFlags);
1979
1980 wxSize size = wxDefaultSize;
1981 if ( width == wxDefaultCoord )
1982 {
1983 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1984 {
1985 size = GetBestSize();
1986 width = size.x;
1987 }
1988 else
1989 {
1990 // just take the current one
1991 width = currentW;
1992 }
1993 }
1994
1995 if ( height == wxDefaultCoord )
1996 {
1997 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1998 {
1999 if ( size.x == wxDefaultCoord )
2000 {
2001 size = GetBestSize();
2002 }
2003 //else: already called GetBestSize() above
2004
2005 height = size.y;
2006 }
2007 else
2008 {
2009 // just take the current one
2010 height = currentH;
2011 }
2012 }
2013
2014 DoMoveWindow(x, y, width, height);
2015 }
2016
2017 void wxWindowMSW::DoSetClientSize(int width, int height)
2018 {
2019 // setting the client size is less obvious than it could have been
2020 // because in the result of changing the total size the window scrollbar
2021 // may [dis]appear and/or its menubar may [un]wrap (and AdjustWindowRect()
2022 // doesn't take neither into account) and so the client size will not be
2023 // correct as the difference between the total and client size changes --
2024 // so we keep changing it until we get it right
2025 //
2026 // normally this loop shouldn't take more than 3 iterations (usually 1 but
2027 // if scrollbars [dis]appear as the result of the first call, then 2 and it
2028 // may become 3 if the window had 0 size originally and so we didn't
2029 // calculate the scrollbar correction correctly during the first iteration)
2030 // but just to be on the safe side we check for it instead of making it an
2031 // "infinite" loop (i.e. leaving break inside as the only way to get out)
2032 for ( int i = 0; i < 4; i++ )
2033 {
2034 RECT rectClient;
2035 ::GetClientRect(GetHwnd(), &rectClient);
2036
2037 // if the size is already ok, stop here (NB: rectClient.left = top = 0)
2038 if ( (rectClient.right == width || width == wxDefaultCoord) &&
2039 (rectClient.bottom == height || height == wxDefaultCoord) )
2040 {
2041 break;
2042 }
2043
2044 // Find the difference between the entire window (title bar and all)
2045 // and the client area; add this to the new client size to move the
2046 // window
2047 RECT rectWin;
2048 ::GetWindowRect(GetHwnd(), &rectWin);
2049
2050 const int widthWin = rectWin.right - rectWin.left,
2051 heightWin = rectWin.bottom - rectWin.top;
2052
2053 // MoveWindow positions the child windows relative to the parent, so
2054 // adjust if necessary
2055 if ( !IsTopLevel() )
2056 {
2057 wxWindow *parent = GetParent();
2058 if ( parent )
2059 {
2060 ::ScreenToClient(GetHwndOf(parent), (POINT *)&rectWin);
2061 }
2062 }
2063
2064 // don't call DoMoveWindow() because we want to move window immediately
2065 // and not defer it here as otherwise the value returned by
2066 // GetClient/WindowRect() wouldn't change as the window wouldn't be
2067 // really resized
2068 if ( !::MoveWindow(GetHwnd(),
2069 rectWin.left,
2070 rectWin.top,
2071 width + widthWin - rectClient.right,
2072 height + heightWin - rectClient.bottom,
2073 TRUE) )
2074 {
2075 wxLogLastError(wxT("MoveWindow"));
2076 }
2077 }
2078 }
2079
2080 wxSize wxWindowMSW::DoGetBorderSize() const
2081 {
2082 wxCoord border;
2083 switch ( GetBorder() )
2084 {
2085 case wxBORDER_STATIC:
2086 case wxBORDER_SIMPLE:
2087 border = 1;
2088 break;
2089
2090 case wxBORDER_SUNKEN:
2091 border = 2;
2092 break;
2093
2094 case wxBORDER_RAISED:
2095 case wxBORDER_DOUBLE:
2096 border = 3;
2097 break;
2098
2099 default:
2100 wxFAIL_MSG( wxT("unknown border style") );
2101 // fall through
2102
2103 case wxBORDER_NONE:
2104 border = 0;
2105 }
2106
2107 return 2*wxSize(border, border);
2108 }
2109
2110 // ---------------------------------------------------------------------------
2111 // text metrics
2112 // ---------------------------------------------------------------------------
2113
2114 int wxWindowMSW::GetCharHeight() const
2115 {
2116 return wxGetTextMetrics(this).tmHeight;
2117 }
2118
2119 int wxWindowMSW::GetCharWidth() const
2120 {
2121 // +1 is needed because Windows apparently adds it when calculating the
2122 // dialog units size in pixels
2123 #if wxDIALOG_UNIT_COMPATIBILITY
2124 return wxGetTextMetrics(this).tmAveCharWidth;
2125 #else
2126 return wxGetTextMetrics(this).tmAveCharWidth + 1;
2127 #endif
2128 }
2129
2130 void wxWindowMSW::DoGetTextExtent(const wxString& string,
2131 int *x, int *y,
2132 int *descent,
2133 int *externalLeading,
2134 const wxFont *fontToUse) const
2135 {
2136 wxASSERT_MSG( !fontToUse || fontToUse->Ok(),
2137 wxT("invalid font in GetTextExtent()") );
2138
2139 HFONT hfontToUse;
2140 if ( fontToUse )
2141 hfontToUse = GetHfontOf(*fontToUse);
2142 else
2143 hfontToUse = GetHfontOf(GetFont());
2144
2145 WindowHDC hdc(GetHwnd());
2146 SelectInHDC selectFont(hdc, hfontToUse);
2147
2148 SIZE sizeRect;
2149 TEXTMETRIC tm;
2150 ::GetTextExtentPoint32(hdc, string.wx_str(), string.length(), &sizeRect);
2151 GetTextMetrics(hdc, &tm);
2152
2153 if ( x )
2154 *x = sizeRect.cx;
2155 if ( y )
2156 *y = sizeRect.cy;
2157 if ( descent )
2158 *descent = tm.tmDescent;
2159 if ( externalLeading )
2160 *externalLeading = tm.tmExternalLeading;
2161 }
2162
2163 // ---------------------------------------------------------------------------
2164 // popup menu
2165 // ---------------------------------------------------------------------------
2166
2167 #if wxUSE_MENUS_NATIVE
2168
2169 // yield for WM_COMMAND events only, i.e. process all WM_COMMANDs in the queue
2170 // immediately, without waiting for the next event loop iteration
2171 //
2172 // NB: this function should probably be made public later as it can almost
2173 // surely replace wxYield() elsewhere as well
2174 static void wxYieldForCommandsOnly()
2175 {
2176 // peek all WM_COMMANDs (it will always return WM_QUIT too but we don't
2177 // want to process it here)
2178 MSG msg;
2179 while ( ::PeekMessage(&msg, (HWND)0, WM_COMMAND, WM_COMMAND, PM_REMOVE) )
2180 {
2181 if ( msg.message == WM_QUIT )
2182 {
2183 // if we retrieved a WM_QUIT, insert back into the message queue.
2184 ::PostQuitMessage(0);
2185 break;
2186 }
2187
2188 // luckily (as we don't have access to wxEventLoopImpl method from here
2189 // anyhow...) we don't need to pre process WM_COMMANDs so dispatch it
2190 // immediately
2191 ::TranslateMessage(&msg);
2192 ::DispatchMessage(&msg);
2193 }
2194 }
2195
2196 bool wxWindowMSW::DoPopupMenu(wxMenu *menu, int x, int y)
2197 {
2198 menu->UpdateUI();
2199
2200 if ( x == wxDefaultCoord && y == wxDefaultCoord )
2201 {
2202 wxPoint mouse = ScreenToClient(wxGetMousePosition());
2203 x = mouse.x; y = mouse.y;
2204 }
2205
2206 HWND hWnd = GetHwnd();
2207 HMENU hMenu = GetHmenuOf(menu);
2208 POINT point;
2209 point.x = x;
2210 point.y = y;
2211 ::ClientToScreen(hWnd, &point);
2212 #if defined(__WXWINCE__)
2213 static const UINT flags = 0;
2214 #else // !__WXWINCE__
2215 UINT flags = TPM_RIGHTBUTTON;
2216 // NT4 doesn't support TPM_RECURSE and simply doesn't show the menu at all
2217 // when it's use, I'm not sure about Win95/98 but prefer to err on the safe
2218 // side and not to use it there neither -- modify the test if it does work
2219 // on these systems
2220 if ( wxGetWinVersion() >= wxWinVersion_5 )
2221 {
2222 // using TPM_RECURSE allows us to show a popup menu while another menu
2223 // is opened which can be useful and is supported by the other
2224 // platforms, so allow it under Windows too
2225 flags |= TPM_RECURSE;
2226 }
2227 #endif // __WXWINCE__/!__WXWINCE__
2228
2229 ::TrackPopupMenu(hMenu, flags, point.x, point.y, 0, hWnd, NULL);
2230
2231 // we need to do it right now as otherwise the events are never going to be
2232 // sent to wxCurrentPopupMenu from HandleCommand()
2233 //
2234 // note that even eliminating (ugly) wxCurrentPopupMenu global wouldn't
2235 // help and we'd still need wxYieldForCommandsOnly() as the menu may be
2236 // destroyed as soon as we return (it can be a local variable in the caller
2237 // for example) and so we do need to process the event immediately
2238 wxYieldForCommandsOnly();
2239
2240 return true;
2241 }
2242
2243 #endif // wxUSE_MENUS_NATIVE
2244
2245 // ===========================================================================
2246 // pre/post message processing
2247 // ===========================================================================
2248
2249 WXLRESULT wxWindowMSW::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2250 {
2251 if ( m_oldWndProc )
2252 return ::CallWindowProc(CASTWNDPROC m_oldWndProc, GetHwnd(), (UINT) nMsg, (WPARAM) wParam, (LPARAM) lParam);
2253 else
2254 return ::DefWindowProc(GetHwnd(), nMsg, wParam, lParam);
2255 }
2256
2257 bool wxWindowMSW::MSWProcessMessage(WXMSG* pMsg)
2258 {
2259 // wxUniversal implements tab traversal itself
2260 #ifndef __WXUNIVERSAL__
2261 if ( m_hWnd != 0 && (GetWindowStyleFlag() & wxTAB_TRAVERSAL) )
2262 {
2263 // intercept dialog navigation keys
2264 MSG *msg = (MSG *)pMsg;
2265
2266 // here we try to do all the job which ::IsDialogMessage() usually does
2267 // internally
2268 if ( msg->message == WM_KEYDOWN )
2269 {
2270 bool bCtrlDown = wxIsCtrlDown();
2271 bool bShiftDown = wxIsShiftDown();
2272
2273 // WM_GETDLGCODE: ask the control if it wants the key for itself,
2274 // don't process it if it's the case (except for Ctrl-Tab/Enter
2275 // combinations which are always processed)
2276 LONG lDlgCode = ::SendMessage(msg->hwnd, WM_GETDLGCODE, 0, 0);
2277
2278 // surprisingly, DLGC_WANTALLKEYS bit mask doesn't contain the
2279 // DLGC_WANTTAB nor DLGC_WANTARROWS bits although, logically,
2280 // it, of course, implies them
2281 if ( lDlgCode & DLGC_WANTALLKEYS )
2282 {
2283 lDlgCode |= DLGC_WANTTAB | DLGC_WANTARROWS;
2284 }
2285
2286 bool bForward = true,
2287 bWindowChange = false,
2288 bFromTab = false;
2289
2290 // should we process this message specially?
2291 bool bProcess = true;
2292 switch ( msg->wParam )
2293 {
2294 case VK_TAB:
2295 if ( (lDlgCode & DLGC_WANTTAB) && !bCtrlDown )
2296 {
2297 // let the control have the TAB
2298 bProcess = false;
2299 }
2300 else // use it for navigation
2301 {
2302 // Ctrl-Tab cycles thru notebook pages
2303 bWindowChange = bCtrlDown;
2304 bForward = !bShiftDown;
2305 bFromTab = true;
2306 }
2307 break;
2308
2309 case VK_UP:
2310 case VK_LEFT:
2311 if ( (lDlgCode & DLGC_WANTARROWS) || bCtrlDown )
2312 bProcess = false;
2313 else
2314 bForward = false;
2315 break;
2316
2317 case VK_DOWN:
2318 case VK_RIGHT:
2319 if ( (lDlgCode & DLGC_WANTARROWS) || bCtrlDown )
2320 bProcess = false;
2321 break;
2322
2323 case VK_PRIOR:
2324 bForward = false;
2325 // fall through
2326
2327 case VK_NEXT:
2328 // we treat PageUp/Dn as arrows because chances are that
2329 // a control which needs arrows also needs them for
2330 // navigation (e.g. wxTextCtrl, wxListCtrl, ...)
2331 if ( (lDlgCode & DLGC_WANTARROWS) && !bCtrlDown )
2332 bProcess = false;
2333 else // OTOH Ctrl-PageUp/Dn works as [Shift-]Ctrl-Tab
2334 bWindowChange = true;
2335 break;
2336
2337 case VK_RETURN:
2338 {
2339 #if wxUSE_BUTTON
2340 // currently active button should get enter press even
2341 // if there is a default button elsewhere so check if
2342 // this window is a button first
2343 wxWindow *btn = NULL;
2344 if ( lDlgCode & DLGC_DEFPUSHBUTTON )
2345 {
2346 // let IsDialogMessage() handle this for all
2347 // buttons except the owner-drawn ones which it
2348 // just seems to ignore
2349 long style = ::GetWindowLong(msg->hwnd, GWL_STYLE);
2350 if ( (style & BS_OWNERDRAW) == BS_OWNERDRAW )
2351 {
2352 // emulate the button click
2353 btn = wxFindWinFromHandle(msg->hwnd);
2354 }
2355
2356 bProcess = false;
2357 }
2358 else // not a button itself, do we have default button?
2359 {
2360 // check if this window or any of its ancestors
2361 // wants the message for itself (we always reserve
2362 // Ctrl-Enter for dialog navigation though)
2363 wxWindow *win = this;
2364 if ( !bCtrlDown )
2365 {
2366 // this will contain the dialog code of this
2367 // window and all of its parent windows in turn
2368 LONG lDlgCode2 = lDlgCode;
2369
2370 while ( win )
2371 {
2372 if ( lDlgCode2 & DLGC_WANTMESSAGE )
2373 {
2374 // as it wants to process Enter itself,
2375 // don't call IsDialogMessage() which
2376 // would consume it
2377 return false;
2378 }
2379
2380 // don't propagate keyboard messages beyond
2381 // the first top level window parent
2382 if ( win->IsTopLevel() )
2383 break;
2384
2385 win = win->GetParent();
2386
2387 lDlgCode2 = ::SendMessage
2388 (
2389 GetHwndOf(win),
2390 WM_GETDLGCODE,
2391 0,
2392 0
2393 );
2394 }
2395 }
2396 else // bCtrlDown
2397 {
2398 win = wxGetTopLevelParent(win);
2399 }
2400
2401 wxTopLevelWindow * const
2402 tlw = wxDynamicCast(win, wxTopLevelWindow);
2403 if ( tlw )
2404 {
2405 btn = wxDynamicCast(tlw->GetDefaultItem(),
2406 wxButton);
2407 }
2408 }
2409
2410 if ( btn && btn->IsEnabled() )
2411 {
2412 btn->MSWCommand(BN_CLICKED, 0 /* unused */);
2413 return true;
2414 }
2415
2416 #endif // wxUSE_BUTTON
2417
2418 #ifdef __WXWINCE__
2419 // map Enter presses into button presses on PDAs
2420 wxJoystickEvent event(wxEVT_JOY_BUTTON_DOWN);
2421 event.SetEventObject(this);
2422 if ( HandleWindowEvent(event) )
2423 return true;
2424 #endif // __WXWINCE__
2425 }
2426 break;
2427
2428 default:
2429 bProcess = false;
2430 }
2431
2432 if ( bProcess )
2433 {
2434 wxNavigationKeyEvent event;
2435 event.SetDirection(bForward);
2436 event.SetWindowChange(bWindowChange);
2437 event.SetFromTab(bFromTab);
2438 event.SetEventObject(this);
2439
2440 if ( HandleWindowEvent(event) )
2441 {
2442 // as we don't call IsDialogMessage(), which would take of
2443 // this by default, we need to manually send this message
2444 // so that controls can change their UI state if needed
2445 MSWUpdateUIState(UIS_CLEAR, UISF_HIDEFOCUS);
2446
2447 return true;
2448 }
2449 }
2450 }
2451
2452 if ( ::IsDialogMessage(GetHwnd(), msg) )
2453 {
2454 // IsDialogMessage() did something...
2455 return true;
2456 }
2457 }
2458 #endif // __WXUNIVERSAL__
2459
2460 #if wxUSE_TOOLTIPS
2461 if ( m_tooltip )
2462 {
2463 // relay mouse move events to the tooltip control
2464 MSG *msg = (MSG *)pMsg;
2465 if ( msg->message == WM_MOUSEMOVE )
2466 wxToolTip::RelayEvent(pMsg);
2467 }
2468 #endif // wxUSE_TOOLTIPS
2469
2470 return false;
2471 }
2472
2473 bool wxWindowMSW::MSWTranslateMessage(WXMSG* pMsg)
2474 {
2475 #if wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
2476 return m_acceleratorTable.Translate(this, pMsg);
2477 #else
2478 (void) pMsg;
2479 return false;
2480 #endif // wxUSE_ACCEL
2481 }
2482
2483 bool wxWindowMSW::MSWShouldPreProcessMessage(WXMSG* msg)
2484 {
2485 // all tests below have to deal with various bugs/misfeatures of
2486 // IsDialogMessage(): we have to prevent it from being called from our
2487 // MSWProcessMessage() in some situations
2488
2489 // don't let IsDialogMessage() get VK_ESCAPE as it _always_ eats the
2490 // message even when there is no cancel button and when the message is
2491 // needed by the control itself: in particular, it prevents the tree in
2492 // place edit control from being closed with Escape in a dialog
2493 if ( msg->message == WM_KEYDOWN && msg->wParam == VK_ESCAPE )
2494 {
2495 return false;
2496 }
2497
2498 // ::IsDialogMessage() is broken and may sometimes hang the application by
2499 // going into an infinite loop when it tries to find the control to give
2500 // focus to when Alt-<key> is pressed, so we try to detect [some of] the
2501 // situations when this may happen and not call it then
2502 if ( msg->message != WM_SYSCHAR )
2503 return true;
2504
2505 // assume we can call it by default
2506 bool canSafelyCallIsDlgMsg = true;
2507
2508 HWND hwndFocus = ::GetFocus();
2509
2510 // if the currently focused window itself has WS_EX_CONTROLPARENT style,
2511 // ::IsDialogMessage() will also enter an infinite loop, because it will
2512 // recursively check the child windows but not the window itself and so if
2513 // none of the children accepts focus it loops forever (as it only stops
2514 // when it gets back to the window it started from)
2515 //
2516 // while it is very unusual that a window with WS_EX_CONTROLPARENT
2517 // style has the focus, it can happen. One such possibility is if
2518 // all windows are either toplevel, wxDialog, wxPanel or static
2519 // controls and no window can actually accept keyboard input.
2520 #if !defined(__WXWINCE__)
2521 if ( ::GetWindowLong(hwndFocus, GWL_EXSTYLE) & WS_EX_CONTROLPARENT )
2522 {
2523 // pessimistic by default
2524 canSafelyCallIsDlgMsg = false;
2525 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2526 node;
2527 node = node->GetNext() )
2528 {
2529 wxWindow * const win = node->GetData();
2530 if ( win->CanAcceptFocus() &&
2531 !wxHasWindowExStyle(win, WS_EX_CONTROLPARENT) )
2532 {
2533 // it shouldn't hang...
2534 canSafelyCallIsDlgMsg = true;
2535
2536 break;
2537 }
2538 }
2539 }
2540 #endif // !__WXWINCE__
2541
2542 if ( canSafelyCallIsDlgMsg )
2543 {
2544 // ::IsDialogMessage() can enter in an infinite loop when the
2545 // currently focused window is disabled or hidden and its
2546 // parent has WS_EX_CONTROLPARENT style, so don't call it in
2547 // this case
2548 while ( hwndFocus )
2549 {
2550 if ( !::IsWindowEnabled(hwndFocus) ||
2551 !::IsWindowVisible(hwndFocus) )
2552 {
2553 // it would enter an infinite loop if we do this!
2554 canSafelyCallIsDlgMsg = false;
2555
2556 break;
2557 }
2558
2559 if ( !(::GetWindowLong(hwndFocus, GWL_STYLE) & WS_CHILD) )
2560 {
2561 // it's a top level window, don't go further -- e.g. even
2562 // if the parent of a dialog is disabled, this doesn't
2563 // break navigation inside the dialog
2564 break;
2565 }
2566
2567 hwndFocus = ::GetParent(hwndFocus);
2568 }
2569 }
2570
2571 return canSafelyCallIsDlgMsg;
2572 }
2573
2574 // ---------------------------------------------------------------------------
2575 // message params unpackers
2576 // ---------------------------------------------------------------------------
2577
2578 void wxWindowMSW::UnpackCommand(WXWPARAM wParam, WXLPARAM lParam,
2579 WORD *id, WXHWND *hwnd, WORD *cmd)
2580 {
2581 *id = LOWORD(wParam);
2582 *hwnd = (WXHWND)lParam;
2583 *cmd = HIWORD(wParam);
2584 }
2585
2586 void wxWindowMSW::UnpackActivate(WXWPARAM wParam, WXLPARAM lParam,
2587 WXWORD *state, WXWORD *minimized, WXHWND *hwnd)
2588 {
2589 *state = LOWORD(wParam);
2590 *minimized = HIWORD(wParam);
2591 *hwnd = (WXHWND)lParam;
2592 }
2593
2594 void wxWindowMSW::UnpackScroll(WXWPARAM wParam, WXLPARAM lParam,
2595 WXWORD *code, WXWORD *pos, WXHWND *hwnd)
2596 {
2597 *code = LOWORD(wParam);
2598 *pos = HIWORD(wParam);
2599 *hwnd = (WXHWND)lParam;
2600 }
2601
2602 void wxWindowMSW::UnpackCtlColor(WXWPARAM wParam, WXLPARAM lParam,
2603 WXHDC *hdc, WXHWND *hwnd)
2604 {
2605 *hwnd = (WXHWND)lParam;
2606 *hdc = (WXHDC)wParam;
2607 }
2608
2609 void wxWindowMSW::UnpackMenuSelect(WXWPARAM wParam, WXLPARAM lParam,
2610 WXWORD *item, WXWORD *flags, WXHMENU *hmenu)
2611 {
2612 *item = (WXWORD)wParam;
2613 *flags = HIWORD(wParam);
2614 *hmenu = (WXHMENU)lParam;
2615 }
2616
2617 // ---------------------------------------------------------------------------
2618 // Main wxWidgets window proc and the window proc for wxWindow
2619 // ---------------------------------------------------------------------------
2620
2621 // Hook for new window just as it's being created, when the window isn't yet
2622 // associated with the handle
2623 static wxWindowMSW *gs_winBeingCreated = NULL;
2624
2625 // implementation of wxWindowCreationHook class: it just sets gs_winBeingCreated to the
2626 // window being created and insures that it's always unset back later
2627 wxWindowCreationHook::wxWindowCreationHook(wxWindowMSW *winBeingCreated)
2628 {
2629 gs_winBeingCreated = winBeingCreated;
2630 }
2631
2632 wxWindowCreationHook::~wxWindowCreationHook()
2633 {
2634 gs_winBeingCreated = NULL;
2635 }
2636
2637 // Main window proc
2638 LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2639 {
2640 // trace all messages: useful for the debugging but noticeably slows down
2641 // the code so don't do it by default
2642 #if wxDEBUG_LEVEL >= 2
2643 // notice that we cast wParam and lParam to long to avoid mismatch with
2644 // format specifiers in 64 bit builds where they are both int64 quantities
2645 //
2646 // casting like this loses information, of course, but it shouldn't matter
2647 // much for this diagnostic code and it keeps the code simple
2648 wxLogTrace("winmsg",
2649 wxT("Processing %s(hWnd=%p, wParam=%08lx, lParam=%08lx)"),
2650 wxGetMessageName(message), hWnd, (long)wParam, (long)lParam);
2651 #endif // wxDEBUG_LEVEL >= 2
2652
2653 wxWindowMSW *wnd = wxFindWinFromHandle(hWnd);
2654
2655 // when we get the first message for the HWND we just created, we associate
2656 // it with wxWindow stored in gs_winBeingCreated
2657 if ( !wnd && gs_winBeingCreated )
2658 {
2659 wxAssociateWinWithHandle(hWnd, gs_winBeingCreated);
2660 wnd = gs_winBeingCreated;
2661 gs_winBeingCreated = NULL;
2662 wnd->SetHWND((WXHWND)hWnd);
2663 }
2664
2665 LRESULT rc;
2666
2667 if ( wnd && wxGUIEventLoop::AllowProcessing(wnd) )
2668 rc = wnd->MSWWindowProc(message, wParam, lParam);
2669 else
2670 rc = ::DefWindowProc(hWnd, message, wParam, lParam);
2671
2672 return rc;
2673 }
2674
2675 WXLRESULT wxWindowMSW::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2676 {
2677 // did we process the message?
2678 bool processed = false;
2679
2680 // the return value
2681 union
2682 {
2683 bool allow;
2684 WXLRESULT result;
2685 WXHBRUSH hBrush;
2686 } rc;
2687
2688 // for most messages we should return 0 when we do process the message
2689 rc.result = 0;
2690
2691 switch ( message )
2692 {
2693 case WM_CREATE:
2694 {
2695 bool mayCreate;
2696 processed = HandleCreate((WXLPCREATESTRUCT)lParam, &mayCreate);
2697 if ( processed )
2698 {
2699 // return 0 to allow window creation
2700 rc.result = mayCreate ? 0 : -1;
2701 }
2702 }
2703 break;
2704
2705 case WM_DESTROY:
2706 // never set processed to true and *always* pass WM_DESTROY to
2707 // DefWindowProc() as Windows may do some internal cleanup when
2708 // processing it and failing to pass the message along may cause
2709 // memory and resource leaks!
2710 (void)HandleDestroy();
2711 break;
2712
2713 case WM_SIZE:
2714 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
2715 break;
2716
2717 case WM_MOVE:
2718 processed = HandleMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
2719 break;
2720
2721 #if !defined(__WXWINCE__)
2722 case WM_MOVING:
2723 {
2724 LPRECT pRect = (LPRECT)lParam;
2725 wxRect rc;
2726 rc.SetLeft(pRect->left);
2727 rc.SetTop(pRect->top);
2728 rc.SetRight(pRect->right);
2729 rc.SetBottom(pRect->bottom);
2730 processed = HandleMoving(rc);
2731 if (processed) {
2732 pRect->left = rc.GetLeft();
2733 pRect->top = rc.GetTop();
2734 pRect->right = rc.GetRight();
2735 pRect->bottom = rc.GetBottom();
2736 }
2737 }
2738 break;
2739
2740 case WM_ENTERSIZEMOVE:
2741 {
2742 processed = HandleEnterSizeMove();
2743 }
2744 break;
2745
2746 case WM_EXITSIZEMOVE:
2747 {
2748 processed = HandleExitSizeMove();
2749 }
2750 break;
2751
2752 case WM_SIZING:
2753 {
2754 LPRECT pRect = (LPRECT)lParam;
2755 wxRect rc;
2756 rc.SetLeft(pRect->left);
2757 rc.SetTop(pRect->top);
2758 rc.SetRight(pRect->right);
2759 rc.SetBottom(pRect->bottom);
2760 processed = HandleSizing(rc);
2761 if (processed) {
2762 pRect->left = rc.GetLeft();
2763 pRect->top = rc.GetTop();
2764 pRect->right = rc.GetRight();
2765 pRect->bottom = rc.GetBottom();
2766 }
2767 }
2768 break;
2769 #endif // !__WXWINCE__
2770
2771 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
2772 case WM_ACTIVATEAPP:
2773 // This implicitly sends a wxEVT_ACTIVATE_APP event
2774 wxTheApp->SetActive(wParam != 0, FindFocus());
2775 break;
2776 #endif
2777
2778 case WM_ACTIVATE:
2779 {
2780 WXWORD state, minimized;
2781 WXHWND hwnd;
2782 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
2783
2784 processed = HandleActivate(state, minimized != 0, (WXHWND)hwnd);
2785 }
2786 break;
2787
2788 case WM_SETFOCUS:
2789 processed = HandleSetFocus((WXHWND)wParam);
2790 break;
2791
2792 case WM_KILLFOCUS:
2793 processed = HandleKillFocus((WXHWND)wParam);
2794 break;
2795
2796 case WM_PRINTCLIENT:
2797 processed = HandlePrintClient((WXHDC)wParam);
2798 break;
2799
2800 case WM_PAINT:
2801 if ( wParam )
2802 {
2803 wxPaintDCEx dc((wxWindow *)this, (WXHDC)wParam);
2804
2805 processed = HandlePaint();
2806 }
2807 else // no DC given
2808 {
2809 processed = HandlePaint();
2810 }
2811 break;
2812
2813 case WM_CLOSE:
2814 #ifdef __WXUNIVERSAL__
2815 // Universal uses its own wxFrame/wxDialog, so we don't receive
2816 // close events unless we have this.
2817 Close();
2818 #endif // __WXUNIVERSAL__
2819
2820 // don't let the DefWindowProc() destroy our window - we'll do it
2821 // ourselves in ~wxWindow
2822 processed = true;
2823 rc.result = TRUE;
2824 break;
2825
2826 case WM_SHOWWINDOW:
2827 processed = HandleShow(wParam != 0, (int)lParam);
2828 break;
2829
2830 case WM_MOUSEMOVE:
2831 processed = HandleMouseMove(GET_X_LPARAM(lParam),
2832 GET_Y_LPARAM(lParam),
2833 wParam);
2834 break;
2835
2836 #ifdef HAVE_TRACKMOUSEEVENT
2837 case WM_MOUSELEAVE:
2838 // filter out excess WM_MOUSELEAVE events sent after PopupMenu()
2839 // (on XP at least)
2840 if ( m_mouseInWindow )
2841 {
2842 GenerateMouseLeave();
2843 }
2844
2845 // always pass processed back as false, this allows the window
2846 // manager to process the message too. This is needed to
2847 // ensure windows XP themes work properly as the mouse moves
2848 // over widgets like buttons. So don't set processed to true here.
2849 break;
2850 #endif // HAVE_TRACKMOUSEEVENT
2851
2852 #if wxUSE_MOUSEWHEEL
2853 case WM_MOUSEWHEEL:
2854 processed = HandleMouseWheel(wParam, lParam);
2855 break;
2856 #endif
2857
2858 case WM_LBUTTONDOWN:
2859 case WM_LBUTTONUP:
2860 case WM_LBUTTONDBLCLK:
2861 case WM_RBUTTONDOWN:
2862 case WM_RBUTTONUP:
2863 case WM_RBUTTONDBLCLK:
2864 case WM_MBUTTONDOWN:
2865 case WM_MBUTTONUP:
2866 case WM_MBUTTONDBLCLK:
2867 #ifdef wxHAS_XBUTTON
2868 case WM_XBUTTONDOWN:
2869 case WM_XBUTTONUP:
2870 case WM_XBUTTONDBLCLK:
2871 #endif // wxHAS_XBUTTON
2872 {
2873 #ifdef __WXMICROWIN__
2874 // MicroWindows seems to ignore the fact that a window is
2875 // disabled. So catch mouse events and throw them away if
2876 // necessary.
2877 wxWindowMSW* win = this;
2878 for ( ;; )
2879 {
2880 if (!win->IsEnabled())
2881 {
2882 processed = true;
2883 break;
2884 }
2885
2886 win = win->GetParent();
2887 if ( !win || win->IsTopLevel() )
2888 break;
2889 }
2890
2891 if ( processed )
2892 break;
2893
2894 #endif // __WXMICROWIN__
2895 int x = GET_X_LPARAM(lParam),
2896 y = GET_Y_LPARAM(lParam);
2897
2898 #ifdef __WXWINCE__
2899 // redirect the event to a static control if necessary by
2900 // finding one under mouse because under CE the static controls
2901 // don't generate mouse events (even with SS_NOTIFY)
2902 wxWindowMSW *win;
2903 if ( GetCapture() == this )
2904 {
2905 // but don't do it if the mouse is captured by this window
2906 // because then it should really get this event itself
2907 win = this;
2908 }
2909 else
2910 {
2911 win = FindWindowForMouseEvent(this, &x, &y);
2912
2913 // this should never happen
2914 wxCHECK_MSG( win, 0,
2915 wxT("FindWindowForMouseEvent() returned NULL") );
2916 }
2917 #ifdef __POCKETPC__
2918 if (IsContextMenuEnabled() && message == WM_LBUTTONDOWN)
2919 {
2920 SHRGINFO shrgi = {0};
2921
2922 shrgi.cbSize = sizeof(SHRGINFO);
2923 shrgi.hwndClient = (HWND) GetHWND();
2924 shrgi.ptDown.x = x;
2925 shrgi.ptDown.y = y;
2926
2927 shrgi.dwFlags = SHRG_RETURNCMD;
2928 // shrgi.dwFlags = SHRG_NOTIFYPARENT;
2929
2930 if (GN_CONTEXTMENU == ::SHRecognizeGesture(&shrgi))
2931 {
2932 wxPoint pt(x, y);
2933 pt = ClientToScreen(pt);
2934
2935 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, GetId(), pt);
2936
2937 evtCtx.SetEventObject(this);
2938 if (HandleWindowEvent(evtCtx))
2939 {
2940 processed = true;
2941 return true;
2942 }
2943 }
2944 }
2945 #endif
2946
2947 #else // !__WXWINCE__
2948 wxWindowMSW *win = this;
2949 #endif // __WXWINCE__/!__WXWINCE__
2950
2951 processed = win->HandleMouseEvent(message, x, y, wParam);
2952
2953 // if the app didn't eat the event, handle it in the default
2954 // way, that is by giving this window the focus
2955 if ( !processed )
2956 {
2957 // for the standard classes their WndProc sets the focus to
2958 // them anyhow and doing it from here results in some weird
2959 // problems, so don't do it for them (unnecessary anyhow)
2960 if ( !win->IsOfStandardClass() )
2961 {
2962 if ( message == WM_LBUTTONDOWN && win->IsFocusable() )
2963 win->SetFocus();
2964 }
2965 }
2966 }
2967 break;
2968
2969 #ifdef MM_JOY1MOVE
2970 case MM_JOY1MOVE:
2971 case MM_JOY2MOVE:
2972 case MM_JOY1ZMOVE:
2973 case MM_JOY2ZMOVE:
2974 case MM_JOY1BUTTONDOWN:
2975 case MM_JOY2BUTTONDOWN:
2976 case MM_JOY1BUTTONUP:
2977 case MM_JOY2BUTTONUP:
2978 processed = HandleJoystickEvent(message,
2979 GET_X_LPARAM(lParam),
2980 GET_Y_LPARAM(lParam),
2981 wParam);
2982 break;
2983 #endif // __WXMICROWIN__
2984
2985 case WM_COMMAND:
2986 {
2987 WORD id, cmd;
2988 WXHWND hwnd;
2989 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
2990
2991 processed = HandleCommand(id, cmd, hwnd);
2992 }
2993 break;
2994
2995 case WM_NOTIFY:
2996 processed = HandleNotify((int)wParam, lParam, &rc.result);
2997 break;
2998
2999 // we only need to reply to WM_NOTIFYFORMAT manually when using MSLU,
3000 // otherwise DefWindowProc() does it perfectly fine for us, but MSLU
3001 // apparently doesn't always behave properly and needs some help
3002 #if wxUSE_UNICODE_MSLU && defined(NF_QUERY)
3003 case WM_NOTIFYFORMAT:
3004 if ( lParam == NF_QUERY )
3005 {
3006 processed = true;
3007 rc.result = NFR_UNICODE;
3008 }
3009 break;
3010 #endif // wxUSE_UNICODE_MSLU
3011
3012 // for these messages we must return true if process the message
3013 #ifdef WM_DRAWITEM
3014 case WM_DRAWITEM:
3015 processed = MSWOnDrawItem(wParam, (WXDRAWITEMSTRUCT *)lParam);
3016 if ( processed )
3017 rc.result = TRUE;
3018 break;
3019
3020 case WM_MEASUREITEM:
3021 processed = MSWOnMeasureItem(wParam, (WXMEASUREITEMSTRUCT *)lParam);
3022 if ( processed )
3023 rc.result = TRUE;
3024 break;
3025 #endif // defined(WM_DRAWITEM)
3026
3027 case WM_GETDLGCODE:
3028 if ( !IsOfStandardClass() || HasFlag(wxWANTS_CHARS) )
3029 {
3030 // we always want to get the char events
3031 rc.result = DLGC_WANTCHARS;
3032
3033 if ( HasFlag(wxWANTS_CHARS) )
3034 {
3035 // in fact, we want everything
3036 rc.result |= DLGC_WANTARROWS |
3037 DLGC_WANTTAB |
3038 DLGC_WANTALLKEYS;
3039 }
3040
3041 processed = true;
3042 }
3043 //else: get the dlg code from the DefWindowProc()
3044 break;
3045
3046 case WM_SYSKEYDOWN:
3047 case WM_KEYDOWN:
3048 // Generate the key down event in any case.
3049 m_lastKeydownProcessed = HandleKeyDown((WORD) wParam, lParam);
3050 if ( m_lastKeydownProcessed )
3051 {
3052 // If it was processed by an event handler, we stop here,
3053 // notably we intentionally don't generate char event then.
3054 processed = true;
3055 }
3056 else // key down event not handled
3057 {
3058 // Examine the event to decide whether we need to generate a
3059 // char event for it ourselves or let Windows do it. Window
3060 // mostly only does it for the keys which produce printable
3061 // characters (although there are exceptions, e.g. VK_ESCAPE or
3062 // VK_BACK (but not VK_DELETE)) while we do it for all keys
3063 // except the modifier ones (the wisdom of this is debatable
3064 // but by now this decision is enshrined forever due to
3065 // backwards compatibility).
3066 switch ( wParam )
3067 {
3068 // No wxEVT_CHAR events are generated for these keys at all.
3069 case VK_SHIFT:
3070 case VK_CONTROL:
3071 case VK_MENU:
3072 case VK_CAPITAL:
3073 case VK_NUMLOCK:
3074 case VK_SCROLL:
3075
3076 // Windows will send us WM_CHAR for these ones so we'll
3077 // generate wxEVT_CHAR for them later when we get it.
3078 case VK_ESCAPE:
3079 case VK_SPACE:
3080 case VK_RETURN:
3081 case VK_BACK:
3082 case VK_TAB:
3083 case VK_ADD:
3084 case VK_SUBTRACT:
3085 case VK_MULTIPLY:
3086 case VK_DIVIDE:
3087 case VK_DECIMAL:
3088 case VK_NUMPAD0:
3089 case VK_NUMPAD1:
3090 case VK_NUMPAD2:
3091 case VK_NUMPAD3:
3092 case VK_NUMPAD4:
3093 case VK_NUMPAD5:
3094 case VK_NUMPAD6:
3095 case VK_NUMPAD7:
3096 case VK_NUMPAD8:
3097 case VK_NUMPAD9:
3098 case VK_OEM_1:
3099 case VK_OEM_2:
3100 case VK_OEM_3:
3101 case VK_OEM_4:
3102 case VK_OEM_5:
3103 case VK_OEM_6:
3104 case VK_OEM_7:
3105 case VK_OEM_PLUS:
3106 case VK_OEM_COMMA:
3107 case VK_OEM_MINUS:
3108 case VK_OEM_PERIOD:
3109 break;
3110
3111 #ifdef VK_APPS
3112 // special case of VK_APPS: treat it the same as right mouse
3113 // click because both usually pop up a context menu
3114 case VK_APPS:
3115 processed = HandleMouseEvent(WM_RBUTTONDOWN, -1, -1, 0);
3116 break;
3117 #endif // VK_APPS
3118
3119 default:
3120 if ( (wParam >= '0' && wParam <= '9') ||
3121 (wParam >= 'A' && wParam <= 'Z') )
3122 {
3123 // We'll get WM_CHAR for those later too.
3124 break;
3125 }
3126
3127 // But for the rest we won't get WM_CHAR later so we do
3128 // need to generate the event right now.
3129 wxKeyEvent event(wxEVT_CHAR);
3130 InitAnyKeyEvent(event, wParam, lParam);
3131
3132 // Set the "extended" bit in lParam because we want to
3133 // generate CHAR events with WXK_HOME and not
3134 // WXK_NUMPAD_HOME even if the "Home" key on numpad was
3135 // pressed.
3136 event.m_keyCode = wxMSWKeyboard::VKToWX
3137 (
3138 wParam,
3139 lParam | (KF_EXTENDED << 16)
3140 );
3141
3142 // Don't produce events without any valid character
3143 // code (even if this shouldn't normally happen...).
3144 if ( event.m_keyCode != WXK_NONE )
3145 processed = HandleWindowEvent(event);
3146 }
3147 }
3148 if (message == WM_SYSKEYDOWN) // Let Windows still handle the SYSKEYs
3149 processed = false;
3150 break;
3151
3152 case WM_SYSKEYUP:
3153 case WM_KEYUP:
3154 #ifdef VK_APPS
3155 // special case of VK_APPS: treat it the same as right mouse button
3156 if ( wParam == VK_APPS )
3157 {
3158 processed = HandleMouseEvent(WM_RBUTTONUP, -1, -1, 0);
3159 }
3160 else
3161 #endif // VK_APPS
3162 {
3163 processed = HandleKeyUp((WORD) wParam, lParam);
3164 }
3165 break;
3166
3167 case WM_SYSCHAR:
3168 case WM_CHAR: // Always an ASCII character
3169 if ( m_lastKeydownProcessed )
3170 {
3171 // The key was handled in the EVT_KEY_DOWN and handling
3172 // a key in an EVT_KEY_DOWN handler is meant, by
3173 // design, to prevent EVT_CHARs from happening
3174 m_lastKeydownProcessed = false;
3175 processed = true;
3176 }
3177 else
3178 {
3179 processed = HandleChar((WORD)wParam, lParam);
3180 }
3181 break;
3182
3183 case WM_IME_STARTCOMPOSITION:
3184 // IME popup needs Escape as it should undo the changes in its
3185 // entry window instead of e.g. closing the dialog for which the
3186 // IME is used (and losing all the changes in the IME window).
3187 gs_modalEntryWindowCount++;
3188 break;
3189
3190 case WM_IME_ENDCOMPOSITION:
3191 gs_modalEntryWindowCount--;
3192 break;
3193
3194 #if wxUSE_HOTKEY
3195 case WM_HOTKEY:
3196 processed = HandleHotKey((WORD)wParam, lParam);
3197 break;
3198 #endif // wxUSE_HOTKEY
3199
3200 case WM_CUT:
3201 case WM_COPY:
3202 case WM_PASTE:
3203 processed = HandleClipboardEvent(message);
3204 break;
3205
3206 case WM_HSCROLL:
3207 case WM_VSCROLL:
3208 {
3209 WXWORD code, pos;
3210 WXHWND hwnd;
3211 UnpackScroll(wParam, lParam, &code, &pos, &hwnd);
3212
3213 processed = MSWOnScroll(message == WM_HSCROLL ? wxHORIZONTAL
3214 : wxVERTICAL,
3215 code, pos, hwnd);
3216 }
3217 break;
3218
3219 // CTLCOLOR messages are sent by children to query the parent for their
3220 // colors
3221 #ifndef __WXMICROWIN__
3222 case WM_CTLCOLORMSGBOX:
3223 case WM_CTLCOLOREDIT:
3224 case WM_CTLCOLORLISTBOX:
3225 case WM_CTLCOLORBTN:
3226 case WM_CTLCOLORDLG:
3227 case WM_CTLCOLORSCROLLBAR:
3228 case WM_CTLCOLORSTATIC:
3229 {
3230 WXHDC hdc;
3231 WXHWND hwnd;
3232 UnpackCtlColor(wParam, lParam, &hdc, &hwnd);
3233
3234 processed = HandleCtlColor(&rc.hBrush, (WXHDC)hdc, (WXHWND)hwnd);
3235 }
3236 break;
3237 #endif // !__WXMICROWIN__
3238
3239 case WM_SYSCOLORCHANGE:
3240 // the return value for this message is ignored
3241 processed = HandleSysColorChange();
3242 break;
3243
3244 #if !defined(__WXWINCE__)
3245 case WM_DISPLAYCHANGE:
3246 processed = HandleDisplayChange();
3247 break;
3248 #endif
3249
3250 case WM_PALETTECHANGED:
3251 processed = HandlePaletteChanged((WXHWND)wParam);
3252 break;
3253
3254 case WM_CAPTURECHANGED:
3255 processed = HandleCaptureChanged((WXHWND)lParam);
3256 break;
3257
3258 case WM_SETTINGCHANGE:
3259 processed = HandleSettingChange(wParam, lParam);
3260 break;
3261
3262 case WM_QUERYNEWPALETTE:
3263 processed = HandleQueryNewPalette();
3264 break;
3265
3266 case WM_ERASEBKGND:
3267 {
3268 #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
3269 // check if an override was configured for this window
3270 EraseBgHooks::const_iterator it = gs_eraseBgHooks.find(this);
3271 if ( it != gs_eraseBgHooks.end() )
3272 processed = it->second->MSWEraseBgHook((WXHDC)wParam);
3273 else
3274 #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
3275 processed = HandleEraseBkgnd((WXHDC)wParam);
3276 }
3277
3278 if ( processed )
3279 {
3280 // we processed the message, i.e. erased the background
3281 rc.result = TRUE;
3282 }
3283 break;
3284
3285 #if !defined(__WXWINCE__)
3286 case WM_DROPFILES:
3287 processed = HandleDropFiles(wParam);
3288 break;
3289 #endif
3290
3291 case WM_INITDIALOG:
3292 processed = HandleInitDialog((WXHWND)wParam);
3293
3294 if ( processed )
3295 {
3296 // we never set focus from here
3297 rc.result = FALSE;
3298 }
3299 break;
3300
3301 #if !defined(__WXWINCE__)
3302 case WM_QUERYENDSESSION:
3303 processed = HandleQueryEndSession(lParam, &rc.allow);
3304 break;
3305
3306 case WM_ENDSESSION:
3307 processed = HandleEndSession(wParam != 0, lParam);
3308 break;
3309
3310 case WM_GETMINMAXINFO:
3311 processed = HandleGetMinMaxInfo((MINMAXINFO*)lParam);
3312 break;
3313 #endif
3314
3315 case WM_SETCURSOR:
3316 processed = HandleSetCursor((WXHWND)wParam,
3317 LOWORD(lParam), // hit test
3318 HIWORD(lParam)); // mouse msg
3319
3320 if ( processed )
3321 {
3322 // returning TRUE stops the DefWindowProc() from further
3323 // processing this message - exactly what we need because we've
3324 // just set the cursor.
3325 rc.result = TRUE;
3326 }
3327 break;
3328
3329 #if wxUSE_ACCESSIBILITY
3330 case WM_GETOBJECT:
3331 {
3332 //WPARAM dwFlags = (WPARAM) (DWORD) wParam;
3333 LPARAM dwObjId = (LPARAM) (DWORD) lParam;
3334
3335 if (dwObjId == (LPARAM)OBJID_CLIENT && GetOrCreateAccessible())
3336 {
3337 return LresultFromObject(IID_IAccessible, wParam, (IUnknown*) GetAccessible()->GetIAccessible());
3338 }
3339 break;
3340 }
3341 #endif
3342
3343 #if defined(WM_HELP)
3344 case WM_HELP:
3345 {
3346 // by default, WM_HELP is propagated by DefWindowProc() upwards
3347 // to the window parent but as we do it ourselves already
3348 // (wxHelpEvent is derived from wxCommandEvent), we don't want
3349 // to get the other events if we process this message at all
3350 processed = true;
3351
3352 // WM_HELP doesn't use lParam under CE
3353 #ifndef __WXWINCE__
3354 HELPINFO* info = (HELPINFO*) lParam;
3355 if ( info->iContextType == HELPINFO_WINDOW )
3356 {
3357 #endif // !__WXWINCE__
3358 wxHelpEvent helpEvent
3359 (
3360 wxEVT_HELP,
3361 GetId(),
3362 #ifdef __WXWINCE__
3363 wxGetMousePosition() // what else?
3364 #else
3365 wxPoint(info->MousePos.x, info->MousePos.y)
3366 #endif
3367 );
3368
3369 helpEvent.SetEventObject(this);
3370 HandleWindowEvent(helpEvent);
3371 #ifndef __WXWINCE__
3372 }
3373 else if ( info->iContextType == HELPINFO_MENUITEM )
3374 {
3375 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId);
3376 helpEvent.SetEventObject(this);
3377 HandleWindowEvent(helpEvent);
3378
3379 }
3380 else // unknown help event?
3381 {
3382 processed = false;
3383 }
3384 #endif // !__WXWINCE__
3385 }
3386 break;
3387 #endif // WM_HELP
3388
3389 #if !defined(__WXWINCE__)
3390 case WM_CONTEXTMENU:
3391 {
3392 // we don't convert from screen to client coordinates as
3393 // the event may be handled by a parent window
3394 wxPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
3395
3396 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, GetId(), pt);
3397
3398 // we could have got an event from our child, reflect it back
3399 // to it if this is the case
3400 wxWindowMSW *win = NULL;
3401 WXHWND hWnd = (WXHWND)wParam;
3402 if ( hWnd != m_hWnd )
3403 {
3404 win = FindItemByHWND(hWnd);
3405 }
3406
3407 if ( !win )
3408 win = this;
3409
3410 evtCtx.SetEventObject(win);
3411 processed = win->HandleWindowEvent(evtCtx);
3412 }
3413 break;
3414 #endif
3415
3416 #if wxUSE_MENUS
3417 case WM_MENUCHAR:
3418 // we're only interested in our own menus, not MF_SYSMENU
3419 if ( HIWORD(wParam) == MF_POPUP )
3420 {
3421 // handle menu chars for ownerdrawn menu items
3422 int i = HandleMenuChar(toupper(LOWORD(wParam)), lParam);
3423 if ( i != wxNOT_FOUND )
3424 {
3425 rc.result = MAKELRESULT(i, MNC_EXECUTE);
3426 processed = true;
3427 }
3428 }
3429 break;
3430 #endif // wxUSE_MENUS
3431
3432 #ifndef __WXWINCE__
3433 case WM_POWERBROADCAST:
3434 {
3435 bool vetoed;
3436 processed = HandlePower(wParam, lParam, &vetoed);
3437 rc.result = processed && vetoed ? BROADCAST_QUERY_DENY : TRUE;
3438 }
3439 break;
3440 #endif // __WXWINCE__
3441
3442 #if wxUSE_UXTHEME
3443 // If we want the default themed border then we need to draw it ourselves
3444 case WM_NCCALCSIZE:
3445 {
3446 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
3447 const wxBorder border = TranslateBorder(GetBorder());
3448 if (theme && border == wxBORDER_THEME)
3449 {
3450 // first ask the widget to calculate the border size
3451 rc.result = MSWDefWindowProc(message, wParam, lParam);
3452 processed = true;
3453
3454 // now alter the client size making room for drawing a
3455 // themed border
3456 RECT *rect;
3457 NCCALCSIZE_PARAMS *csparam = NULL;
3458 if ( wParam )
3459 {
3460 csparam = (NCCALCSIZE_PARAMS *)lParam;
3461 rect = &csparam->rgrc[0];
3462 }
3463 else
3464 {
3465 rect = (RECT *)lParam;
3466 }
3467
3468 wxUxThemeHandle hTheme((const wxWindow *)this, L"EDIT");
3469 RECT rcClient = { 0, 0, 0, 0 };
3470 wxClientDC dc((wxWindow *)this);
3471 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
3472
3473 if ( theme->GetThemeBackgroundContentRect
3474 (
3475 hTheme,
3476 GetHdcOf(*impl),
3477 EP_EDITTEXT,
3478 ETS_NORMAL,
3479 rect,
3480 &rcClient) == S_OK )
3481 {
3482 InflateRect(&rcClient, -1, -1);
3483 if (wParam)
3484 csparam->rgrc[0] = rcClient;
3485 else
3486 *((RECT*)lParam) = rcClient;
3487
3488 // WVR_REDRAW triggers a bug whereby child windows are moved up and left,
3489 // so don't use.
3490 // rc.result = WVR_REDRAW;
3491 }
3492 }
3493 }
3494 break;
3495
3496 case WM_NCPAINT:
3497 {
3498 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
3499 const wxBorder border = TranslateBorder(GetBorder());
3500 if (theme && border == wxBORDER_THEME)
3501 {
3502 // first ask the widget to paint its non-client area, such as scrollbars, etc.
3503 rc.result = MSWDefWindowProc(message, wParam, lParam);
3504 processed = true;
3505
3506 wxUxThemeHandle hTheme((const wxWindow *)this, L"EDIT");
3507 wxWindowDC dc((wxWindow *)this);
3508 wxMSWDCImpl *impl = (wxMSWDCImpl*) dc.GetImpl();
3509
3510 // Clip the DC so that you only draw on the non-client area
3511 RECT rcBorder;
3512 wxCopyRectToRECT(GetSize(), rcBorder);
3513
3514 RECT rcClient;
3515 theme->GetThemeBackgroundContentRect(
3516 hTheme, GetHdcOf(*impl), EP_EDITTEXT, ETS_NORMAL, &rcBorder, &rcClient);
3517 InflateRect(&rcClient, -1, -1);
3518
3519 ::ExcludeClipRect(GetHdcOf(*impl), rcClient.left, rcClient.top,
3520 rcClient.right, rcClient.bottom);
3521
3522 // Make sure the background is in a proper state
3523 if (theme->IsThemeBackgroundPartiallyTransparent(hTheme, EP_EDITTEXT, ETS_NORMAL))
3524 {
3525 theme->DrawThemeParentBackground(GetHwnd(), GetHdcOf(*impl), &rcBorder);
3526 }
3527
3528 // Draw the border
3529 int nState;
3530 if ( !IsEnabled() )
3531 nState = ETS_DISABLED;
3532 // should we check this?
3533 //else if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & ES_READONLY)
3534 // nState = ETS_READONLY;
3535 else
3536 nState = ETS_NORMAL;
3537 theme->DrawThemeBackground(hTheme, GetHdcOf(*impl), EP_EDITTEXT, nState, &rcBorder, NULL);
3538 }
3539 }
3540 break;
3541
3542 #endif // wxUSE_UXTHEME
3543
3544 default:
3545 // try a custom message handler
3546 const MSWMessageHandlers::const_iterator
3547 i = gs_messageHandlers.find(message);
3548 if ( i != gs_messageHandlers.end() )
3549 {
3550 processed = (*i->second)(this, message, wParam, lParam);
3551 }
3552 }
3553
3554 if ( !processed )
3555 {
3556 #if wxDEBUG_LEVEL >= 2
3557 wxLogTrace("winmsg", wxT("Forwarding %s to DefWindowProc."),
3558 wxGetMessageName(message));
3559 #endif // wxDEBUG_LEVEL >= 2
3560 rc.result = MSWDefWindowProc(message, wParam, lParam);
3561 }
3562
3563 return rc.result;
3564 }
3565
3566 // ----------------------------------------------------------------------------
3567 // wxWindow <-> HWND map
3568 // ----------------------------------------------------------------------------
3569
3570 wxWindow *wxFindWinFromHandle(HWND hwnd)
3571 {
3572 WindowHandles::const_iterator i = gs_windowHandles.find(hwnd);
3573 return i == gs_windowHandles.end() ? NULL : i->second;
3574 }
3575
3576 void wxAssociateWinWithHandle(HWND hwnd, wxWindowMSW *win)
3577 {
3578 // adding NULL hwnd is (first) surely a result of an error and
3579 // (secondly) breaks menu command processing
3580 wxCHECK_RET( hwnd != (HWND)NULL,
3581 wxT("attempt to add a NULL hwnd to window list ignored") );
3582
3583 #if wxDEBUG_LEVEL
3584 WindowHandles::const_iterator i = gs_windowHandles.find(hwnd);
3585 if ( i != gs_windowHandles.end() )
3586 {
3587 if ( i->second != win )
3588 {
3589 wxFAIL_MSG(
3590 wxString::Format(
3591 wxT("HWND %p already associated with another window (%s)"),
3592 hwnd, win->GetClassInfo()->GetClassName()
3593 )
3594 );
3595 }
3596 //else: this actually happens currently because we associate the window
3597 // with its HWND during creation (if we create it) and also when
3598 // SubclassWin() is called later, this is ok
3599 }
3600 #endif // wxDEBUG_LEVEL
3601
3602 gs_windowHandles[hwnd] = (wxWindow *)win;
3603 }
3604
3605 void wxRemoveHandleAssociation(wxWindowMSW *win)
3606 {
3607 gs_windowHandles.erase(GetHwndOf(win));
3608 }
3609
3610 // ----------------------------------------------------------------------------
3611 // various MSW speciic class dependent functions
3612 // ----------------------------------------------------------------------------
3613
3614 // Default destroyer - override if you destroy it in some other way
3615 // (e.g. with MDI child windows)
3616 void wxWindowMSW::MSWDestroyWindow()
3617 {
3618 }
3619
3620 void wxWindowMSW::MSWGetCreateWindowCoords(const wxPoint& pos,
3621 const wxSize& size,
3622 int& x, int& y,
3623 int& w, int& h) const
3624 {
3625 // CW_USEDEFAULT can't be used for child windows so just position them at
3626 // the origin by default
3627 x = pos.x == wxDefaultCoord ? 0 : pos.x;
3628 y = pos.y == wxDefaultCoord ? 0 : pos.y;
3629
3630 AdjustForParentClientOrigin(x, y);
3631
3632 // We don't have any clearly good choice for the size by default neither
3633 // but we must use something non-zero.
3634 w = WidthDefault(size.x);
3635 h = HeightDefault(size.y);
3636
3637 /*
3638 NB: there used to be some code here which set the initial size of the
3639 window to the client size of the parent if no explicit size was
3640 specified. This was wrong because wxWidgets programs often assume
3641 that they get a WM_SIZE (EVT_SIZE) upon creation, however this broke
3642 it. To see why, you should understand that Windows sends WM_SIZE from
3643 inside ::CreateWindow() anyhow. However, ::CreateWindow() is called
3644 from some base class ctor and so this WM_SIZE is not processed in the
3645 real class' OnSize() (because it's not fully constructed yet and the
3646 event goes to some base class OnSize() instead). So the WM_SIZE we
3647 rely on is the one sent when the parent frame resizes its children
3648 but here is the problem: if the child already has just the right
3649 size, nothing will happen as both wxWidgets and Windows check for
3650 this and ignore any attempts to change the window size to the size it
3651 already has - so no WM_SIZE would be sent.
3652 */
3653 }
3654
3655 WXHWND wxWindowMSW::MSWGetParent() const
3656 {
3657 return m_parent ? m_parent->GetHWND() : WXHWND(NULL);
3658 }
3659
3660 bool wxWindowMSW::MSWCreate(const wxChar *wclass,
3661 const wxChar *title,
3662 const wxPoint& pos,
3663 const wxSize& size,
3664 WXDWORD style,
3665 WXDWORD extendedStyle)
3666 {
3667 // check a common bug in the user code: if the window is created with a
3668 // non-default ctor and Create() is called too, we'd create 2 HWND for a
3669 // single wxWindow object and this results in all sorts of trouble,
3670 // especially for wxTLWs
3671 wxCHECK_MSG( !m_hWnd, true, "window can't be recreated" );
3672
3673 // this can happen if this function is called using the return value of
3674 // wxApp::GetRegisteredClassName() which failed
3675 wxCHECK_MSG( wclass, false, "failed to register window class?" );
3676
3677
3678 // choose the position/size for the new window
3679 int x, y, w, h;
3680 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
3681
3682 // controlId is menu handle for the top level windows, so set it to 0
3683 // unless we're creating a child window
3684 int controlId = style & WS_CHILD ? GetId() : 0;
3685
3686 // for each class "Foo" we have we also have "FooNR" ("no repaint") class
3687 // which is the same but without CS_[HV]REDRAW class styles so using it
3688 // ensures that the window is not fully repainted on each resize
3689 wxString className(wclass);
3690 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
3691 {
3692 className += wxApp::GetNoRedrawClassSuffix();
3693 }
3694
3695 // do create the window
3696 wxWindowCreationHook hook(this);
3697
3698 m_hWnd = (WXHWND)::CreateWindowEx
3699 (
3700 extendedStyle,
3701 className.wx_str(),
3702 title ? title : m_windowName.wx_str(),
3703 style,
3704 x, y, w, h,
3705 (HWND)MSWGetParent(),
3706 (HMENU)wxUIntToPtr(controlId),
3707 wxGetInstance(),
3708 NULL // no extra data
3709 );
3710
3711 if ( !m_hWnd )
3712 {
3713 wxLogSysError(_("Can't create window of class %s"), className.c_str());
3714
3715 return false;
3716 }
3717
3718 SubclassWin(m_hWnd);
3719
3720 return true;
3721 }
3722
3723 // ===========================================================================
3724 // MSW message handlers
3725 // ===========================================================================
3726
3727 // ---------------------------------------------------------------------------
3728 // WM_NOTIFY
3729 // ---------------------------------------------------------------------------
3730
3731 bool wxWindowMSW::HandleNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
3732 {
3733 #ifndef __WXMICROWIN__
3734 LPNMHDR hdr = (LPNMHDR)lParam;
3735 HWND hWnd = hdr->hwndFrom;
3736 wxWindow *win = wxFindWinFromHandle(hWnd);
3737
3738 // if the control is one of our windows, let it handle the message itself
3739 if ( win )
3740 {
3741 return win->MSWOnNotify(idCtrl, lParam, result);
3742 }
3743
3744 // VZ: why did we do it? normally this is unnecessary and, besides, it
3745 // breaks the message processing for the toolbars because the tooltip
3746 // notifications were being forwarded to the toolbar child controls
3747 // (if it had any) before being passed to the toolbar itself, so in my
3748 // example the tooltip for the combobox was always shown instead of the
3749 // correct button tooltips
3750 #if 0
3751 // try all our children
3752 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
3753 while ( node )
3754 {
3755 wxWindow *child = node->GetData();
3756 if ( child->MSWOnNotify(idCtrl, lParam, result) )
3757 {
3758 return true;
3759 }
3760
3761 node = node->GetNext();
3762 }
3763 #endif // 0
3764
3765 // by default, handle it ourselves
3766 return MSWOnNotify(idCtrl, lParam, result);
3767 #else // __WXMICROWIN__
3768 return false;
3769 #endif
3770 }
3771
3772 #if wxUSE_TOOLTIPS
3773
3774 bool wxWindowMSW::HandleTooltipNotify(WXUINT code,
3775 WXLPARAM lParam,
3776 const wxString& ttip)
3777 {
3778 // I don't know why it happens, but the versions of comctl32.dll starting
3779 // from 4.70 sometimes send TTN_NEEDTEXTW even to ANSI programs (normally,
3780 // this message is supposed to be sent to Unicode programs only) -- hence
3781 // we need to handle it as well, otherwise no tooltips will be shown in
3782 // this case
3783 #ifndef __WXWINCE__
3784 if ( !(code == (WXUINT) TTN_NEEDTEXTA || code == (WXUINT) TTN_NEEDTEXTW)
3785 || ttip.empty() )
3786 {
3787 // not a tooltip message or no tooltip to show anyhow
3788 return false;
3789 }
3790 #endif
3791
3792 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
3793
3794 // We don't want to use the szText buffer because it has a limit of 80
3795 // bytes and this is not enough, especially for Unicode build where it
3796 // limits the tooltip string length to only 40 characters
3797 //
3798 // The best would be, of course, to not impose any length limitations at
3799 // all but then the buffer would have to be dynamic and someone would have
3800 // to free it and we don't have the tooltip owner object here any more, so
3801 // for now use our own static buffer with a higher fixed max length.
3802 //
3803 // Note that using a static buffer should not be a problem as only a single
3804 // tooltip can be shown at the same time anyhow.
3805 #if !wxUSE_UNICODE
3806 if ( code == (WXUINT) TTN_NEEDTEXTW )
3807 {
3808 // We need to convert tooltip from multi byte to Unicode on the fly.
3809 static wchar_t buf[513];
3810
3811 // Truncate tooltip length if needed as otherwise we might not have
3812 // enough space for it in the buffer and MultiByteToWideChar() would
3813 // return an error
3814 size_t tipLength = wxMin(ttip.length(), WXSIZEOF(buf) - 1);
3815
3816 // Convert to WideChar without adding the NULL character. The NULL
3817 // character is added afterwards (this is more efficient).
3818 int len = ::MultiByteToWideChar
3819 (
3820 CP_ACP,
3821 0, // no flags
3822 ttip.wx_str(),
3823 tipLength,
3824 buf,
3825 WXSIZEOF(buf) - 1
3826 );
3827
3828 if ( !len )
3829 {
3830 wxLogLastError(wxT("MultiByteToWideChar()"));
3831 }
3832
3833 buf[len] = L'\0';
3834 ttText->lpszText = (LPSTR) buf;
3835 }
3836 else // TTN_NEEDTEXTA
3837 #endif // !wxUSE_UNICODE
3838 {
3839 // we get here if we got TTN_NEEDTEXTA (only happens in ANSI build) or
3840 // if we got TTN_NEEDTEXTW in Unicode build: in this case we just have
3841 // to copy the string we have into the buffer
3842 static wxChar buf[513];
3843 wxStrlcpy(buf, ttip.c_str(), WXSIZEOF(buf));
3844 ttText->lpszText = buf;
3845 }
3846
3847 return true;
3848 }
3849
3850 #endif // wxUSE_TOOLTIPS
3851
3852 bool wxWindowMSW::MSWOnNotify(int WXUNUSED(idCtrl),
3853 WXLPARAM lParam,
3854 WXLPARAM* WXUNUSED(result))
3855 {
3856 #if wxUSE_TOOLTIPS
3857 if ( m_tooltip )
3858 {
3859 NMHDR* hdr = (NMHDR *)lParam;
3860 if ( HandleTooltipNotify(hdr->code, lParam, m_tooltip->GetTip()))
3861 {
3862 // processed
3863 return true;
3864 }
3865 }
3866 #else
3867 wxUnusedVar(lParam);
3868 #endif // wxUSE_TOOLTIPS
3869
3870 return false;
3871 }
3872
3873 // ---------------------------------------------------------------------------
3874 // end session messages
3875 // ---------------------------------------------------------------------------
3876
3877 bool wxWindowMSW::HandleQueryEndSession(long logOff, bool *mayEnd)
3878 {
3879 #ifdef ENDSESSION_LOGOFF
3880 wxCloseEvent event(wxEVT_QUERY_END_SESSION, wxID_ANY);
3881 event.SetEventObject(wxTheApp);
3882 event.SetCanVeto(true);
3883 event.SetLoggingOff(logOff == (long)ENDSESSION_LOGOFF);
3884
3885 bool rc = wxTheApp->ProcessEvent(event);
3886
3887 if ( rc )
3888 {
3889 // we may end only if the app didn't veto session closing (double
3890 // negation...)
3891 *mayEnd = !event.GetVeto();
3892 }
3893
3894 return rc;
3895 #else
3896 wxUnusedVar(logOff);
3897 wxUnusedVar(mayEnd);
3898 return false;
3899 #endif
3900 }
3901
3902 bool wxWindowMSW::HandleEndSession(bool endSession, long logOff)
3903 {
3904 #ifdef ENDSESSION_LOGOFF
3905 // do nothing if the session isn't ending
3906 if ( !endSession )
3907 return false;
3908
3909 // only send once
3910 if ( (this != wxTheApp->GetTopWindow()) )
3911 return false;
3912
3913 wxCloseEvent event(wxEVT_END_SESSION, wxID_ANY);
3914 event.SetEventObject(wxTheApp);
3915 event.SetCanVeto(false);
3916 event.SetLoggingOff((logOff & ENDSESSION_LOGOFF) != 0);
3917
3918 return wxTheApp->ProcessEvent(event);
3919 #else
3920 wxUnusedVar(endSession);
3921 wxUnusedVar(logOff);
3922 return false;
3923 #endif
3924 }
3925
3926 // ---------------------------------------------------------------------------
3927 // window creation/destruction
3928 // ---------------------------------------------------------------------------
3929
3930 bool wxWindowMSW::HandleCreate(WXLPCREATESTRUCT WXUNUSED_IN_WINCE(cs),
3931 bool *mayCreate)
3932 {
3933 // VZ: why is this commented out for WinCE? If it doesn't support
3934 // WS_EX_CONTROLPARENT at all it should be somehow handled globally,
3935 // not with multiple #ifdef's!
3936 #ifndef __WXWINCE__
3937 if ( ((CREATESTRUCT *)cs)->dwExStyle & WS_EX_CONTROLPARENT )
3938 EnsureParentHasControlParentStyle(GetParent());
3939 #endif // !__WXWINCE__
3940
3941 *mayCreate = true;
3942
3943 return true;
3944 }
3945
3946 bool wxWindowMSW::HandleDestroy()
3947 {
3948 // delete our drop target if we've got one
3949 #if wxUSE_DRAG_AND_DROP
3950 if ( m_dropTarget != NULL )
3951 {
3952 m_dropTarget->Revoke(m_hWnd);
3953
3954 wxDELETE(m_dropTarget);
3955 }
3956 #endif // wxUSE_DRAG_AND_DROP
3957
3958 // WM_DESTROY handled
3959 return true;
3960 }
3961
3962 // ---------------------------------------------------------------------------
3963 // activation/focus
3964 // ---------------------------------------------------------------------------
3965
3966 bool wxWindowMSW::HandleActivate(int state,
3967 bool WXUNUSED(minimized),
3968 WXHWND WXUNUSED(activate))
3969 {
3970 wxActivateEvent event(wxEVT_ACTIVATE,
3971 (state == WA_ACTIVE) || (state == WA_CLICKACTIVE),
3972 m_windowId);
3973 event.SetEventObject(this);
3974
3975 return HandleWindowEvent(event);
3976 }
3977
3978 bool wxWindowMSW::HandleSetFocus(WXHWND hwnd)
3979 {
3980 // Strangly enough, some controls get set focus events when they are being
3981 // deleted, even if they already had focus before.
3982 if ( m_isBeingDeleted )
3983 {
3984 return false;
3985 }
3986
3987 // notify the parent keeping track of focus for the kbd navigation
3988 // purposes that we got it
3989 wxChildFocusEvent eventFocus((wxWindow *)this);
3990 (void)HandleWindowEvent(eventFocus);
3991
3992 #if wxUSE_CARET
3993 // Deal with caret
3994 if ( m_caret )
3995 {
3996 m_caret->OnSetFocus();
3997 }
3998 #endif // wxUSE_CARET
3999
4000 wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
4001 event.SetEventObject(this);
4002
4003 // wxFindWinFromHandle() may return NULL, it is ok
4004 event.SetWindow(wxFindWinFromHandle(hwnd));
4005
4006 return HandleWindowEvent(event);
4007 }
4008
4009 bool wxWindowMSW::HandleKillFocus(WXHWND hwnd)
4010 {
4011 #if wxUSE_CARET
4012 // Deal with caret
4013 if ( m_caret )
4014 {
4015 m_caret->OnKillFocus();
4016 }
4017 #endif // wxUSE_CARET
4018
4019 // Don't send the event when in the process of being deleted. This can
4020 // only cause problems if the event handler tries to access the object.
4021 if ( m_isBeingDeleted )
4022 {
4023 return false;
4024 }
4025
4026 wxFocusEvent event(wxEVT_KILL_FOCUS, m_windowId);
4027 event.SetEventObject(this);
4028
4029 // wxFindWinFromHandle() may return NULL, it is ok
4030 event.SetWindow(wxFindWinFromHandle(hwnd));
4031
4032 return HandleWindowEvent(event);
4033 }
4034
4035 // ---------------------------------------------------------------------------
4036 // labels
4037 // ---------------------------------------------------------------------------
4038
4039 void wxWindowMSW::SetLabel( const wxString& label)
4040 {
4041 SetWindowText(GetHwnd(), label.c_str());
4042 }
4043
4044 wxString wxWindowMSW::GetLabel() const
4045 {
4046 return wxGetWindowText(GetHWND());
4047 }
4048
4049 // ---------------------------------------------------------------------------
4050 // miscellaneous
4051 // ---------------------------------------------------------------------------
4052
4053 bool wxWindowMSW::HandleShow(bool show, int WXUNUSED(status))
4054 {
4055 wxShowEvent event(GetId(), show);
4056 event.SetEventObject(this);
4057
4058 return HandleWindowEvent(event);
4059 }
4060
4061 bool wxWindowMSW::HandleInitDialog(WXHWND WXUNUSED(hWndFocus))
4062 {
4063 wxInitDialogEvent event(GetId());
4064 event.SetEventObject(this);
4065
4066 return HandleWindowEvent(event);
4067 }
4068
4069 bool wxWindowMSW::HandleDropFiles(WXWPARAM wParam)
4070 {
4071 #if defined (__WXMICROWIN__) || defined(__WXWINCE__)
4072 wxUnusedVar(wParam);
4073 return false;
4074 #else // __WXMICROWIN__
4075 HDROP hFilesInfo = (HDROP) wParam;
4076
4077 // Get the total number of files dropped
4078 UINT gwFilesDropped = ::DragQueryFile
4079 (
4080 (HDROP)hFilesInfo,
4081 (UINT)-1,
4082 (LPTSTR)0,
4083 (UINT)0
4084 );
4085
4086 wxString *files = new wxString[gwFilesDropped];
4087 for ( UINT wIndex = 0; wIndex < gwFilesDropped; wIndex++ )
4088 {
4089 // first get the needed buffer length (+1 for terminating NUL)
4090 size_t len = ::DragQueryFile(hFilesInfo, wIndex, NULL, 0) + 1;
4091
4092 // and now get the file name
4093 ::DragQueryFile(hFilesInfo, wIndex,
4094 wxStringBuffer(files[wIndex], len), len);
4095 }
4096
4097 wxDropFilesEvent event(wxEVT_DROP_FILES, gwFilesDropped, files);
4098 event.SetEventObject(this);
4099
4100 POINT dropPoint;
4101 DragQueryPoint(hFilesInfo, (LPPOINT) &dropPoint);
4102 event.m_pos.x = dropPoint.x;
4103 event.m_pos.y = dropPoint.y;
4104
4105 DragFinish(hFilesInfo);
4106
4107 return HandleWindowEvent(event);
4108 #endif
4109 }
4110
4111
4112 bool wxWindowMSW::HandleSetCursor(WXHWND WXUNUSED(hWnd),
4113 short nHitTest,
4114 int WXUNUSED(mouseMsg))
4115 {
4116 #ifndef __WXMICROWIN__
4117 // the logic is as follows:
4118 // 0. if we're busy, set the busy cursor (even for non client elements)
4119 // 1. don't set custom cursor for non client area of enabled windows
4120 // 2. ask user EVT_SET_CURSOR handler for the cursor
4121 // 3. if still no cursor but we're in a TLW, set the global cursor
4122
4123 HCURSOR hcursor = 0;
4124 if ( wxIsBusy() )
4125 {
4126 hcursor = wxGetCurrentBusyCursor();
4127 }
4128 else // not busy
4129 {
4130 if ( nHitTest != HTCLIENT )
4131 return false;
4132
4133 // first ask the user code - it may wish to set the cursor in some very
4134 // specific way (for example, depending on the current position)
4135 POINT pt;
4136 #ifdef __WXWINCE__
4137 if ( !::GetCursorPosWinCE(&pt))
4138 #else
4139 if ( !::GetCursorPos(&pt) )
4140 #endif
4141 {
4142 wxLogLastError(wxT("GetCursorPos"));
4143 }
4144
4145 int x = pt.x,
4146 y = pt.y;
4147 ScreenToClient(&x, &y);
4148 wxSetCursorEvent event(x, y);
4149 event.SetId(GetId());
4150 event.SetEventObject(this);
4151
4152 bool processedEvtSetCursor = HandleWindowEvent(event);
4153 if ( processedEvtSetCursor && event.HasCursor() )
4154 {
4155 hcursor = GetHcursorOf(event.GetCursor());
4156 }
4157
4158 if ( !hcursor )
4159 {
4160 // the test for processedEvtSetCursor is here to prevent using
4161 // m_cursor if the user code caught EVT_SET_CURSOR() and returned
4162 // nothing from it - this is a way to say that our cursor shouldn't
4163 // be used for this point
4164 if ( !processedEvtSetCursor && m_cursor.Ok() )
4165 {
4166 hcursor = GetHcursorOf(m_cursor);
4167 }
4168
4169 if ( !hcursor && !GetParent() )
4170 {
4171 const wxCursor *cursor = wxGetGlobalCursor();
4172 if ( cursor && cursor->Ok() )
4173 {
4174 hcursor = GetHcursorOf(*cursor);
4175 }
4176 }
4177 }
4178 }
4179
4180
4181 if ( hcursor )
4182 {
4183 ::SetCursor(hcursor);
4184
4185 // cursor set, stop here
4186 return true;
4187 }
4188 #endif // __WXMICROWIN__
4189
4190 // pass up the window chain
4191 return false;
4192 }
4193
4194 bool wxWindowMSW::HandlePower(WXWPARAM WXUNUSED_IN_WINCE(wParam),
4195 WXLPARAM WXUNUSED(lParam),
4196 bool *WXUNUSED_IN_WINCE(vetoed))
4197 {
4198 #ifdef __WXWINCE__
4199 // FIXME
4200 return false;
4201 #else
4202 wxEventType evtType;
4203 switch ( wParam )
4204 {
4205 case PBT_APMQUERYSUSPEND:
4206 evtType = wxEVT_POWER_SUSPENDING;
4207 break;
4208
4209 case PBT_APMQUERYSUSPENDFAILED:
4210 evtType = wxEVT_POWER_SUSPEND_CANCEL;
4211 break;
4212
4213 case PBT_APMSUSPEND:
4214 evtType = wxEVT_POWER_SUSPENDED;
4215 break;
4216
4217 case PBT_APMRESUMESUSPEND:
4218 evtType = wxEVT_POWER_RESUME;
4219 break;
4220
4221 default:
4222 wxLogDebug(wxT("Unknown WM_POWERBROADCAST(%d) event"), wParam);
4223 // fall through
4224
4225 // these messages are currently not mapped to wx events
4226 case PBT_APMQUERYSTANDBY:
4227 case PBT_APMQUERYSTANDBYFAILED:
4228 case PBT_APMSTANDBY:
4229 case PBT_APMRESUMESTANDBY:
4230 case PBT_APMBATTERYLOW:
4231 case PBT_APMPOWERSTATUSCHANGE:
4232 case PBT_APMOEMEVENT:
4233 case PBT_APMRESUMECRITICAL:
4234 #ifdef PBT_APMRESUMEAUTOMATIC
4235 case PBT_APMRESUMEAUTOMATIC:
4236 #endif
4237 evtType = wxEVT_NULL;
4238 break;
4239 }
4240
4241 // don't handle unknown messages
4242 if ( evtType == wxEVT_NULL )
4243 return false;
4244
4245 // TODO: notify about PBTF_APMRESUMEFROMFAILURE in case of resume events?
4246
4247 wxPowerEvent event(evtType);
4248 if ( !HandleWindowEvent(event) )
4249 return false;
4250
4251 *vetoed = event.IsVetoed();
4252
4253 return true;
4254 #endif
4255 }
4256
4257 bool wxWindowMSW::IsDoubleBuffered() const
4258 {
4259 for ( const wxWindowMSW *win = this; win; win = win->GetParent() )
4260 {
4261 if ( wxHasWindowExStyle(win, WS_EX_COMPOSITED) )
4262 return true;
4263
4264 if ( win->IsTopLevel() )
4265 break;
4266 }
4267
4268 return false;
4269 }
4270
4271 void wxWindowMSW::SetDoubleBuffered(bool on)
4272 {
4273 // Get the current extended style bits
4274 long exstyle = wxGetWindowExStyle(this);
4275
4276 // Twiddle the bit as needed
4277 if ( on )
4278 exstyle |= WS_EX_COMPOSITED;
4279 else
4280 exstyle &= ~WS_EX_COMPOSITED;
4281
4282 // put it back
4283 wxSetWindowExStyle(this, exstyle);
4284 }
4285
4286 // ---------------------------------------------------------------------------
4287 // owner drawn stuff
4288 // ---------------------------------------------------------------------------
4289
4290 #if (wxUSE_OWNER_DRAWN && wxUSE_MENUS_NATIVE) || \
4291 (wxUSE_CONTROLS && !defined(__WXUNIVERSAL__))
4292 #define WXUNUSED_UNLESS_ODRAWN(param) param
4293 #else
4294 #define WXUNUSED_UNLESS_ODRAWN(param)
4295 #endif
4296
4297 bool
4298 wxWindowMSW::MSWOnDrawItem(int WXUNUSED_UNLESS_ODRAWN(id),
4299 WXDRAWITEMSTRUCT * WXUNUSED_UNLESS_ODRAWN(itemStruct))
4300 {
4301 #if wxUSE_OWNER_DRAWN
4302
4303 #if wxUSE_MENUS_NATIVE
4304 // is it a menu item?
4305 DRAWITEMSTRUCT *pDrawStruct = (DRAWITEMSTRUCT *)itemStruct;
4306 if ( id == 0 && pDrawStruct->CtlType == ODT_MENU )
4307 {
4308 wxMenuItem *pMenuItem = (wxMenuItem *)(pDrawStruct->itemData);
4309
4310 // see comment before the same test in MSWOnMeasureItem() below
4311 if ( !pMenuItem )
4312 return false;
4313
4314 wxCHECK_MSG( wxDynamicCast(pMenuItem, wxMenuItem),
4315 false, wxT("MSWOnDrawItem: bad wxMenuItem pointer") );
4316
4317 // prepare to call OnDrawItem(): notice using of wxDCTemp to prevent
4318 // the DC from being released
4319 wxDCTemp dc((WXHDC)pDrawStruct->hDC);
4320 wxRect rect(pDrawStruct->rcItem.left, pDrawStruct->rcItem.top,
4321 pDrawStruct->rcItem.right - pDrawStruct->rcItem.left,
4322 pDrawStruct->rcItem.bottom - pDrawStruct->rcItem.top);
4323
4324 return pMenuItem->OnDrawItem
4325 (
4326 dc,
4327 rect,
4328 (wxOwnerDrawn::wxODAction)pDrawStruct->itemAction,
4329 (wxOwnerDrawn::wxODStatus)pDrawStruct->itemState
4330 );
4331 }
4332 #endif // wxUSE_MENUS_NATIVE
4333
4334 #endif // USE_OWNER_DRAWN
4335
4336 #if wxUSE_CONTROLS && !defined(__WXUNIVERSAL__)
4337
4338 #if wxUSE_OWNER_DRAWN
4339 wxControl *item = wxDynamicCast(FindItem(id), wxControl);
4340 #else // !wxUSE_OWNER_DRAWN
4341 // we may still have owner-drawn buttons internally because we have to make
4342 // them owner-drawn to support colour change
4343 wxControl *item =
4344 # if wxUSE_BUTTON
4345 wxDynamicCast(FindItem(id), wxButton)
4346 # else
4347 NULL
4348 # endif
4349 ;
4350 #endif // USE_OWNER_DRAWN
4351
4352 if ( item )
4353 {
4354 return item->MSWOnDraw(itemStruct);
4355 }
4356
4357 #endif // wxUSE_CONTROLS
4358
4359 return false;
4360 }
4361
4362 bool
4363 wxWindowMSW::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
4364 {
4365 #if wxUSE_OWNER_DRAWN && wxUSE_MENUS_NATIVE
4366 // is it a menu item?
4367 MEASUREITEMSTRUCT *pMeasureStruct = (MEASUREITEMSTRUCT *)itemStruct;
4368 if ( id == 0 && pMeasureStruct->CtlType == ODT_MENU )
4369 {
4370 wxMenuItem *pMenuItem = (wxMenuItem *)(pMeasureStruct->itemData);
4371
4372 // according to Carsten Fuchs the pointer may be NULL under XP if an
4373 // MDI child frame is initially maximized, see this for more info:
4374 // http://article.gmane.org/gmane.comp.lib.wxwidgets.general/27745
4375 //
4376 // so silently ignore it instead of asserting
4377 if ( !pMenuItem )
4378 return false;
4379
4380 wxCHECK_MSG( wxDynamicCast(pMenuItem, wxMenuItem),
4381 false, wxT("MSWOnMeasureItem: bad wxMenuItem pointer") );
4382
4383 size_t w, h;
4384 bool rc = pMenuItem->OnMeasureItem(&w, &h);
4385
4386 pMeasureStruct->itemWidth = w;
4387 pMeasureStruct->itemHeight = h;
4388
4389 return rc;
4390 }
4391
4392 wxControl *item = wxDynamicCast(FindItem(id), wxControl);
4393 if ( item )
4394 {
4395 return item->MSWOnMeasure(itemStruct);
4396 }
4397 #else
4398 wxUnusedVar(id);
4399 wxUnusedVar(itemStruct);
4400 #endif // wxUSE_OWNER_DRAWN && wxUSE_MENUS_NATIVE
4401
4402 return false;
4403 }
4404
4405 // ---------------------------------------------------------------------------
4406 // colours and palettes
4407 // ---------------------------------------------------------------------------
4408
4409 bool wxWindowMSW::HandleSysColorChange()
4410 {
4411 wxSysColourChangedEvent event;
4412 event.SetEventObject(this);
4413
4414 (void)HandleWindowEvent(event);
4415
4416 // always let the system carry on the default processing to allow the
4417 // native controls to react to the colours update
4418 return false;
4419 }
4420
4421 bool wxWindowMSW::HandleDisplayChange()
4422 {
4423 wxDisplayChangedEvent event;
4424 event.SetEventObject(this);
4425
4426 return HandleWindowEvent(event);
4427 }
4428
4429 #ifndef __WXMICROWIN__
4430
4431 bool wxWindowMSW::HandleCtlColor(WXHBRUSH *brush, WXHDC hDC, WXHWND hWnd)
4432 {
4433 #if !wxUSE_CONTROLS || defined(__WXUNIVERSAL__)
4434 wxUnusedVar(hDC);
4435 wxUnusedVar(hWnd);
4436 #else
4437 wxControl *item = wxDynamicCast(FindItemByHWND(hWnd, true), wxControl);
4438
4439 if ( item )
4440 *brush = item->MSWControlColor(hDC, hWnd);
4441 else
4442 #endif // wxUSE_CONTROLS
4443 *brush = NULL;
4444
4445 return *brush != NULL;
4446 }
4447
4448 #endif // __WXMICROWIN__
4449
4450 bool wxWindowMSW::HandlePaletteChanged(WXHWND hWndPalChange)
4451 {
4452 #if wxUSE_PALETTE
4453 // same as below except we don't respond to our own messages
4454 if ( hWndPalChange != GetHWND() )
4455 {
4456 // check to see if we our our parents have a custom palette
4457 wxWindowMSW *win = this;
4458 while ( win && !win->HasCustomPalette() )
4459 {
4460 win = win->GetParent();
4461 }
4462
4463 if ( win && win->HasCustomPalette() )
4464 {
4465 // realize the palette to see whether redrawing is needed
4466 HDC hdc = ::GetDC((HWND) hWndPalChange);
4467 win->m_palette.SetHPALETTE((WXHPALETTE)
4468 ::SelectPalette(hdc, GetHpaletteOf(win->m_palette), FALSE));
4469
4470 int result = ::RealizePalette(hdc);
4471
4472 // restore the palette (before releasing the DC)
4473 win->m_palette.SetHPALETTE((WXHPALETTE)
4474 ::SelectPalette(hdc, GetHpaletteOf(win->m_palette), FALSE));
4475 ::RealizePalette(hdc);
4476 ::ReleaseDC((HWND) hWndPalChange, hdc);
4477
4478 // now check for the need to redraw
4479 if (result > 0)
4480 ::InvalidateRect((HWND) hWndPalChange, NULL, TRUE);
4481 }
4482
4483 }
4484 #endif // wxUSE_PALETTE
4485
4486 wxPaletteChangedEvent event(GetId());
4487 event.SetEventObject(this);
4488 event.SetChangedWindow(wxFindWinFromHandle(hWndPalChange));
4489
4490 return HandleWindowEvent(event);
4491 }
4492
4493 bool wxWindowMSW::HandleCaptureChanged(WXHWND hWndGainedCapture)
4494 {
4495 // notify windows on the capture stack about lost capture
4496 // (see http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863):
4497 wxWindowBase::NotifyCaptureLost();
4498
4499 wxWindow *win = wxFindWinFromHandle(hWndGainedCapture);
4500 wxMouseCaptureChangedEvent event(GetId(), win);
4501 event.SetEventObject(this);
4502 return HandleWindowEvent(event);
4503 }
4504
4505 bool wxWindowMSW::HandleSettingChange(WXWPARAM wParam, WXLPARAM lParam)
4506 {
4507 // despite MSDN saying "(This message cannot be sent directly to a window.)"
4508 // we need to send this to child windows (it is only sent to top-level
4509 // windows) so {list,tree}ctrls can adjust their font size if necessary
4510 // this is exactly how explorer does it to enable the font size changes
4511
4512 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
4513 while ( node )
4514 {
4515 // top-level windows already get this message from the system
4516 wxWindow *win = node->GetData();
4517 if ( !win->IsTopLevel() )
4518 {
4519 ::SendMessage(GetHwndOf(win), WM_SETTINGCHANGE, wParam, lParam);
4520 }
4521
4522 node = node->GetNext();
4523 }
4524
4525 // let the system handle it
4526 return false;
4527 }
4528
4529 bool wxWindowMSW::HandleQueryNewPalette()
4530 {
4531
4532 #if wxUSE_PALETTE
4533 // check to see if we our our parents have a custom palette
4534 wxWindowMSW *win = this;
4535 while (!win->HasCustomPalette() && win->GetParent()) win = win->GetParent();
4536 if (win->HasCustomPalette()) {
4537 /* realize the palette to see whether redrawing is needed */
4538 HDC hdc = ::GetDC((HWND) GetHWND());
4539 win->m_palette.SetHPALETTE( (WXHPALETTE)
4540 ::SelectPalette(hdc, (HPALETTE) win->m_palette.GetHPALETTE(), FALSE) );
4541
4542 int result = ::RealizePalette(hdc);
4543 /* restore the palette (before releasing the DC) */
4544 win->m_palette.SetHPALETTE( (WXHPALETTE)
4545 ::SelectPalette(hdc, (HPALETTE) win->m_palette.GetHPALETTE(), TRUE) );
4546 ::RealizePalette(hdc);
4547 ::ReleaseDC((HWND) GetHWND(), hdc);
4548 /* now check for the need to redraw */
4549 if (result > 0)
4550 ::InvalidateRect((HWND) GetHWND(), NULL, TRUE);
4551 }
4552 #endif // wxUSE_PALETTE
4553
4554 wxQueryNewPaletteEvent event(GetId());
4555 event.SetEventObject(this);
4556
4557 return HandleWindowEvent(event) && event.GetPaletteRealized();
4558 }
4559
4560 // Responds to colour changes: passes event on to children.
4561 void wxWindowMSW::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
4562 {
4563 // the top level window also reset the standard colour map as it might have
4564 // changed (there is no need to do it for the non top level windows as we
4565 // only have to do it once)
4566 if ( IsTopLevel() )
4567 {
4568 // FIXME-MT
4569 gs_hasStdCmap = false;
4570 }
4571 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
4572 while ( node )
4573 {
4574 // Only propagate to non-top-level windows because Windows already
4575 // sends this event to all top-level ones
4576 wxWindow *win = node->GetData();
4577 if ( !win->IsTopLevel() )
4578 {
4579 // we need to send the real WM_SYSCOLORCHANGE and not just trigger
4580 // EVT_SYS_COLOUR_CHANGED call because the latter wouldn't work for
4581 // the standard controls
4582 ::SendMessage(GetHwndOf(win), WM_SYSCOLORCHANGE, 0, 0);
4583 }
4584
4585 node = node->GetNext();
4586 }
4587 }
4588
4589 extern wxCOLORMAP *wxGetStdColourMap()
4590 {
4591 static COLORREF s_stdColours[wxSTD_COL_MAX];
4592 static wxCOLORMAP s_cmap[wxSTD_COL_MAX];
4593
4594 if ( !gs_hasStdCmap )
4595 {
4596 static bool s_coloursInit = false;
4597
4598 if ( !s_coloursInit )
4599 {
4600 // When a bitmap is loaded, the RGB values can change (apparently
4601 // because Windows adjusts them to care for the old programs always
4602 // using 0xc0c0c0 while the transparent colour for the new Windows
4603 // versions is different). But we do this adjustment ourselves so
4604 // we want to avoid Windows' "help" and for this we need to have a
4605 // reference bitmap which can tell us what the RGB values change
4606 // to.
4607 wxLogNull logNo; // suppress error if we couldn't load the bitmap
4608 wxBitmap stdColourBitmap(wxT("wxBITMAP_STD_COLOURS"));
4609 if ( stdColourBitmap.Ok() )
4610 {
4611 // the pixels in the bitmap must correspond to wxSTD_COL_XXX!
4612 wxASSERT_MSG( stdColourBitmap.GetWidth() == wxSTD_COL_MAX,
4613 wxT("forgot to update wxBITMAP_STD_COLOURS!") );
4614
4615 wxMemoryDC memDC;
4616 memDC.SelectObject(stdColourBitmap);
4617
4618 wxColour colour;
4619 for ( size_t i = 0; i < WXSIZEOF(s_stdColours); i++ )
4620 {
4621 memDC.GetPixel(i, 0, &colour);
4622 s_stdColours[i] = wxColourToRGB(colour);
4623 }
4624 }
4625 else // wxBITMAP_STD_COLOURS couldn't be loaded
4626 {
4627 s_stdColours[0] = RGB(000,000,000); // black
4628 s_stdColours[1] = RGB(128,128,128); // dark grey
4629 s_stdColours[2] = RGB(192,192,192); // light grey
4630 s_stdColours[3] = RGB(255,255,255); // white
4631 //s_stdColours[4] = RGB(000,000,255); // blue
4632 //s_stdColours[5] = RGB(255,000,255); // magenta
4633 }
4634
4635 s_coloursInit = true;
4636 }
4637
4638 gs_hasStdCmap = true;
4639
4640 // create the colour map
4641 #define INIT_CMAP_ENTRY(col) \
4642 s_cmap[wxSTD_COL_##col].from = s_stdColours[wxSTD_COL_##col]; \
4643 s_cmap[wxSTD_COL_##col].to = ::GetSysColor(COLOR_##col)
4644
4645 INIT_CMAP_ENTRY(BTNTEXT);
4646 INIT_CMAP_ENTRY(BTNSHADOW);
4647 INIT_CMAP_ENTRY(BTNFACE);
4648 INIT_CMAP_ENTRY(BTNHIGHLIGHT);
4649
4650 #undef INIT_CMAP_ENTRY
4651 }
4652
4653 return s_cmap;
4654 }
4655
4656 #if wxUSE_UXTHEME && !defined(TMT_FILLCOLOR)
4657 #define TMT_FILLCOLOR 3802
4658 #define TMT_TEXTCOLOR 3803
4659 #define TMT_BORDERCOLOR 3801
4660 #endif
4661
4662 wxColour wxWindowMSW::MSWGetThemeColour(const wchar_t *themeName,
4663 int themePart,
4664 int themeState,
4665 MSWThemeColour themeColour,
4666 wxSystemColour fallback) const
4667 {
4668 #if wxUSE_UXTHEME
4669 const wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
4670 if ( theme )
4671 {
4672 int themeProperty = 0;
4673
4674 // TODO: Convert this into a table? Sure would be faster.
4675 switch ( themeColour )
4676 {
4677 case ThemeColourBackground:
4678 themeProperty = TMT_FILLCOLOR;
4679 break;
4680 case ThemeColourText:
4681 themeProperty = TMT_TEXTCOLOR;
4682 break;
4683 case ThemeColourBorder:
4684 themeProperty = TMT_BORDERCOLOR;
4685 break;
4686 default:
4687 wxFAIL_MSG(wxT("unsupported theme colour"));
4688 };
4689
4690 wxUxThemeHandle hTheme((const wxWindow *)this, themeName);
4691 COLORREF col;
4692 HRESULT hr = theme->GetThemeColor
4693 (
4694 hTheme,
4695 themePart,
4696 themeState,
4697 themeProperty,
4698 &col
4699 );
4700
4701 if ( SUCCEEDED(hr) )
4702 return wxRGBToColour(col);
4703
4704 wxLogApiError(
4705 wxString::Format(
4706 "GetThemeColor(%s, %i, %i, %i)",
4707 themeName, themePart, themeState, themeProperty),
4708 hr);
4709 }
4710 #else
4711 wxUnusedVar(themeName);
4712 wxUnusedVar(themePart);
4713 wxUnusedVar(themeState);
4714 wxUnusedVar(themeColour);
4715 #endif
4716 return wxSystemSettings::GetColour(fallback);
4717 }
4718
4719 // ---------------------------------------------------------------------------
4720 // painting
4721 // ---------------------------------------------------------------------------
4722
4723 // this variable is used to check that a paint event handler which processed
4724 // the event did create a wxPaintDC inside its code and called BeginPaint() to
4725 // validate the invalidated window area as otherwise we'd keep getting an
4726 // endless stream of WM_PAINT messages for this window resulting in a lot of
4727 // difficult to debug problems (e.g. impossibility to repaint other windows,
4728 // lack of timer and idle events and so on)
4729 extern bool wxDidCreatePaintDC;
4730 bool wxDidCreatePaintDC = false;
4731
4732 bool wxWindowMSW::HandlePaint()
4733 {
4734 HRGN hRegion = ::CreateRectRgn(0, 0, 0, 0); // Dummy call to get a handle
4735 if ( !hRegion )
4736 {
4737 wxLogLastError(wxT("CreateRectRgn"));
4738 }
4739 if ( ::GetUpdateRgn(GetHwnd(), hRegion, FALSE) == ERROR )
4740 {
4741 wxLogLastError(wxT("GetUpdateRgn"));
4742 }
4743
4744 m_updateRegion = wxRegion((WXHRGN) hRegion);
4745
4746 wxDidCreatePaintDC = false;
4747
4748 wxPaintEvent event(m_windowId);
4749 event.SetEventObject(this);
4750
4751 bool processed = HandleWindowEvent(event);
4752
4753 if ( processed && !wxDidCreatePaintDC )
4754 {
4755 // do call MSWDefWindowProc() to validate the update region to avoid
4756 // the problems mentioned above
4757 processed = false;
4758 }
4759
4760 // note that we must generate NC event after the normal one as otherwise
4761 // BeginPaint() will happily overwrite our decorations with the background
4762 // colour
4763 wxNcPaintEvent eventNc(m_windowId);
4764 eventNc.SetEventObject(this);
4765 HandleWindowEvent(eventNc);
4766
4767 // don't keep an HRGN we don't need any longer (GetUpdateRegion() can only
4768 // be called from inside the event handlers called above)
4769 m_updateRegion.Clear();
4770
4771 return processed;
4772 }
4773
4774 // Can be called from an application's OnPaint handler
4775 void wxWindowMSW::OnPaint(wxPaintEvent& event)
4776 {
4777 #ifdef __WXUNIVERSAL__
4778 event.Skip();
4779 #else
4780 HDC hDC = (HDC) wxPaintDCImpl::FindDCInCache((wxWindow*) event.GetEventObject());
4781 if (hDC != 0)
4782 {
4783 MSWDefWindowProc(WM_PAINT, (WPARAM) hDC, 0);
4784 }
4785 #endif
4786 }
4787
4788 bool wxWindowMSW::HandleEraseBkgnd(WXHDC hdc)
4789 {
4790 switch ( GetBackgroundStyle() )
4791 {
4792 case wxBG_STYLE_ERASE:
4793 case wxBG_STYLE_COLOUR:
4794 // we need to generate an erase background event
4795 {
4796 wxDCTemp dc(hdc, GetClientSize());
4797 wxDCTempImpl *impl = (wxDCTempImpl*) dc.GetImpl();
4798
4799 impl->SetHDC(hdc);
4800 impl->SetWindow((wxWindow *)this);
4801
4802 wxEraseEvent event(m_windowId, &dc);
4803 event.SetEventObject(this);
4804 bool rc = HandleWindowEvent(event);
4805
4806 // must be called manually as ~wxDC doesn't do anything for
4807 // wxDCTemp
4808 impl->SelectOldObjects(hdc);
4809
4810 if ( rc )
4811 {
4812 // background erased by the user-defined handler
4813 return true;
4814 }
4815 }
4816 // fall through
4817
4818 case wxBG_STYLE_SYSTEM:
4819 if ( !DoEraseBackground(hdc) )
4820 {
4821 // let the default processing to take place if we didn't erase
4822 // the background ourselves
4823 return false;
4824 }
4825 break;
4826
4827 case wxBG_STYLE_PAINT:
4828 case wxBG_STYLE_TRANSPARENT:
4829 // no need to do anything here at all, background will be entirely
4830 // redrawn in WM_PAINT handler
4831 break;
4832
4833 default:
4834 wxFAIL_MSG( "unknown background style" );
4835 }
4836
4837 return true;
4838 }
4839
4840 #ifdef wxHAS_MSW_BACKGROUND_ERASE_HOOK
4841
4842 bool wxWindowMSW::MSWHasEraseBgHook() const
4843 {
4844 return gs_eraseBgHooks.find(const_cast<wxWindowMSW *>(this))
4845 != gs_eraseBgHooks.end();
4846 }
4847
4848 void wxWindowMSW::MSWSetEraseBgHook(wxWindow *child)
4849 {
4850 if ( child )
4851 {
4852 if ( !gs_eraseBgHooks.insert(
4853 EraseBgHooks::value_type(this, child)).second )
4854 {
4855 wxFAIL_MSG( wxT("Setting erase background hook twice?") );
4856 }
4857 }
4858 else // reset the hook
4859 {
4860 if ( gs_eraseBgHooks.erase(this) != 1 )
4861 {
4862 wxFAIL_MSG( wxT("Resetting erase background which was not set?") );
4863 }
4864 }
4865 }
4866
4867 #endif // wxHAS_MSW_BACKGROUND_ERASE_HOOK
4868
4869 bool wxWindowMSW::DoEraseBackground(WXHDC hDC)
4870 {
4871 HBRUSH hbr = (HBRUSH)MSWGetBgBrush(hDC);
4872 if ( !hbr )
4873 return false;
4874
4875 // erase just the client area of the window, this is important for the
4876 // frames to avoid drawing over the toolbar part of the window (you might
4877 // think using WS_CLIPCHILDREN would prevent this from happening, but it
4878 // clearly doesn't)
4879 RECT rc;
4880 wxCopyRectToRECT(GetClientRect(), rc);
4881 ::FillRect((HDC)hDC, &rc, hbr);
4882
4883 return true;
4884 }
4885
4886 WXHBRUSH
4887 wxWindowMSW::MSWGetBgBrushForChild(WXHDC hDC, wxWindowMSW *child)
4888 {
4889 // Test for the custom background brush first.
4890 WXHBRUSH hbrush = MSWGetCustomBgBrush();
4891 if ( hbrush )
4892 {
4893 // We assume that this is either a stipple or hatched brush and not a
4894 // solid one as otherwise it would have been enough to set the
4895 // background colour and such brushes need to be positioned correctly
4896 // in order to align when different windows are painted, so do it here.
4897 RECT rc;
4898 ::GetWindowRect(GetHwndOf(child), &rc);
4899
4900 ::MapWindowPoints(NULL, GetHwnd(), (POINT *)&rc, 1);
4901
4902 if ( !::SetBrushOrgEx((HDC)hDC, -rc.left, -rc.top, NULL) )
4903 {
4904 wxLogLastError(wxT("SetBrushOrgEx(bg brush)"));
4905 }
4906
4907 return hbrush;
4908 }
4909
4910 // Otherwise see if we have a custom background colour.
4911 if ( m_hasBgCol )
4912 {
4913 wxBrush *
4914 brush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour());
4915
4916 return (WXHBRUSH)GetHbrushOf(*brush);
4917 }
4918
4919 return 0;
4920 }
4921
4922 WXHBRUSH wxWindowMSW::MSWGetBgBrush(WXHDC hDC)
4923 {
4924 for ( wxWindowMSW *win = this; win; win = win->GetParent() )
4925 {
4926 WXHBRUSH hBrush = win->MSWGetBgBrushForChild(hDC, this);
4927 if ( hBrush )
4928 return hBrush;
4929
4930 // don't use the parent background if we're not transparent
4931 if ( !win->HasTransparentBackground() )
4932 break;
4933
4934 // background is not inherited beyond top level windows
4935 if ( win->IsTopLevel() )
4936 break;
4937 }
4938
4939 return 0;
4940 }
4941
4942 bool wxWindowMSW::HandlePrintClient(WXHDC hDC)
4943 {
4944 // we receive this message when DrawThemeParentBackground() is
4945 // called from def window proc of several controls under XP and we
4946 // must draw properly themed background here
4947 //
4948 // note that naively I'd expect filling the client rect with the
4949 // brush returned by MSWGetBgBrush() work -- but for some reason it
4950 // doesn't and we have to call parents MSWPrintChild() which is
4951 // supposed to call DrawThemeBackground() with appropriate params
4952 //
4953 // also note that in this case lParam == PRF_CLIENT but we're
4954 // clearly expected to paint the background and nothing else!
4955
4956 if ( IsTopLevel() || InheritsBackgroundColour() )
4957 return false;
4958
4959 // sometimes we don't want the parent to handle it at all, instead
4960 // return whatever value this window wants
4961 if ( !MSWShouldPropagatePrintChild() )
4962 return MSWPrintChild(hDC, (wxWindow *)this);
4963
4964 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
4965 {
4966 if ( win->MSWPrintChild(hDC, (wxWindow *)this) )
4967 return true;
4968
4969 if ( win->IsTopLevel() || win->InheritsBackgroundColour() )
4970 break;
4971 }
4972
4973 return false;
4974 }
4975
4976 // ---------------------------------------------------------------------------
4977 // moving and resizing
4978 // ---------------------------------------------------------------------------
4979
4980 bool wxWindowMSW::HandleMinimize()
4981 {
4982 wxIconizeEvent event(m_windowId);
4983 event.SetEventObject(this);
4984
4985 return HandleWindowEvent(event);
4986 }
4987
4988 bool wxWindowMSW::HandleMaximize()
4989 {
4990 wxMaximizeEvent event(m_windowId);
4991 event.SetEventObject(this);
4992
4993 return HandleWindowEvent(event);
4994 }
4995
4996 bool wxWindowMSW::HandleMove(int x, int y)
4997 {
4998 wxPoint point(x,y);
4999 wxMoveEvent event(point, m_windowId);
5000 event.SetEventObject(this);
5001
5002 return HandleWindowEvent(event);
5003 }
5004
5005 bool wxWindowMSW::HandleMoving(wxRect& rect)
5006 {
5007 wxMoveEvent event(rect, m_windowId);
5008 event.SetEventObject(this);
5009
5010 bool rc = HandleWindowEvent(event);
5011 if (rc)
5012 rect = event.GetRect();
5013 return rc;
5014 }
5015
5016 bool wxWindowMSW::HandleEnterSizeMove()
5017 {
5018 wxMoveEvent event(wxPoint(0,0), m_windowId);
5019 event.SetEventType(wxEVT_MOVE_START);
5020 event.SetEventObject(this);
5021
5022 return HandleWindowEvent(event);
5023 }
5024
5025 bool wxWindowMSW::HandleExitSizeMove()
5026 {
5027 wxMoveEvent event(wxPoint(0,0), m_windowId);
5028 event.SetEventType(wxEVT_MOVE_END);
5029 event.SetEventObject(this);
5030
5031 return HandleWindowEvent(event);
5032 }
5033
5034 bool wxWindowMSW::HandleSize(int WXUNUSED(w), int WXUNUSED(h), WXUINT wParam)
5035 {
5036 #if wxUSE_DEFERRED_SIZING
5037 // when we resize this window, its children are probably going to be
5038 // repositioned as well, prepare to use DeferWindowPos() for them
5039 int numChildren = 0;
5040 for ( HWND child = ::GetWindow(GetHwndOf(this), GW_CHILD);
5041 child;
5042 child = ::GetWindow(child, GW_HWNDNEXT) )
5043 {
5044 numChildren ++;
5045 }
5046
5047 // Protect against valid m_hDWP being overwritten
5048 bool useDefer = false;
5049
5050 if ( numChildren > 1 )
5051 {
5052 if (!m_hDWP)
5053 {
5054 m_hDWP = (WXHANDLE)::BeginDeferWindowPos(numChildren);
5055 if ( !m_hDWP )
5056 {
5057 wxLogLastError(wxT("BeginDeferWindowPos"));
5058 }
5059 if (m_hDWP)
5060 useDefer = true;
5061 }
5062 }
5063 #endif // wxUSE_DEFERRED_SIZING
5064
5065 // update this window size
5066 bool processed = false;
5067 switch ( wParam )
5068 {
5069 default:
5070 wxFAIL_MSG( wxT("unexpected WM_SIZE parameter") );
5071 // fall through nevertheless
5072
5073 case SIZE_MAXHIDE:
5074 case SIZE_MAXSHOW:
5075 // we're not interested in these messages at all
5076 break;
5077
5078 case SIZE_MINIMIZED:
5079 processed = HandleMinimize();
5080 break;
5081
5082 case SIZE_MAXIMIZED:
5083 /* processed = */ HandleMaximize();
5084 // fall through to send a normal size event as well
5085
5086 case SIZE_RESTORED:
5087 // don't use w and h parameters as they specify the client size
5088 // while according to the docs EVT_SIZE handler is supposed to
5089 // receive the total size
5090 wxSizeEvent event(GetSize(), m_windowId);
5091 event.SetEventObject(this);
5092
5093 processed = HandleWindowEvent(event);
5094 }
5095
5096 #if wxUSE_DEFERRED_SIZING
5097 // and finally change the positions of all child windows at once
5098 if ( useDefer && m_hDWP )
5099 {
5100 // reset m_hDWP to NULL so that child windows don't try to use our
5101 // m_hDWP after we call EndDeferWindowPos() on it (this shouldn't
5102 // happen anyhow normally but who knows what weird flow of control we
5103 // may have depending on what the users EVT_SIZE handler does...)
5104 HDWP hDWP = (HDWP)m_hDWP;
5105 m_hDWP = NULL;
5106
5107 // do put all child controls in place at once
5108 if ( !::EndDeferWindowPos(hDWP) )
5109 {
5110 wxLogLastError(wxT("EndDeferWindowPos"));
5111 }
5112
5113 // Reset our children's pending pos/size values.
5114 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
5115 node;
5116 node = node->GetNext() )
5117 {
5118 wxWindowMSW * const child = node->GetData();
5119 child->MSWEndDeferWindowPos();
5120 }
5121 }
5122 #endif // wxUSE_DEFERRED_SIZING
5123
5124 return processed;
5125 }
5126
5127 bool wxWindowMSW::HandleSizing(wxRect& rect)
5128 {
5129 wxSizeEvent event(rect, m_windowId);
5130 event.SetEventObject(this);
5131
5132 bool rc = HandleWindowEvent(event);
5133 if (rc)
5134 rect = event.GetRect();
5135 return rc;
5136 }
5137
5138 bool wxWindowMSW::HandleGetMinMaxInfo(void *WXUNUSED_IN_WINCE(mmInfo))
5139 {
5140 #ifdef __WXWINCE__
5141 return false;
5142 #else
5143 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
5144
5145 bool rc = false;
5146
5147 int minWidth = GetMinWidth(),
5148 minHeight = GetMinHeight(),
5149 maxWidth = GetMaxWidth(),
5150 maxHeight = GetMaxHeight();
5151
5152 if ( minWidth != wxDefaultCoord )
5153 {
5154 info->ptMinTrackSize.x = minWidth;
5155 rc = true;
5156 }
5157
5158 if ( minHeight != wxDefaultCoord )
5159 {
5160 info->ptMinTrackSize.y = minHeight;
5161 rc = true;
5162 }
5163
5164 if ( maxWidth != wxDefaultCoord )
5165 {
5166 info->ptMaxTrackSize.x = maxWidth;
5167 rc = true;
5168 }
5169
5170 if ( maxHeight != wxDefaultCoord )
5171 {
5172 info->ptMaxTrackSize.y = maxHeight;
5173 rc = true;
5174 }
5175
5176 return rc;
5177 #endif
5178 }
5179
5180 // ---------------------------------------------------------------------------
5181 // command messages
5182 // ---------------------------------------------------------------------------
5183
5184 bool wxWindowMSW::HandleCommand(WXWORD id_, WXWORD cmd, WXHWND control)
5185 {
5186 // sign extend to int from short before comparing with the other int ids
5187 int id = (signed short)id_;
5188
5189 #if wxUSE_MENUS_NATIVE
5190 if ( !cmd && wxCurrentPopupMenu )
5191 {
5192 wxMenu *popupMenu = wxCurrentPopupMenu;
5193 wxCurrentPopupMenu = NULL;
5194
5195 return popupMenu->MSWCommand(cmd, id);
5196 }
5197 #endif // wxUSE_MENUS_NATIVE
5198
5199 wxWindow *win = NULL;
5200
5201 // first try to find it from HWND - this works even with the broken
5202 // programs using the same ids for different controls
5203 if ( control )
5204 {
5205 win = wxFindWinFromHandle(control);
5206 }
5207
5208 // try the id
5209 if ( !win )
5210 {
5211 win = FindItem(id);
5212 }
5213
5214 if ( win )
5215 {
5216 return win->MSWCommand(cmd, id);
5217 }
5218
5219 // the messages sent from the in-place edit control used by the treectrl
5220 // for label editing have id == 0, but they should _not_ be treated as menu
5221 // messages (they are EN_XXX ones, in fact) so don't translate anything
5222 // coming from a control to wxEVT_COMMAND_MENU_SELECTED
5223 if ( !control )
5224 {
5225 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id);
5226 event.SetEventObject(this);
5227 event.SetInt(id);
5228
5229 return HandleWindowEvent(event);
5230 }
5231 else
5232 {
5233 #if wxUSE_SPINCTRL && !defined(__WXUNIVERSAL__)
5234 // the text ctrl which is logically part of wxSpinCtrl sends WM_COMMAND
5235 // notifications to its parent which we want to reflect back to
5236 // wxSpinCtrl
5237 wxSpinCtrl *spin = wxSpinCtrl::GetSpinForTextCtrl(control);
5238 if ( spin && spin->ProcessTextCommand(cmd, id) )
5239 return true;
5240 #endif // wxUSE_SPINCTRL
5241
5242 #if wxUSE_CHOICE && defined(__SMARTPHONE__)
5243 // the listbox ctrl which is logically part of wxChoice sends WM_COMMAND
5244 // notifications to its parent which we want to reflect back to
5245 // wxChoice
5246 wxChoice *choice = wxChoice::GetChoiceForListBox(control);
5247 if ( choice && choice->MSWCommand(cmd, id) )
5248 return true;
5249 #endif
5250 }
5251
5252 return false;
5253 }
5254
5255 // ---------------------------------------------------------------------------
5256 // mouse events
5257 // ---------------------------------------------------------------------------
5258
5259 void wxWindowMSW::InitMouseEvent(wxMouseEvent& event,
5260 int x, int y,
5261 WXUINT flags)
5262 {
5263 // our client coords are not quite the same as Windows ones
5264 wxPoint pt = GetClientAreaOrigin();
5265 event.m_x = x - pt.x;
5266 event.m_y = y - pt.y;
5267
5268 event.m_shiftDown = (flags & MK_SHIFT) != 0;
5269 event.m_controlDown = (flags & MK_CONTROL) != 0;
5270 event.m_leftDown = (flags & MK_LBUTTON) != 0;
5271 event.m_middleDown = (flags & MK_MBUTTON) != 0;
5272 event.m_rightDown = (flags & MK_RBUTTON) != 0;
5273 #ifdef wxHAS_XBUTTON
5274 event.m_aux1Down = (flags & MK_XBUTTON1) != 0;
5275 event.m_aux2Down = (flags & MK_XBUTTON2) != 0;
5276 #endif // wxHAS_XBUTTON
5277 event.m_altDown = ::wxIsAltDown();
5278
5279 #ifndef __WXWINCE__
5280 event.SetTimestamp(::GetMessageTime());
5281 #endif
5282
5283 event.SetEventObject(this);
5284 event.SetId(GetId());
5285
5286 #if wxUSE_MOUSEEVENT_HACK
5287 gs_lastMouseEvent.pos = ClientToScreen(wxPoint(x, y));
5288 gs_lastMouseEvent.type = event.GetEventType();
5289 #endif // wxUSE_MOUSEEVENT_HACK
5290 }
5291
5292 #ifdef __WXWINCE__
5293 // Windows doesn't send the mouse events to the static controls (which are
5294 // transparent in the sense that their WM_NCHITTEST handler returns
5295 // HTTRANSPARENT) at all but we want all controls to receive the mouse events
5296 // and so we manually check if we don't have a child window under mouse and if
5297 // we do, send the event to it instead of the window Windows had sent WM_XXX
5298 // to.
5299 //
5300 // Notice that this is not done for the mouse move events because this could
5301 // (would?) be too slow, but only for clicks which means that the static texts
5302 // still don't get move, enter nor leave events.
5303 static wxWindowMSW *FindWindowForMouseEvent(wxWindowMSW *win, int *x, int *y)
5304 {
5305 wxCHECK_MSG( x && y, win, wxT("NULL pointer in FindWindowForMouseEvent") );
5306
5307 // first try to find a non transparent child: this allows us to send events
5308 // to a static text which is inside a static box, for example
5309 POINT pt = { *x, *y };
5310 HWND hwnd = GetHwndOf(win),
5311 hwndUnderMouse;
5312
5313 #ifdef __WXWINCE__
5314 hwndUnderMouse = ::ChildWindowFromPoint
5315 (
5316 hwnd,
5317 pt
5318 );
5319 #else
5320 hwndUnderMouse = ::ChildWindowFromPointEx
5321 (
5322 hwnd,
5323 pt,
5324 CWP_SKIPINVISIBLE |
5325 CWP_SKIPDISABLED |
5326 CWP_SKIPTRANSPARENT
5327 );
5328 #endif
5329
5330 if ( !hwndUnderMouse || hwndUnderMouse == hwnd )
5331 {
5332 // now try any child window at all
5333 hwndUnderMouse = ::ChildWindowFromPoint(hwnd, pt);
5334 }
5335
5336 // check that we have a child window which is susceptible to receive mouse
5337 // events: for this it must be shown and enabled
5338 if ( hwndUnderMouse &&
5339 hwndUnderMouse != hwnd &&
5340 ::IsWindowVisible(hwndUnderMouse) &&
5341 ::IsWindowEnabled(hwndUnderMouse) )
5342 {
5343 wxWindow *winUnderMouse = wxFindWinFromHandle(hwndUnderMouse);
5344 if ( winUnderMouse )
5345 {
5346 // translate the mouse coords to the other window coords
5347 win->ClientToScreen(x, y);
5348 winUnderMouse->ScreenToClient(x, y);
5349
5350 win = winUnderMouse;
5351 }
5352 }
5353
5354 return win;
5355 }
5356 #endif // __WXWINCE__
5357
5358 bool wxWindowMSW::HandleMouseEvent(WXUINT msg, int x, int y, WXUINT flags)
5359 {
5360 // the mouse events take consecutive IDs from WM_MOUSEFIRST to
5361 // WM_MOUSELAST, so it's enough to subtract WM_MOUSEMOVE == WM_MOUSEFIRST
5362 // from the message id and take the value in the table to get wxWin event
5363 // id
5364 static const wxEventType eventsMouse[] =
5365 {
5366 wxEVT_MOTION,
5367 wxEVT_LEFT_DOWN,
5368 wxEVT_LEFT_UP,
5369 wxEVT_LEFT_DCLICK,
5370 wxEVT_RIGHT_DOWN,
5371 wxEVT_RIGHT_UP,
5372 wxEVT_RIGHT_DCLICK,
5373 wxEVT_MIDDLE_DOWN,
5374 wxEVT_MIDDLE_UP,
5375 wxEVT_MIDDLE_DCLICK,
5376 0, // this one is for wxEVT_MOTION which is not used here
5377 wxEVT_AUX1_DOWN,
5378 wxEVT_AUX1_UP,
5379 wxEVT_AUX1_DCLICK,
5380 wxEVT_AUX2_DOWN,
5381 wxEVT_AUX2_UP,
5382 wxEVT_AUX2_DCLICK
5383 };
5384
5385 #ifdef wxHAS_XBUTTON
5386 // the same messages are used for both auxillary mouse buttons so we need
5387 // to adjust the index manually
5388 switch ( msg )
5389 {
5390 case WM_XBUTTONDOWN:
5391 case WM_XBUTTONUP:
5392 case WM_XBUTTONDBLCLK:
5393 if ( flags & MK_XBUTTON2 )
5394 msg += wxEVT_AUX2_DOWN - wxEVT_AUX1_DOWN;
5395 }
5396 #endif // wxHAS_XBUTTON
5397
5398 wxMouseEvent event(eventsMouse[msg - WM_MOUSEMOVE]);
5399 InitMouseEvent(event, x, y, flags);
5400
5401 return HandleWindowEvent(event);
5402 }
5403
5404 bool wxWindowMSW::HandleMouseMove(int x, int y, WXUINT flags)
5405 {
5406 if ( !m_mouseInWindow )
5407 {
5408 // it would be wrong to assume that just because we get a mouse move
5409 // event that the mouse is inside the window: although this is usually
5410 // true, it is not if we had captured the mouse, so we need to check
5411 // the mouse coordinates here
5412 if ( !HasCapture() || IsMouseInWindow() )
5413 {
5414 // Generate an ENTER event
5415 m_mouseInWindow = true;
5416
5417 #ifdef HAVE_TRACKMOUSEEVENT
5418 typedef BOOL (WINAPI *_TrackMouseEvent_t)(LPTRACKMOUSEEVENT);
5419 #ifdef __WXWINCE__
5420 static const _TrackMouseEvent_t
5421 s_pfn_TrackMouseEvent = _TrackMouseEvent;
5422 #else // !__WXWINCE__
5423 static _TrackMouseEvent_t s_pfn_TrackMouseEvent;
5424 static bool s_initDone = false;
5425 if ( !s_initDone )
5426 {
5427 // see comment in wxApp::GetComCtl32Version() explaining the
5428 // use of wxLoadedDLL
5429 wxLoadedDLL dllComCtl32(wxT("comctl32.dll"));
5430 if ( dllComCtl32.IsLoaded() )
5431 {
5432 s_pfn_TrackMouseEvent = (_TrackMouseEvent_t)
5433 dllComCtl32.RawGetSymbol(wxT("_TrackMouseEvent"));
5434 }
5435
5436 s_initDone = true;
5437 }
5438
5439 if ( s_pfn_TrackMouseEvent )
5440 #endif // __WXWINCE__/!__WXWINCE__
5441 {
5442 WinStruct<TRACKMOUSEEVENT> trackinfo;
5443
5444 trackinfo.dwFlags = TME_LEAVE;
5445 trackinfo.hwndTrack = GetHwnd();
5446
5447 (*s_pfn_TrackMouseEvent)(&trackinfo);
5448 }
5449 #endif // HAVE_TRACKMOUSEEVENT
5450
5451 wxMouseEvent event(wxEVT_ENTER_WINDOW);
5452 InitMouseEvent(event, x, y, flags);
5453
5454 (void)HandleWindowEvent(event);
5455 }
5456 }
5457 #ifdef HAVE_TRACKMOUSEEVENT
5458 else // mouse not in window
5459 {
5460 // Check if we need to send a LEAVE event
5461 // Windows doesn't send WM_MOUSELEAVE if the mouse has been captured so
5462 // send it here if we are using native mouse leave tracking
5463 if ( HasCapture() && !IsMouseInWindow() )
5464 {
5465 GenerateMouseLeave();
5466 }
5467 }
5468 #endif // HAVE_TRACKMOUSEEVENT
5469
5470 #if wxUSE_MOUSEEVENT_HACK
5471 // Windows often generates mouse events even if mouse position hasn't
5472 // changed (http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/66576)
5473 //
5474 // Filter this out as it can result in unexpected behaviour compared to
5475 // other platforms
5476 if ( gs_lastMouseEvent.type == wxEVT_RIGHT_DOWN ||
5477 gs_lastMouseEvent.type == wxEVT_LEFT_DOWN ||
5478 gs_lastMouseEvent.type == wxEVT_MIDDLE_DOWN ||
5479 gs_lastMouseEvent.type == wxEVT_MOTION )
5480 {
5481 if ( ClientToScreen(wxPoint(x, y)) == gs_lastMouseEvent.pos )
5482 {
5483 gs_lastMouseEvent.type = wxEVT_MOTION;
5484
5485 return false;
5486 }
5487 }
5488 #endif // wxUSE_MOUSEEVENT_HACK
5489
5490 return HandleMouseEvent(WM_MOUSEMOVE, x, y, flags);
5491 }
5492
5493
5494 bool wxWindowMSW::HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam)
5495 {
5496 #if wxUSE_MOUSEWHEEL
5497 // notice that WM_MOUSEWHEEL position is in screen coords (as it's
5498 // forwarded up to the parent by DefWindowProc()) and not in the client
5499 // ones as all the other messages, translate them to the client coords for
5500 // consistency
5501 const wxPoint
5502 pt = ScreenToClient(wxPoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
5503 wxMouseEvent event(wxEVT_MOUSEWHEEL);
5504 InitMouseEvent(event, pt.x, pt.y, LOWORD(wParam));
5505 event.m_wheelRotation = (short)HIWORD(wParam);
5506 event.m_wheelDelta = WHEEL_DELTA;
5507
5508 static int s_linesPerRotation = -1;
5509 if ( s_linesPerRotation == -1 )
5510 {
5511 if ( !::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
5512 &s_linesPerRotation, 0))
5513 {
5514 // this is not supposed to happen
5515 wxLogLastError(wxT("SystemParametersInfo(GETWHEELSCROLLLINES)"));
5516
5517 // the default is 3, so use it if SystemParametersInfo() failed
5518 s_linesPerRotation = 3;
5519 }
5520 }
5521
5522 event.m_linesPerAction = s_linesPerRotation;
5523 return HandleWindowEvent(event);
5524
5525 #else // !wxUSE_MOUSEWHEEL
5526 wxUnusedVar(wParam);
5527 wxUnusedVar(lParam);
5528
5529 return false;
5530 #endif // wxUSE_MOUSEWHEEL/!wxUSE_MOUSEWHEEL
5531 }
5532
5533 void wxWindowMSW::GenerateMouseLeave()
5534 {
5535 m_mouseInWindow = false;
5536
5537 int state = 0;
5538 if ( wxIsShiftDown() )
5539 state |= MK_SHIFT;
5540 if ( wxIsCtrlDown() )
5541 state |= MK_CONTROL;
5542
5543 // Only the high-order bit should be tested
5544 if ( GetKeyState( VK_LBUTTON ) & (1<<15) )
5545 state |= MK_LBUTTON;
5546 if ( GetKeyState( VK_MBUTTON ) & (1<<15) )
5547 state |= MK_MBUTTON;
5548 if ( GetKeyState( VK_RBUTTON ) & (1<<15) )
5549 state |= MK_RBUTTON;
5550
5551 POINT pt;
5552 #ifdef __WXWINCE__
5553 if ( !::GetCursorPosWinCE(&pt) )
5554 #else
5555 if ( !::GetCursorPos(&pt) )
5556 #endif
5557 {
5558 wxLogLastError(wxT("GetCursorPos"));
5559 }
5560
5561 // we need to have client coordinates here for symmetry with
5562 // wxEVT_ENTER_WINDOW
5563 RECT rect = wxGetWindowRect(GetHwnd());
5564 pt.x -= rect.left;
5565 pt.y -= rect.top;
5566
5567 wxMouseEvent event(wxEVT_LEAVE_WINDOW);
5568 InitMouseEvent(event, pt.x, pt.y, state);
5569
5570 (void)HandleWindowEvent(event);
5571 }
5572
5573 // ---------------------------------------------------------------------------
5574 // keyboard handling
5575 // ---------------------------------------------------------------------------
5576
5577 namespace
5578 {
5579
5580 // Implementation of InitAnyKeyEvent() which can also be used when there is no
5581 // associated window: this can happen for the wxEVT_CHAR_HOOK events created by
5582 // the global keyboard hook (e.g. the event might have happened in a non-wx
5583 // window).
5584 void
5585 MSWInitAnyKeyEvent(wxKeyEvent& event,
5586 WXWPARAM wParam,
5587 WXLPARAM lParam,
5588 const wxWindowBase *win /* may be NULL */)
5589 {
5590 if ( win )
5591 {
5592 event.SetId(win->GetId());
5593 event.SetEventObject(const_cast<wxWindowBase *>(win));
5594 }
5595 else // No associated window.
5596 {
5597 // Use wxID_ANY for compatibility with the old code even if wxID_NONE
5598 // would arguably make more sense.
5599 event.SetId(wxID_ANY);
5600 }
5601
5602 event.m_shiftDown = wxIsShiftDown();
5603 event.m_controlDown = wxIsCtrlDown();
5604 event.m_altDown = (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN;
5605
5606 event.m_rawCode = (wxUint32) wParam;
5607 event.m_rawFlags = (wxUint32) lParam;
5608 #ifndef __WXWINCE__
5609 event.SetTimestamp(::GetMessageTime());
5610 #endif
5611
5612 // Event coordinates must be in window client coordinates system which
5613 // doesn't make sense if there is no window.
5614 //
5615 // We could use screen coordinates for such events but this would make the
5616 // logic of the event handlers more complicated: you'd need to test for the
5617 // event object and interpret the coordinates differently according to
5618 // whether it's NULL or not so unless somebody really asks for this let's
5619 // just avoid the issue.
5620 if ( win )
5621 {
5622 const wxPoint mousePos = win->ScreenToClient(wxGetMousePosition());
5623 event.m_x = mousePos.x;
5624 event.m_y = mousePos.y;
5625 }
5626 }
5627
5628 } // anonymous namespace
5629
5630 void
5631 wxWindowMSW::InitAnyKeyEvent(wxKeyEvent& event,
5632 WXWPARAM wParam,
5633 WXLPARAM lParam) const
5634 {
5635 MSWInitAnyKeyEvent(event, wParam, lParam, this);
5636 }
5637
5638 wxKeyEvent
5639 wxWindowMSW::CreateKeyEvent(wxEventType evType,
5640 WXWPARAM wParam,
5641 WXLPARAM lParam) const
5642 {
5643 // Catch any attempts to use this with WM_CHAR, it wouldn't work because
5644 // wParam is supposed to be a virtual key and not a character here.
5645 wxASSERT_MSG( evType != wxEVT_CHAR && evType != wxEVT_CHAR_HOOK,
5646 "CreateKeyEvent() can't be used for char events" );
5647
5648 wxKeyEvent event(evType);
5649 InitAnyKeyEvent(event, wParam, lParam);
5650
5651 event.m_keyCode = wxMSWKeyboard::VKToWX
5652 (
5653 wParam,
5654 lParam
5655 #if wxUSE_UNICODE
5656 , &event.m_uniChar
5657 #endif // wxUSE_UNICODE
5658 );
5659
5660 return event;
5661 }
5662
5663 // isASCII is true only when we're called from WM_CHAR handler and not from
5664 // WM_KEYDOWN one
5665 bool wxWindowMSW::HandleChar(WXWPARAM wParam, WXLPARAM lParam)
5666 {
5667 wxKeyEvent event(wxEVT_CHAR);
5668 InitAnyKeyEvent(event, wParam, lParam);
5669
5670 #if wxUSE_UNICODE
5671 // TODO: wParam uses UTF-16 so this is incorrect for characters outside of
5672 // the BMP, we should use WM_UNICHAR to handle them.
5673 event.m_uniChar = wParam;
5674 #endif // wxUSE_UNICODE
5675
5676 // Set non-Unicode key code too for compatibility if possible.
5677 if ( wParam < 0x80 )
5678 {
5679 // It's an ASCII character, no need to translate it.
5680 event.m_keyCode = wParam;
5681 }
5682 else
5683 {
5684 // Check if this key can be represented (as a single character) in the
5685 // current locale.
5686 const wchar_t wc = wParam;
5687 char ch;
5688 if ( wxConvLibc.FromWChar(&ch, 1, &wc, 1) != wxCONV_FAILED )
5689 {
5690 // For compatibility continue to provide the key code in this field
5691 // even though using GetUnicodeKey() is recommended now.
5692 event.m_keyCode = static_cast<unsigned char>(ch);
5693 }
5694 //else: Key can't be represented in the current locale, leave m_keyCode
5695 // as WXK_NONE and use GetUnicodeKey() to access the character.
5696 }
5697
5698 // the alphanumeric keys produced by pressing AltGr+something on European
5699 // keyboards have both Ctrl and Alt modifiers which may confuse the user
5700 // code as, normally, keys with Ctrl and/or Alt don't result in anything
5701 // alphanumeric, so pretend that there are no modifiers at all (the
5702 // KEY_DOWN event would still have the correct modifiers if they're really
5703 // needed)
5704 if ( event.m_controlDown && event.m_altDown &&
5705 (event.m_keyCode >= 32 && event.m_keyCode < 256) )
5706 {
5707 event.m_controlDown =
5708 event.m_altDown = false;
5709 }
5710
5711 return HandleWindowEvent(event);
5712 }
5713
5714 bool wxWindowMSW::HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam)
5715 {
5716 wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_DOWN, wParam, lParam));
5717 return HandleWindowEvent(event);
5718 }
5719
5720 bool wxWindowMSW::HandleKeyUp(WXWPARAM wParam, WXLPARAM lParam)
5721 {
5722 wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_UP, wParam, lParam));
5723 return HandleWindowEvent(event);
5724 }
5725
5726 #if wxUSE_MENUS
5727 int wxWindowMSW::HandleMenuChar(int WXUNUSED_IN_WINCE(chAccel),
5728 WXLPARAM WXUNUSED_IN_WINCE(lParam))
5729 {
5730 // FIXME: implement GetMenuItemCount for WinCE, possibly
5731 // in terms of GetMenuItemInfo
5732 #ifndef __WXWINCE__
5733 const HMENU hmenu = (HMENU)lParam;
5734
5735 MENUITEMINFO mii;
5736 wxZeroMemory(mii);
5737 mii.cbSize = sizeof(MENUITEMINFO);
5738
5739 // we could use MIIM_FTYPE here as we only need to know if the item is
5740 // ownerdrawn or not and not dwTypeData which MIIM_TYPE also returns, but
5741 // MIIM_FTYPE is not supported under Win95
5742 mii.fMask = MIIM_TYPE | MIIM_DATA;
5743
5744 // find if we have this letter in any owner drawn item
5745 const int count = ::GetMenuItemCount(hmenu);
5746 for ( int i = 0; i < count; i++ )
5747 {
5748 // previous loop iteration could modify it, reset it back before
5749 // calling GetMenuItemInfo() to prevent it from overflowing dwTypeData
5750 mii.cch = 0;
5751
5752 if ( ::GetMenuItemInfo(hmenu, i, TRUE, &mii) )
5753 {
5754 if ( mii.fType == MFT_OWNERDRAW )
5755 {
5756 // dwItemData member of the MENUITEMINFO is a
5757 // pointer to the associated wxMenuItem -- see the
5758 // menu creation code
5759 wxMenuItem *item = (wxMenuItem*)mii.dwItemData;
5760
5761 const wxString label(item->GetItemLabel());
5762 const wxChar *p = wxStrchr(label.wx_str(), wxT('&'));
5763 while ( p++ )
5764 {
5765 if ( *p == wxT('&') )
5766 {
5767 // this is not the accel char, find the real one
5768 p = wxStrchr(p + 1, wxT('&'));
5769 }
5770 else // got the accel char
5771 {
5772 // FIXME-UNICODE: this comparison doesn't risk to work
5773 // for non ASCII accelerator characters I'm afraid, but
5774 // what can we do?
5775 if ( (wchar_t)wxToupper(*p) == (wchar_t)chAccel )
5776 {
5777 return i;
5778 }
5779 else
5780 {
5781 // this one doesn't match
5782 break;
5783 }
5784 }
5785 }
5786 }
5787 }
5788 else // failed to get the menu text?
5789 {
5790 // it's not fatal, so don't show error, but still log it
5791 wxLogLastError(wxT("GetMenuItemInfo"));
5792 }
5793 }
5794 #endif
5795 return wxNOT_FOUND;
5796 }
5797
5798 #endif // wxUSE_MENUS
5799
5800 bool wxWindowMSW::HandleClipboardEvent(WXUINT nMsg)
5801 {
5802 const wxEventType type = nMsg == WM_CUT ? wxEVT_COMMAND_TEXT_CUT
5803 : nMsg == WM_COPY ? wxEVT_COMMAND_TEXT_COPY
5804 : /* nMsg == WM_PASTE */ wxEVT_COMMAND_TEXT_PASTE;
5805 wxClipboardTextEvent evt(type, GetId());
5806
5807 evt.SetEventObject(this);
5808
5809 return HandleWindowEvent(evt);
5810 }
5811
5812 // ---------------------------------------------------------------------------
5813 // joystick
5814 // ---------------------------------------------------------------------------
5815
5816 bool wxWindowMSW::HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags)
5817 {
5818 #ifdef JOY_BUTTON1
5819 int change = 0;
5820 if ( flags & JOY_BUTTON1CHG )
5821 change = wxJOY_BUTTON1;
5822 if ( flags & JOY_BUTTON2CHG )
5823 change = wxJOY_BUTTON2;
5824 if ( flags & JOY_BUTTON3CHG )
5825 change = wxJOY_BUTTON3;
5826 if ( flags & JOY_BUTTON4CHG )
5827 change = wxJOY_BUTTON4;
5828
5829 int buttons = 0;
5830 if ( flags & JOY_BUTTON1 )
5831 buttons |= wxJOY_BUTTON1;
5832 if ( flags & JOY_BUTTON2 )
5833 buttons |= wxJOY_BUTTON2;
5834 if ( flags & JOY_BUTTON3 )
5835 buttons |= wxJOY_BUTTON3;
5836 if ( flags & JOY_BUTTON4 )
5837 buttons |= wxJOY_BUTTON4;
5838
5839 // the event ids aren't consecutive so we can't use table based lookup
5840 int joystick;
5841 wxEventType eventType;
5842 switch ( msg )
5843 {
5844 case MM_JOY1MOVE:
5845 joystick = 1;
5846 eventType = wxEVT_JOY_MOVE;
5847 break;
5848
5849 case MM_JOY2MOVE:
5850 joystick = 2;
5851 eventType = wxEVT_JOY_MOVE;
5852 break;
5853
5854 case MM_JOY1ZMOVE:
5855 joystick = 1;
5856 eventType = wxEVT_JOY_ZMOVE;
5857 break;
5858
5859 case MM_JOY2ZMOVE:
5860 joystick = 2;
5861 eventType = wxEVT_JOY_ZMOVE;
5862 break;
5863
5864 case MM_JOY1BUTTONDOWN:
5865 joystick = 1;
5866 eventType = wxEVT_JOY_BUTTON_DOWN;
5867 break;
5868
5869 case MM_JOY2BUTTONDOWN:
5870 joystick = 2;
5871 eventType = wxEVT_JOY_BUTTON_DOWN;
5872 break;
5873
5874 case MM_JOY1BUTTONUP:
5875 joystick = 1;
5876 eventType = wxEVT_JOY_BUTTON_UP;
5877 break;
5878
5879 case MM_JOY2BUTTONUP:
5880 joystick = 2;
5881 eventType = wxEVT_JOY_BUTTON_UP;
5882 break;
5883
5884 default:
5885 wxFAIL_MSG(wxT("no such joystick event"));
5886
5887 return false;
5888 }
5889
5890 wxJoystickEvent event(eventType, buttons, joystick, change);
5891 event.SetPosition(wxPoint(x, y));
5892 event.SetEventObject(this);
5893
5894 return HandleWindowEvent(event);
5895 #else
5896 wxUnusedVar(msg);
5897 wxUnusedVar(x);
5898 wxUnusedVar(y);
5899 wxUnusedVar(flags);
5900 return false;
5901 #endif
5902 }
5903
5904 // ---------------------------------------------------------------------------
5905 // scrolling
5906 // ---------------------------------------------------------------------------
5907
5908 bool wxWindowMSW::MSWOnScroll(int orientation, WXWORD wParam,
5909 WXWORD pos, WXHWND control)
5910 {
5911 if ( control && control != m_hWnd ) // Prevent infinite recursion
5912 {
5913 wxWindow *child = wxFindWinFromHandle(control);
5914 if ( child )
5915 return child->MSWOnScroll(orientation, wParam, pos, control);
5916 }
5917
5918 wxScrollWinEvent event;
5919 event.SetPosition(pos);
5920 event.SetOrientation(orientation);
5921 event.SetEventObject(this);
5922
5923 switch ( wParam )
5924 {
5925 case SB_TOP:
5926 event.SetEventType(wxEVT_SCROLLWIN_TOP);
5927 break;
5928
5929 case SB_BOTTOM:
5930 event.SetEventType(wxEVT_SCROLLWIN_BOTTOM);
5931 break;
5932
5933 case SB_LINEUP:
5934 event.SetEventType(wxEVT_SCROLLWIN_LINEUP);
5935 break;
5936
5937 case SB_LINEDOWN:
5938 event.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
5939 break;
5940
5941 case SB_PAGEUP:
5942 event.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
5943 break;
5944
5945 case SB_PAGEDOWN:
5946 event.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
5947 break;
5948
5949 case SB_THUMBPOSITION:
5950 case SB_THUMBTRACK:
5951 // under Win32, the scrollbar range and position are 32 bit integers,
5952 // but WM_[HV]SCROLL only carry the low 16 bits of them, so we must
5953 // explicitly query the scrollbar for the correct position (this must
5954 // be done only for these two SB_ events as they are the only one
5955 // carrying the scrollbar position)
5956 {
5957 WinStruct<SCROLLINFO> scrollInfo;
5958 scrollInfo.fMask = SIF_TRACKPOS;
5959
5960 if ( !::GetScrollInfo(GetHwnd(),
5961 WXOrientToSB(orientation),
5962 &scrollInfo) )
5963 {
5964 // Not necessarily an error, if there are no scrollbars yet.
5965 // wxLogLastError(wxT("GetScrollInfo"));
5966 }
5967
5968 event.SetPosition(scrollInfo.nTrackPos);
5969 }
5970
5971 event.SetEventType( wParam == SB_THUMBPOSITION
5972 ? wxEVT_SCROLLWIN_THUMBRELEASE
5973 : wxEVT_SCROLLWIN_THUMBTRACK );
5974 break;
5975
5976 default:
5977 return false;
5978 }
5979
5980 return HandleWindowEvent(event);
5981 }
5982
5983 // ----------------------------------------------------------------------------
5984 // custom message handlers
5985 // ----------------------------------------------------------------------------
5986
5987 /* static */ bool
5988 wxWindowMSW::MSWRegisterMessageHandler(int msg, MSWMessageHandler handler)
5989 {
5990 wxCHECK_MSG( gs_messageHandlers.find(msg) == gs_messageHandlers.end(),
5991 false, wxT("registering handler for the same message twice") );
5992
5993 gs_messageHandlers[msg] = handler;
5994 return true;
5995 }
5996
5997 /* static */ void
5998 wxWindowMSW::MSWUnregisterMessageHandler(int msg, MSWMessageHandler handler)
5999 {
6000 const MSWMessageHandlers::iterator i = gs_messageHandlers.find(msg);
6001 wxCHECK_RET( i != gs_messageHandlers.end() && i->second == handler,
6002 wxT("unregistering non-registered handler?") );
6003
6004 gs_messageHandlers.erase(i);
6005 }
6006
6007 // ===========================================================================
6008 // global functions
6009 // ===========================================================================
6010
6011 void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont& the_font)
6012 {
6013 TEXTMETRIC tm;
6014 HDC dc = ::GetDC((HWND) wnd);
6015 HFONT was = 0;
6016
6017 // the_font.UseResource();
6018 // the_font.RealizeResource();
6019 HFONT fnt = (HFONT)the_font.GetResourceHandle(); // const_cast
6020 if ( fnt )
6021 was = (HFONT) SelectObject(dc,fnt);
6022
6023 GetTextMetrics(dc, &tm);
6024 if ( fnt && was )
6025 {
6026 SelectObject(dc,was);
6027 }
6028 ReleaseDC((HWND)wnd, dc);
6029
6030 if ( x )
6031 *x = tm.tmAveCharWidth;
6032 if ( y )
6033 *y = tm.tmHeight + tm.tmExternalLeading;
6034
6035 // the_font.ReleaseResource();
6036 }
6037
6038 // ----------------------------------------------------------------------------
6039 // keyboard codes
6040 // ----------------------------------------------------------------------------
6041
6042 namespace wxMSWKeyboard
6043 {
6044
6045 namespace
6046 {
6047
6048 // use the "extended" bit of lParam to distinguish extended keys from normal
6049 // keys as the same virtual key code is sent for both by Windows
6050 inline
6051 int ChooseNormalOrExtended(int lParam, int keyNormal, int keyExtended)
6052 {
6053 // except that if lParam is 0, it means we don't have real lParam from
6054 // WM_KEYDOWN but are just translating just a VK constant (e.g. done from
6055 // msw/treectrl.cpp when processing TVN_KEYDOWN) -- then assume this is a
6056 // non-numpad (hence extended) key as this is a more common case
6057 return !lParam || (HIWORD(lParam) & KF_EXTENDED) ? keyExtended : keyNormal;
6058 }
6059
6060 // this array contains the Windows virtual key codes which map one to one to
6061 // WXK_xxx constants and is used in wxMSWKeyboard::VKToWX/WXToVK() below
6062 //
6063 // note that keys having a normal and numpad version (e.g. WXK_HOME and
6064 // WXK_NUMPAD_HOME) are not included in this table as the mapping is not 1-to-1
6065 const struct wxKeyMapping
6066 {
6067 int vk;
6068 wxKeyCode wxk;
6069 } gs_specialKeys[] =
6070 {
6071 { VK_CANCEL, WXK_CANCEL },
6072 { VK_BACK, WXK_BACK },
6073 { VK_TAB, WXK_TAB },
6074 { VK_CLEAR, WXK_CLEAR },
6075 { VK_SHIFT, WXK_SHIFT },
6076 { VK_CONTROL, WXK_CONTROL },
6077 { VK_MENU , WXK_ALT },
6078 { VK_PAUSE, WXK_PAUSE },
6079 { VK_CAPITAL, WXK_CAPITAL },
6080 { VK_SPACE, WXK_SPACE },
6081 { VK_ESCAPE, WXK_ESCAPE },
6082 { VK_SELECT, WXK_SELECT },
6083 { VK_PRINT, WXK_PRINT },
6084 { VK_EXECUTE, WXK_EXECUTE },
6085 { VK_SNAPSHOT, WXK_SNAPSHOT },
6086 { VK_HELP, WXK_HELP },
6087
6088 { VK_NUMPAD0, WXK_NUMPAD0 },
6089 { VK_NUMPAD1, WXK_NUMPAD1 },
6090 { VK_NUMPAD2, WXK_NUMPAD2 },
6091 { VK_NUMPAD3, WXK_NUMPAD3 },
6092 { VK_NUMPAD4, WXK_NUMPAD4 },
6093 { VK_NUMPAD5, WXK_NUMPAD5 },
6094 { VK_NUMPAD6, WXK_NUMPAD6 },
6095 { VK_NUMPAD7, WXK_NUMPAD7 },
6096 { VK_NUMPAD8, WXK_NUMPAD8 },
6097 { VK_NUMPAD9, WXK_NUMPAD9 },
6098 { VK_MULTIPLY, WXK_NUMPAD_MULTIPLY },
6099 { VK_ADD, WXK_NUMPAD_ADD },
6100 { VK_SUBTRACT, WXK_NUMPAD_SUBTRACT },
6101 { VK_DECIMAL, WXK_NUMPAD_DECIMAL },
6102 { VK_DIVIDE, WXK_NUMPAD_DIVIDE },
6103
6104 { VK_F1, WXK_F1 },
6105 { VK_F2, WXK_F2 },
6106 { VK_F3, WXK_F3 },
6107 { VK_F4, WXK_F4 },
6108 { VK_F5, WXK_F5 },
6109 { VK_F6, WXK_F6 },
6110 { VK_F7, WXK_F7 },
6111 { VK_F8, WXK_F8 },
6112 { VK_F9, WXK_F9 },
6113 { VK_F10, WXK_F10 },
6114 { VK_F11, WXK_F11 },
6115 { VK_F12, WXK_F12 },
6116 { VK_F13, WXK_F13 },
6117 { VK_F14, WXK_F14 },
6118 { VK_F15, WXK_F15 },
6119 { VK_F16, WXK_F16 },
6120 { VK_F17, WXK_F17 },
6121 { VK_F18, WXK_F18 },
6122 { VK_F19, WXK_F19 },
6123 { VK_F20, WXK_F20 },
6124 { VK_F21, WXK_F21 },
6125 { VK_F22, WXK_F22 },
6126 { VK_F23, WXK_F23 },
6127 { VK_F24, WXK_F24 },
6128
6129 { VK_NUMLOCK, WXK_NUMLOCK },
6130 { VK_SCROLL, WXK_SCROLL },
6131
6132 #ifdef VK_APPS
6133 { VK_LWIN, WXK_WINDOWS_LEFT },
6134 { VK_RWIN, WXK_WINDOWS_RIGHT },
6135 { VK_APPS, WXK_WINDOWS_MENU },
6136 #endif // VK_APPS defined
6137 };
6138
6139 } // anonymous namespace
6140
6141 int VKToWX(WXWORD vk, WXLPARAM lParam, wchar_t *uc)
6142 {
6143 int wxk;
6144
6145 // check the table first
6146 for ( size_t n = 0; n < WXSIZEOF(gs_specialKeys); n++ )
6147 {
6148 if ( gs_specialKeys[n].vk == vk )
6149 {
6150 wxk = gs_specialKeys[n].wxk;
6151 if ( wxk < WXK_START )
6152 {
6153 // Unicode code for this key is the same as its ASCII code.
6154 if ( uc )
6155 *uc = wxk;
6156 }
6157
6158 return wxk;
6159 }
6160 }
6161
6162 // keys requiring special handling
6163 switch ( vk )
6164 {
6165 case VK_OEM_1:
6166 case VK_OEM_PLUS:
6167 case VK_OEM_COMMA:
6168 case VK_OEM_MINUS:
6169 case VK_OEM_PERIOD:
6170 case VK_OEM_2:
6171 case VK_OEM_3:
6172 case VK_OEM_4:
6173 case VK_OEM_5:
6174 case VK_OEM_6:
6175 case VK_OEM_7:
6176 // MapVirtualKey() returns 0 if it fails to convert the virtual
6177 // key which nicely corresponds to our WXK_NONE.
6178 wxk = ::MapVirtualKey(vk, MAPVK_VK_TO_CHAR);
6179
6180 if ( HIWORD(wxk) & 0x8000 )
6181 {
6182 // It's a dead key and we don't return anything at all for them
6183 // as we simply don't have any way to indicate the difference
6184 // between e.g. a normal "'" and "'" as a dead key -- and
6185 // generating the same events for them just doesn't seem like a
6186 // good idea.
6187 wxk = WXK_NONE;
6188 }
6189
6190 // In any case return this as a Unicode character value.
6191 if ( uc )
6192 *uc = wxk;
6193
6194 // For compatibility with the old non-Unicode code we continue
6195 // returning key codes for Latin-1 characters directly
6196 // (normally it would really only make sense to do it for the
6197 // ASCII characters, not Latin-1 ones).
6198 if ( wxk > 255 )
6199 {
6200 // But for anything beyond this we can only return the key
6201 // value as a real Unicode character, not a wxKeyCode
6202 // because this enum values clash with Unicode characters
6203 // (e.g. WXK_LBUTTON also happens to be U+012C a.k.a.
6204 // "LATIN CAPITAL LETTER I WITH BREVE").
6205 wxk = WXK_NONE;
6206 }
6207 break;
6208
6209 // handle extended keys
6210 case VK_PRIOR:
6211 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_PAGEUP, WXK_PAGEUP);
6212 break;
6213
6214 case VK_NEXT:
6215 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_PAGEDOWN, WXK_PAGEDOWN);
6216 break;
6217
6218 case VK_END:
6219 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_END, WXK_END);
6220 break;
6221
6222 case VK_HOME:
6223 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_HOME, WXK_HOME);
6224 break;
6225
6226 case VK_LEFT:
6227 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_LEFT, WXK_LEFT);
6228 break;
6229
6230 case VK_UP:
6231 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_UP, WXK_UP);
6232 break;
6233
6234 case VK_RIGHT:
6235 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_RIGHT, WXK_RIGHT);
6236 break;
6237
6238 case VK_DOWN:
6239 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_DOWN, WXK_DOWN);
6240 break;
6241
6242 case VK_INSERT:
6243 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_INSERT, WXK_INSERT);
6244 break;
6245
6246 case VK_DELETE:
6247 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_DELETE, WXK_DELETE);
6248 break;
6249
6250 case VK_RETURN:
6251 // don't use ChooseNormalOrExtended() here as the keys are reversed
6252 // here: numpad enter is the extended one
6253 wxk = HIWORD(lParam) & KF_EXTENDED ? WXK_NUMPAD_ENTER : WXK_RETURN;
6254 break;
6255
6256 default:
6257 if ( (vk >= '0' && vk <= '9') || (vk >= 'A' && vk <= 'Z') )
6258 {
6259 // A simple alphanumeric key and the values of them coincide in
6260 // Windows and wx for both ASCII and Unicode codes.
6261 wxk = vk;
6262 }
6263 else // Something we simply don't know about at all.
6264 {
6265 wxk = WXK_NONE;
6266 }
6267
6268 if ( uc )
6269 *uc = vk;
6270 }
6271
6272 return wxk;
6273 }
6274
6275 WXWORD WXToVK(int wxk, bool *isExtended)
6276 {
6277 // check the table first
6278 for ( size_t n = 0; n < WXSIZEOF(gs_specialKeys); n++ )
6279 {
6280 if ( gs_specialKeys[n].wxk == wxk )
6281 {
6282 // All extended keys (i.e. non-numpad versions of the keys that
6283 // exist both in the numpad and outside of it) are dealt with
6284 // below.
6285 if ( isExtended )
6286 *isExtended = false;
6287
6288 return gs_specialKeys[n].vk;
6289 }
6290 }
6291
6292 // and then check for special keys not included in the table
6293 bool extended = false;
6294 WXWORD vk;
6295 switch ( wxk )
6296 {
6297 case WXK_PAGEUP:
6298 extended = true;
6299 case WXK_NUMPAD_PAGEUP:
6300 vk = VK_PRIOR;
6301 break;
6302
6303 case WXK_PAGEDOWN:
6304 extended = true;
6305 case WXK_NUMPAD_PAGEDOWN:
6306 vk = VK_NEXT;
6307 break;
6308
6309 case WXK_END:
6310 extended = true;
6311 case WXK_NUMPAD_END:
6312 vk = VK_END;
6313 break;
6314
6315 case WXK_HOME:
6316 extended = true;
6317 case WXK_NUMPAD_HOME:
6318 vk = VK_HOME;
6319 break;
6320
6321 case WXK_LEFT:
6322 extended = true;
6323 case WXK_NUMPAD_LEFT:
6324 vk = VK_LEFT;
6325 break;
6326
6327 case WXK_UP:
6328 extended = true;
6329 case WXK_NUMPAD_UP:
6330 vk = VK_UP;
6331 break;
6332
6333 case WXK_RIGHT:
6334 extended = true;
6335 case WXK_NUMPAD_RIGHT:
6336 vk = VK_RIGHT;
6337 break;
6338
6339 case WXK_DOWN:
6340 extended = true;
6341 case WXK_NUMPAD_DOWN:
6342 vk = VK_DOWN;
6343 break;
6344
6345 case WXK_INSERT:
6346 extended = true;
6347 case WXK_NUMPAD_INSERT:
6348 vk = VK_INSERT;
6349 break;
6350
6351 case WXK_DELETE:
6352 extended = true;
6353 case WXK_NUMPAD_DELETE:
6354 vk = VK_DELETE;
6355 break;
6356
6357 default:
6358 // no VkKeyScan() under CE unfortunately, we need to test how does
6359 // it handle OEM keys
6360 #ifndef __WXWINCE__
6361 // check to see if its one of the OEM key codes.
6362 BYTE vks = LOBYTE(VkKeyScan(wxk));
6363 if ( vks != 0xff )
6364 {
6365 vk = vks;
6366 }
6367 else
6368 #endif // !__WXWINCE__
6369 {
6370 vk = (WXWORD)wxk;
6371 }
6372 }
6373
6374 if ( isExtended )
6375 *isExtended = extended;
6376
6377 return vk;
6378 }
6379
6380 } // namespace wxMSWKeyboard
6381
6382 // small helper for wxGetKeyState() and wxGetMouseState()
6383 static inline bool wxIsKeyDown(WXWORD vk)
6384 {
6385 // SM_SWAPBUTTON is not available under CE, so don't swap buttons there
6386 #ifdef SM_SWAPBUTTON
6387 if ( vk == VK_LBUTTON || vk == VK_RBUTTON )
6388 {
6389 if ( ::GetSystemMetrics(SM_SWAPBUTTON) )
6390 {
6391 if ( vk == VK_LBUTTON )
6392 vk = VK_RBUTTON;
6393 else // vk == VK_RBUTTON
6394 vk = VK_LBUTTON;
6395 }
6396 }
6397 #endif // SM_SWAPBUTTON
6398
6399 // the low order bit indicates whether the key was pressed since the last
6400 // call and the high order one indicates whether it is down right now and
6401 // we only want that one
6402 return (GetAsyncKeyState(vk) & (1<<15)) != 0;
6403 }
6404
6405 bool wxGetKeyState(wxKeyCode key)
6406 {
6407 // although this does work under Windows, it is not supported under other
6408 // platforms so don't allow it, you must use wxGetMouseState() instead
6409 wxASSERT_MSG( key != VK_LBUTTON &&
6410 key != VK_RBUTTON &&
6411 key != VK_MBUTTON,
6412 wxT("can't use wxGetKeyState() for mouse buttons") );
6413
6414 const WXWORD vk = wxMSWKeyboard::WXToVK(key);
6415
6416 // if the requested key is a LED key, return true if the led is pressed
6417 if ( key == WXK_NUMLOCK || key == WXK_CAPITAL || key == WXK_SCROLL )
6418 {
6419 // low order bit means LED is highlighted and high order one means the
6420 // key is down; for compatibility with the other ports return true if
6421 // either one is set
6422 return GetKeyState(vk) != 0;
6423
6424 }
6425 else // normal key
6426 {
6427 return wxIsKeyDown(vk);
6428 }
6429 }
6430
6431
6432 wxMouseState wxGetMouseState()
6433 {
6434 wxMouseState ms;
6435 POINT pt;
6436 GetCursorPos( &pt );
6437
6438 ms.SetX(pt.x);
6439 ms.SetY(pt.y);
6440 ms.SetLeftDown(wxIsKeyDown(VK_LBUTTON));
6441 ms.SetMiddleDown(wxIsKeyDown(VK_MBUTTON));
6442 ms.SetRightDown(wxIsKeyDown(VK_RBUTTON));
6443 #ifdef wxHAS_XBUTTON
6444 ms.SetAux1Down(wxIsKeyDown(VK_XBUTTON1));
6445 ms.SetAux2Down(wxIsKeyDown(VK_XBUTTON2));
6446 #endif // wxHAS_XBUTTON
6447
6448 ms.SetControlDown(wxIsCtrlDown ());
6449 ms.SetShiftDown (wxIsShiftDown());
6450 ms.SetAltDown (wxIsAltDown ());
6451 // ms.SetMetaDown();
6452
6453 return ms;
6454 }
6455
6456
6457 wxWindow *wxGetActiveWindow()
6458 {
6459 HWND hWnd = GetActiveWindow();
6460 if ( hWnd != 0 )
6461 {
6462 return wxFindWinFromHandle(hWnd);
6463 }
6464 return NULL;
6465 }
6466
6467 extern wxWindow *wxGetWindowFromHWND(WXHWND hWnd)
6468 {
6469 HWND hwnd = (HWND)hWnd;
6470
6471 // For a radiobutton, we get the radiobox from GWL_USERDATA (which is set
6472 // by code in msw/radiobox.cpp), for all the others we just search up the
6473 // window hierarchy
6474 wxWindow *win = NULL;
6475 if ( hwnd )
6476 {
6477 win = wxFindWinFromHandle(hwnd);
6478 if ( !win )
6479 {
6480 #if wxUSE_RADIOBOX && !defined(__WXUNIVERSAL__)
6481 // native radiobuttons return DLGC_RADIOBUTTON here and for any
6482 // wxWindow class which overrides WM_GETDLGCODE processing to
6483 // do it as well, win would be already non NULL
6484 if ( ::SendMessage(hwnd, WM_GETDLGCODE, 0, 0) & DLGC_RADIOBUTTON )
6485 {
6486 win = wxRadioBox::GetFromRadioButtonHWND(hwnd);
6487 }
6488 //else: it's a wxRadioButton, not a radiobutton from wxRadioBox
6489 #endif // wxUSE_RADIOBOX
6490
6491 // spin control text buddy window should be mapped to spin ctrl
6492 // itself so try it too
6493 #if wxUSE_SPINCTRL && !defined(__WXUNIVERSAL__)
6494 if ( !win )
6495 {
6496 win = wxSpinCtrl::GetSpinForTextCtrl((WXHWND)hwnd);
6497 }
6498 #endif // wxUSE_SPINCTRL
6499 }
6500 }
6501
6502 while ( hwnd && !win )
6503 {
6504 // this is a really ugly hack needed to avoid mistakenly returning the
6505 // parent frame wxWindow for the find/replace modeless dialog HWND -
6506 // this, in turn, is needed to call IsDialogMessage() from
6507 // wxApp::ProcessMessage() as for this we must return NULL from here
6508 //
6509 // FIXME: this is clearly not the best way to do it but I think we'll
6510 // need to change HWND <-> wxWindow code more heavily than I can
6511 // do it now to fix it
6512 #ifndef __WXMICROWIN__
6513 if ( ::GetWindow(hwnd, GW_OWNER) )
6514 {
6515 // it's a dialog box, don't go upwards
6516 break;
6517 }
6518 #endif
6519
6520 hwnd = ::GetParent(hwnd);
6521 win = wxFindWinFromHandle(hwnd);
6522 }
6523
6524 return win;
6525 }
6526
6527 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
6528
6529 // Windows keyboard hook. Allows interception of e.g. F1, ESCAPE
6530 // in active frames and dialogs, regardless of where the focus is.
6531 static HHOOK wxTheKeyboardHook = 0;
6532
6533 int APIENTRY
6534 wxKeyboardHook(int nCode, WORD wParam, DWORD lParam)
6535 {
6536 DWORD hiWord = HIWORD(lParam);
6537 if ( nCode != HC_NOREMOVE && ((hiWord & KF_UP) == 0) )
6538 {
6539 wchar_t uc;
6540 int id = wxMSWKeyboard::VKToWX(wParam, lParam, &uc);
6541
6542 // Don't intercept keyboard entry (notably Escape) if a modal window
6543 // (not managed by wx, e.g. IME one) is currently opened as more often
6544 // than not it needs all the keys for itself.
6545 //
6546 // Also don't catch it if a window currently captures the mouse as
6547 // Escape is normally used to release the mouse capture and if you
6548 // really need to catch all the keys in the window that has mouse
6549 // capture it can be easily done in its own EVT_CHAR handler as it is
6550 // certain to have focus while it has the capture.
6551 if ( !gs_modalEntryWindowCount && !::GetCapture() )
6552 {
6553 if ( id != WXK_NONE
6554 #if wxUSE_UNICODE
6555 || static_cast<int>(uc) != WXK_NONE
6556 #endif // wxUSE_UNICODE
6557 )
6558 {
6559 const wxWindow * const win = wxGetActiveWindow();
6560
6561 wxKeyEvent event(wxEVT_CHAR_HOOK);
6562 MSWInitAnyKeyEvent(event, wParam, lParam, win);
6563
6564 event.m_keyCode = id;
6565 #if wxUSE_UNICODE
6566 event.m_uniChar = uc;
6567 #endif // wxUSE_UNICODE
6568
6569 wxEvtHandler * const handler = win ? win->GetEventHandler()
6570 : wxTheApp;
6571
6572 if ( handler && handler->ProcessEvent(event) )
6573 {
6574 // processed
6575 return 1;
6576 }
6577 }
6578 }
6579 }
6580
6581 return (int)CallNextHookEx(wxTheKeyboardHook, nCode, wParam, lParam);
6582 }
6583
6584 void wxSetKeyboardHook(bool doIt)
6585 {
6586 if ( doIt )
6587 {
6588 wxTheKeyboardHook = ::SetWindowsHookEx
6589 (
6590 WH_KEYBOARD,
6591 (HOOKPROC)wxKeyboardHook,
6592 NULL, // must be NULL for process hook
6593 ::GetCurrentThreadId()
6594 );
6595 if ( !wxTheKeyboardHook )
6596 {
6597 wxLogLastError(wxT("SetWindowsHookEx(wxKeyboardHook)"));
6598 }
6599 }
6600 else // uninstall
6601 {
6602 if ( wxTheKeyboardHook )
6603 ::UnhookWindowsHookEx(wxTheKeyboardHook);
6604 }
6605 }
6606
6607 #endif // !__WXMICROWIN__
6608
6609 #if wxDEBUG_LEVEL >= 2
6610 const wxChar *wxGetMessageName(int message)
6611 {
6612 switch ( message )
6613 {
6614 case 0x0000: return wxT("WM_NULL");
6615 case 0x0001: return wxT("WM_CREATE");
6616 case 0x0002: return wxT("WM_DESTROY");
6617 case 0x0003: return wxT("WM_MOVE");
6618 case 0x0005: return wxT("WM_SIZE");
6619 case 0x0006: return wxT("WM_ACTIVATE");
6620 case 0x0007: return wxT("WM_SETFOCUS");
6621 case 0x0008: return wxT("WM_KILLFOCUS");
6622 case 0x000A: return wxT("WM_ENABLE");
6623 case 0x000B: return wxT("WM_SETREDRAW");
6624 case 0x000C: return wxT("WM_SETTEXT");
6625 case 0x000D: return wxT("WM_GETTEXT");
6626 case 0x000E: return wxT("WM_GETTEXTLENGTH");
6627 case 0x000F: return wxT("WM_PAINT");
6628 case 0x0010: return wxT("WM_CLOSE");
6629 case 0x0011: return wxT("WM_QUERYENDSESSION");
6630 case 0x0012: return wxT("WM_QUIT");
6631 case 0x0013: return wxT("WM_QUERYOPEN");
6632 case 0x0014: return wxT("WM_ERASEBKGND");
6633 case 0x0015: return wxT("WM_SYSCOLORCHANGE");
6634 case 0x0016: return wxT("WM_ENDSESSION");
6635 case 0x0017: return wxT("WM_SYSTEMERROR");
6636 case 0x0018: return wxT("WM_SHOWWINDOW");
6637 case 0x0019: return wxT("WM_CTLCOLOR");
6638 case 0x001A: return wxT("WM_WININICHANGE");
6639 case 0x001B: return wxT("WM_DEVMODECHANGE");
6640 case 0x001C: return wxT("WM_ACTIVATEAPP");
6641 case 0x001D: return wxT("WM_FONTCHANGE");
6642 case 0x001E: return wxT("WM_TIMECHANGE");
6643 case 0x001F: return wxT("WM_CANCELMODE");
6644 case 0x0020: return wxT("WM_SETCURSOR");
6645 case 0x0021: return wxT("WM_MOUSEACTIVATE");
6646 case 0x0022: return wxT("WM_CHILDACTIVATE");
6647 case 0x0023: return wxT("WM_QUEUESYNC");
6648 case 0x0024: return wxT("WM_GETMINMAXINFO");
6649 case 0x0026: return wxT("WM_PAINTICON");
6650 case 0x0027: return wxT("WM_ICONERASEBKGND");
6651 case 0x0028: return wxT("WM_NEXTDLGCTL");
6652 case 0x002A: return wxT("WM_SPOOLERSTATUS");
6653 case 0x002B: return wxT("WM_DRAWITEM");
6654 case 0x002C: return wxT("WM_MEASUREITEM");
6655 case 0x002D: return wxT("WM_DELETEITEM");
6656 case 0x002E: return wxT("WM_VKEYTOITEM");
6657 case 0x002F: return wxT("WM_CHARTOITEM");
6658 case 0x0030: return wxT("WM_SETFONT");
6659 case 0x0031: return wxT("WM_GETFONT");
6660 case 0x0037: return wxT("WM_QUERYDRAGICON");
6661 case 0x0039: return wxT("WM_COMPAREITEM");
6662 case 0x0041: return wxT("WM_COMPACTING");
6663 case 0x0044: return wxT("WM_COMMNOTIFY");
6664 case 0x0046: return wxT("WM_WINDOWPOSCHANGING");
6665 case 0x0047: return wxT("WM_WINDOWPOSCHANGED");
6666 case 0x0048: return wxT("WM_POWER");
6667
6668 case 0x004A: return wxT("WM_COPYDATA");
6669 case 0x004B: return wxT("WM_CANCELJOURNAL");
6670 case 0x004E: return wxT("WM_NOTIFY");
6671 case 0x0050: return wxT("WM_INPUTLANGCHANGEREQUEST");
6672 case 0x0051: return wxT("WM_INPUTLANGCHANGE");
6673 case 0x0052: return wxT("WM_TCARD");
6674 case 0x0053: return wxT("WM_HELP");
6675 case 0x0054: return wxT("WM_USERCHANGED");
6676 case 0x0055: return wxT("WM_NOTIFYFORMAT");
6677 case 0x007B: return wxT("WM_CONTEXTMENU");
6678 case 0x007C: return wxT("WM_STYLECHANGING");
6679 case 0x007D: return wxT("WM_STYLECHANGED");
6680 case 0x007E: return wxT("WM_DISPLAYCHANGE");
6681 case 0x007F: return wxT("WM_GETICON");
6682 case 0x0080: return wxT("WM_SETICON");
6683
6684 case 0x0081: return wxT("WM_NCCREATE");
6685 case 0x0082: return wxT("WM_NCDESTROY");
6686 case 0x0083: return wxT("WM_NCCALCSIZE");
6687 case 0x0084: return wxT("WM_NCHITTEST");
6688 case 0x0085: return wxT("WM_NCPAINT");
6689 case 0x0086: return wxT("WM_NCACTIVATE");
6690 case 0x0087: return wxT("WM_GETDLGCODE");
6691 case 0x00A0: return wxT("WM_NCMOUSEMOVE");
6692 case 0x00A1: return wxT("WM_NCLBUTTONDOWN");
6693 case 0x00A2: return wxT("WM_NCLBUTTONUP");
6694 case 0x00A3: return wxT("WM_NCLBUTTONDBLCLK");
6695 case 0x00A4: return wxT("WM_NCRBUTTONDOWN");
6696 case 0x00A5: return wxT("WM_NCRBUTTONUP");
6697 case 0x00A6: return wxT("WM_NCRBUTTONDBLCLK");
6698 case 0x00A7: return wxT("WM_NCMBUTTONDOWN");
6699 case 0x00A8: return wxT("WM_NCMBUTTONUP");
6700 case 0x00A9: return wxT("WM_NCMBUTTONDBLCLK");
6701
6702 case 0x00B0: return wxT("EM_GETSEL");
6703 case 0x00B1: return wxT("EM_SETSEL");
6704 case 0x00B2: return wxT("EM_GETRECT");
6705 case 0x00B3: return wxT("EM_SETRECT");
6706 case 0x00B4: return wxT("EM_SETRECTNP");
6707 case 0x00B5: return wxT("EM_SCROLL");
6708 case 0x00B6: return wxT("EM_LINESCROLL");
6709 case 0x00B7: return wxT("EM_SCROLLCARET");
6710 case 0x00B8: return wxT("EM_GETMODIFY");
6711 case 0x00B9: return wxT("EM_SETMODIFY");
6712 case 0x00BA: return wxT("EM_GETLINECOUNT");
6713 case 0x00BB: return wxT("EM_LINEINDEX");
6714 case 0x00BC: return wxT("EM_SETHANDLE");
6715 case 0x00BD: return wxT("EM_GETHANDLE");
6716 case 0x00BE: return wxT("EM_GETTHUMB");
6717 case 0x00C1: return wxT("EM_LINELENGTH");
6718 case 0x00C2: return wxT("EM_REPLACESEL");
6719 case 0x00C4: return wxT("EM_GETLINE");
6720 case 0x00C5: return wxT("EM_LIMITTEXT/EM_SETLIMITTEXT"); /* ;win40 Name change */
6721 case 0x00C6: return wxT("EM_CANUNDO");
6722 case 0x00C7: return wxT("EM_UNDO");
6723 case 0x00C8: return wxT("EM_FMTLINES");
6724 case 0x00C9: return wxT("EM_LINEFROMCHAR");
6725 case 0x00CB: return wxT("EM_SETTABSTOPS");
6726 case 0x00CC: return wxT("EM_SETPASSWORDCHAR");
6727 case 0x00CD: return wxT("EM_EMPTYUNDOBUFFER");
6728 case 0x00CE: return wxT("EM_GETFIRSTVISIBLELINE");
6729 case 0x00CF: return wxT("EM_SETREADONLY");
6730 case 0x00D0: return wxT("EM_SETWORDBREAKPROC");
6731 case 0x00D1: return wxT("EM_GETWORDBREAKPROC");
6732 case 0x00D2: return wxT("EM_GETPASSWORDCHAR");
6733 case 0x00D3: return wxT("EM_SETMARGINS");
6734 case 0x00D4: return wxT("EM_GETMARGINS");
6735 case 0x00D5: return wxT("EM_GETLIMITTEXT");
6736 case 0x00D6: return wxT("EM_POSFROMCHAR");
6737 case 0x00D7: return wxT("EM_CHARFROMPOS");
6738 case 0x00D8: return wxT("EM_SETIMESTATUS");
6739 case 0x00D9: return wxT("EM_GETIMESTATUS");
6740
6741 case 0x0100: return wxT("WM_KEYDOWN");
6742 case 0x0101: return wxT("WM_KEYUP");
6743 case 0x0102: return wxT("WM_CHAR");
6744 case 0x0103: return wxT("WM_DEADCHAR");
6745 case 0x0104: return wxT("WM_SYSKEYDOWN");
6746 case 0x0105: return wxT("WM_SYSKEYUP");
6747 case 0x0106: return wxT("WM_SYSCHAR");
6748 case 0x0107: return wxT("WM_SYSDEADCHAR");
6749 case 0x0108: return wxT("WM_KEYLAST");
6750
6751 case 0x010D: return wxT("WM_IME_STARTCOMPOSITION");
6752 case 0x010E: return wxT("WM_IME_ENDCOMPOSITION");
6753 case 0x010F: return wxT("WM_IME_COMPOSITION");
6754
6755 case 0x0110: return wxT("WM_INITDIALOG");
6756 case 0x0111: return wxT("WM_COMMAND");
6757 case 0x0112: return wxT("WM_SYSCOMMAND");
6758 case 0x0113: return wxT("WM_TIMER");
6759 case 0x0114: return wxT("WM_HSCROLL");
6760 case 0x0115: return wxT("WM_VSCROLL");
6761 case 0x0116: return wxT("WM_INITMENU");
6762 case 0x0117: return wxT("WM_INITMENUPOPUP");
6763 case 0x011F: return wxT("WM_MENUSELECT");
6764 case 0x0120: return wxT("WM_MENUCHAR");
6765 case 0x0121: return wxT("WM_ENTERIDLE");
6766
6767 case 0x0127: return wxT("WM_CHANGEUISTATE");
6768 case 0x0128: return wxT("WM_UPDATEUISTATE");
6769 case 0x0129: return wxT("WM_QUERYUISTATE");
6770
6771 case 0x0132: return wxT("WM_CTLCOLORMSGBOX");
6772 case 0x0133: return wxT("WM_CTLCOLOREDIT");
6773 case 0x0134: return wxT("WM_CTLCOLORLISTBOX");
6774 case 0x0135: return wxT("WM_CTLCOLORBTN");
6775 case 0x0136: return wxT("WM_CTLCOLORDLG");
6776 case 0x0137: return wxT("WM_CTLCOLORSCROLLBAR");
6777 case 0x0138: return wxT("WM_CTLCOLORSTATIC");
6778 case 0x01E1: return wxT("MN_GETHMENU");
6779
6780 case 0x0200: return wxT("WM_MOUSEMOVE");
6781 case 0x0201: return wxT("WM_LBUTTONDOWN");
6782 case 0x0202: return wxT("WM_LBUTTONUP");
6783 case 0x0203: return wxT("WM_LBUTTONDBLCLK");
6784 case 0x0204: return wxT("WM_RBUTTONDOWN");
6785 case 0x0205: return wxT("WM_RBUTTONUP");
6786 case 0x0206: return wxT("WM_RBUTTONDBLCLK");
6787 case 0x0207: return wxT("WM_MBUTTONDOWN");
6788 case 0x0208: return wxT("WM_MBUTTONUP");
6789 case 0x0209: return wxT("WM_MBUTTONDBLCLK");
6790 case 0x020A: return wxT("WM_MOUSEWHEEL");
6791 case 0x020B: return wxT("WM_XBUTTONDOWN");
6792 case 0x020C: return wxT("WM_XBUTTONUP");
6793 case 0x020D: return wxT("WM_XBUTTONDBLCLK");
6794 case 0x0210: return wxT("WM_PARENTNOTIFY");
6795 case 0x0211: return wxT("WM_ENTERMENULOOP");
6796 case 0x0212: return wxT("WM_EXITMENULOOP");
6797
6798 case 0x0213: return wxT("WM_NEXTMENU");
6799 case 0x0214: return wxT("WM_SIZING");
6800 case 0x0215: return wxT("WM_CAPTURECHANGED");
6801 case 0x0216: return wxT("WM_MOVING");
6802 case 0x0218: return wxT("WM_POWERBROADCAST");
6803 case 0x0219: return wxT("WM_DEVICECHANGE");
6804
6805 case 0x0220: return wxT("WM_MDICREATE");
6806 case 0x0221: return wxT("WM_MDIDESTROY");
6807 case 0x0222: return wxT("WM_MDIACTIVATE");
6808 case 0x0223: return wxT("WM_MDIRESTORE");
6809 case 0x0224: return wxT("WM_MDINEXT");
6810 case 0x0225: return wxT("WM_MDIMAXIMIZE");
6811 case 0x0226: return wxT("WM_MDITILE");
6812 case 0x0227: return wxT("WM_MDICASCADE");
6813 case 0x0228: return wxT("WM_MDIICONARRANGE");
6814 case 0x0229: return wxT("WM_MDIGETACTIVE");
6815 case 0x0230: return wxT("WM_MDISETMENU");
6816 case 0x0233: return wxT("WM_DROPFILES");
6817
6818 case 0x0281: return wxT("WM_IME_SETCONTEXT");
6819 case 0x0282: return wxT("WM_IME_NOTIFY");
6820 case 0x0283: return wxT("WM_IME_CONTROL");
6821 case 0x0284: return wxT("WM_IME_COMPOSITIONFULL");
6822 case 0x0285: return wxT("WM_IME_SELECT");
6823 case 0x0286: return wxT("WM_IME_CHAR");
6824 case 0x0290: return wxT("WM_IME_KEYDOWN");
6825 case 0x0291: return wxT("WM_IME_KEYUP");
6826
6827 case 0x02A0: return wxT("WM_NCMOUSEHOVER");
6828 case 0x02A1: return wxT("WM_MOUSEHOVER");
6829 case 0x02A2: return wxT("WM_NCMOUSELEAVE");
6830 case 0x02A3: return wxT("WM_MOUSELEAVE");
6831
6832 case 0x0300: return wxT("WM_CUT");
6833 case 0x0301: return wxT("WM_COPY");
6834 case 0x0302: return wxT("WM_PASTE");
6835 case 0x0303: return wxT("WM_CLEAR");
6836 case 0x0304: return wxT("WM_UNDO");
6837 case 0x0305: return wxT("WM_RENDERFORMAT");
6838 case 0x0306: return wxT("WM_RENDERALLFORMATS");
6839 case 0x0307: return wxT("WM_DESTROYCLIPBOARD");
6840 case 0x0308: return wxT("WM_DRAWCLIPBOARD");
6841 case 0x0309: return wxT("WM_PAINTCLIPBOARD");
6842 case 0x030A: return wxT("WM_VSCROLLCLIPBOARD");
6843 case 0x030B: return wxT("WM_SIZECLIPBOARD");
6844 case 0x030C: return wxT("WM_ASKCBFORMATNAME");
6845 case 0x030D: return wxT("WM_CHANGECBCHAIN");
6846 case 0x030E: return wxT("WM_HSCROLLCLIPBOARD");
6847 case 0x030F: return wxT("WM_QUERYNEWPALETTE");
6848 case 0x0310: return wxT("WM_PALETTEISCHANGING");
6849 case 0x0311: return wxT("WM_PALETTECHANGED");
6850 case 0x0312: return wxT("WM_HOTKEY");
6851
6852 case 0x0317: return wxT("WM_PRINT");
6853 case 0x0318: return wxT("WM_PRINTCLIENT");
6854
6855 // common controls messages - although they're not strictly speaking
6856 // standard, it's nice to decode them nevertheless
6857
6858 // listview
6859 case 0x1000 + 0: return wxT("LVM_GETBKCOLOR");
6860 case 0x1000 + 1: return wxT("LVM_SETBKCOLOR");
6861 case 0x1000 + 2: return wxT("LVM_GETIMAGELIST");
6862 case 0x1000 + 3: return wxT("LVM_SETIMAGELIST");
6863 case 0x1000 + 4: return wxT("LVM_GETITEMCOUNT");
6864 case 0x1000 + 5: return wxT("LVM_GETITEMA");
6865 case 0x1000 + 75: return wxT("LVM_GETITEMW");
6866 case 0x1000 + 6: return wxT("LVM_SETITEMA");
6867 case 0x1000 + 76: return wxT("LVM_SETITEMW");
6868 case 0x1000 + 7: return wxT("LVM_INSERTITEMA");
6869 case 0x1000 + 77: return wxT("LVM_INSERTITEMW");
6870 case 0x1000 + 8: return wxT("LVM_DELETEITEM");
6871 case 0x1000 + 9: return wxT("LVM_DELETEALLITEMS");
6872 case 0x1000 + 10: return wxT("LVM_GETCALLBACKMASK");
6873 case 0x1000 + 11: return wxT("LVM_SETCALLBACKMASK");
6874 case 0x1000 + 12: return wxT("LVM_GETNEXTITEM");
6875 case 0x1000 + 13: return wxT("LVM_FINDITEMA");
6876 case 0x1000 + 83: return wxT("LVM_FINDITEMW");
6877 case 0x1000 + 14: return wxT("LVM_GETITEMRECT");
6878 case 0x1000 + 15: return wxT("LVM_SETITEMPOSITION");
6879 case 0x1000 + 16: return wxT("LVM_GETITEMPOSITION");
6880 case 0x1000 + 17: return wxT("LVM_GETSTRINGWIDTHA");
6881 case 0x1000 + 87: return wxT("LVM_GETSTRINGWIDTHW");
6882 case 0x1000 + 18: return wxT("LVM_HITTEST");
6883 case 0x1000 + 19: return wxT("LVM_ENSUREVISIBLE");
6884 case 0x1000 + 20: return wxT("LVM_SCROLL");
6885 case 0x1000 + 21: return wxT("LVM_REDRAWITEMS");
6886 case 0x1000 + 22: return wxT("LVM_ARRANGE");
6887 case 0x1000 + 23: return wxT("LVM_EDITLABELA");
6888 case 0x1000 + 118: return wxT("LVM_EDITLABELW");
6889 case 0x1000 + 24: return wxT("LVM_GETEDITCONTROL");
6890 case 0x1000 + 25: return wxT("LVM_GETCOLUMNA");
6891 case 0x1000 + 95: return wxT("LVM_GETCOLUMNW");
6892 case 0x1000 + 26: return wxT("LVM_SETCOLUMNA");
6893 case 0x1000 + 96: return wxT("LVM_SETCOLUMNW");
6894 case 0x1000 + 27: return wxT("LVM_INSERTCOLUMNA");
6895 case 0x1000 + 97: return wxT("LVM_INSERTCOLUMNW");
6896 case 0x1000 + 28: return wxT("LVM_DELETECOLUMN");
6897 case 0x1000 + 29: return wxT("LVM_GETCOLUMNWIDTH");
6898 case 0x1000 + 30: return wxT("LVM_SETCOLUMNWIDTH");
6899 case 0x1000 + 31: return wxT("LVM_GETHEADER");
6900 case 0x1000 + 33: return wxT("LVM_CREATEDRAGIMAGE");
6901 case 0x1000 + 34: return wxT("LVM_GETVIEWRECT");
6902 case 0x1000 + 35: return wxT("LVM_GETTEXTCOLOR");
6903 case 0x1000 + 36: return wxT("LVM_SETTEXTCOLOR");
6904 case 0x1000 + 37: return wxT("LVM_GETTEXTBKCOLOR");
6905 case 0x1000 + 38: return wxT("LVM_SETTEXTBKCOLOR");
6906 case 0x1000 + 39: return wxT("LVM_GETTOPINDEX");
6907 case 0x1000 + 40: return wxT("LVM_GETCOUNTPERPAGE");
6908 case 0x1000 + 41: return wxT("LVM_GETORIGIN");
6909 case 0x1000 + 42: return wxT("LVM_UPDATE");
6910 case 0x1000 + 43: return wxT("LVM_SETITEMSTATE");
6911 case 0x1000 + 44: return wxT("LVM_GETITEMSTATE");
6912 case 0x1000 + 45: return wxT("LVM_GETITEMTEXTA");
6913 case 0x1000 + 115: return wxT("LVM_GETITEMTEXTW");
6914 case 0x1000 + 46: return wxT("LVM_SETITEMTEXTA");
6915 case 0x1000 + 116: return wxT("LVM_SETITEMTEXTW");
6916 case 0x1000 + 47: return wxT("LVM_SETITEMCOUNT");
6917 case 0x1000 + 48: return wxT("LVM_SORTITEMS");
6918 case 0x1000 + 49: return wxT("LVM_SETITEMPOSITION32");
6919 case 0x1000 + 50: return wxT("LVM_GETSELECTEDCOUNT");
6920 case 0x1000 + 51: return wxT("LVM_GETITEMSPACING");
6921 case 0x1000 + 52: return wxT("LVM_GETISEARCHSTRINGA");
6922 case 0x1000 + 117: return wxT("LVM_GETISEARCHSTRINGW");
6923 case 0x1000 + 53: return wxT("LVM_SETICONSPACING");
6924 case 0x1000 + 54: return wxT("LVM_SETEXTENDEDLISTVIEWSTYLE");
6925 case 0x1000 + 55: return wxT("LVM_GETEXTENDEDLISTVIEWSTYLE");
6926 case 0x1000 + 56: return wxT("LVM_GETSUBITEMRECT");
6927 case 0x1000 + 57: return wxT("LVM_SUBITEMHITTEST");
6928 case 0x1000 + 58: return wxT("LVM_SETCOLUMNORDERARRAY");
6929 case 0x1000 + 59: return wxT("LVM_GETCOLUMNORDERARRAY");
6930 case 0x1000 + 60: return wxT("LVM_SETHOTITEM");
6931 case 0x1000 + 61: return wxT("LVM_GETHOTITEM");
6932 case 0x1000 + 62: return wxT("LVM_SETHOTCURSOR");
6933 case 0x1000 + 63: return wxT("LVM_GETHOTCURSOR");
6934 case 0x1000 + 64: return wxT("LVM_APPROXIMATEVIEWRECT");
6935 case 0x1000 + 65: return wxT("LVM_SETWORKAREA");
6936
6937 // tree view
6938 case 0x1100 + 0: return wxT("TVM_INSERTITEMA");
6939 case 0x1100 + 50: return wxT("TVM_INSERTITEMW");
6940 case 0x1100 + 1: return wxT("TVM_DELETEITEM");
6941 case 0x1100 + 2: return wxT("TVM_EXPAND");
6942 case 0x1100 + 4: return wxT("TVM_GETITEMRECT");
6943 case 0x1100 + 5: return wxT("TVM_GETCOUNT");
6944 case 0x1100 + 6: return wxT("TVM_GETINDENT");
6945 case 0x1100 + 7: return wxT("TVM_SETINDENT");
6946 case 0x1100 + 8: return wxT("TVM_GETIMAGELIST");
6947 case 0x1100 + 9: return wxT("TVM_SETIMAGELIST");
6948 case 0x1100 + 10: return wxT("TVM_GETNEXTITEM");
6949 case 0x1100 + 11: return wxT("TVM_SELECTITEM");
6950 case 0x1100 + 12: return wxT("TVM_GETITEMA");
6951 case 0x1100 + 62: return wxT("TVM_GETITEMW");
6952 case 0x1100 + 13: return wxT("TVM_SETITEMA");
6953 case 0x1100 + 63: return wxT("TVM_SETITEMW");
6954 case 0x1100 + 14: return wxT("TVM_EDITLABELA");
6955 case 0x1100 + 65: return wxT("TVM_EDITLABELW");
6956 case 0x1100 + 15: return wxT("TVM_GETEDITCONTROL");
6957 case 0x1100 + 16: return wxT("TVM_GETVISIBLECOUNT");
6958 case 0x1100 + 17: return wxT("TVM_HITTEST");
6959 case 0x1100 + 18: return wxT("TVM_CREATEDRAGIMAGE");
6960 case 0x1100 + 19: return wxT("TVM_SORTCHILDREN");
6961 case 0x1100 + 20: return wxT("TVM_ENSUREVISIBLE");
6962 case 0x1100 + 21: return wxT("TVM_SORTCHILDRENCB");
6963 case 0x1100 + 22: return wxT("TVM_ENDEDITLABELNOW");
6964 case 0x1100 + 23: return wxT("TVM_GETISEARCHSTRINGA");
6965 case 0x1100 + 64: return wxT("TVM_GETISEARCHSTRINGW");
6966 case 0x1100 + 24: return wxT("TVM_SETTOOLTIPS");
6967 case 0x1100 + 25: return wxT("TVM_GETTOOLTIPS");
6968
6969 // header
6970 case 0x1200 + 0: return wxT("HDM_GETITEMCOUNT");
6971 case 0x1200 + 1: return wxT("HDM_INSERTITEMA");
6972 case 0x1200 + 10: return wxT("HDM_INSERTITEMW");
6973 case 0x1200 + 2: return wxT("HDM_DELETEITEM");
6974 case 0x1200 + 3: return wxT("HDM_GETITEMA");
6975 case 0x1200 + 11: return wxT("HDM_GETITEMW");
6976 case 0x1200 + 4: return wxT("HDM_SETITEMA");
6977 case 0x1200 + 12: return wxT("HDM_SETITEMW");
6978 case 0x1200 + 5: return wxT("HDM_LAYOUT");
6979 case 0x1200 + 6: return wxT("HDM_HITTEST");
6980 case 0x1200 + 7: return wxT("HDM_GETITEMRECT");
6981 case 0x1200 + 8: return wxT("HDM_SETIMAGELIST");
6982 case 0x1200 + 9: return wxT("HDM_GETIMAGELIST");
6983 case 0x1200 + 15: return wxT("HDM_ORDERTOINDEX");
6984 case 0x1200 + 16: return wxT("HDM_CREATEDRAGIMAGE");
6985 case 0x1200 + 17: return wxT("HDM_GETORDERARRAY");
6986 case 0x1200 + 18: return wxT("HDM_SETORDERARRAY");
6987 case 0x1200 + 19: return wxT("HDM_SETHOTDIVIDER");
6988
6989 // tab control
6990 case 0x1300 + 2: return wxT("TCM_GETIMAGELIST");
6991 case 0x1300 + 3: return wxT("TCM_SETIMAGELIST");
6992 case 0x1300 + 4: return wxT("TCM_GETITEMCOUNT");
6993 case 0x1300 + 5: return wxT("TCM_GETITEMA");
6994 case 0x1300 + 60: return wxT("TCM_GETITEMW");
6995 case 0x1300 + 6: return wxT("TCM_SETITEMA");
6996 case 0x1300 + 61: return wxT("TCM_SETITEMW");
6997 case 0x1300 + 7: return wxT("TCM_INSERTITEMA");
6998 case 0x1300 + 62: return wxT("TCM_INSERTITEMW");
6999 case 0x1300 + 8: return wxT("TCM_DELETEITEM");
7000 case 0x1300 + 9: return wxT("TCM_DELETEALLITEMS");
7001 case 0x1300 + 10: return wxT("TCM_GETITEMRECT");
7002 case 0x1300 + 11: return wxT("TCM_GETCURSEL");
7003 case 0x1300 + 12: return wxT("TCM_SETCURSEL");
7004 case 0x1300 + 13: return wxT("TCM_HITTEST");
7005 case 0x1300 + 14: return wxT("TCM_SETITEMEXTRA");
7006 case 0x1300 + 40: return wxT("TCM_ADJUSTRECT");
7007 case 0x1300 + 41: return wxT("TCM_SETITEMSIZE");
7008 case 0x1300 + 42: return wxT("TCM_REMOVEIMAGE");
7009 case 0x1300 + 43: return wxT("TCM_SETPADDING");
7010 case 0x1300 + 44: return wxT("TCM_GETROWCOUNT");
7011 case 0x1300 + 45: return wxT("TCM_GETTOOLTIPS");
7012 case 0x1300 + 46: return wxT("TCM_SETTOOLTIPS");
7013 case 0x1300 + 47: return wxT("TCM_GETCURFOCUS");
7014 case 0x1300 + 48: return wxT("TCM_SETCURFOCUS");
7015 case 0x1300 + 49: return wxT("TCM_SETMINTABWIDTH");
7016 case 0x1300 + 50: return wxT("TCM_DESELECTALL");
7017
7018 // toolbar
7019 case WM_USER+1: return wxT("TB_ENABLEBUTTON");
7020 case WM_USER+2: return wxT("TB_CHECKBUTTON");
7021 case WM_USER+3: return wxT("TB_PRESSBUTTON");
7022 case WM_USER+4: return wxT("TB_HIDEBUTTON");
7023 case WM_USER+5: return wxT("TB_INDETERMINATE");
7024 case WM_USER+9: return wxT("TB_ISBUTTONENABLED");
7025 case WM_USER+10: return wxT("TB_ISBUTTONCHECKED");
7026 case WM_USER+11: return wxT("TB_ISBUTTONPRESSED");
7027 case WM_USER+12: return wxT("TB_ISBUTTONHIDDEN");
7028 case WM_USER+13: return wxT("TB_ISBUTTONINDETERMINATE");
7029 case WM_USER+17: return wxT("TB_SETSTATE");
7030 case WM_USER+18: return wxT("TB_GETSTATE");
7031 case WM_USER+19: return wxT("TB_ADDBITMAP");
7032 case WM_USER+20: return wxT("TB_ADDBUTTONS");
7033 case WM_USER+21: return wxT("TB_INSERTBUTTON");
7034 case WM_USER+22: return wxT("TB_DELETEBUTTON");
7035 case WM_USER+23: return wxT("TB_GETBUTTON");
7036 case WM_USER+24: return wxT("TB_BUTTONCOUNT");
7037 case WM_USER+25: return wxT("TB_COMMANDTOINDEX");
7038 case WM_USER+26: return wxT("TB_SAVERESTOREA");
7039 case WM_USER+76: return wxT("TB_SAVERESTOREW");
7040 case WM_USER+27: return wxT("TB_CUSTOMIZE");
7041 case WM_USER+28: return wxT("TB_ADDSTRINGA");
7042 case WM_USER+77: return wxT("TB_ADDSTRINGW");
7043 case WM_USER+29: return wxT("TB_GETITEMRECT");
7044 case WM_USER+30: return wxT("TB_BUTTONSTRUCTSIZE");
7045 case WM_USER+31: return wxT("TB_SETBUTTONSIZE");
7046 case WM_USER+32: return wxT("TB_SETBITMAPSIZE");
7047 case WM_USER+33: return wxT("TB_AUTOSIZE");
7048 case WM_USER+35: return wxT("TB_GETTOOLTIPS");
7049 case WM_USER+36: return wxT("TB_SETTOOLTIPS");
7050 case WM_USER+37: return wxT("TB_SETPARENT");
7051 case WM_USER+39: return wxT("TB_SETROWS");
7052 case WM_USER+40: return wxT("TB_GETROWS");
7053 case WM_USER+42: return wxT("TB_SETCMDID");
7054 case WM_USER+43: return wxT("TB_CHANGEBITMAP");
7055 case WM_USER+44: return wxT("TB_GETBITMAP");
7056 case WM_USER+45: return wxT("TB_GETBUTTONTEXTA");
7057 case WM_USER+75: return wxT("TB_GETBUTTONTEXTW");
7058 case WM_USER+46: return wxT("TB_REPLACEBITMAP");
7059 case WM_USER+47: return wxT("TB_SETINDENT");
7060 case WM_USER+48: return wxT("TB_SETIMAGELIST");
7061 case WM_USER+49: return wxT("TB_GETIMAGELIST");
7062 case WM_USER+50: return wxT("TB_LOADIMAGES");
7063 case WM_USER+51: return wxT("TB_GETRECT");
7064 case WM_USER+52: return wxT("TB_SETHOTIMAGELIST");
7065 case WM_USER+53: return wxT("TB_GETHOTIMAGELIST");
7066 case WM_USER+54: return wxT("TB_SETDISABLEDIMAGELIST");
7067 case WM_USER+55: return wxT("TB_GETDISABLEDIMAGELIST");
7068 case WM_USER+56: return wxT("TB_SETSTYLE");
7069 case WM_USER+57: return wxT("TB_GETSTYLE");
7070 case WM_USER+58: return wxT("TB_GETBUTTONSIZE");
7071 case WM_USER+59: return wxT("TB_SETBUTTONWIDTH");
7072 case WM_USER+60: return wxT("TB_SETMAXTEXTROWS");
7073 case WM_USER+61: return wxT("TB_GETTEXTROWS");
7074 case WM_USER+41: return wxT("TB_GETBITMAPFLAGS");
7075
7076 default:
7077 static wxString s_szBuf;
7078 s_szBuf.Printf(wxT("<unknown message = %d>"), message);
7079 return s_szBuf.c_str();
7080 }
7081 }
7082 #endif // wxDEBUG_LEVEL >= 2
7083
7084 static TEXTMETRIC wxGetTextMetrics(const wxWindowMSW *win)
7085 {
7086 // prepare the DC
7087 TEXTMETRIC tm;
7088 HWND hwnd = GetHwndOf(win);
7089 HDC hdc = ::GetDC(hwnd);
7090
7091 #if !wxDIALOG_UNIT_COMPATIBILITY
7092 // and select the current font into it
7093 HFONT hfont = GetHfontOf(win->GetFont());
7094 if ( hfont )
7095 {
7096 hfont = (HFONT)::SelectObject(hdc, hfont);
7097 }
7098 #endif
7099
7100 // finally retrieve the text metrics from it
7101 GetTextMetrics(hdc, &tm);
7102
7103 #if !wxDIALOG_UNIT_COMPATIBILITY
7104 // and clean up
7105 if ( hfont )
7106 {
7107 (void)::SelectObject(hdc, hfont);
7108 }
7109 #endif
7110
7111 ::ReleaseDC(hwnd, hdc);
7112
7113 return tm;
7114 }
7115
7116 // Find the wxWindow at the current mouse position, returning the mouse
7117 // position.
7118 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
7119 {
7120 pt = wxGetMousePosition();
7121 return wxFindWindowAtPoint(pt);
7122 }
7123
7124 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
7125 {
7126 POINT pt2;
7127 pt2.x = pt.x;
7128 pt2.y = pt.y;
7129
7130 HWND hWnd = ::WindowFromPoint(pt2);
7131
7132 return wxGetWindowFromHWND((WXHWND)hWnd);
7133 }
7134
7135 // Get the current mouse position.
7136 wxPoint wxGetMousePosition()
7137 {
7138 POINT pt;
7139 #ifdef __WXWINCE__
7140 GetCursorPosWinCE(&pt);
7141 #else
7142 GetCursorPos( & pt );
7143 #endif
7144
7145 return wxPoint(pt.x, pt.y);
7146 }
7147
7148 #if wxUSE_HOTKEY
7149
7150 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
7151 static void WinCEUnregisterHotKey(int modifiers, int id)
7152 {
7153 // Register hotkeys for the hardware buttons
7154 HINSTANCE hCoreDll;
7155 typedef BOOL (WINAPI *UnregisterFunc1Proc)(UINT, UINT);
7156
7157 UnregisterFunc1Proc procUnregisterFunc;
7158 hCoreDll = LoadLibrary(wxT("coredll.dll"));
7159 if (hCoreDll)
7160 {
7161 procUnregisterFunc = (UnregisterFunc1Proc)GetProcAddress(hCoreDll, wxT("UnregisterFunc1"));
7162 if (procUnregisterFunc)
7163 procUnregisterFunc(modifiers, id);
7164 FreeLibrary(hCoreDll);
7165 }
7166 }
7167 #endif
7168
7169 bool wxWindowMSW::RegisterHotKey(int hotkeyId, int modifiers, int keycode)
7170 {
7171 UINT win_modifiers=0;
7172 if ( modifiers & wxMOD_ALT )
7173 win_modifiers |= MOD_ALT;
7174 if ( modifiers & wxMOD_SHIFT )
7175 win_modifiers |= MOD_SHIFT;
7176 if ( modifiers & wxMOD_CONTROL )
7177 win_modifiers |= MOD_CONTROL;
7178 if ( modifiers & wxMOD_WIN )
7179 win_modifiers |= MOD_WIN;
7180
7181 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
7182 // Required for PPC and Smartphone hardware buttons
7183 if (keycode >= WXK_SPECIAL1 && keycode <= WXK_SPECIAL20)
7184 WinCEUnregisterHotKey(win_modifiers, hotkeyId);
7185 #endif
7186
7187 if ( !::RegisterHotKey(GetHwnd(), hotkeyId, win_modifiers, keycode) )
7188 {
7189 wxLogLastError(wxT("RegisterHotKey"));
7190
7191 return false;
7192 }
7193
7194 return true;
7195 }
7196
7197 bool wxWindowMSW::UnregisterHotKey(int hotkeyId)
7198 {
7199 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
7200 WinCEUnregisterHotKey(MOD_WIN, hotkeyId);
7201 #endif
7202
7203 if ( !::UnregisterHotKey(GetHwnd(), hotkeyId) )
7204 {
7205 wxLogLastError(wxT("UnregisterHotKey"));
7206
7207 return false;
7208 }
7209
7210 return true;
7211 }
7212
7213 #if wxUSE_ACCEL
7214
7215 bool wxWindowMSW::HandleHotKey(WXWPARAM wParam, WXLPARAM lParam)
7216 {
7217 int win_modifiers = LOWORD(lParam);
7218
7219 wxKeyEvent event(CreateKeyEvent(wxEVT_HOTKEY, HIWORD(lParam)));
7220 event.SetId(wParam);
7221 event.m_shiftDown = (win_modifiers & MOD_SHIFT) != 0;
7222 event.m_controlDown = (win_modifiers & MOD_CONTROL) != 0;
7223 event.m_altDown = (win_modifiers & MOD_ALT) != 0;
7224 event.m_metaDown = (win_modifiers & MOD_WIN) != 0;
7225
7226 return HandleWindowEvent(event);
7227 }
7228
7229 #endif // wxUSE_ACCEL
7230
7231 #endif // wxUSE_HOTKEY
7232
7233 // Not tested under WinCE
7234 #ifndef __WXWINCE__
7235
7236 // this class installs a message hook which really wakes up our idle processing
7237 // each time a WM_NULL is received (wxWakeUpIdle does this), even if we're
7238 // sitting inside a local modal loop (e.g. a menu is opened or scrollbar is
7239 // being dragged or even inside ::MessageBox()) and so don't control message
7240 // dispatching otherwise
7241 class wxIdleWakeUpModule : public wxModule
7242 {
7243 public:
7244 virtual bool OnInit()
7245 {
7246 ms_hMsgHookProc = ::SetWindowsHookEx
7247 (
7248 WH_GETMESSAGE,
7249 &wxIdleWakeUpModule::MsgHookProc,
7250 NULL,
7251 GetCurrentThreadId()
7252 );
7253
7254 if ( !ms_hMsgHookProc )
7255 {
7256 wxLogLastError(wxT("SetWindowsHookEx(WH_GETMESSAGE)"));
7257
7258 return false;
7259 }
7260
7261 return true;
7262 }
7263
7264 virtual void OnExit()
7265 {
7266 ::UnhookWindowsHookEx(wxIdleWakeUpModule::ms_hMsgHookProc);
7267 }
7268
7269 static LRESULT CALLBACK MsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
7270 {
7271 MSG *msg = (MSG*)lParam;
7272
7273 // only process the message if it is actually going to be removed from
7274 // the message queue, this prevents that the same event from being
7275 // processed multiple times if now someone just called PeekMessage()
7276 if ( msg->message == WM_NULL && wParam == PM_REMOVE )
7277 {
7278 wxTheApp->ProcessPendingEvents();
7279 }
7280
7281 return CallNextHookEx(ms_hMsgHookProc, nCode, wParam, lParam);
7282 }
7283
7284 private:
7285 static HHOOK ms_hMsgHookProc;
7286
7287 DECLARE_DYNAMIC_CLASS(wxIdleWakeUpModule)
7288 };
7289
7290 HHOOK wxIdleWakeUpModule::ms_hMsgHookProc = 0;
7291
7292 IMPLEMENT_DYNAMIC_CLASS(wxIdleWakeUpModule, wxModule)
7293
7294 #endif // __WXWINCE__
7295
7296 #ifdef __WXWINCE__
7297
7298 #if wxUSE_STATBOX
7299 static void wxAdjustZOrder(wxWindow* parent)
7300 {
7301 if (parent->IsKindOf(CLASSINFO(wxStaticBox)))
7302 {
7303 // Set the z-order correctly
7304 SetWindowPos((HWND) parent->GetHWND(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
7305 }
7306
7307 wxWindowList::compatibility_iterator current = parent->GetChildren().GetFirst();
7308 while (current)
7309 {
7310 wxWindow *childWin = current->GetData();
7311 wxAdjustZOrder(childWin);
7312 current = current->GetNext();
7313 }
7314 }
7315 #endif
7316
7317 // We need to adjust the z-order of static boxes in WinCE, to
7318 // make 'contained' controls visible
7319 void wxWindowMSW::OnInitDialog( wxInitDialogEvent& event )
7320 {
7321 #if wxUSE_STATBOX
7322 wxAdjustZOrder(this);
7323 #endif
7324
7325 event.Skip();
7326 }
7327 #endif