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