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