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