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