]> git.saurik.com Git - wxWidgets.git/blob - src/msw/window.cpp
Fix for [1250089] 'wxHtmlHelpFrame uses constants instead of variables'.
[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
4630 #if wxUSE_MOUSEEVENT_HACK
4631 // Window gets a click down message followed by a mouse move message even
4632 // if position isn't changed! We want to discard the trailing move event
4633 // if x and y are the same.
4634 if ( (m_lastMouseEvent == wxEVT_RIGHT_DOWN ||
4635 m_lastMouseEvent == wxEVT_LEFT_DOWN ||
4636 m_lastMouseEvent == wxEVT_MIDDLE_DOWN) &&
4637 (m_lastMouseX == x && m_lastMouseY == y) )
4638 {
4639 m_lastMouseEvent = wxEVT_MOTION;
4640
4641 return false;
4642 }
4643 #endif // wxUSE_MOUSEEVENT_HACK
4644
4645 return HandleMouseEvent(WM_MOUSEMOVE, x, y, flags);
4646 }
4647
4648
4649 bool wxWindowMSW::HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam)
4650 {
4651 #if wxUSE_MOUSEWHEEL
4652 // notice that WM_MOUSEWHEEL position is in screen coords (as it's
4653 // forwarded up to the parent by DefWindowProc()) and not in the client
4654 // ones as all the other messages, translate them to the client coords for
4655 // consistency
4656 const wxPoint
4657 pt = ScreenToClient(wxPoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
4658 wxMouseEvent event(wxEVT_MOUSEWHEEL);
4659 InitMouseEvent(event, pt.x, pt.y, LOWORD(wParam));
4660 event.m_wheelRotation = (short)HIWORD(wParam);
4661 event.m_wheelDelta = WHEEL_DELTA;
4662
4663 static int s_linesPerRotation = -1;
4664 if ( s_linesPerRotation == -1 )
4665 {
4666 if ( !::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
4667 &s_linesPerRotation, 0))
4668 {
4669 // this is not supposed to happen
4670 wxLogLastError(_T("SystemParametersInfo(GETWHEELSCROLLLINES)"));
4671
4672 // the default is 3, so use it if SystemParametersInfo() failed
4673 s_linesPerRotation = 3;
4674 }
4675 }
4676
4677 event.m_linesPerAction = s_linesPerRotation;
4678 return GetEventHandler()->ProcessEvent(event);
4679
4680 #else // !wxUSE_MOUSEWHEEL
4681 wxUnusedVar(wParam);
4682 wxUnusedVar(lParam);
4683
4684 return false;
4685 #endif // wxUSE_MOUSEWHEEL/!wxUSE_MOUSEWHEEL
4686 }
4687
4688 void wxWindowMSW::GenerateMouseLeave()
4689 {
4690 m_mouseInWindow = false;
4691
4692 int state = 0;
4693 if ( wxIsShiftDown() )
4694 state |= MK_SHIFT;
4695 if ( wxIsCtrlDown() )
4696 state |= MK_CONTROL;
4697
4698 // Only the high-order bit should be tested
4699 if ( GetKeyState( VK_LBUTTON ) & (1<<15) )
4700 state |= MK_LBUTTON;
4701 if ( GetKeyState( VK_MBUTTON ) & (1<<15) )
4702 state |= MK_MBUTTON;
4703 if ( GetKeyState( VK_RBUTTON ) & (1<<15) )
4704 state |= MK_RBUTTON;
4705
4706 POINT pt;
4707 #ifdef __WXWINCE__
4708 if ( !::GetCursorPosWinCE(&pt) )
4709 #else
4710 if ( !::GetCursorPos(&pt) )
4711 #endif
4712 {
4713 wxLogLastError(_T("GetCursorPos"));
4714 }
4715
4716 // we need to have client coordinates here for symmetry with
4717 // wxEVT_ENTER_WINDOW
4718 RECT rect = wxGetWindowRect(GetHwnd());
4719 pt.x -= rect.left;
4720 pt.y -= rect.top;
4721
4722 wxMouseEvent event(wxEVT_LEAVE_WINDOW);
4723 InitMouseEvent(event, pt.x, pt.y, state);
4724
4725 (void)GetEventHandler()->ProcessEvent(event);
4726 }
4727
4728 // ---------------------------------------------------------------------------
4729 // keyboard handling
4730 // ---------------------------------------------------------------------------
4731
4732 // create the key event of the given type for the given key - used by
4733 // HandleChar and HandleKeyDown/Up
4734 wxKeyEvent wxWindowMSW::CreateKeyEvent(wxEventType evType,
4735 int id,
4736 WXLPARAM lParam,
4737 WXWPARAM wParam) const
4738 {
4739 wxKeyEvent event(evType);
4740 event.SetId(GetId());
4741 event.m_shiftDown = wxIsShiftDown();
4742 event.m_controlDown = wxIsCtrlDown();
4743 event.m_altDown = (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN;
4744
4745 event.SetEventObject((wxWindow *)this); // const_cast
4746 event.m_keyCode = id;
4747 #if wxUSE_UNICODE
4748 event.m_uniChar = (wxChar) wParam;
4749 #endif
4750 event.m_rawCode = (wxUint32) wParam;
4751 event.m_rawFlags = (wxUint32) lParam;
4752 #ifndef __WXWINCE__
4753 event.SetTimestamp(::GetMessageTime());
4754 #endif
4755
4756 // translate the position to client coords
4757 POINT pt;
4758 #ifdef __WXWINCE__
4759 GetCursorPosWinCE(&pt);
4760 #else
4761 GetCursorPos(&pt);
4762 #endif
4763 RECT rect;
4764 GetWindowRect(GetHwnd(),&rect);
4765 pt.x -= rect.left;
4766 pt.y -= rect.top;
4767
4768 event.m_x = pt.x;
4769 event.m_y = pt.y;
4770
4771 return event;
4772 }
4773
4774 // isASCII is true only when we're called from WM_CHAR handler and not from
4775 // WM_KEYDOWN one
4776 bool wxWindowMSW::HandleChar(WXWPARAM wParam, WXLPARAM lParam, bool isASCII)
4777 {
4778 int id;
4779 if ( isASCII )
4780 {
4781 // If 1 -> 26, translate to either special keycode or just set
4782 // ctrlDown. IOW, Ctrl-C should result in keycode == 3 and
4783 // ControlDown() == true.
4784 id = wParam;
4785 if ( (id > 0) && (id < 27) )
4786 {
4787 switch (id)
4788 {
4789 case 13:
4790 id = WXK_RETURN;
4791 break;
4792
4793 case 8:
4794 id = WXK_BACK;
4795 break;
4796
4797 case 9:
4798 id = WXK_TAB;
4799 break;
4800
4801 default:
4802 //ctrlDown = true;
4803 break;
4804 }
4805 }
4806 }
4807 else // we're called from WM_KEYDOWN
4808 {
4809 id = wxCharCodeMSWToWX(wParam, lParam);
4810 if ( id == 0 )
4811 {
4812 // it's ASCII and will be processed here only when called from
4813 // WM_CHAR (i.e. when isASCII = true), don't process it now
4814 return false;
4815 }
4816 }
4817
4818 wxKeyEvent event(CreateKeyEvent(wxEVT_CHAR, id, lParam, wParam));
4819
4820 // the alphanumeric keys produced by pressing AltGr+something on European
4821 // keyboards have both Ctrl and Alt modifiers which may confuse the user
4822 // code as, normally, keys with Ctrl and/or Alt don't result in anything
4823 // alphanumeric, so pretend that there are no modifiers at all (the
4824 // KEY_DOWN event would still have the correct modifiers if they're really
4825 // needed)
4826 if ( event.m_controlDown && event.m_altDown &&
4827 (id >= 32 && id < 256) )
4828 {
4829 event.m_controlDown =
4830 event.m_altDown = false;
4831 }
4832
4833 return GetEventHandler()->ProcessEvent(event);
4834 }
4835
4836 bool wxWindowMSW::HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam)
4837 {
4838 int id = wxCharCodeMSWToWX(wParam, lParam);
4839
4840 if ( !id )
4841 {
4842 // normal ASCII char
4843 id = wParam;
4844 }
4845
4846 if ( id != -1 ) // VZ: does this ever happen (FIXME)?
4847 {
4848 wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_DOWN, id, lParam, wParam));
4849 if ( GetEventHandler()->ProcessEvent(event) )
4850 {
4851 return true;
4852 }
4853 }
4854
4855 return false;
4856 }
4857
4858 bool wxWindowMSW::HandleKeyUp(WXWPARAM wParam, WXLPARAM lParam)
4859 {
4860 int id = wxCharCodeMSWToWX(wParam, lParam);
4861
4862 if ( !id )
4863 {
4864 // normal ASCII char
4865 id = wParam;
4866 }
4867
4868 if ( id != -1 ) // VZ: does this ever happen (FIXME)?
4869 {
4870 wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_UP, id, lParam, wParam));
4871 if ( GetEventHandler()->ProcessEvent(event) )
4872 return true;
4873 }
4874
4875 return false;
4876 }
4877
4878 int wxWindowMSW::HandleMenuChar(int WXUNUSED_IN_WINCE(chAccel),
4879 WXLPARAM WXUNUSED_IN_WINCE(lParam))
4880 {
4881 // FIXME: implement GetMenuItemCount for WinCE, possibly
4882 // in terms of GetMenuItemInfo
4883 #ifndef __WXWINCE__
4884 const HMENU hmenu = (HMENU)lParam;
4885
4886 MENUITEMINFO mii;
4887 wxZeroMemory(mii);
4888 mii.cbSize = sizeof(MENUITEMINFO);
4889 mii.fMask = MIIM_TYPE | MIIM_DATA;
4890
4891 // find if we have this letter in any owner drawn item
4892 const int count = ::GetMenuItemCount(hmenu);
4893 for ( int i = 0; i < count; i++ )
4894 {
4895 if ( ::GetMenuItemInfo(hmenu, i, TRUE, &mii) )
4896 {
4897 if ( mii.fType == MFT_OWNERDRAW )
4898 {
4899 // dwItemData member of the MENUITEMINFO is a
4900 // pointer to the associated wxMenuItem -- see the
4901 // menu creation code
4902 wxMenuItem *item = (wxMenuItem*)mii.dwItemData;
4903
4904 const wxChar *p = wxStrchr(item->GetText(), _T('&'));
4905 while ( p++ )
4906 {
4907 if ( *p == _T('&') )
4908 {
4909 // this is not the accel char, find the real one
4910 p = wxStrchr(p + 1, _T('&'));
4911 }
4912 else // got the accel char
4913 {
4914 // FIXME-UNICODE: this comparison doesn't risk to work
4915 // for non ASCII accelerator characters I'm afraid, but
4916 // what can we do?
4917 if ( (wchar_t)wxToupper(*p) == (wchar_t)chAccel )
4918 {
4919 return i;
4920 }
4921 else
4922 {
4923 // this one doesn't match
4924 break;
4925 }
4926 }
4927 }
4928 }
4929 }
4930 else // failed to get the menu text?
4931 {
4932 // it's not fatal, so don't show error, but still log
4933 // it
4934 wxLogLastError(_T("GetMenuItemInfo"));
4935 }
4936 }
4937 #endif
4938 return wxNOT_FOUND;
4939 }
4940
4941 // ---------------------------------------------------------------------------
4942 // joystick
4943 // ---------------------------------------------------------------------------
4944
4945 bool wxWindowMSW::HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags)
4946 {
4947 #ifdef JOY_BUTTON1
4948 int change = 0;
4949 if ( flags & JOY_BUTTON1CHG )
4950 change = wxJOY_BUTTON1;
4951 if ( flags & JOY_BUTTON2CHG )
4952 change = wxJOY_BUTTON2;
4953 if ( flags & JOY_BUTTON3CHG )
4954 change = wxJOY_BUTTON3;
4955 if ( flags & JOY_BUTTON4CHG )
4956 change = wxJOY_BUTTON4;
4957
4958 int buttons = 0;
4959 if ( flags & JOY_BUTTON1 )
4960 buttons |= wxJOY_BUTTON1;
4961 if ( flags & JOY_BUTTON2 )
4962 buttons |= wxJOY_BUTTON2;
4963 if ( flags & JOY_BUTTON3 )
4964 buttons |= wxJOY_BUTTON3;
4965 if ( flags & JOY_BUTTON4 )
4966 buttons |= wxJOY_BUTTON4;
4967
4968 // the event ids aren't consecutive so we can't use table based lookup
4969 int joystick;
4970 wxEventType eventType;
4971 switch ( msg )
4972 {
4973 case MM_JOY1MOVE:
4974 joystick = 1;
4975 eventType = wxEVT_JOY_MOVE;
4976 break;
4977
4978 case MM_JOY2MOVE:
4979 joystick = 2;
4980 eventType = wxEVT_JOY_MOVE;
4981 break;
4982
4983 case MM_JOY1ZMOVE:
4984 joystick = 1;
4985 eventType = wxEVT_JOY_ZMOVE;
4986 break;
4987
4988 case MM_JOY2ZMOVE:
4989 joystick = 2;
4990 eventType = wxEVT_JOY_ZMOVE;
4991 break;
4992
4993 case MM_JOY1BUTTONDOWN:
4994 joystick = 1;
4995 eventType = wxEVT_JOY_BUTTON_DOWN;
4996 break;
4997
4998 case MM_JOY2BUTTONDOWN:
4999 joystick = 2;
5000 eventType = wxEVT_JOY_BUTTON_DOWN;
5001 break;
5002
5003 case MM_JOY1BUTTONUP:
5004 joystick = 1;
5005 eventType = wxEVT_JOY_BUTTON_UP;
5006 break;
5007
5008 case MM_JOY2BUTTONUP:
5009 joystick = 2;
5010 eventType = wxEVT_JOY_BUTTON_UP;
5011 break;
5012
5013 default:
5014 wxFAIL_MSG(wxT("no such joystick event"));
5015
5016 return false;
5017 }
5018
5019 wxJoystickEvent event(eventType, buttons, joystick, change);
5020 event.SetPosition(wxPoint(x, y));
5021 event.SetEventObject(this);
5022
5023 return GetEventHandler()->ProcessEvent(event);
5024 #else
5025 wxUnusedVar(msg);
5026 wxUnusedVar(x);
5027 wxUnusedVar(y);
5028 wxUnusedVar(flags);
5029 return false;
5030 #endif
5031 }
5032
5033 // ---------------------------------------------------------------------------
5034 // scrolling
5035 // ---------------------------------------------------------------------------
5036
5037 bool wxWindowMSW::MSWOnScroll(int orientation, WXWORD wParam,
5038 WXWORD pos, WXHWND control)
5039 {
5040 if ( control && control != m_hWnd ) // Prevent infinite recursion
5041 {
5042 wxWindow *child = wxFindWinFromHandle(control);
5043 if ( child )
5044 return child->MSWOnScroll(orientation, wParam, pos, control);
5045 }
5046
5047 wxScrollWinEvent event;
5048 event.SetPosition(pos);
5049 event.SetOrientation(orientation);
5050 event.SetEventObject(this);
5051
5052 switch ( wParam )
5053 {
5054 case SB_TOP:
5055 event.SetEventType(wxEVT_SCROLLWIN_TOP);
5056 break;
5057
5058 case SB_BOTTOM:
5059 event.SetEventType(wxEVT_SCROLLWIN_BOTTOM);
5060 break;
5061
5062 case SB_LINEUP:
5063 event.SetEventType(wxEVT_SCROLLWIN_LINEUP);
5064 break;
5065
5066 case SB_LINEDOWN:
5067 event.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
5068 break;
5069
5070 case SB_PAGEUP:
5071 event.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
5072 break;
5073
5074 case SB_PAGEDOWN:
5075 event.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
5076 break;
5077
5078 case SB_THUMBPOSITION:
5079 case SB_THUMBTRACK:
5080 // under Win32, the scrollbar range and position are 32 bit integers,
5081 // but WM_[HV]SCROLL only carry the low 16 bits of them, so we must
5082 // explicitly query the scrollbar for the correct position (this must
5083 // be done only for these two SB_ events as they are the only one
5084 // carrying the scrollbar position)
5085 {
5086 WinStruct<SCROLLINFO> scrollInfo;
5087 scrollInfo.fMask = SIF_TRACKPOS;
5088
5089 if ( !::GetScrollInfo(GetHwnd(),
5090 orientation == wxHORIZONTAL ? SB_HORZ
5091 : SB_VERT,
5092 &scrollInfo) )
5093 {
5094 // Not necessarily an error, if there are no scrollbars yet.
5095 // wxLogLastError(_T("GetScrollInfo"));
5096 }
5097
5098 event.SetPosition(scrollInfo.nTrackPos);
5099 }
5100
5101 event.SetEventType( wParam == SB_THUMBPOSITION
5102 ? wxEVT_SCROLLWIN_THUMBRELEASE
5103 : wxEVT_SCROLLWIN_THUMBTRACK );
5104 break;
5105
5106 default:
5107 return false;
5108 }
5109
5110 return GetEventHandler()->ProcessEvent(event);
5111 }
5112
5113 // ===========================================================================
5114 // global functions
5115 // ===========================================================================
5116
5117 void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont& the_font)
5118 {
5119 TEXTMETRIC tm;
5120 HDC dc = ::GetDC((HWND) wnd);
5121 HFONT was = 0;
5122
5123 // the_font.UseResource();
5124 // the_font.RealizeResource();
5125 HFONT fnt = (HFONT)the_font.GetResourceHandle(); // const_cast
5126 if ( fnt )
5127 was = (HFONT) SelectObject(dc,fnt);
5128
5129 GetTextMetrics(dc, &tm);
5130 if ( fnt && was )
5131 {
5132 SelectObject(dc,was);
5133 }
5134 ReleaseDC((HWND)wnd, dc);
5135
5136 if ( x )
5137 *x = tm.tmAveCharWidth;
5138 if ( y )
5139 *y = tm.tmHeight + tm.tmExternalLeading;
5140
5141 // the_font.ReleaseResource();
5142 }
5143
5144 // Returns 0 if was a normal ASCII value, not a special key. This indicates that
5145 // the key should be ignored by WM_KEYDOWN and processed by WM_CHAR instead.
5146 int wxCharCodeMSWToWX(int keySym, WXLPARAM lParam)
5147 {
5148 int id;
5149 switch (keySym)
5150 {
5151 case VK_CANCEL: id = WXK_CANCEL; break;
5152 case VK_BACK: id = WXK_BACK; break;
5153 case VK_TAB: id = WXK_TAB; break;
5154 case VK_CLEAR: id = WXK_CLEAR; break;
5155 case VK_SHIFT: id = WXK_SHIFT; break;
5156 case VK_CONTROL: id = WXK_CONTROL; break;
5157 case VK_MENU : id = WXK_ALT; break;
5158 case VK_PAUSE: id = WXK_PAUSE; break;
5159 case VK_CAPITAL: id = WXK_CAPITAL; break;
5160 case VK_SPACE: id = WXK_SPACE; break;
5161 case VK_ESCAPE: id = WXK_ESCAPE; break;
5162 case VK_PRIOR: id = WXK_PRIOR; break;
5163 case VK_NEXT : id = WXK_NEXT; break;
5164 case VK_END: id = WXK_END; break;
5165 case VK_HOME : id = WXK_HOME; break;
5166 case VK_LEFT : id = WXK_LEFT; break;
5167 case VK_UP: id = WXK_UP; break;
5168 case VK_RIGHT: id = WXK_RIGHT; break;
5169 case VK_DOWN : id = WXK_DOWN; break;
5170 case VK_SELECT: id = WXK_SELECT; break;
5171 case VK_PRINT: id = WXK_PRINT; break;
5172 case VK_EXECUTE: id = WXK_EXECUTE; break;
5173 case VK_INSERT: id = WXK_INSERT; break;
5174 case VK_DELETE: id = WXK_DELETE; break;
5175 case VK_HELP : id = WXK_HELP; break;
5176 case VK_NUMPAD0: id = WXK_NUMPAD0; break;
5177 case VK_NUMPAD1: id = WXK_NUMPAD1; break;
5178 case VK_NUMPAD2: id = WXK_NUMPAD2; break;
5179 case VK_NUMPAD3: id = WXK_NUMPAD3; break;
5180 case VK_NUMPAD4: id = WXK_NUMPAD4; break;
5181 case VK_NUMPAD5: id = WXK_NUMPAD5; break;
5182 case VK_NUMPAD6: id = WXK_NUMPAD6; break;
5183 case VK_NUMPAD7: id = WXK_NUMPAD7; break;
5184 case VK_NUMPAD8: id = WXK_NUMPAD8; break;
5185 case VK_NUMPAD9: id = WXK_NUMPAD9; break;
5186 case VK_MULTIPLY: id = WXK_NUMPAD_MULTIPLY; break;
5187 case VK_ADD: id = WXK_NUMPAD_ADD; break;
5188 case VK_SUBTRACT: id = WXK_NUMPAD_SUBTRACT; break;
5189 case VK_DECIMAL: id = WXK_NUMPAD_DECIMAL; break;
5190 case VK_DIVIDE: id = WXK_NUMPAD_DIVIDE; break;
5191 case VK_F1: id = WXK_F1; break;
5192 case VK_F2: id = WXK_F2; break;
5193 case VK_F3: id = WXK_F3; break;
5194 case VK_F4: id = WXK_F4; break;
5195 case VK_F5: id = WXK_F5; break;
5196 case VK_F6: id = WXK_F6; break;
5197 case VK_F7: id = WXK_F7; break;
5198 case VK_F8: id = WXK_F8; break;
5199 case VK_F9: id = WXK_F9; break;
5200 case VK_F10: id = WXK_F10; break;
5201 case VK_F11: id = WXK_F11; break;
5202 case VK_F12: id = WXK_F12; break;
5203 case VK_F13: id = WXK_F13; break;
5204 case VK_F14: id = WXK_F14; break;
5205 case VK_F15: id = WXK_F15; break;
5206 case VK_F16: id = WXK_F16; break;
5207 case VK_F17: id = WXK_F17; break;
5208 case VK_F18: id = WXK_F18; break;
5209 case VK_F19: id = WXK_F19; break;
5210 case VK_F20: id = WXK_F20; break;
5211 case VK_F21: id = WXK_F21; break;
5212 case VK_F22: id = WXK_F22; break;
5213 case VK_F23: id = WXK_F23; break;
5214 case VK_F24: id = WXK_F24; break;
5215 case VK_NUMLOCK: id = WXK_NUMLOCK; break;
5216 case VK_SCROLL: id = WXK_SCROLL; break;
5217
5218 // the mapping for these keys may be incorrect on non-US keyboards so
5219 // maybe we shouldn't map them to ASCII values at all
5220 case VK_OEM_1: id = ';'; break;
5221 case VK_OEM_PLUS: id = '+'; break;
5222 case VK_OEM_COMMA: id = ','; break;
5223 case VK_OEM_MINUS: id = '-'; break;
5224 case VK_OEM_PERIOD: id = '.'; break;
5225 case VK_OEM_2: id = '/'; break;
5226 case VK_OEM_3: id = '~'; break;
5227 case VK_OEM_4: id = '['; break;
5228 case VK_OEM_5: id = '\\'; break;
5229 case VK_OEM_6: id = ']'; break;
5230 case VK_OEM_7: id = '\''; break;
5231
5232 #ifdef VK_APPS
5233 case VK_LWIN: id = WXK_WINDOWS_LEFT; break;
5234 case VK_RWIN: id = WXK_WINDOWS_RIGHT; break;
5235 case VK_APPS: id = WXK_WINDOWS_MENU; break;
5236 #endif // VK_APPS defined
5237
5238 case VK_RETURN:
5239 // the same key is sent for both the "return" key on the main
5240 // keyboard and the numeric keypad but we want to distinguish
5241 // between them: we do this using the "extended" bit (24) of lParam
5242 id = lParam & (1 << 24) ? WXK_NUMPAD_ENTER : WXK_RETURN;
5243 break;
5244
5245 default:
5246 id = 0;
5247 }
5248
5249 return id;
5250 }
5251
5252 WXWORD wxCharCodeWXToMSW(int id, bool *isVirtual)
5253 {
5254 *isVirtual = true;
5255 WXWORD keySym;
5256 switch (id)
5257 {
5258 case WXK_CANCEL: keySym = VK_CANCEL; break;
5259 case WXK_CLEAR: keySym = VK_CLEAR; break;
5260 case WXK_SHIFT: keySym = VK_SHIFT; break;
5261 case WXK_CONTROL: keySym = VK_CONTROL; break;
5262 case WXK_ALT: keySym = VK_MENU; break;
5263 case WXK_PAUSE: keySym = VK_PAUSE; break;
5264 case WXK_CAPITAL: keySym = VK_CAPITAL; break;
5265 case WXK_PRIOR: keySym = VK_PRIOR; break;
5266 case WXK_NEXT : keySym = VK_NEXT; break;
5267 case WXK_END: keySym = VK_END; break;
5268 case WXK_HOME : keySym = VK_HOME; break;
5269 case WXK_LEFT : keySym = VK_LEFT; break;
5270 case WXK_UP: keySym = VK_UP; break;
5271 case WXK_RIGHT: keySym = VK_RIGHT; break;
5272 case WXK_DOWN : keySym = VK_DOWN; break;
5273 case WXK_SELECT: keySym = VK_SELECT; break;
5274 case WXK_PRINT: keySym = VK_PRINT; break;
5275 case WXK_EXECUTE: keySym = VK_EXECUTE; break;
5276 case WXK_INSERT: keySym = VK_INSERT; break;
5277 case WXK_DELETE: keySym = VK_DELETE; break;
5278 case WXK_HELP : keySym = VK_HELP; break;
5279 case WXK_NUMPAD0: keySym = VK_NUMPAD0; break;
5280 case WXK_NUMPAD1: keySym = VK_NUMPAD1; break;
5281 case WXK_NUMPAD2: keySym = VK_NUMPAD2; break;
5282 case WXK_NUMPAD3: keySym = VK_NUMPAD3; break;
5283 case WXK_NUMPAD4: keySym = VK_NUMPAD4; break;
5284 case WXK_NUMPAD5: keySym = VK_NUMPAD5; break;
5285 case WXK_NUMPAD6: keySym = VK_NUMPAD6; break;
5286 case WXK_NUMPAD7: keySym = VK_NUMPAD7; break;
5287 case WXK_NUMPAD8: keySym = VK_NUMPAD8; break;
5288 case WXK_NUMPAD9: keySym = VK_NUMPAD9; break;
5289 case WXK_NUMPAD_MULTIPLY: keySym = VK_MULTIPLY; break;
5290 case WXK_NUMPAD_ADD: keySym = VK_ADD; break;
5291 case WXK_NUMPAD_SUBTRACT: keySym = VK_SUBTRACT; break;
5292 case WXK_NUMPAD_DECIMAL: keySym = VK_DECIMAL; break;
5293 case WXK_NUMPAD_DIVIDE: keySym = VK_DIVIDE; break;
5294 case WXK_F1: keySym = VK_F1; break;
5295 case WXK_F2: keySym = VK_F2; break;
5296 case WXK_F3: keySym = VK_F3; break;
5297 case WXK_F4: keySym = VK_F4; break;
5298 case WXK_F5: keySym = VK_F5; break;
5299 case WXK_F6: keySym = VK_F6; break;
5300 case WXK_F7: keySym = VK_F7; break;
5301 case WXK_F8: keySym = VK_F8; break;
5302 case WXK_F9: keySym = VK_F9; break;
5303 case WXK_F10: keySym = VK_F10; break;
5304 case WXK_F11: keySym = VK_F11; break;
5305 case WXK_F12: keySym = VK_F12; break;
5306 case WXK_F13: keySym = VK_F13; break;
5307 case WXK_F14: keySym = VK_F14; break;
5308 case WXK_F15: keySym = VK_F15; break;
5309 case WXK_F16: keySym = VK_F16; break;
5310 case WXK_F17: keySym = VK_F17; break;
5311 case WXK_F18: keySym = VK_F18; break;
5312 case WXK_F19: keySym = VK_F19; break;
5313 case WXK_F20: keySym = VK_F20; break;
5314 case WXK_F21: keySym = VK_F21; break;
5315 case WXK_F22: keySym = VK_F22; break;
5316 case WXK_F23: keySym = VK_F23; break;
5317 case WXK_F24: keySym = VK_F24; break;
5318 case WXK_NUMLOCK: keySym = VK_NUMLOCK; break;
5319 case WXK_SCROLL: keySym = VK_SCROLL; break;
5320 default:
5321 {
5322 *isVirtual = false;
5323 keySym = (WORD)id;
5324 break;
5325 }
5326 }
5327 return keySym;
5328 }
5329
5330 bool wxGetKeyState(wxKeyCode key)
5331 {
5332 bool bVirtual;
5333
5334 wxASSERT_MSG(key != WXK_LBUTTON && key != WXK_RBUTTON && key !=
5335 WXK_MBUTTON, wxT("can't use wxGetKeyState() for mouse buttons"));
5336
5337 //High order with GetAsyncKeyState only available on WIN32
5338 #ifdef __WIN32__
5339 //If the requested key is a LED key, return
5340 //true if the led is pressed
5341 if (key == WXK_NUMLOCK ||
5342 key == WXK_CAPITAL ||
5343 key == WXK_SCROLL)
5344 {
5345 #endif
5346 //low order bit means LED is highlighted,
5347 //high order means key is down
5348 //Here, for compat with other ports we want both
5349 return GetKeyState( wxCharCodeWXToMSW(key, &bVirtual) ) != 0;
5350
5351 #ifdef __WIN32__
5352 }
5353 else
5354 {
5355 //normal key
5356 //low order bit means key pressed since last call
5357 //high order means key is down
5358 //We want only the high order bit - the key may not be down if only low order
5359 return ( GetAsyncKeyState( wxCharCodeWXToMSW(key, &bVirtual) ) & (1<<15) ) != 0;
5360 }
5361 #endif
5362 }
5363
5364 wxWindow *wxGetActiveWindow()
5365 {
5366 HWND hWnd = GetActiveWindow();
5367 if ( hWnd != 0 )
5368 {
5369 return wxFindWinFromHandle((WXHWND) hWnd);
5370 }
5371 return NULL;
5372 }
5373
5374 extern wxWindow *wxGetWindowFromHWND(WXHWND hWnd)
5375 {
5376 HWND hwnd = (HWND)hWnd;
5377
5378 // For a radiobutton, we get the radiobox from GWL_USERDATA (which is set
5379 // by code in msw/radiobox.cpp), for all the others we just search up the
5380 // window hierarchy
5381 wxWindow *win = (wxWindow *)NULL;
5382 if ( hwnd )
5383 {
5384 win = wxFindWinFromHandle((WXHWND)hwnd);
5385 if ( !win )
5386 {
5387 #if wxUSE_RADIOBOX
5388 // native radiobuttons return DLGC_RADIOBUTTON here and for any
5389 // wxWindow class which overrides WM_GETDLGCODE processing to
5390 // do it as well, win would be already non NULL
5391 if ( ::SendMessage(hwnd, WM_GETDLGCODE, 0, 0) & DLGC_RADIOBUTTON )
5392 {
5393 win = (wxWindow *)wxGetWindowUserData(hwnd);
5394 }
5395 //else: it's a wxRadioButton, not a radiobutton from wxRadioBox
5396 #endif // wxUSE_RADIOBOX
5397
5398 // spin control text buddy window should be mapped to spin ctrl
5399 // itself so try it too
5400 #if wxUSE_SPINCTRL && !defined(__WXUNIVERSAL__)
5401 if ( !win )
5402 {
5403 win = wxSpinCtrl::GetSpinForTextCtrl((WXHWND)hwnd);
5404 }
5405 #endif // wxUSE_SPINCTRL
5406 }
5407 }
5408
5409 while ( hwnd && !win )
5410 {
5411 // this is a really ugly hack needed to avoid mistakenly returning the
5412 // parent frame wxWindow for the find/replace modeless dialog HWND -
5413 // this, in turn, is needed to call IsDialogMessage() from
5414 // wxApp::ProcessMessage() as for this we must return NULL from here
5415 //
5416 // FIXME: this is clearly not the best way to do it but I think we'll
5417 // need to change HWND <-> wxWindow code more heavily than I can
5418 // do it now to fix it
5419 #ifndef __WXMICROWIN__
5420 if ( ::GetWindow(hwnd, GW_OWNER) )
5421 {
5422 // it's a dialog box, don't go upwards
5423 break;
5424 }
5425 #endif
5426
5427 hwnd = ::GetParent(hwnd);
5428 win = wxFindWinFromHandle((WXHWND)hwnd);
5429 }
5430
5431 return win;
5432 }
5433
5434 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
5435
5436 // Windows keyboard hook. Allows interception of e.g. F1, ESCAPE
5437 // in active frames and dialogs, regardless of where the focus is.
5438 static HHOOK wxTheKeyboardHook = 0;
5439 static FARPROC wxTheKeyboardHookProc = 0;
5440 int APIENTRY _EXPORT
5441 wxKeyboardHook(int nCode, WORD wParam, DWORD lParam);
5442
5443 void wxSetKeyboardHook(bool doIt)
5444 {
5445 if ( doIt )
5446 {
5447 wxTheKeyboardHookProc = MakeProcInstance((FARPROC) wxKeyboardHook, wxGetInstance());
5448 wxTheKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) wxTheKeyboardHookProc, wxGetInstance(),
5449
5450 GetCurrentThreadId()
5451 // (DWORD)GetCurrentProcess()); // This is another possibility. Which is right?
5452 );
5453 }
5454 else
5455 {
5456 UnhookWindowsHookEx(wxTheKeyboardHook);
5457 }
5458 }
5459
5460 int APIENTRY _EXPORT
5461 wxKeyboardHook(int nCode, WORD wParam, DWORD lParam)
5462 {
5463 DWORD hiWord = HIWORD(lParam);
5464 if ( nCode != HC_NOREMOVE && ((hiWord & KF_UP) == 0) )
5465 {
5466 int id = wxCharCodeMSWToWX(wParam, lParam);
5467 if ( id != 0 )
5468 {
5469 wxKeyEvent event(wxEVT_CHAR_HOOK);
5470 if ( (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN )
5471 event.m_altDown = true;
5472
5473 event.SetEventObject(NULL);
5474 event.m_keyCode = id;
5475 event.m_shiftDown = wxIsShiftDown();
5476 event.m_controlDown = wxIsCtrlDown();
5477 #ifndef __WXWINCE__
5478 event.SetTimestamp(::GetMessageTime());
5479 #endif
5480 wxWindow *win = wxGetActiveWindow();
5481 wxEvtHandler *handler;
5482 if ( win )
5483 {
5484 handler = win->GetEventHandler();
5485 event.SetId(win->GetId());
5486 }
5487 else
5488 {
5489 handler = wxTheApp;
5490 event.SetId(wxID_ANY);
5491 }
5492
5493 if ( handler && handler->ProcessEvent(event) )
5494 {
5495 // processed
5496 return 1;
5497 }
5498 }
5499 }
5500
5501 return (int)CallNextHookEx(wxTheKeyboardHook, nCode, wParam, lParam);
5502 }
5503
5504 #endif // !__WXMICROWIN__
5505
5506 #ifdef __WXDEBUG__
5507 const wxChar *wxGetMessageName(int message)
5508 {
5509 switch ( message )
5510 {
5511 case 0x0000: return wxT("WM_NULL");
5512 case 0x0001: return wxT("WM_CREATE");
5513 case 0x0002: return wxT("WM_DESTROY");
5514 case 0x0003: return wxT("WM_MOVE");
5515 case 0x0005: return wxT("WM_SIZE");
5516 case 0x0006: return wxT("WM_ACTIVATE");
5517 case 0x0007: return wxT("WM_SETFOCUS");
5518 case 0x0008: return wxT("WM_KILLFOCUS");
5519 case 0x000A: return wxT("WM_ENABLE");
5520 case 0x000B: return wxT("WM_SETREDRAW");
5521 case 0x000C: return wxT("WM_SETTEXT");
5522 case 0x000D: return wxT("WM_GETTEXT");
5523 case 0x000E: return wxT("WM_GETTEXTLENGTH");
5524 case 0x000F: return wxT("WM_PAINT");
5525 case 0x0010: return wxT("WM_CLOSE");
5526 case 0x0011: return wxT("WM_QUERYENDSESSION");
5527 case 0x0012: return wxT("WM_QUIT");
5528 case 0x0013: return wxT("WM_QUERYOPEN");
5529 case 0x0014: return wxT("WM_ERASEBKGND");
5530 case 0x0015: return wxT("WM_SYSCOLORCHANGE");
5531 case 0x0016: return wxT("WM_ENDSESSION");
5532 case 0x0017: return wxT("WM_SYSTEMERROR");
5533 case 0x0018: return wxT("WM_SHOWWINDOW");
5534 case 0x0019: return wxT("WM_CTLCOLOR");
5535 case 0x001A: return wxT("WM_WININICHANGE");
5536 case 0x001B: return wxT("WM_DEVMODECHANGE");
5537 case 0x001C: return wxT("WM_ACTIVATEAPP");
5538 case 0x001D: return wxT("WM_FONTCHANGE");
5539 case 0x001E: return wxT("WM_TIMECHANGE");
5540 case 0x001F: return wxT("WM_CANCELMODE");
5541 case 0x0020: return wxT("WM_SETCURSOR");
5542 case 0x0021: return wxT("WM_MOUSEACTIVATE");
5543 case 0x0022: return wxT("WM_CHILDACTIVATE");
5544 case 0x0023: return wxT("WM_QUEUESYNC");
5545 case 0x0024: return wxT("WM_GETMINMAXINFO");
5546 case 0x0026: return wxT("WM_PAINTICON");
5547 case 0x0027: return wxT("WM_ICONERASEBKGND");
5548 case 0x0028: return wxT("WM_NEXTDLGCTL");
5549 case 0x002A: return wxT("WM_SPOOLERSTATUS");
5550 case 0x002B: return wxT("WM_DRAWITEM");
5551 case 0x002C: return wxT("WM_MEASUREITEM");
5552 case 0x002D: return wxT("WM_DELETEITEM");
5553 case 0x002E: return wxT("WM_VKEYTOITEM");
5554 case 0x002F: return wxT("WM_CHARTOITEM");
5555 case 0x0030: return wxT("WM_SETFONT");
5556 case 0x0031: return wxT("WM_GETFONT");
5557 case 0x0037: return wxT("WM_QUERYDRAGICON");
5558 case 0x0039: return wxT("WM_COMPAREITEM");
5559 case 0x0041: return wxT("WM_COMPACTING");
5560 case 0x0044: return wxT("WM_COMMNOTIFY");
5561 case 0x0046: return wxT("WM_WINDOWPOSCHANGING");
5562 case 0x0047: return wxT("WM_WINDOWPOSCHANGED");
5563 case 0x0048: return wxT("WM_POWER");
5564
5565 case 0x004A: return wxT("WM_COPYDATA");
5566 case 0x004B: return wxT("WM_CANCELJOURNAL");
5567 case 0x004E: return wxT("WM_NOTIFY");
5568 case 0x0050: return wxT("WM_INPUTLANGCHANGEREQUEST");
5569 case 0x0051: return wxT("WM_INPUTLANGCHANGE");
5570 case 0x0052: return wxT("WM_TCARD");
5571 case 0x0053: return wxT("WM_HELP");
5572 case 0x0054: return wxT("WM_USERCHANGED");
5573 case 0x0055: return wxT("WM_NOTIFYFORMAT");
5574 case 0x007B: return wxT("WM_CONTEXTMENU");
5575 case 0x007C: return wxT("WM_STYLECHANGING");
5576 case 0x007D: return wxT("WM_STYLECHANGED");
5577 case 0x007E: return wxT("WM_DISPLAYCHANGE");
5578 case 0x007F: return wxT("WM_GETICON");
5579 case 0x0080: return wxT("WM_SETICON");
5580
5581 case 0x0081: return wxT("WM_NCCREATE");
5582 case 0x0082: return wxT("WM_NCDESTROY");
5583 case 0x0083: return wxT("WM_NCCALCSIZE");
5584 case 0x0084: return wxT("WM_NCHITTEST");
5585 case 0x0085: return wxT("WM_NCPAINT");
5586 case 0x0086: return wxT("WM_NCACTIVATE");
5587 case 0x0087: return wxT("WM_GETDLGCODE");
5588 case 0x00A0: return wxT("WM_NCMOUSEMOVE");
5589 case 0x00A1: return wxT("WM_NCLBUTTONDOWN");
5590 case 0x00A2: return wxT("WM_NCLBUTTONUP");
5591 case 0x00A3: return wxT("WM_NCLBUTTONDBLCLK");
5592 case 0x00A4: return wxT("WM_NCRBUTTONDOWN");
5593 case 0x00A5: return wxT("WM_NCRBUTTONUP");
5594 case 0x00A6: return wxT("WM_NCRBUTTONDBLCLK");
5595 case 0x00A7: return wxT("WM_NCMBUTTONDOWN");
5596 case 0x00A8: return wxT("WM_NCMBUTTONUP");
5597 case 0x00A9: return wxT("WM_NCMBUTTONDBLCLK");
5598 case 0x0100: return wxT("WM_KEYDOWN");
5599 case 0x0101: return wxT("WM_KEYUP");
5600 case 0x0102: return wxT("WM_CHAR");
5601 case 0x0103: return wxT("WM_DEADCHAR");
5602 case 0x0104: return wxT("WM_SYSKEYDOWN");
5603 case 0x0105: return wxT("WM_SYSKEYUP");
5604 case 0x0106: return wxT("WM_SYSCHAR");
5605 case 0x0107: return wxT("WM_SYSDEADCHAR");
5606 case 0x0108: return wxT("WM_KEYLAST");
5607
5608 case 0x010D: return wxT("WM_IME_STARTCOMPOSITION");
5609 case 0x010E: return wxT("WM_IME_ENDCOMPOSITION");
5610 case 0x010F: return wxT("WM_IME_COMPOSITION");
5611
5612 case 0x0110: return wxT("WM_INITDIALOG");
5613 case 0x0111: return wxT("WM_COMMAND");
5614 case 0x0112: return wxT("WM_SYSCOMMAND");
5615 case 0x0113: return wxT("WM_TIMER");
5616 case 0x0114: return wxT("WM_HSCROLL");
5617 case 0x0115: return wxT("WM_VSCROLL");
5618 case 0x0116: return wxT("WM_INITMENU");
5619 case 0x0117: return wxT("WM_INITMENUPOPUP");
5620 case 0x011F: return wxT("WM_MENUSELECT");
5621 case 0x0120: return wxT("WM_MENUCHAR");
5622 case 0x0121: return wxT("WM_ENTERIDLE");
5623 case 0x0200: return wxT("WM_MOUSEMOVE");
5624 case 0x0201: return wxT("WM_LBUTTONDOWN");
5625 case 0x0202: return wxT("WM_LBUTTONUP");
5626 case 0x0203: return wxT("WM_LBUTTONDBLCLK");
5627 case 0x0204: return wxT("WM_RBUTTONDOWN");
5628 case 0x0205: return wxT("WM_RBUTTONUP");
5629 case 0x0206: return wxT("WM_RBUTTONDBLCLK");
5630 case 0x0207: return wxT("WM_MBUTTONDOWN");
5631 case 0x0208: return wxT("WM_MBUTTONUP");
5632 case 0x0209: return wxT("WM_MBUTTONDBLCLK");
5633 case 0x020A: return wxT("WM_MOUSEWHEEL");
5634 case 0x0210: return wxT("WM_PARENTNOTIFY");
5635 case 0x0211: return wxT("WM_ENTERMENULOOP");
5636 case 0x0212: return wxT("WM_EXITMENULOOP");
5637
5638 case 0x0213: return wxT("WM_NEXTMENU");
5639 case 0x0214: return wxT("WM_SIZING");
5640 case 0x0215: return wxT("WM_CAPTURECHANGED");
5641 case 0x0216: return wxT("WM_MOVING");
5642 case 0x0218: return wxT("WM_POWERBROADCAST");
5643 case 0x0219: return wxT("WM_DEVICECHANGE");
5644
5645 case 0x0220: return wxT("WM_MDICREATE");
5646 case 0x0221: return wxT("WM_MDIDESTROY");
5647 case 0x0222: return wxT("WM_MDIACTIVATE");
5648 case 0x0223: return wxT("WM_MDIRESTORE");
5649 case 0x0224: return wxT("WM_MDINEXT");
5650 case 0x0225: return wxT("WM_MDIMAXIMIZE");
5651 case 0x0226: return wxT("WM_MDITILE");
5652 case 0x0227: return wxT("WM_MDICASCADE");
5653 case 0x0228: return wxT("WM_MDIICONARRANGE");
5654 case 0x0229: return wxT("WM_MDIGETACTIVE");
5655 case 0x0230: return wxT("WM_MDISETMENU");
5656 case 0x0233: return wxT("WM_DROPFILES");
5657
5658 case 0x0281: return wxT("WM_IME_SETCONTEXT");
5659 case 0x0282: return wxT("WM_IME_NOTIFY");
5660 case 0x0283: return wxT("WM_IME_CONTROL");
5661 case 0x0284: return wxT("WM_IME_COMPOSITIONFULL");
5662 case 0x0285: return wxT("WM_IME_SELECT");
5663 case 0x0286: return wxT("WM_IME_CHAR");
5664 case 0x0290: return wxT("WM_IME_KEYDOWN");
5665 case 0x0291: return wxT("WM_IME_KEYUP");
5666
5667 case 0x0300: return wxT("WM_CUT");
5668 case 0x0301: return wxT("WM_COPY");
5669 case 0x0302: return wxT("WM_PASTE");
5670 case 0x0303: return wxT("WM_CLEAR");
5671 case 0x0304: return wxT("WM_UNDO");
5672 case 0x0305: return wxT("WM_RENDERFORMAT");
5673 case 0x0306: return wxT("WM_RENDERALLFORMATS");
5674 case 0x0307: return wxT("WM_DESTROYCLIPBOARD");
5675 case 0x0308: return wxT("WM_DRAWCLIPBOARD");
5676 case 0x0309: return wxT("WM_PAINTCLIPBOARD");
5677 case 0x030A: return wxT("WM_VSCROLLCLIPBOARD");
5678 case 0x030B: return wxT("WM_SIZECLIPBOARD");
5679 case 0x030C: return wxT("WM_ASKCBFORMATNAME");
5680 case 0x030D: return wxT("WM_CHANGECBCHAIN");
5681 case 0x030E: return wxT("WM_HSCROLLCLIPBOARD");
5682 case 0x030F: return wxT("WM_QUERYNEWPALETTE");
5683 case 0x0310: return wxT("WM_PALETTEISCHANGING");
5684 case 0x0311: return wxT("WM_PALETTECHANGED");
5685 #if wxUSE_HOTKEY
5686 case 0x0312: return wxT("WM_HOTKEY");
5687 #endif
5688
5689 // common controls messages - although they're not strictly speaking
5690 // standard, it's nice to decode them nevertheless
5691
5692 // listview
5693 case 0x1000 + 0: return wxT("LVM_GETBKCOLOR");
5694 case 0x1000 + 1: return wxT("LVM_SETBKCOLOR");
5695 case 0x1000 + 2: return wxT("LVM_GETIMAGELIST");
5696 case 0x1000 + 3: return wxT("LVM_SETIMAGELIST");
5697 case 0x1000 + 4: return wxT("LVM_GETITEMCOUNT");
5698 case 0x1000 + 5: return wxT("LVM_GETITEMA");
5699 case 0x1000 + 75: return wxT("LVM_GETITEMW");
5700 case 0x1000 + 6: return wxT("LVM_SETITEMA");
5701 case 0x1000 + 76: return wxT("LVM_SETITEMW");
5702 case 0x1000 + 7: return wxT("LVM_INSERTITEMA");
5703 case 0x1000 + 77: return wxT("LVM_INSERTITEMW");
5704 case 0x1000 + 8: return wxT("LVM_DELETEITEM");
5705 case 0x1000 + 9: return wxT("LVM_DELETEALLITEMS");
5706 case 0x1000 + 10: return wxT("LVM_GETCALLBACKMASK");
5707 case 0x1000 + 11: return wxT("LVM_SETCALLBACKMASK");
5708 case 0x1000 + 12: return wxT("LVM_GETNEXTITEM");
5709 case 0x1000 + 13: return wxT("LVM_FINDITEMA");
5710 case 0x1000 + 83: return wxT("LVM_FINDITEMW");
5711 case 0x1000 + 14: return wxT("LVM_GETITEMRECT");
5712 case 0x1000 + 15: return wxT("LVM_SETITEMPOSITION");
5713 case 0x1000 + 16: return wxT("LVM_GETITEMPOSITION");
5714 case 0x1000 + 17: return wxT("LVM_GETSTRINGWIDTHA");
5715 case 0x1000 + 87: return wxT("LVM_GETSTRINGWIDTHW");
5716 case 0x1000 + 18: return wxT("LVM_HITTEST");
5717 case 0x1000 + 19: return wxT("LVM_ENSUREVISIBLE");
5718 case 0x1000 + 20: return wxT("LVM_SCROLL");
5719 case 0x1000 + 21: return wxT("LVM_REDRAWITEMS");
5720 case 0x1000 + 22: return wxT("LVM_ARRANGE");
5721 case 0x1000 + 23: return wxT("LVM_EDITLABELA");
5722 case 0x1000 + 118: return wxT("LVM_EDITLABELW");
5723 case 0x1000 + 24: return wxT("LVM_GETEDITCONTROL");
5724 case 0x1000 + 25: return wxT("LVM_GETCOLUMNA");
5725 case 0x1000 + 95: return wxT("LVM_GETCOLUMNW");
5726 case 0x1000 + 26: return wxT("LVM_SETCOLUMNA");
5727 case 0x1000 + 96: return wxT("LVM_SETCOLUMNW");
5728 case 0x1000 + 27: return wxT("LVM_INSERTCOLUMNA");
5729 case 0x1000 + 97: return wxT("LVM_INSERTCOLUMNW");
5730 case 0x1000 + 28: return wxT("LVM_DELETECOLUMN");
5731 case 0x1000 + 29: return wxT("LVM_GETCOLUMNWIDTH");
5732 case 0x1000 + 30: return wxT("LVM_SETCOLUMNWIDTH");
5733 case 0x1000 + 31: return wxT("LVM_GETHEADER");
5734 case 0x1000 + 33: return wxT("LVM_CREATEDRAGIMAGE");
5735 case 0x1000 + 34: return wxT("LVM_GETVIEWRECT");
5736 case 0x1000 + 35: return wxT("LVM_GETTEXTCOLOR");
5737 case 0x1000 + 36: return wxT("LVM_SETTEXTCOLOR");
5738 case 0x1000 + 37: return wxT("LVM_GETTEXTBKCOLOR");
5739 case 0x1000 + 38: return wxT("LVM_SETTEXTBKCOLOR");
5740 case 0x1000 + 39: return wxT("LVM_GETTOPINDEX");
5741 case 0x1000 + 40: return wxT("LVM_GETCOUNTPERPAGE");
5742 case 0x1000 + 41: return wxT("LVM_GETORIGIN");
5743 case 0x1000 + 42: return wxT("LVM_UPDATE");
5744 case 0x1000 + 43: return wxT("LVM_SETITEMSTATE");
5745 case 0x1000 + 44: return wxT("LVM_GETITEMSTATE");
5746 case 0x1000 + 45: return wxT("LVM_GETITEMTEXTA");
5747 case 0x1000 + 115: return wxT("LVM_GETITEMTEXTW");
5748 case 0x1000 + 46: return wxT("LVM_SETITEMTEXTA");
5749 case 0x1000 + 116: return wxT("LVM_SETITEMTEXTW");
5750 case 0x1000 + 47: return wxT("LVM_SETITEMCOUNT");
5751 case 0x1000 + 48: return wxT("LVM_SORTITEMS");
5752 case 0x1000 + 49: return wxT("LVM_SETITEMPOSITION32");
5753 case 0x1000 + 50: return wxT("LVM_GETSELECTEDCOUNT");
5754 case 0x1000 + 51: return wxT("LVM_GETITEMSPACING");
5755 case 0x1000 + 52: return wxT("LVM_GETISEARCHSTRINGA");
5756 case 0x1000 + 117: return wxT("LVM_GETISEARCHSTRINGW");
5757 case 0x1000 + 53: return wxT("LVM_SETICONSPACING");
5758 case 0x1000 + 54: return wxT("LVM_SETEXTENDEDLISTVIEWSTYLE");
5759 case 0x1000 + 55: return wxT("LVM_GETEXTENDEDLISTVIEWSTYLE");
5760 case 0x1000 + 56: return wxT("LVM_GETSUBITEMRECT");
5761 case 0x1000 + 57: return wxT("LVM_SUBITEMHITTEST");
5762 case 0x1000 + 58: return wxT("LVM_SETCOLUMNORDERARRAY");
5763 case 0x1000 + 59: return wxT("LVM_GETCOLUMNORDERARRAY");
5764 case 0x1000 + 60: return wxT("LVM_SETHOTITEM");
5765 case 0x1000 + 61: return wxT("LVM_GETHOTITEM");
5766 case 0x1000 + 62: return wxT("LVM_SETHOTCURSOR");
5767 case 0x1000 + 63: return wxT("LVM_GETHOTCURSOR");
5768 case 0x1000 + 64: return wxT("LVM_APPROXIMATEVIEWRECT");
5769 case 0x1000 + 65: return wxT("LVM_SETWORKAREA");
5770
5771 // tree view
5772 case 0x1100 + 0: return wxT("TVM_INSERTITEMA");
5773 case 0x1100 + 50: return wxT("TVM_INSERTITEMW");
5774 case 0x1100 + 1: return wxT("TVM_DELETEITEM");
5775 case 0x1100 + 2: return wxT("TVM_EXPAND");
5776 case 0x1100 + 4: return wxT("TVM_GETITEMRECT");
5777 case 0x1100 + 5: return wxT("TVM_GETCOUNT");
5778 case 0x1100 + 6: return wxT("TVM_GETINDENT");
5779 case 0x1100 + 7: return wxT("TVM_SETINDENT");
5780 case 0x1100 + 8: return wxT("TVM_GETIMAGELIST");
5781 case 0x1100 + 9: return wxT("TVM_SETIMAGELIST");
5782 case 0x1100 + 10: return wxT("TVM_GETNEXTITEM");
5783 case 0x1100 + 11: return wxT("TVM_SELECTITEM");
5784 case 0x1100 + 12: return wxT("TVM_GETITEMA");
5785 case 0x1100 + 62: return wxT("TVM_GETITEMW");
5786 case 0x1100 + 13: return wxT("TVM_SETITEMA");
5787 case 0x1100 + 63: return wxT("TVM_SETITEMW");
5788 case 0x1100 + 14: return wxT("TVM_EDITLABELA");
5789 case 0x1100 + 65: return wxT("TVM_EDITLABELW");
5790 case 0x1100 + 15: return wxT("TVM_GETEDITCONTROL");
5791 case 0x1100 + 16: return wxT("TVM_GETVISIBLECOUNT");
5792 case 0x1100 + 17: return wxT("TVM_HITTEST");
5793 case 0x1100 + 18: return wxT("TVM_CREATEDRAGIMAGE");
5794 case 0x1100 + 19: return wxT("TVM_SORTCHILDREN");
5795 case 0x1100 + 20: return wxT("TVM_ENSUREVISIBLE");
5796 case 0x1100 + 21: return wxT("TVM_SORTCHILDRENCB");
5797 case 0x1100 + 22: return wxT("TVM_ENDEDITLABELNOW");
5798 case 0x1100 + 23: return wxT("TVM_GETISEARCHSTRINGA");
5799 case 0x1100 + 64: return wxT("TVM_GETISEARCHSTRINGW");
5800 case 0x1100 + 24: return wxT("TVM_SETTOOLTIPS");
5801 case 0x1100 + 25: return wxT("TVM_GETTOOLTIPS");
5802
5803 // header
5804 case 0x1200 + 0: return wxT("HDM_GETITEMCOUNT");
5805 case 0x1200 + 1: return wxT("HDM_INSERTITEMA");
5806 case 0x1200 + 10: return wxT("HDM_INSERTITEMW");
5807 case 0x1200 + 2: return wxT("HDM_DELETEITEM");
5808 case 0x1200 + 3: return wxT("HDM_GETITEMA");
5809 case 0x1200 + 11: return wxT("HDM_GETITEMW");
5810 case 0x1200 + 4: return wxT("HDM_SETITEMA");
5811 case 0x1200 + 12: return wxT("HDM_SETITEMW");
5812 case 0x1200 + 5: return wxT("HDM_LAYOUT");
5813 case 0x1200 + 6: return wxT("HDM_HITTEST");
5814 case 0x1200 + 7: return wxT("HDM_GETITEMRECT");
5815 case 0x1200 + 8: return wxT("HDM_SETIMAGELIST");
5816 case 0x1200 + 9: return wxT("HDM_GETIMAGELIST");
5817 case 0x1200 + 15: return wxT("HDM_ORDERTOINDEX");
5818 case 0x1200 + 16: return wxT("HDM_CREATEDRAGIMAGE");
5819 case 0x1200 + 17: return wxT("HDM_GETORDERARRAY");
5820 case 0x1200 + 18: return wxT("HDM_SETORDERARRAY");
5821 case 0x1200 + 19: return wxT("HDM_SETHOTDIVIDER");
5822
5823 // tab control
5824 case 0x1300 + 2: return wxT("TCM_GETIMAGELIST");
5825 case 0x1300 + 3: return wxT("TCM_SETIMAGELIST");
5826 case 0x1300 + 4: return wxT("TCM_GETITEMCOUNT");
5827 case 0x1300 + 5: return wxT("TCM_GETITEMA");
5828 case 0x1300 + 60: return wxT("TCM_GETITEMW");
5829 case 0x1300 + 6: return wxT("TCM_SETITEMA");
5830 case 0x1300 + 61: return wxT("TCM_SETITEMW");
5831 case 0x1300 + 7: return wxT("TCM_INSERTITEMA");
5832 case 0x1300 + 62: return wxT("TCM_INSERTITEMW");
5833 case 0x1300 + 8: return wxT("TCM_DELETEITEM");
5834 case 0x1300 + 9: return wxT("TCM_DELETEALLITEMS");
5835 case 0x1300 + 10: return wxT("TCM_GETITEMRECT");
5836 case 0x1300 + 11: return wxT("TCM_GETCURSEL");
5837 case 0x1300 + 12: return wxT("TCM_SETCURSEL");
5838 case 0x1300 + 13: return wxT("TCM_HITTEST");
5839 case 0x1300 + 14: return wxT("TCM_SETITEMEXTRA");
5840 case 0x1300 + 40: return wxT("TCM_ADJUSTRECT");
5841 case 0x1300 + 41: return wxT("TCM_SETITEMSIZE");
5842 case 0x1300 + 42: return wxT("TCM_REMOVEIMAGE");
5843 case 0x1300 + 43: return wxT("TCM_SETPADDING");
5844 case 0x1300 + 44: return wxT("TCM_GETROWCOUNT");
5845 case 0x1300 + 45: return wxT("TCM_GETTOOLTIPS");
5846 case 0x1300 + 46: return wxT("TCM_SETTOOLTIPS");
5847 case 0x1300 + 47: return wxT("TCM_GETCURFOCUS");
5848 case 0x1300 + 48: return wxT("TCM_SETCURFOCUS");
5849 case 0x1300 + 49: return wxT("TCM_SETMINTABWIDTH");
5850 case 0x1300 + 50: return wxT("TCM_DESELECTALL");
5851
5852 // toolbar
5853 case WM_USER+1: return wxT("TB_ENABLEBUTTON");
5854 case WM_USER+2: return wxT("TB_CHECKBUTTON");
5855 case WM_USER+3: return wxT("TB_PRESSBUTTON");
5856 case WM_USER+4: return wxT("TB_HIDEBUTTON");
5857 case WM_USER+5: return wxT("TB_INDETERMINATE");
5858 case WM_USER+9: return wxT("TB_ISBUTTONENABLED");
5859 case WM_USER+10: return wxT("TB_ISBUTTONCHECKED");
5860 case WM_USER+11: return wxT("TB_ISBUTTONPRESSED");
5861 case WM_USER+12: return wxT("TB_ISBUTTONHIDDEN");
5862 case WM_USER+13: return wxT("TB_ISBUTTONINDETERMINATE");
5863 case WM_USER+17: return wxT("TB_SETSTATE");
5864 case WM_USER+18: return wxT("TB_GETSTATE");
5865 case WM_USER+19: return wxT("TB_ADDBITMAP");
5866 case WM_USER+20: return wxT("TB_ADDBUTTONS");
5867 case WM_USER+21: return wxT("TB_INSERTBUTTON");
5868 case WM_USER+22: return wxT("TB_DELETEBUTTON");
5869 case WM_USER+23: return wxT("TB_GETBUTTON");
5870 case WM_USER+24: return wxT("TB_BUTTONCOUNT");
5871 case WM_USER+25: return wxT("TB_COMMANDTOINDEX");
5872 case WM_USER+26: return wxT("TB_SAVERESTOREA");
5873 case WM_USER+76: return wxT("TB_SAVERESTOREW");
5874 case WM_USER+27: return wxT("TB_CUSTOMIZE");
5875 case WM_USER+28: return wxT("TB_ADDSTRINGA");
5876 case WM_USER+77: return wxT("TB_ADDSTRINGW");
5877 case WM_USER+29: return wxT("TB_GETITEMRECT");
5878 case WM_USER+30: return wxT("TB_BUTTONSTRUCTSIZE");
5879 case WM_USER+31: return wxT("TB_SETBUTTONSIZE");
5880 case WM_USER+32: return wxT("TB_SETBITMAPSIZE");
5881 case WM_USER+33: return wxT("TB_AUTOSIZE");
5882 case WM_USER+35: return wxT("TB_GETTOOLTIPS");
5883 case WM_USER+36: return wxT("TB_SETTOOLTIPS");
5884 case WM_USER+37: return wxT("TB_SETPARENT");
5885 case WM_USER+39: return wxT("TB_SETROWS");
5886 case WM_USER+40: return wxT("TB_GETROWS");
5887 case WM_USER+42: return wxT("TB_SETCMDID");
5888 case WM_USER+43: return wxT("TB_CHANGEBITMAP");
5889 case WM_USER+44: return wxT("TB_GETBITMAP");
5890 case WM_USER+45: return wxT("TB_GETBUTTONTEXTA");
5891 case WM_USER+75: return wxT("TB_GETBUTTONTEXTW");
5892 case WM_USER+46: return wxT("TB_REPLACEBITMAP");
5893 case WM_USER+47: return wxT("TB_SETINDENT");
5894 case WM_USER+48: return wxT("TB_SETIMAGELIST");
5895 case WM_USER+49: return wxT("TB_GETIMAGELIST");
5896 case WM_USER+50: return wxT("TB_LOADIMAGES");
5897 case WM_USER+51: return wxT("TB_GETRECT");
5898 case WM_USER+52: return wxT("TB_SETHOTIMAGELIST");
5899 case WM_USER+53: return wxT("TB_GETHOTIMAGELIST");
5900 case WM_USER+54: return wxT("TB_SETDISABLEDIMAGELIST");
5901 case WM_USER+55: return wxT("TB_GETDISABLEDIMAGELIST");
5902 case WM_USER+56: return wxT("TB_SETSTYLE");
5903 case WM_USER+57: return wxT("TB_GETSTYLE");
5904 case WM_USER+58: return wxT("TB_GETBUTTONSIZE");
5905 case WM_USER+59: return wxT("TB_SETBUTTONWIDTH");
5906 case WM_USER+60: return wxT("TB_SETMAXTEXTROWS");
5907 case WM_USER+61: return wxT("TB_GETTEXTROWS");
5908 case WM_USER+41: return wxT("TB_GETBITMAPFLAGS");
5909
5910 default:
5911 static wxString s_szBuf;
5912 s_szBuf.Printf(wxT("<unknown message = %d>"), message);
5913 return s_szBuf.c_str();
5914 }
5915 }
5916 #endif //__WXDEBUG__
5917
5918 static TEXTMETRIC wxGetTextMetrics(const wxWindowMSW *win)
5919 {
5920 // prepare the DC
5921 TEXTMETRIC tm;
5922 HWND hwnd = GetHwndOf(win);
5923 HDC hdc = ::GetDC(hwnd);
5924
5925 #if !wxDIALOG_UNIT_COMPATIBILITY
5926 // and select the current font into it
5927 HFONT hfont = GetHfontOf(win->GetFont());
5928 if ( hfont )
5929 {
5930 hfont = (HFONT)::SelectObject(hdc, hfont);
5931 }
5932 #endif
5933
5934 // finally retrieve the text metrics from it
5935 GetTextMetrics(hdc, &tm);
5936
5937 #if !wxDIALOG_UNIT_COMPATIBILITY
5938 // and clean up
5939 if ( hfont )
5940 {
5941 (void)::SelectObject(hdc, hfont);
5942 }
5943 #endif
5944
5945 ::ReleaseDC(hwnd, hdc);
5946
5947 return tm;
5948 }
5949
5950 // Find the wxWindow at the current mouse position, returning the mouse
5951 // position.
5952 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
5953 {
5954 pt = wxGetMousePosition();
5955 return wxFindWindowAtPoint(pt);
5956 }
5957
5958 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
5959 {
5960 POINT pt2;
5961 pt2.x = pt.x;
5962 pt2.y = pt.y;
5963 HWND hWndHit = ::WindowFromPoint(pt2);
5964
5965 wxWindow* win = wxFindWinFromHandle((WXHWND) hWndHit) ;
5966 HWND hWnd = hWndHit;
5967
5968 // Try to find a window with a wxWindow associated with it
5969 while (!win && (hWnd != 0))
5970 {
5971 hWnd = ::GetParent(hWnd);
5972 win = wxFindWinFromHandle((WXHWND) hWnd) ;
5973 }
5974 return win;
5975 }
5976
5977 // Get the current mouse position.
5978 wxPoint wxGetMousePosition()
5979 {
5980 POINT pt;
5981 #ifdef __WXWINCE__
5982 GetCursorPosWinCE(&pt);
5983 #else
5984 GetCursorPos( & pt );
5985 #endif
5986
5987 return wxPoint(pt.x, pt.y);
5988 }
5989
5990 #if wxUSE_HOTKEY
5991
5992 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
5993 static void WinCEUnregisterHotKey(int modifiers, int id)
5994 {
5995 // Register hotkeys for the hardware buttons
5996 HINSTANCE hCoreDll;
5997 typedef BOOL (WINAPI *UnregisterFunc1Proc)(UINT, UINT);
5998
5999 UnregisterFunc1Proc procUnregisterFunc;
6000 hCoreDll = LoadLibrary(_T("coredll.dll"));
6001 if (hCoreDll)
6002 {
6003 procUnregisterFunc = (UnregisterFunc1Proc)GetProcAddress(hCoreDll, _T("UnregisterFunc1"));
6004 if (procUnregisterFunc)
6005 procUnregisterFunc(modifiers, id);
6006 FreeLibrary(hCoreDll);
6007 }
6008 }
6009 #endif
6010
6011 bool wxWindowMSW::RegisterHotKey(int hotkeyId, int modifiers, int keycode)
6012 {
6013 UINT win_modifiers=0;
6014 if ( modifiers & wxMOD_ALT )
6015 win_modifiers |= MOD_ALT;
6016 if ( modifiers & wxMOD_SHIFT )
6017 win_modifiers |= MOD_SHIFT;
6018 if ( modifiers & wxMOD_CONTROL )
6019 win_modifiers |= MOD_CONTROL;
6020 if ( modifiers & wxMOD_WIN )
6021 win_modifiers |= MOD_WIN;
6022
6023 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
6024 // Required for PPC and Smartphone hardware buttons
6025 if (keycode >= WXK_SPECIAL1 && keycode <= WXK_SPECIAL20)
6026 WinCEUnregisterHotKey(win_modifiers, hotkeyId);
6027 #endif
6028
6029 if ( !::RegisterHotKey(GetHwnd(), hotkeyId, win_modifiers, keycode) )
6030 {
6031 wxLogLastError(_T("RegisterHotKey"));
6032
6033 return false;
6034 }
6035
6036 return true;
6037 }
6038
6039 bool wxWindowMSW::UnregisterHotKey(int hotkeyId)
6040 {
6041 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
6042 WinCEUnregisterHotKey(MOD_WIN, hotkeyId);
6043 #endif
6044
6045 if ( !::UnregisterHotKey(GetHwnd(), hotkeyId) )
6046 {
6047 wxLogLastError(_T("UnregisterHotKey"));
6048
6049 return false;
6050 }
6051
6052 return true;
6053 }
6054
6055 #if wxUSE_ACCEL
6056
6057 bool wxWindowMSW::HandleHotKey(WXWPARAM wParam, WXLPARAM lParam)
6058 {
6059 int hotkeyId = wParam;
6060 int virtualKey = HIWORD(lParam);
6061 int win_modifiers = LOWORD(lParam);
6062
6063 wxKeyEvent event(CreateKeyEvent(wxEVT_HOTKEY, virtualKey, wParam, lParam));
6064 event.SetId(hotkeyId);
6065 event.m_shiftDown = (win_modifiers & MOD_SHIFT) != 0;
6066 event.m_controlDown = (win_modifiers & MOD_CONTROL) != 0;
6067 event.m_altDown = (win_modifiers & MOD_ALT) != 0;
6068 event.m_metaDown = (win_modifiers & MOD_WIN) != 0;
6069
6070 return GetEventHandler()->ProcessEvent(event);
6071 }
6072
6073 #endif // wxUSE_ACCEL
6074
6075 #endif // wxUSE_HOTKEY
6076
6077 // Not tested under WinCE
6078 #ifndef __WXWINCE__
6079
6080 // this class installs a message hook which really wakes up our idle processing
6081 // each time a WM_NULL is received (wxWakeUpIdle does this), even if we're
6082 // sitting inside a local modal loop (e.g. a menu is opened or scrollbar is
6083 // being dragged or even inside ::MessageBox()) and so don't control message
6084 // dispatching otherwise
6085 class wxIdleWakeUpModule : public wxModule
6086 {
6087 public:
6088 virtual bool OnInit()
6089 {
6090 ms_hMsgHookProc = ::SetWindowsHookEx
6091 (
6092 WH_GETMESSAGE,
6093 &wxIdleWakeUpModule::MsgHookProc,
6094 NULL,
6095 GetCurrentThreadId()
6096 );
6097
6098 if ( !ms_hMsgHookProc )
6099 {
6100 wxLogLastError(_T("SetWindowsHookEx(WH_GETMESSAGE)"));
6101
6102 return false;
6103 }
6104
6105 return true;
6106 }
6107
6108 virtual void OnExit()
6109 {
6110 ::UnhookWindowsHookEx(wxIdleWakeUpModule::ms_hMsgHookProc);
6111 }
6112
6113 static LRESULT CALLBACK MsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
6114 {
6115 MSG *msg = (MSG*)lParam;
6116
6117 // only process the message if it is actually going to be removed from
6118 // the message queue, this prevents that the same event from being
6119 // processed multiple times if now someone just called PeekMessage()
6120 if ( msg->message == WM_NULL && wParam == PM_REMOVE )
6121 {
6122 wxTheApp->ProcessPendingEvents();
6123 }
6124
6125 return CallNextHookEx(ms_hMsgHookProc, nCode, wParam, lParam);
6126 };
6127
6128 private:
6129 static HHOOK ms_hMsgHookProc;
6130
6131 DECLARE_DYNAMIC_CLASS(wxIdleWakeUpModule)
6132 };
6133
6134 HHOOK wxIdleWakeUpModule::ms_hMsgHookProc = 0;
6135
6136 IMPLEMENT_DYNAMIC_CLASS(wxIdleWakeUpModule, wxModule)
6137
6138 #endif // __WXWINCE__
6139
6140 #ifdef __WXWINCE__
6141
6142 #if wxUSE_STATBOX
6143 static void wxAdjustZOrder(wxWindow* parent)
6144 {
6145 if (parent->IsKindOf(CLASSINFO(wxStaticBox)))
6146 {
6147 // Set the z-order correctly
6148 SetWindowPos((HWND) parent->GetHWND(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
6149 }
6150
6151 wxWindowList::compatibility_iterator current = parent->GetChildren().GetFirst();
6152 while (current)
6153 {
6154 wxWindow *childWin = current->GetData();
6155 wxAdjustZOrder(childWin);
6156 current = current->GetNext();
6157 }
6158 }
6159 #endif
6160
6161 // We need to adjust the z-order of static boxes in WinCE, to
6162 // make 'contained' controls visible
6163 void wxWindowMSW::OnInitDialog( wxInitDialogEvent& event )
6164 {
6165 #if wxUSE_STATBOX
6166 wxAdjustZOrder(this);
6167 #endif
6168
6169 event.Skip();
6170 }
6171 #endif
6172