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