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