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