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