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