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