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