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