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