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