]> git.saurik.com Git - wxWidgets.git/blob - src/msw/window.cpp
wxXmlNode::GetAttribute's pointer argument must not be NULL, check for it
[wxWidgets.git] / src / msw / window.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/window.cpp
3 // Purpose: wxWindowMSW
4 // Author: Julian Smart
5 // Modified by: VZ on 13.05.99: no more Default(), MSWOnXXX() reorganisation
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/window.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/msw/wrapwin.h"
31 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/msw/missing.h"
33 #include "wx/accel.h"
34 #include "wx/menu.h"
35 #include "wx/dc.h"
36 #include "wx/dcclient.h"
37 #include "wx/dcmemory.h"
38 #include "wx/utils.h"
39 #include "wx/app.h"
40 #include "wx/layout.h"
41 #include "wx/dialog.h"
42 #include "wx/frame.h"
43 #include "wx/listbox.h"
44 #include "wx/button.h"
45 #include "wx/msgdlg.h"
46 #include "wx/settings.h"
47 #include "wx/statbox.h"
48 #include "wx/sizer.h"
49 #include "wx/intl.h"
50 #include "wx/log.h"
51 #include "wx/textctrl.h"
52 #include "wx/menuitem.h"
53 #include "wx/module.h"
54 #endif
55
56 #if wxUSE_OWNER_DRAWN && !defined(__WXUNIVERSAL__)
57 #include "wx/ownerdrw.h"
58 #endif
59
60 #include "wx/hashmap.h"
61 #include "wx/evtloop.h"
62 #include "wx/power.h"
63 #include "wx/sysopt.h"
64
65 #if wxUSE_DRAG_AND_DROP
66 #include "wx/dnd.h"
67 #endif
68
69 #if wxUSE_ACCESSIBILITY
70 #include "wx/access.h"
71 #include <ole2.h>
72 #include <oleacc.h>
73 #ifndef WM_GETOBJECT
74 #define WM_GETOBJECT 0x003D
75 #endif
76 #ifndef OBJID_CLIENT
77 #define OBJID_CLIENT 0xFFFFFFFC
78 #endif
79 #endif
80
81 #include "wx/msw/private.h"
82
83 #if wxUSE_TOOLTIPS
84 #include "wx/tooltip.h"
85 #endif
86
87 #if wxUSE_CARET
88 #include "wx/caret.h"
89 #endif // wxUSE_CARET
90
91 #if wxUSE_SPINCTRL
92 #include "wx/spinctrl.h"
93 #endif // wxUSE_SPINCTRL
94
95 #include "wx/notebook.h"
96 #include "wx/listctrl.h"
97 #include "wx/dynlib.h"
98
99 #include <string.h>
100
101 #if (!defined(__GNUWIN32_OLD__) && !defined(__WXMICROWIN__) /* && !defined(__WXWINCE__) */ ) || defined(__CYGWIN10__)
102 #include <shellapi.h>
103 #include <mmsystem.h>
104 #endif
105
106 #ifdef __WIN32__
107 #include <windowsx.h>
108 #endif
109
110 #if !defined __WXWINCE__ && !defined NEED_PBT_H
111 #include <pbt.h>
112 #endif
113
114 #if defined(__WXWINCE__)
115 #include "wx/msw/wince/missing.h"
116 #ifdef __POCKETPC__
117 #include <windows.h>
118 #include <shellapi.h>
119 #include <ole2.h>
120 #include <aygshell.h>
121 #endif
122 #endif
123
124 #if wxUSE_UXTHEME
125 #include "wx/msw/uxtheme.h"
126 #define EP_EDITTEXT 1
127 #define ETS_NORMAL 1
128 #define ETS_HOT 2
129 #define ETS_SELECTED 3
130 #define ETS_DISABLED 4
131 #define ETS_FOCUSED 5
132 #define ETS_READONLY 6
133 #define ETS_ASSIST 7
134 #endif
135
136 #if defined(TME_LEAVE) && defined(WM_MOUSELEAVE) && wxUSE_DYNLIB_CLASS
137 #define HAVE_TRACKMOUSEEVENT
138 #endif // everything needed for TrackMouseEvent()
139
140 // if this is set to 1, we use deferred window sizing to reduce flicker when
141 // resizing complicated window hierarchies, but this can in theory result in
142 // different behaviour than the old code so we keep the possibility to use it
143 // by setting this to 0 (in the future this should be removed completely)
144 #ifdef __WXWINCE__
145 #define USE_DEFERRED_SIZING 0
146 #else
147 #define USE_DEFERRED_SIZING 1
148 #endif
149
150 // set this to 1 to filter out duplicate mouse events, e.g. mouse move events
151 // when mouse position didnd't change
152 #ifdef __WXWINCE__
153 #define wxUSE_MOUSEEVENT_HACK 0
154 #else
155 #define wxUSE_MOUSEEVENT_HACK 1
156 #endif
157
158 // not all compilers/platforms have X button related declarations (notably
159 // Windows CE doesn't, and probably some old SDKs don't neither)
160 #ifdef WM_XBUTTONDOWN
161 #define wxHAS_XBUTTON
162 #endif
163
164 // ---------------------------------------------------------------------------
165 // global variables
166 // ---------------------------------------------------------------------------
167
168 #if wxUSE_MENUS_NATIVE
169 wxMenu *wxCurrentPopupMenu = NULL;
170 #endif // wxUSE_MENUS_NATIVE
171
172 #ifdef __WXWINCE__
173 extern wxChar *wxCanvasClassName;
174 #else
175 extern const wxChar *wxCanvasClassName;
176 #endif
177
178 // true if we had already created the std colour map, used by
179 // wxGetStdColourMap() and wxWindow::OnSysColourChanged() (FIXME-MT)
180 static bool gs_hasStdCmap = false;
181
182 // last mouse event information we need to filter out the duplicates
183 #if wxUSE_MOUSEEVENT_HACK
184 static struct MouseEventInfoDummy
185 {
186 // mouse position (in screen coordinates)
187 wxPoint pos;
188
189 // last mouse event type
190 wxEventType type;
191 } gs_lastMouseEvent;
192 #endif // wxUSE_MOUSEEVENT_HACK
193
194 // hash containing the registered handlers for the custom messages
195 WX_DECLARE_HASH_MAP(int, wxWindow::MSWMessageHandler,
196 wxIntegerHash, wxIntegerEqual,
197 MSWMessageHandlers);
198
199 static MSWMessageHandlers gs_messageHandlers;
200
201 // ---------------------------------------------------------------------------
202 // private functions
203 // ---------------------------------------------------------------------------
204
205 // the window proc for all our windows
206 LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message,
207 WPARAM wParam, LPARAM lParam);
208
209
210 #ifdef __WXDEBUG__
211 const wxChar *wxGetMessageName(int message);
212 #endif //__WXDEBUG__
213
214 void wxRemoveHandleAssociation(wxWindowMSW *win);
215 extern void wxAssociateWinWithHandle(HWND hWnd, wxWindowMSW *win);
216 wxWindow *wxFindWinFromHandle(WXHWND hWnd);
217
218 // get the text metrics for the current font
219 static TEXTMETRIC wxGetTextMetrics(const wxWindowMSW *win);
220
221 #ifdef __WXWINCE__
222 // find the window for the mouse event at the specified position
223 static wxWindowMSW *FindWindowForMouseEvent(wxWindowMSW *win, int *x, int *y);
224 #endif // __WXWINCE__
225
226 // wrapper around BringWindowToTop() API
227 static inline void wxBringWindowToTop(HWND hwnd)
228 {
229 #ifdef __WXMICROWIN__
230 // It seems that MicroWindows brings the _parent_ of the window to the top,
231 // which can be the wrong one.
232
233 // activate (set focus to) specified window
234 ::SetFocus(hwnd);
235 #endif
236
237 // raise top level parent to top of z order
238 if (!::SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE))
239 {
240 wxLogLastError(_T("SetWindowPos"));
241 }
242 }
243
244 #ifndef __WXWINCE__
245
246 // ensure that all our parent windows have WS_EX_CONTROLPARENT style
247 static void EnsureParentHasControlParentStyle(wxWindow *parent)
248 {
249 /*
250 If we have WS_EX_CONTROLPARENT flag we absolutely *must* set it for our
251 parent as well as otherwise several Win32 functions using
252 GetNextDlgTabItem() to iterate over all controls such as
253 IsDialogMessage() or DefDlgProc() would enter an infinite loop: indeed,
254 all of them iterate over all the controls starting from the currently
255 focused one and stop iterating when they get back to the focus but
256 unless all parents have WS_EX_CONTROLPARENT bit set, they would never
257 get back to the initial (focused) window: as we do have this style,
258 GetNextDlgTabItem() will leave this window and continue in its parent,
259 but if the parent doesn't have it, it wouldn't recurse inside it later
260 on and so wouldn't have a chance of getting back to this window either.
261 */
262 while ( parent && !parent->IsTopLevel() )
263 {
264 LONG exStyle = ::GetWindowLong(GetHwndOf(parent), GWL_EXSTYLE);
265 if ( !(exStyle & WS_EX_CONTROLPARENT) )
266 {
267 // force the parent to have this style
268 ::SetWindowLong(GetHwndOf(parent), GWL_EXSTYLE,
269 exStyle | WS_EX_CONTROLPARENT);
270 }
271
272 parent = parent->GetParent();
273 }
274 }
275
276 #endif // !__WXWINCE__
277
278 #ifdef __WXWINCE__
279 // On Windows CE, GetCursorPos can return an error, so use this function
280 // instead
281 bool GetCursorPosWinCE(POINT* pt)
282 {
283 if (!GetCursorPos(pt))
284 {
285 DWORD pos = GetMessagePos();
286 pt->x = LOWORD(pos);
287 pt->y = HIWORD(pos);
288 }
289 return true;
290 }
291 #endif
292
293 // ---------------------------------------------------------------------------
294 // event tables
295 // ---------------------------------------------------------------------------
296
297 // in wxUniv/MSW this class is abstract because it doesn't have DoPopupMenu()
298 // method
299 #ifdef __WXUNIVERSAL__
300 IMPLEMENT_ABSTRACT_CLASS(wxWindowMSW, wxWindowBase)
301 #else // __WXMSW__
302 #if wxUSE_EXTENDED_RTTI
303
304 // windows that are created from a parent window during its Create method, eg. spin controls in a calendar controls
305 // must never been streamed out separately otherwise chaos occurs. Right now easiest is to test for negative ids, as
306 // windows with negative ids never can be recreated anyway
307
308 bool wxWindowStreamingCallback( const wxObject *object, wxWriter * , wxPersister * , wxxVariantArray & )
309 {
310 const wxWindow * win = dynamic_cast<const wxWindow*>(object) ;
311 if ( win && win->GetId() < 0 )
312 return false ;
313 return true ;
314 }
315
316 IMPLEMENT_DYNAMIC_CLASS_XTI_CALLBACK(wxWindow, wxWindowBase,"wx/window.h", wxWindowStreamingCallback)
317
318 // make wxWindowList known before the property is used
319
320 wxCOLLECTION_TYPE_INFO( wxWindow* , wxWindowList ) ;
321
322 template<> void wxCollectionToVariantArray( wxWindowList const &theList, wxxVariantArray &value)
323 {
324 wxListCollectionToVariantArray<wxWindowList::compatibility_iterator>( theList , value ) ;
325 }
326
327 WX_DEFINE_FLAGS( wxWindowStyle )
328
329 wxBEGIN_FLAGS( wxWindowStyle )
330 // new style border flags, we put them first to
331 // use them for streaming out
332
333 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
334 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
335 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
336 wxFLAGS_MEMBER(wxBORDER_RAISED)
337 wxFLAGS_MEMBER(wxBORDER_STATIC)
338 wxFLAGS_MEMBER(wxBORDER_NONE)
339
340 // old style border flags
341 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
342 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
343 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
344 wxFLAGS_MEMBER(wxRAISED_BORDER)
345 wxFLAGS_MEMBER(wxSTATIC_BORDER)
346 wxFLAGS_MEMBER(wxBORDER)
347
348 // standard window styles
349 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
350 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
351 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
352 wxFLAGS_MEMBER(wxWANTS_CHARS)
353 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
354 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
355 wxFLAGS_MEMBER(wxVSCROLL)
356 wxFLAGS_MEMBER(wxHSCROLL)
357
358 wxEND_FLAGS( wxWindowStyle )
359
360 wxBEGIN_PROPERTIES_TABLE(wxWindow)
361 wxEVENT_PROPERTY( Close , wxEVT_CLOSE_WINDOW , wxCloseEvent)
362 wxEVENT_PROPERTY( Create , wxEVT_CREATE , wxWindowCreateEvent )
363 wxEVENT_PROPERTY( Destroy , wxEVT_DESTROY , wxWindowDestroyEvent )
364 // Always constructor Properties first
365
366 wxREADONLY_PROPERTY( Parent,wxWindow*, GetParent, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
367 wxPROPERTY( Id,wxWindowID, SetId, GetId, -1 /*wxID_ANY*/ , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
368 wxPROPERTY( Position,wxPoint, SetPosition , GetPosition, wxDefaultPosition , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // pos
369 wxPROPERTY( Size,wxSize, SetSize, GetSize, wxDefaultSize , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // size
370 wxPROPERTY( WindowStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
371
372 // Then all relations of the object graph
373
374 wxREADONLY_PROPERTY_COLLECTION( Children , wxWindowList , wxWindowBase* , GetWindowChildren , wxPROP_OBJECT_GRAPH /*flags*/ , wxT("Helpstring") , wxT("group"))
375
376 // and finally all other properties
377
378 wxPROPERTY( ExtraStyle , long , SetExtraStyle , GetExtraStyle , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // extstyle
379 wxPROPERTY( BackgroundColour , wxColour , SetBackgroundColour , GetBackgroundColour , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // bg
380 wxPROPERTY( ForegroundColour , wxColour , SetForegroundColour , GetForegroundColour , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // fg
381 wxPROPERTY( Enabled , bool , Enable , IsEnabled , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
382 wxPROPERTY( Shown , bool , Show , IsShown , wxxVariant((bool)true) , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
383 #if 0
384 // possible property candidates (not in xrc) or not valid in all subclasses
385 wxPROPERTY( Title,wxString, SetTitle, GetTitle, wxEmptyString )
386 wxPROPERTY( Font , wxFont , SetFont , GetWindowFont , )
387 wxPROPERTY( Label,wxString, SetLabel, GetLabel, wxEmptyString )
388 // MaxHeight, Width , MinHeight , Width
389 // TODO switch label to control and title to toplevels
390
391 wxPROPERTY( ThemeEnabled , bool , SetThemeEnabled , GetThemeEnabled , )
392 //wxPROPERTY( Cursor , wxCursor , SetCursor , GetCursor , )
393 // wxPROPERTY( ToolTip , wxString , SetToolTip , GetToolTipText , )
394 wxPROPERTY( AutoLayout , bool , SetAutoLayout , GetAutoLayout , )
395
396
397
398 #endif
399 wxEND_PROPERTIES_TABLE()
400
401 wxBEGIN_HANDLERS_TABLE(wxWindow)
402 wxEND_HANDLERS_TABLE()
403
404 wxCONSTRUCTOR_DUMMY(wxWindow)
405
406 #else
407 IMPLEMENT_DYNAMIC_CLASS(wxWindow, wxWindowBase)
408 #endif
409 #endif // __WXUNIVERSAL__/__WXMSW__
410
411 BEGIN_EVENT_TABLE(wxWindowMSW, wxWindowBase)
412 EVT_SYS_COLOUR_CHANGED(wxWindowMSW::OnSysColourChanged)
413 EVT_ERASE_BACKGROUND(wxWindowMSW::OnEraseBackground)
414 #ifdef __WXWINCE__
415 EVT_INIT_DIALOG(wxWindowMSW::OnInitDialog)
416 #endif
417 END_EVENT_TABLE()
418
419 // ===========================================================================
420 // implementation
421 // ===========================================================================
422
423 // ---------------------------------------------------------------------------
424 // wxWindow utility functions
425 // ---------------------------------------------------------------------------
426
427 // Find an item given the MS Windows id
428 wxWindow *wxWindowMSW::FindItem(long id) const
429 {
430 #if wxUSE_CONTROLS
431 wxControl *item = wxDynamicCastThis(wxControl);
432 if ( item )
433 {
434 // is it us or one of our "internal" children?
435 if ( item->GetId() == id
436 #ifndef __WXUNIVERSAL__
437 || (item->GetSubcontrols().Index(id) != wxNOT_FOUND)
438 #endif // __WXUNIVERSAL__
439 )
440 {
441 return item;
442 }
443 }
444 #endif // wxUSE_CONTROLS
445
446 wxWindowList::compatibility_iterator current = GetChildren().GetFirst();
447 while (current)
448 {
449 wxWindow *childWin = current->GetData();
450
451 wxWindow *wnd = childWin->FindItem(id);
452 if ( wnd )
453 return wnd;
454
455 current = current->GetNext();
456 }
457
458 return NULL;
459 }
460
461 // Find an item given the MS Windows handle
462 wxWindow *wxWindowMSW::FindItemByHWND(WXHWND hWnd, bool controlOnly) const
463 {
464 wxWindowList::compatibility_iterator current = GetChildren().GetFirst();
465 while (current)
466 {
467 wxWindow *parent = current->GetData();
468
469 // Do a recursive search.
470 wxWindow *wnd = parent->FindItemByHWND(hWnd);
471 if ( wnd )
472 return wnd;
473
474 if ( !controlOnly
475 #if wxUSE_CONTROLS
476 || parent->IsKindOf(CLASSINFO(wxControl))
477 #endif // wxUSE_CONTROLS
478 )
479 {
480 wxWindow *item = current->GetData();
481 if ( item->GetHWND() == hWnd )
482 return item;
483 else
484 {
485 if ( item->ContainsHWND(hWnd) )
486 return item;
487 }
488 }
489
490 current = current->GetNext();
491 }
492 return NULL;
493 }
494
495 // Default command handler
496 bool wxWindowMSW::MSWCommand(WXUINT WXUNUSED(param), WXWORD WXUNUSED(id))
497 {
498 return false;
499 }
500
501 // ----------------------------------------------------------------------------
502 // constructors and such
503 // ----------------------------------------------------------------------------
504
505 void wxWindowMSW::Init()
506 {
507 // MSW specific
508 m_isBeingDeleted = false;
509 m_oldWndProc = NULL;
510 m_mouseInWindow = false;
511 m_lastKeydownProcessed = false;
512
513 m_frozenness = 0;
514
515 m_hWnd = 0;
516 m_hDWP = 0;
517
518 m_xThumbSize = 0;
519 m_yThumbSize = 0;
520
521 m_pendingPosition = wxDefaultPosition;
522 m_pendingSize = wxDefaultSize;
523
524 #ifdef __POCKETPC__
525 m_contextMenuEnabled = false;
526 #endif
527 }
528
529 // Destructor
530 wxWindowMSW::~wxWindowMSW()
531 {
532 m_isBeingDeleted = true;
533
534 #ifndef __WXUNIVERSAL__
535 // VS: make sure there's no wxFrame with last focus set to us:
536 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
537 {
538 wxTopLevelWindow *frame = wxDynamicCast(win, wxTopLevelWindow);
539 if ( frame )
540 {
541 if ( frame->GetLastFocus() == this )
542 {
543 frame->SetLastFocus(NULL);
544 }
545
546 // apparently sometimes we can end up with our grand parent
547 // pointing to us as well: this is surely a bug in focus handling
548 // code but it's not clear where it happens so for now just try to
549 // fix it here by not breaking out of the loop
550 //break;
551 }
552 }
553 #endif // __WXUNIVERSAL__
554
555 // VS: destroy children first and _then_ detach *this from its parent.
556 // If we did it the other way around, children wouldn't be able
557 // find their parent frame (see above).
558 DestroyChildren();
559
560 if ( m_hWnd )
561 {
562 // VZ: test temp removed to understand what really happens here
563 //if (::IsWindow(GetHwnd()))
564 {
565 if ( !::DestroyWindow(GetHwnd()) )
566 wxLogLastError(wxT("DestroyWindow"));
567 }
568
569 // remove hWnd <-> wxWindow association
570 wxRemoveHandleAssociation(this);
571 }
572
573 }
574
575 // real construction (Init() must have been called before!)
576 bool wxWindowMSW::Create(wxWindow *parent,
577 wxWindowID id,
578 const wxPoint& pos,
579 const wxSize& size,
580 long style,
581 const wxString& name)
582 {
583 wxCHECK_MSG( parent, false, wxT("can't create wxWindow without parent") );
584
585 if ( !CreateBase(parent, id, pos, size, style, wxDefaultValidator, name) )
586 return false;
587
588 parent->AddChild(this);
589
590 WXDWORD exstyle;
591 DWORD msflags = MSWGetCreateWindowFlags(&exstyle);
592
593 #ifdef __WXUNIVERSAL__
594 // no borders, we draw them ourselves
595 exstyle &= ~(WS_EX_DLGMODALFRAME |
596 WS_EX_STATICEDGE |
597 WS_EX_CLIENTEDGE |
598 WS_EX_WINDOWEDGE);
599 msflags &= ~WS_BORDER;
600 #endif // wxUniversal
601
602 if ( IsShown() )
603 {
604 msflags |= WS_VISIBLE;
605 }
606
607 if ( !MSWCreate(wxCanvasClassName, NULL, pos, size, msflags, exstyle) )
608 return false;
609
610 InheritAttributes();
611
612 return true;
613 }
614
615 // ---------------------------------------------------------------------------
616 // basic operations
617 // ---------------------------------------------------------------------------
618
619 void wxWindowMSW::SetFocus()
620 {
621 HWND hWnd = GetHwnd();
622 wxCHECK_RET( hWnd, _T("can't set focus to invalid window") );
623
624 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
625 ::SetLastError(0);
626 #endif
627
628 if ( !::SetFocus(hWnd) )
629 {
630 #if defined(__WXDEBUG__) && !defined(__WXMICROWIN__)
631 // was there really an error?
632 DWORD dwRes = ::GetLastError();
633 if ( dwRes )
634 {
635 HWND hwndFocus = ::GetFocus();
636 if ( hwndFocus != hWnd )
637 {
638 wxLogApiError(_T("SetFocus"), dwRes);
639 }
640 }
641 #endif // Debug
642 }
643 }
644
645 void wxWindowMSW::SetFocusFromKbd()
646 {
647 // when the focus is given to the control with DLGC_HASSETSEL style from
648 // keyboard its contents should be entirely selected: this is what
649 // ::IsDialogMessage() does and so we should do it as well to provide the
650 // same LNF as the native programs
651 if ( ::SendMessage(GetHwnd(), WM_GETDLGCODE, 0, 0) & DLGC_HASSETSEL )
652 {
653 ::SendMessage(GetHwnd(), EM_SETSEL, 0, -1);
654 }
655
656 // do this after (maybe) setting the selection as like this when
657 // wxEVT_SET_FOCUS handler is called, the selection would have been already
658 // set correctly -- this may be important
659 wxWindowBase::SetFocusFromKbd();
660 }
661
662 // Get the window with the focus
663 wxWindow *wxWindowBase::DoFindFocus()
664 {
665 HWND hWnd = ::GetFocus();
666 if ( hWnd )
667 {
668 return wxGetWindowFromHWND((WXHWND)hWnd);
669 }
670
671 return NULL;
672 }
673
674 void wxWindowMSW::DoEnable( bool enable )
675 {
676 HWND hWnd = GetHwnd();
677 if ( hWnd )
678 ::EnableWindow(hWnd, (BOOL)enable);
679 }
680
681 bool wxWindowMSW::Show(bool show)
682 {
683 if ( !wxWindowBase::Show(show) )
684 return false;
685
686 HWND hWnd = GetHwnd();
687
688 // we could be called before the underlying window is created (this is
689 // actually useful to prevent it from being initially shown), e.g.
690 //
691 // wxFoo *foo = new wxFoo;
692 // foo->Hide();
693 // foo->Create(parent, ...);
694 //
695 // should work without errors
696 if ( hWnd )
697 {
698 ::ShowWindow(hWnd, show ? SW_SHOW : SW_HIDE);
699 }
700
701 return true;
702 }
703
704 // Raise the window to the top of the Z order
705 void wxWindowMSW::Raise()
706 {
707 wxBringWindowToTop(GetHwnd());
708 }
709
710 // Lower the window to the bottom of the Z order
711 void wxWindowMSW::Lower()
712 {
713 ::SetWindowPos(GetHwnd(), HWND_BOTTOM, 0, 0, 0, 0,
714 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
715 }
716
717 void wxWindowMSW::DoCaptureMouse()
718 {
719 HWND hWnd = GetHwnd();
720 if ( hWnd )
721 {
722 ::SetCapture(hWnd);
723 }
724 }
725
726 void wxWindowMSW::DoReleaseMouse()
727 {
728 if ( !::ReleaseCapture() )
729 {
730 wxLogLastError(_T("ReleaseCapture"));
731 }
732 }
733
734 /* static */ wxWindow *wxWindowBase::GetCapture()
735 {
736 HWND hwnd = ::GetCapture();
737 return hwnd ? wxFindWinFromHandle((WXHWND)hwnd) : (wxWindow *)NULL;
738 }
739
740 bool wxWindowMSW::SetFont(const wxFont& font)
741 {
742 if ( !wxWindowBase::SetFont(font) )
743 {
744 // nothing to do
745 return false;
746 }
747
748 HWND hWnd = GetHwnd();
749 if ( hWnd != 0 )
750 {
751 WXHANDLE hFont = m_font.GetResourceHandle();
752
753 wxASSERT_MSG( hFont, wxT("should have valid font") );
754
755 ::SendMessage(hWnd, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));
756 }
757
758 return true;
759 }
760 bool wxWindowMSW::SetCursor(const wxCursor& cursor)
761 {
762 if ( !wxWindowBase::SetCursor(cursor) )
763 {
764 // no change
765 return false;
766 }
767
768 // don't "overwrite" busy cursor
769 if ( m_cursor.Ok() && !wxIsBusy() )
770 {
771 // normally we should change the cursor only if it's over this window
772 // but we should do it always if we capture the mouse currently
773 bool set = HasCapture();
774 if ( !set )
775 {
776 HWND hWnd = GetHwnd();
777
778 POINT point;
779 #ifdef __WXWINCE__
780 ::GetCursorPosWinCE(&point);
781 #else
782 ::GetCursorPos(&point);
783 #endif
784
785 RECT rect = wxGetWindowRect(hWnd);
786
787 set = ::PtInRect(&rect, point) != 0;
788 }
789
790 if ( set )
791 {
792 ::SetCursor(GetHcursorOf(m_cursor));
793 }
794 //else: will be set later when the mouse enters this window
795 }
796
797 return true;
798 }
799
800 void wxWindowMSW::WarpPointer(int x, int y)
801 {
802 ClientToScreen(&x, &y);
803
804 if ( !::SetCursorPos(x, y) )
805 {
806 wxLogLastError(_T("SetCursorPos"));
807 }
808 }
809
810 void wxWindowMSW::MSWUpdateUIState(int action, int state)
811 {
812 // WM_CHANGEUISTATE only appeared in Windows 2000 so it can do us no good
813 // to use it on older systems -- and could possibly do some harm
814 static int s_needToUpdate = -1;
815 if ( s_needToUpdate == -1 )
816 {
817 int verMaj, verMin;
818 s_needToUpdate = wxGetOsVersion(&verMaj, &verMin) == wxOS_WINDOWS_NT &&
819 verMaj >= 5;
820 }
821
822 if ( s_needToUpdate )
823 {
824 // we send WM_CHANGEUISTATE so if nothing needs changing then the system
825 // won't send WM_UPDATEUISTATE
826 ::SendMessage(GetHwnd(), WM_CHANGEUISTATE, MAKEWPARAM(action, state), 0);
827 }
828 }
829
830 // ---------------------------------------------------------------------------
831 // scrolling stuff
832 // ---------------------------------------------------------------------------
833
834 inline int GetScrollPosition(HWND hWnd, int wOrient)
835 {
836 #ifdef __WXMICROWIN__
837 return ::GetScrollPosWX(hWnd, wOrient);
838 #else
839 WinStruct<SCROLLINFO> scrollInfo;
840 scrollInfo.cbSize = sizeof(SCROLLINFO);
841 scrollInfo.fMask = SIF_POS;
842 ::GetScrollInfo(hWnd, wOrient, &scrollInfo );
843
844 return scrollInfo.nPos;
845
846 #endif
847 }
848
849 int wxWindowMSW::GetScrollPos(int orient) const
850 {
851 HWND hWnd = GetHwnd();
852 wxCHECK_MSG( hWnd, 0, _T("no HWND in GetScrollPos") );
853
854 return GetScrollPosition(hWnd, orient == wxHORIZONTAL ? SB_HORZ : SB_VERT);
855 }
856
857 // This now returns the whole range, not just the number
858 // of positions that we can scroll.
859 int wxWindowMSW::GetScrollRange(int orient) const
860 {
861 int maxPos;
862 HWND hWnd = GetHwnd();
863 if ( !hWnd )
864 return 0;
865 #if 0
866 ::GetScrollRange(hWnd, orient == wxHORIZONTAL ? SB_HORZ : SB_VERT,
867 &minPos, &maxPos);
868 #endif
869 WinStruct<SCROLLINFO> scrollInfo;
870 scrollInfo.fMask = SIF_RANGE;
871 if ( !::GetScrollInfo(hWnd,
872 orient == wxHORIZONTAL ? SB_HORZ : SB_VERT,
873 &scrollInfo) )
874 {
875 // Most of the time this is not really an error, since the return
876 // value can also be zero when there is no scrollbar yet.
877 // wxLogLastError(_T("GetScrollInfo"));
878 }
879 maxPos = scrollInfo.nMax;
880
881 // undo "range - 1" done in SetScrollbar()
882 return maxPos + 1;
883 }
884
885 int wxWindowMSW::GetScrollThumb(int orient) const
886 {
887 return orient == wxHORIZONTAL ? m_xThumbSize : m_yThumbSize;
888 }
889
890 void wxWindowMSW::SetScrollPos(int orient, int pos, bool refresh)
891 {
892 HWND hWnd = GetHwnd();
893 wxCHECK_RET( hWnd, _T("SetScrollPos: no HWND") );
894
895 WinStruct<SCROLLINFO> info;
896 info.nPage = 0;
897 info.nMin = 0;
898 info.nPos = pos;
899 info.fMask = SIF_POS;
900 if ( HasFlag(wxALWAYS_SHOW_SB) )
901 {
902 // disable scrollbar instead of removing it then
903 info.fMask |= SIF_DISABLENOSCROLL;
904 }
905
906 ::SetScrollInfo(hWnd, orient == wxHORIZONTAL ? SB_HORZ : SB_VERT,
907 &info, refresh);
908 }
909
910 // New function that will replace some of the above.
911 void wxWindowMSW::SetScrollbar(int orient,
912 int pos,
913 int pageSize,
914 int range,
915 bool refresh)
916 {
917 WinStruct<SCROLLINFO> info;
918 info.nPage = pageSize;
919 info.nMin = 0; // range is nMax - nMin + 1
920 info.nMax = range - 1; // as both nMax and nMax are inclusive
921 info.nPos = pos;
922 info.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
923 if ( HasFlag(wxALWAYS_SHOW_SB) )
924 {
925 // disable scrollbar instead of removing it then
926 info.fMask |= SIF_DISABLENOSCROLL;
927 }
928
929 HWND hWnd = GetHwnd();
930 if ( hWnd )
931 {
932 // We have to set the variables here to make them valid in events
933 // triggered by ::SetScrollInfo()
934 *(orient == wxHORIZONTAL ? &m_xThumbSize : &m_yThumbSize) = pageSize;
935
936 ::SetScrollInfo(hWnd, orient == wxHORIZONTAL ? SB_HORZ : SB_VERT,
937 &info, refresh);
938 }
939 }
940
941 void wxWindowMSW::ScrollWindow(int dx, int dy, const wxRect *prect)
942 {
943 RECT rect;
944 RECT *pr;
945 if ( prect )
946 {
947 wxCopyRectToRECT(*prect, rect);
948 pr = &rect;
949 }
950 else
951 {
952 pr = NULL;
953
954 }
955
956 #ifdef __WXWINCE__
957 // FIXME: is this the exact equivalent of the line below?
958 ::ScrollWindowEx(GetHwnd(), dx, dy, pr, pr, 0, 0, SW_SCROLLCHILDREN|SW_ERASE|SW_INVALIDATE);
959 #else
960 ::ScrollWindow(GetHwnd(), dx, dy, pr, pr);
961 #endif
962 }
963
964 static bool ScrollVertically(HWND hwnd, int kind, int count)
965 {
966 int posStart = GetScrollPosition(hwnd, SB_VERT);
967
968 int pos = posStart;
969 for ( int n = 0; n < count; n++ )
970 {
971 ::SendMessage(hwnd, WM_VSCROLL, kind, 0);
972
973 int posNew = GetScrollPosition(hwnd, SB_VERT);
974 if ( posNew == pos )
975 {
976 // don't bother to continue, we're already at top/bottom
977 break;
978 }
979
980 pos = posNew;
981 }
982
983 return pos != posStart;
984 }
985
986 bool wxWindowMSW::ScrollLines(int lines)
987 {
988 bool down = lines > 0;
989
990 return ScrollVertically(GetHwnd(),
991 down ? SB_LINEDOWN : SB_LINEUP,
992 down ? lines : -lines);
993 }
994
995 bool wxWindowMSW::ScrollPages(int pages)
996 {
997 bool down = pages > 0;
998
999 return ScrollVertically(GetHwnd(),
1000 down ? SB_PAGEDOWN : SB_PAGEUP,
1001 down ? pages : -pages);
1002 }
1003
1004 // ----------------------------------------------------------------------------
1005 // RTL support
1006 // ----------------------------------------------------------------------------
1007
1008 void wxWindowMSW::SetLayoutDirection(wxLayoutDirection dir)
1009 {
1010 #ifdef __WXWINCE__
1011 wxUnusedVar(dir);
1012 #else
1013 const HWND hwnd = GetHwnd();
1014 wxCHECK_RET( hwnd, _T("layout direction must be set after window creation") );
1015
1016 LONG styleOld = ::GetWindowLong(hwnd, GWL_EXSTYLE);
1017
1018 LONG styleNew = styleOld;
1019 switch ( dir )
1020 {
1021 case wxLayout_LeftToRight:
1022 styleNew &= ~WS_EX_LAYOUTRTL;
1023 break;
1024
1025 case wxLayout_RightToLeft:
1026 styleNew |= WS_EX_LAYOUTRTL;
1027 break;
1028
1029 default:
1030 wxFAIL_MSG(_T("unsupported layout direction"));
1031 break;
1032 }
1033
1034 if ( styleNew != styleOld )
1035 {
1036 ::SetWindowLong(hwnd, GWL_EXSTYLE, styleNew);
1037 }
1038 #endif
1039 }
1040
1041 wxLayoutDirection wxWindowMSW::GetLayoutDirection() const
1042 {
1043 #ifdef __WXWINCE__
1044 return wxLayout_Default;
1045 #else
1046 const HWND hwnd = GetHwnd();
1047 wxCHECK_MSG( hwnd, wxLayout_Default, _T("invalid window") );
1048
1049 return ::GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_LAYOUTRTL
1050 ? wxLayout_RightToLeft
1051 : wxLayout_LeftToRight;
1052 #endif
1053 }
1054
1055 wxCoord
1056 wxWindowMSW::AdjustForLayoutDirection(wxCoord x,
1057 wxCoord WXUNUSED(width),
1058 wxCoord WXUNUSED(widthTotal)) const
1059 {
1060 // Win32 mirrors the coordinates of RTL windows automatically, so don't
1061 // redo it ourselves
1062 return x;
1063 }
1064
1065 // ---------------------------------------------------------------------------
1066 // subclassing
1067 // ---------------------------------------------------------------------------
1068
1069 void wxWindowMSW::SubclassWin(WXHWND hWnd)
1070 {
1071 wxASSERT_MSG( !m_oldWndProc, wxT("subclassing window twice?") );
1072
1073 HWND hwnd = (HWND)hWnd;
1074 wxCHECK_RET( ::IsWindow(hwnd), wxT("invalid HWND in SubclassWin") );
1075
1076 wxAssociateWinWithHandle(hwnd, this);
1077
1078 m_oldWndProc = (WXFARPROC)wxGetWindowProc((HWND)hWnd);
1079
1080 // we don't need to subclass the window of our own class (in the Windows
1081 // sense of the word)
1082 if ( !wxCheckWindowWndProc(hWnd, (WXFARPROC)wxWndProc) )
1083 {
1084 wxSetWindowProc(hwnd, wxWndProc);
1085 }
1086 else
1087 {
1088 // don't bother restoring it either: this also makes it easy to
1089 // implement IsOfStandardClass() method which returns true for the
1090 // standard controls and false for the wxWidgets own windows as it can
1091 // simply check m_oldWndProc
1092 m_oldWndProc = NULL;
1093 }
1094
1095 // we're officially created now, send the event
1096 wxWindowCreateEvent event((wxWindow *)this);
1097 (void)GetEventHandler()->ProcessEvent(event);
1098 }
1099
1100 void wxWindowMSW::UnsubclassWin()
1101 {
1102 wxRemoveHandleAssociation(this);
1103
1104 // Restore old Window proc
1105 HWND hwnd = GetHwnd();
1106 if ( hwnd )
1107 {
1108 SetHWND(0);
1109
1110 wxCHECK_RET( ::IsWindow(hwnd), wxT("invalid HWND in UnsubclassWin") );
1111
1112 if ( m_oldWndProc )
1113 {
1114 if ( !wxCheckWindowWndProc((WXHWND)hwnd, m_oldWndProc) )
1115 {
1116 wxSetWindowProc(hwnd, (WNDPROC)m_oldWndProc);
1117 }
1118
1119 m_oldWndProc = NULL;
1120 }
1121 }
1122 }
1123
1124 void wxWindowMSW::AssociateHandle(WXWidget handle)
1125 {
1126 if ( m_hWnd )
1127 {
1128 if ( !::DestroyWindow(GetHwnd()) )
1129 wxLogLastError(wxT("DestroyWindow"));
1130 }
1131
1132 WXHWND wxhwnd = (WXHWND)handle;
1133
1134 SetHWND(wxhwnd);
1135 SubclassWin(wxhwnd);
1136 }
1137
1138 void wxWindowMSW::DissociateHandle()
1139 {
1140 // this also calls SetHWND(0) for us
1141 UnsubclassWin();
1142 }
1143
1144
1145 bool wxCheckWindowWndProc(WXHWND hWnd,
1146 WXFARPROC WXUNUSED(wndProc))
1147 {
1148 // TODO: This list of window class names should be factored out so they can be
1149 // managed in one place and then accessed from here and other places, such as
1150 // wxApp::RegisterWindowClasses() and wxApp::UnregisterWindowClasses()
1151
1152 #ifdef __WXWINCE__
1153 extern wxChar *wxCanvasClassName;
1154 extern wxChar *wxCanvasClassNameNR;
1155 #else
1156 extern const wxChar *wxCanvasClassName;
1157 extern const wxChar *wxCanvasClassNameNR;
1158 #endif
1159 extern const wxChar *wxMDIFrameClassName;
1160 extern const wxChar *wxMDIFrameClassNameNoRedraw;
1161 extern const wxChar *wxMDIChildFrameClassName;
1162 extern const wxChar *wxMDIChildFrameClassNameNoRedraw;
1163 wxString str(wxGetWindowClass(hWnd));
1164 if (str == wxCanvasClassName ||
1165 str == wxCanvasClassNameNR ||
1166 #if wxUSE_GLCANVAS
1167 str == _T("wxGLCanvasClass") ||
1168 str == _T("wxGLCanvasClassNR") ||
1169 #endif // wxUSE_GLCANVAS
1170 str == wxMDIFrameClassName ||
1171 str == wxMDIFrameClassNameNoRedraw ||
1172 str == wxMDIChildFrameClassName ||
1173 str == wxMDIChildFrameClassNameNoRedraw ||
1174 str == _T("wxTLWHiddenParent"))
1175 return true; // Effectively means don't subclass
1176 else
1177 return false;
1178 }
1179
1180 // ----------------------------------------------------------------------------
1181 // Style handling
1182 // ----------------------------------------------------------------------------
1183
1184 void wxWindowMSW::SetWindowStyleFlag(long flags)
1185 {
1186 long flagsOld = GetWindowStyleFlag();
1187 if ( flags == flagsOld )
1188 return;
1189
1190 // update the internal variable
1191 wxWindowBase::SetWindowStyleFlag(flags);
1192
1193 // and the real window flags
1194 MSWUpdateStyle(flagsOld, GetExtraStyle());
1195 }
1196
1197 void wxWindowMSW::SetExtraStyle(long exflags)
1198 {
1199 long exflagsOld = GetExtraStyle();
1200 if ( exflags == exflagsOld )
1201 return;
1202
1203 // update the internal variable
1204 wxWindowBase::SetExtraStyle(exflags);
1205
1206 // and the real window flags
1207 MSWUpdateStyle(GetWindowStyleFlag(), exflagsOld);
1208 }
1209
1210 void wxWindowMSW::MSWUpdateStyle(long flagsOld, long exflagsOld)
1211 {
1212 // now update the Windows style as well if needed - and if the window had
1213 // been already created
1214 if ( !GetHwnd() )
1215 return;
1216
1217 // we may need to call SetWindowPos() when we change some styles
1218 bool callSWP = false;
1219
1220 WXDWORD exstyle;
1221 long style = MSWGetStyle(GetWindowStyleFlag(), &exstyle);
1222
1223 // this is quite a horrible hack but we need it because MSWGetStyle()
1224 // doesn't take exflags as parameter but uses GetExtraStyle() internally
1225 // and so we have to modify the window exflags temporarily to get the
1226 // correct exstyleOld
1227 long exflagsNew = GetExtraStyle();
1228 wxWindowBase::SetExtraStyle(exflagsOld);
1229
1230 WXDWORD exstyleOld;
1231 long styleOld = MSWGetStyle(flagsOld, &exstyleOld);
1232
1233 wxWindowBase::SetExtraStyle(exflagsNew);
1234
1235
1236 if ( style != styleOld )
1237 {
1238 // some flags (e.g. WS_VISIBLE or WS_DISABLED) should not be changed by
1239 // this function so instead of simply setting the style to the new
1240 // value we clear the bits which were set in styleOld but are set in
1241 // the new one and set the ones which were not set before
1242 long styleReal = ::GetWindowLong(GetHwnd(), GWL_STYLE);
1243 styleReal &= ~styleOld;
1244 styleReal |= style;
1245
1246 ::SetWindowLong(GetHwnd(), GWL_STYLE, styleReal);
1247
1248 // we need to call SetWindowPos() if any of the styles affecting the
1249 // frame appearance have changed
1250 callSWP = ((styleOld ^ style ) & (WS_BORDER |
1251 WS_THICKFRAME |
1252 WS_CAPTION |
1253 WS_DLGFRAME |
1254 WS_MAXIMIZEBOX |
1255 WS_MINIMIZEBOX |
1256 WS_SYSMENU) ) != 0;
1257 }
1258
1259 // and the extended style
1260 long exstyleReal = ::GetWindowLong(GetHwnd(), GWL_EXSTYLE);
1261
1262 if ( exstyle != exstyleOld )
1263 {
1264 exstyleReal &= ~exstyleOld;
1265 exstyleReal |= exstyle;
1266
1267 ::SetWindowLong(GetHwnd(), GWL_EXSTYLE, exstyleReal);
1268
1269 // ex style changes don't take effect without calling SetWindowPos
1270 callSWP = true;
1271 }
1272
1273 if ( callSWP )
1274 {
1275 // we must call SetWindowPos() to flush the cached extended style and
1276 // also to make the change to wxSTAY_ON_TOP style take effect: just
1277 // setting the style simply doesn't work
1278 if ( !::SetWindowPos(GetHwnd(),
1279 exstyleReal & WS_EX_TOPMOST ? HWND_TOPMOST
1280 : HWND_NOTOPMOST,
1281 0, 0, 0, 0,
1282 SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED) )
1283 {
1284 wxLogLastError(_T("SetWindowPos"));
1285 }
1286 }
1287 }
1288
1289 wxBorder wxWindowMSW::GetDefaultBorderForControl() const
1290 {
1291 // we want to automatically give controls a sunken style (confusingly,
1292 // it may not really mean sunken at all as we map it to WS_EX_CLIENTEDGE
1293 // which is not sunken at all under Windows XP -- rather, just the default)
1294
1295 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
1296 return wxBORDER_SIMPLE;
1297 #else
1298 #if wxUSE_UXTHEME
1299 if (CanApplyThemeBorder())
1300 {
1301 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
1302 if (theme)
1303 return wxBORDER_THEME;
1304 }
1305 #endif
1306 return wxBORDER_SUNKEN;
1307 #endif
1308 }
1309
1310 wxBorder wxWindowMSW::GetDefaultBorder() const
1311 {
1312 return GetDefaultBorderForControl();
1313 }
1314
1315 WXDWORD wxWindowMSW::MSWGetStyle(long flags, WXDWORD *exstyle) const
1316 {
1317 // translate common wxWidgets styles to Windows ones
1318
1319 // most of windows are child ones, those which are not (such as
1320 // wxTopLevelWindow) should remove WS_CHILD in their MSWGetStyle()
1321 WXDWORD style = WS_CHILD;
1322
1323 // using this flag results in very significant reduction in flicker,
1324 // especially with controls inside the static boxes (as the interior of the
1325 // box is not redrawn twice), but sometimes results in redraw problems, so
1326 // optionally allow the old code to continue to use it provided a special
1327 // system option is turned on
1328 if ( !wxSystemOptions::GetOptionInt(wxT("msw.window.no-clip-children"))
1329 || (flags & wxCLIP_CHILDREN) )
1330 style |= WS_CLIPCHILDREN;
1331
1332 // it doesn't seem useful to use WS_CLIPSIBLINGS here as we officially
1333 // don't support overlapping windows and it only makes sense for them and,
1334 // presumably, gives the system some extra work (to manage more clipping
1335 // regions), so avoid it alltogether
1336
1337
1338 if ( flags & wxVSCROLL )
1339 style |= WS_VSCROLL;
1340
1341 if ( flags & wxHSCROLL )
1342 style |= WS_HSCROLL;
1343
1344 const wxBorder border = GetBorder(flags);
1345
1346 // WS_BORDER is only required for wxBORDER_SIMPLE
1347 if ( border == wxBORDER_SIMPLE )
1348 style |= WS_BORDER;
1349
1350 // now deal with ext style if the caller wants it
1351 if ( exstyle )
1352 {
1353 *exstyle = 0;
1354
1355 #ifndef __WXWINCE__
1356 if ( flags & wxTRANSPARENT_WINDOW )
1357 *exstyle |= WS_EX_TRANSPARENT;
1358 #endif
1359
1360 switch ( border )
1361 {
1362 default:
1363 case wxBORDER_DEFAULT:
1364 wxFAIL_MSG( _T("unknown border style") );
1365 // fall through
1366
1367 case wxBORDER_NONE:
1368 case wxBORDER_SIMPLE:
1369 case wxBORDER_THEME:
1370 break;
1371
1372 case wxBORDER_STATIC:
1373 *exstyle |= WS_EX_STATICEDGE;
1374 break;
1375
1376 case wxBORDER_RAISED:
1377 *exstyle |= WS_EX_DLGMODALFRAME;
1378 break;
1379
1380 case wxBORDER_SUNKEN:
1381 *exstyle |= WS_EX_CLIENTEDGE;
1382 style &= ~WS_BORDER;
1383 break;
1384
1385 // case wxBORDER_DOUBLE:
1386 // *exstyle |= WS_EX_DLGMODALFRAME;
1387 // break;
1388 }
1389
1390 // wxUniv doesn't use Windows dialog navigation functions at all
1391 #if !defined(__WXUNIVERSAL__) && !defined(__WXWINCE__)
1392 // to make the dialog navigation work with the nested panels we must
1393 // use this style (top level windows such as dialogs don't need it)
1394 if ( (flags & wxTAB_TRAVERSAL) && !IsTopLevel() )
1395 {
1396 *exstyle |= WS_EX_CONTROLPARENT;
1397 }
1398 #endif // __WXUNIVERSAL__
1399 }
1400
1401 return style;
1402 }
1403
1404 // Setup background and foreground colours correctly
1405 void wxWindowMSW::SetupColours()
1406 {
1407 if ( GetParent() )
1408 SetBackgroundColour(GetParent()->GetBackgroundColour());
1409 }
1410
1411 bool wxWindowMSW::IsMouseInWindow() const
1412 {
1413 // get the mouse position
1414 POINT pt;
1415 #ifdef __WXWINCE__
1416 ::GetCursorPosWinCE(&pt);
1417 #else
1418 ::GetCursorPos(&pt);
1419 #endif
1420
1421 // find the window which currently has the cursor and go up the window
1422 // chain until we find this window - or exhaust it
1423 HWND hwnd = ::WindowFromPoint(pt);
1424 while ( hwnd && (hwnd != GetHwnd()) )
1425 hwnd = ::GetParent(hwnd);
1426
1427 return hwnd != NULL;
1428 }
1429
1430 void wxWindowMSW::OnInternalIdle()
1431 {
1432 #ifndef HAVE_TRACKMOUSEEVENT
1433 // Check if we need to send a LEAVE event
1434 if ( m_mouseInWindow )
1435 {
1436 // note that we should generate the leave event whether the window has
1437 // or doesn't have mouse capture
1438 if ( !IsMouseInWindow() )
1439 {
1440 GenerateMouseLeave();
1441 }
1442 }
1443 #endif // !HAVE_TRACKMOUSEEVENT
1444
1445 if (wxUpdateUIEvent::CanUpdate(this) && IsShown())
1446 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1447 }
1448
1449 // Set this window to be the child of 'parent'.
1450 bool wxWindowMSW::Reparent(wxWindowBase *parent)
1451 {
1452 if ( !wxWindowBase::Reparent(parent) )
1453 return false;
1454
1455 HWND hWndChild = GetHwnd();
1456 HWND hWndParent = GetParent() ? GetWinHwnd(GetParent()) : (HWND)0;
1457
1458 ::SetParent(hWndChild, hWndParent);
1459
1460 #ifndef __WXWINCE__
1461 if ( ::GetWindowLong(hWndChild, GWL_EXSTYLE) & WS_EX_CONTROLPARENT )
1462 {
1463 EnsureParentHasControlParentStyle(GetParent());
1464 }
1465 #endif // !__WXWINCE__
1466
1467 return true;
1468 }
1469
1470 static inline void SendSetRedraw(HWND hwnd, bool on)
1471 {
1472 #ifndef __WXMICROWIN__
1473 ::SendMessage(hwnd, WM_SETREDRAW, (WPARAM)on, 0);
1474 #endif
1475 }
1476
1477 void wxWindowMSW::Freeze()
1478 {
1479 if ( !m_frozenness++ )
1480 {
1481 if ( IsShown() )
1482 SendSetRedraw(GetHwnd(), false);
1483 }
1484 }
1485
1486 void wxWindowMSW::Thaw()
1487 {
1488 wxASSERT_MSG( m_frozenness > 0, _T("Thaw() without matching Freeze()") );
1489
1490 if ( --m_frozenness == 0 )
1491 {
1492 if ( IsShown() )
1493 {
1494 SendSetRedraw(GetHwnd(), true);
1495
1496 // we need to refresh everything or otherwise the invalidated area
1497 // is not going to be repainted
1498 Refresh();
1499 }
1500 }
1501 }
1502
1503 void wxWindowMSW::Refresh(bool eraseBack, const wxRect *rect)
1504 {
1505 HWND hWnd = GetHwnd();
1506 if ( hWnd )
1507 {
1508 RECT mswRect;
1509 const RECT *pRect;
1510 if ( rect )
1511 {
1512 wxCopyRectToRECT(*rect, mswRect);
1513 pRect = &mswRect;
1514 }
1515 else
1516 {
1517 pRect = NULL;
1518 }
1519
1520 // RedrawWindow not available on SmartPhone or eVC++ 3
1521 #if !defined(__SMARTPHONE__) && !(defined(_WIN32_WCE) && _WIN32_WCE < 400)
1522 UINT flags = RDW_INVALIDATE | RDW_ALLCHILDREN;
1523 if ( eraseBack )
1524 flags |= RDW_ERASE;
1525
1526 ::RedrawWindow(hWnd, pRect, NULL, flags);
1527 #else
1528 ::InvalidateRect(hWnd, pRect, eraseBack);
1529 #endif
1530 }
1531 }
1532
1533 void wxWindowMSW::Update()
1534 {
1535 if ( !::UpdateWindow(GetHwnd()) )
1536 {
1537 wxLogLastError(_T("UpdateWindow"));
1538 }
1539
1540 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
1541 // just calling UpdateWindow() is not enough, what we did in our WM_PAINT
1542 // handler needs to be really drawn right now
1543 (void)::GdiFlush();
1544 #endif // __WIN32__
1545 }
1546
1547 // ---------------------------------------------------------------------------
1548 // drag and drop
1549 // ---------------------------------------------------------------------------
1550
1551 #if wxUSE_DRAG_AND_DROP || !defined(__WXWINCE__)
1552
1553 #if wxUSE_STATBOX
1554
1555 // we need to lower the sibling static boxes so controls contained within can be
1556 // a drop target
1557 static void AdjustStaticBoxZOrder(wxWindow *parent)
1558 {
1559 // no sibling static boxes if we have no parent (ie TLW)
1560 if ( !parent )
1561 return;
1562
1563 for ( wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst();
1564 node;
1565 node = node->GetNext() )
1566 {
1567 wxStaticBox *statbox = wxDynamicCast(node->GetData(), wxStaticBox);
1568 if ( statbox )
1569 {
1570 ::SetWindowPos(GetHwndOf(statbox), HWND_BOTTOM, 0, 0, 0, 0,
1571 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
1572 }
1573 }
1574 }
1575
1576 #else // !wxUSE_STATBOX
1577
1578 static inline void AdjustStaticBoxZOrder(wxWindow * WXUNUSED(parent))
1579 {
1580 }
1581
1582 #endif // wxUSE_STATBOX/!wxUSE_STATBOX
1583
1584 #endif // drag and drop is used
1585
1586 #if wxUSE_DRAG_AND_DROP
1587 void wxWindowMSW::SetDropTarget(wxDropTarget *pDropTarget)
1588 {
1589 if ( m_dropTarget != 0 ) {
1590 m_dropTarget->Revoke(m_hWnd);
1591 delete m_dropTarget;
1592 }
1593
1594 m_dropTarget = pDropTarget;
1595 if ( m_dropTarget != 0 )
1596 {
1597 AdjustStaticBoxZOrder(GetParent());
1598 m_dropTarget->Register(m_hWnd);
1599 }
1600 }
1601 #endif // wxUSE_DRAG_AND_DROP
1602
1603 // old-style file manager drag&drop support: we retain the old-style
1604 // DragAcceptFiles in parallel with SetDropTarget.
1605 void wxWindowMSW::DragAcceptFiles(bool WXUNUSED_IN_WINCE(accept))
1606 {
1607 #ifndef __WXWINCE__
1608 HWND hWnd = GetHwnd();
1609 if ( hWnd )
1610 {
1611 AdjustStaticBoxZOrder(GetParent());
1612 ::DragAcceptFiles(hWnd, (BOOL)accept);
1613 }
1614 #endif
1615 }
1616
1617 // ----------------------------------------------------------------------------
1618 // tooltips
1619 // ----------------------------------------------------------------------------
1620
1621 #if wxUSE_TOOLTIPS
1622
1623 void wxWindowMSW::DoSetToolTip(wxToolTip *tooltip)
1624 {
1625 wxWindowBase::DoSetToolTip(tooltip);
1626
1627 if ( m_tooltip )
1628 m_tooltip->SetWindow((wxWindow *)this);
1629 }
1630
1631 #endif // wxUSE_TOOLTIPS
1632
1633 // ---------------------------------------------------------------------------
1634 // moving and resizing
1635 // ---------------------------------------------------------------------------
1636
1637 bool wxWindowMSW::IsSizeDeferred() const
1638 {
1639 #if USE_DEFERRED_SIZING
1640 if ( m_pendingPosition != wxDefaultPosition ||
1641 m_pendingSize != wxDefaultSize )
1642 return true;
1643 #endif // USE_DEFERRED_SIZING
1644
1645 return false;
1646 }
1647
1648 // Get total size
1649 void wxWindowMSW::DoGetSize(int *x, int *y) const
1650 {
1651 #if USE_DEFERRED_SIZING
1652 // if SetSize() had been called at wx level but not realized at Windows
1653 // level yet (i.e. EndDeferWindowPos() not called), we still should return
1654 // the new and not the old position to the other wx code
1655 if ( m_pendingSize != wxDefaultSize )
1656 {
1657 if ( x )
1658 *x = m_pendingSize.x;
1659 if ( y )
1660 *y = m_pendingSize.y;
1661 }
1662 else // use current size
1663 #endif // USE_DEFERRED_SIZING
1664 {
1665 RECT rect = wxGetWindowRect(GetHwnd());
1666
1667 if ( x )
1668 *x = rect.right - rect.left;
1669 if ( y )
1670 *y = rect.bottom - rect.top;
1671 }
1672 }
1673
1674 // Get size *available for subwindows* i.e. excluding menu bar etc.
1675 void wxWindowMSW::DoGetClientSize(int *x, int *y) const
1676 {
1677 #if USE_DEFERRED_SIZING
1678 if ( m_pendingSize != wxDefaultSize )
1679 {
1680 // we need to calculate the client size corresponding to pending size
1681 RECT rect;
1682 rect.left = m_pendingPosition.x;
1683 rect.top = m_pendingPosition.y;
1684 rect.right = rect.left + m_pendingSize.x;
1685 rect.bottom = rect.top + m_pendingSize.y;
1686
1687 ::SendMessage(GetHwnd(), WM_NCCALCSIZE, FALSE, (LPARAM)&rect);
1688
1689 if ( x )
1690 *x = rect.right - rect.left;
1691 if ( y )
1692 *y = rect.bottom - rect.top;
1693 }
1694 else
1695 #endif // USE_DEFERRED_SIZING
1696 {
1697 RECT rect = wxGetClientRect(GetHwnd());
1698
1699 if ( x )
1700 *x = rect.right;
1701 if ( y )
1702 *y = rect.bottom;
1703 }
1704 }
1705
1706 void wxWindowMSW::DoGetPosition(int *x, int *y) const
1707 {
1708 wxWindow * const parent = GetParent();
1709
1710 wxPoint pos;
1711 if ( m_pendingPosition != wxDefaultPosition )
1712 {
1713 pos = m_pendingPosition;
1714 }
1715 else // use current position
1716 {
1717 RECT rect = wxGetWindowRect(GetHwnd());
1718
1719 POINT point;
1720 point.x = rect.left;
1721 point.y = rect.top;
1722
1723 // we do the adjustments with respect to the parent only for the "real"
1724 // children, not for the dialogs/frames
1725 if ( !IsTopLevel() )
1726 {
1727 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
1728 {
1729 // In RTL mode, we want the logical left x-coordinate,
1730 // which would be the physical right x-coordinate.
1731 point.x = rect.right;
1732 }
1733
1734 // Since we now have the absolute screen coords, if there's a
1735 // parent we must subtract its top left corner
1736 if ( parent )
1737 {
1738 ::ScreenToClient(GetHwndOf(parent), &point);
1739 }
1740 }
1741
1742 pos.x = point.x;
1743 pos.y = point.y;
1744 }
1745
1746 // we also must adjust by the client area offset: a control which is just
1747 // under a toolbar could be at (0, 30) in Windows but at (0, 0) in wx
1748 if ( parent && !IsTopLevel() )
1749 {
1750 const wxPoint pt(parent->GetClientAreaOrigin());
1751 pos.x -= pt.x;
1752 pos.y -= pt.y;
1753 }
1754
1755 if ( x )
1756 *x = pos.x;
1757 if ( y )
1758 *y = pos.y;
1759 }
1760
1761 void wxWindowMSW::DoScreenToClient(int *x, int *y) const
1762 {
1763 POINT pt;
1764 if ( x )
1765 pt.x = *x;
1766 if ( y )
1767 pt.y = *y;
1768
1769 ::ScreenToClient(GetHwnd(), &pt);
1770
1771 if ( x )
1772 *x = pt.x;
1773 if ( y )
1774 *y = pt.y;
1775 }
1776
1777 void wxWindowMSW::DoClientToScreen(int *x, int *y) const
1778 {
1779 POINT pt;
1780 if ( x )
1781 pt.x = *x;
1782 if ( y )
1783 pt.y = *y;
1784
1785 ::ClientToScreen(GetHwnd(), &pt);
1786
1787 if ( x )
1788 *x = pt.x;
1789 if ( y )
1790 *y = pt.y;
1791 }
1792
1793 bool
1794 wxWindowMSW::DoMoveSibling(WXHWND hwnd, int x, int y, int width, int height)
1795 {
1796 #if USE_DEFERRED_SIZING
1797 // if our parent had prepared a defer window handle for us, use it (unless
1798 // we are a top level window)
1799 wxWindowMSW * const parent = IsTopLevel() ? NULL : GetParent();
1800
1801 HDWP hdwp = parent ? (HDWP)parent->m_hDWP : NULL;
1802 if ( hdwp )
1803 {
1804 hdwp = ::DeferWindowPos(hdwp, (HWND)hwnd, NULL, x, y, width, height,
1805 SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
1806 if ( !hdwp )
1807 {
1808 wxLogLastError(_T("DeferWindowPos"));
1809 }
1810 }
1811
1812 if ( parent )
1813 {
1814 // hdwp must be updated as it may have been changed
1815 parent->m_hDWP = (WXHANDLE)hdwp;
1816 }
1817
1818 if ( hdwp )
1819 {
1820 // did deferred move, remember new coordinates of the window as they're
1821 // different from what Windows would return for it
1822 return true;
1823 }
1824
1825 // otherwise (or if deferring failed) move the window in place immediately
1826 #endif // USE_DEFERRED_SIZING
1827 if ( !::MoveWindow((HWND)hwnd, x, y, width, height, IsShown()) )
1828 {
1829 wxLogLastError(wxT("MoveWindow"));
1830 }
1831
1832 // if USE_DEFERRED_SIZING, indicates that we didn't use deferred move,
1833 // ignored otherwise
1834 return false;
1835 }
1836
1837 void wxWindowMSW::DoMoveWindow(int x, int y, int width, int height)
1838 {
1839 // TODO: is this consistent with other platforms?
1840 // Still, negative width or height shouldn't be allowed
1841 if (width < 0)
1842 width = 0;
1843 if (height < 0)
1844 height = 0;
1845
1846 if ( DoMoveSibling(m_hWnd, x, y, width, height) )
1847 {
1848 #if USE_DEFERRED_SIZING
1849 m_pendingPosition = wxPoint(x, y);
1850 m_pendingSize = wxSize(width, height);
1851 #endif // USE_DEFERRED_SIZING
1852 }
1853 }
1854
1855 // set the size of the window: if the dimensions are positive, just use them,
1856 // but if any of them is equal to -1, it means that we must find the value for
1857 // it ourselves (unless sizeFlags contains wxSIZE_ALLOW_MINUS_ONE flag, in
1858 // which case -1 is a valid value for x and y)
1859 //
1860 // If sizeFlags contains wxSIZE_AUTO_WIDTH/HEIGHT flags (default), we calculate
1861 // the width/height to best suit our contents, otherwise we reuse the current
1862 // width/height
1863 void wxWindowMSW::DoSetSize(int x, int y, int width, int height, int sizeFlags)
1864 {
1865 // get the current size and position...
1866 int currentX, currentY;
1867 int currentW, currentH;
1868
1869 GetPosition(&currentX, &currentY);
1870 GetSize(&currentW, &currentH);
1871
1872 // ... and don't do anything (avoiding flicker) if it's already ok unless
1873 // we're forced to resize the window
1874 if ( x == currentX && y == currentY &&
1875 width == currentW && height == currentH &&
1876 !(sizeFlags & wxSIZE_FORCE) )
1877 {
1878 return;
1879 }
1880
1881 if ( x == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1882 x = currentX;
1883 if ( y == wxDefaultCoord && !(sizeFlags & wxSIZE_ALLOW_MINUS_ONE) )
1884 y = currentY;
1885
1886 AdjustForParentClientOrigin(x, y, sizeFlags);
1887
1888 wxSize size = wxDefaultSize;
1889 if ( width == wxDefaultCoord )
1890 {
1891 if ( sizeFlags & wxSIZE_AUTO_WIDTH )
1892 {
1893 size = DoGetBestSize();
1894 width = size.x;
1895 }
1896 else
1897 {
1898 // just take the current one
1899 width = currentW;
1900 }
1901 }
1902
1903 if ( height == wxDefaultCoord )
1904 {
1905 if ( sizeFlags & wxSIZE_AUTO_HEIGHT )
1906 {
1907 if ( size.x == wxDefaultCoord )
1908 {
1909 size = DoGetBestSize();
1910 }
1911 //else: already called DoGetBestSize() above
1912
1913 height = size.y;
1914 }
1915 else
1916 {
1917 // just take the current one
1918 height = currentH;
1919 }
1920 }
1921
1922 DoMoveWindow(x, y, width, height);
1923 }
1924
1925 void wxWindowMSW::DoSetClientSize(int width, int height)
1926 {
1927 // setting the client size is less obvious than it could have been
1928 // because in the result of changing the total size the window scrollbar
1929 // may [dis]appear and/or its menubar may [un]wrap (and AdjustWindowRect()
1930 // doesn't take neither into account) and so the client size will not be
1931 // correct as the difference between the total and client size changes --
1932 // so we keep changing it until we get it right
1933 //
1934 // normally this loop shouldn't take more than 3 iterations (usually 1 but
1935 // if scrollbars [dis]appear as the result of the first call, then 2 and it
1936 // may become 3 if the window had 0 size originally and so we didn't
1937 // calculate the scrollbar correction correctly during the first iteration)
1938 // but just to be on the safe side we check for it instead of making it an
1939 // "infinite" loop (i.e. leaving break inside as the only way to get out)
1940 for ( int i = 0; i < 4; i++ )
1941 {
1942 RECT rectClient;
1943 ::GetClientRect(GetHwnd(), &rectClient);
1944
1945 // if the size is already ok, stop here (NB: rectClient.left = top = 0)
1946 if ( (rectClient.right == width || width == wxDefaultCoord) &&
1947 (rectClient.bottom == height || height == wxDefaultCoord) )
1948 {
1949 break;
1950 }
1951
1952 // Find the difference between the entire window (title bar and all)
1953 // and the client area; add this to the new client size to move the
1954 // window
1955 RECT rectWin;
1956 ::GetWindowRect(GetHwnd(), &rectWin);
1957
1958 const int widthWin = rectWin.right - rectWin.left,
1959 heightWin = rectWin.bottom - rectWin.top;
1960
1961 // MoveWindow positions the child windows relative to the parent, so
1962 // adjust if necessary
1963 if ( !IsTopLevel() )
1964 {
1965 wxWindow *parent = GetParent();
1966 if ( parent )
1967 {
1968 ::ScreenToClient(GetHwndOf(parent), (POINT *)&rectWin);
1969 }
1970 }
1971
1972 // don't call DoMoveWindow() because we want to move window immediately
1973 // and not defer it here as otherwise the value returned by
1974 // GetClient/WindowRect() wouldn't change as the window wouldn't be
1975 // really resized
1976 if ( !::MoveWindow(GetHwnd(),
1977 rectWin.left,
1978 rectWin.top,
1979 width + widthWin - rectClient.right,
1980 height + heightWin - rectClient.bottom,
1981 TRUE) )
1982 {
1983 wxLogLastError(_T("MoveWindow"));
1984 }
1985 }
1986 }
1987
1988 // ---------------------------------------------------------------------------
1989 // text metrics
1990 // ---------------------------------------------------------------------------
1991
1992 int wxWindowMSW::GetCharHeight() const
1993 {
1994 return wxGetTextMetrics(this).tmHeight;
1995 }
1996
1997 int wxWindowMSW::GetCharWidth() const
1998 {
1999 // +1 is needed because Windows apparently adds it when calculating the
2000 // dialog units size in pixels
2001 #if wxDIALOG_UNIT_COMPATIBILITY
2002 return wxGetTextMetrics(this).tmAveCharWidth;
2003 #else
2004 return wxGetTextMetrics(this).tmAveCharWidth + 1;
2005 #endif
2006 }
2007
2008 void wxWindowMSW::GetTextExtent(const wxString& string,
2009 int *x, int *y,
2010 int *descent, int *externalLeading,
2011 const wxFont *theFont) const
2012 {
2013 wxASSERT_MSG( !theFont || theFont->Ok(),
2014 _T("invalid font in GetTextExtent()") );
2015
2016 wxFont fontToUse;
2017 if (theFont)
2018 fontToUse = *theFont;
2019 else
2020 fontToUse = GetFont();
2021
2022 WindowHDC hdc(GetHwnd());
2023 SelectInHDC selectFont(hdc, GetHfontOf(fontToUse));
2024
2025 SIZE sizeRect;
2026 TEXTMETRIC tm;
2027 ::GetTextExtentPoint32(hdc, string.wx_str(), string.length(), &sizeRect);
2028 GetTextMetrics(hdc, &tm);
2029
2030 if ( x )
2031 *x = sizeRect.cx;
2032 if ( y )
2033 *y = sizeRect.cy;
2034 if ( descent )
2035 *descent = tm.tmDescent;
2036 if ( externalLeading )
2037 *externalLeading = tm.tmExternalLeading;
2038 }
2039
2040 // ---------------------------------------------------------------------------
2041 // popup menu
2042 // ---------------------------------------------------------------------------
2043
2044 #if wxUSE_MENUS_NATIVE
2045
2046 // yield for WM_COMMAND events only, i.e. process all WM_COMMANDs in the queue
2047 // immediately, without waiting for the next event loop iteration
2048 //
2049 // NB: this function should probably be made public later as it can almost
2050 // surely replace wxYield() elsewhere as well
2051 static void wxYieldForCommandsOnly()
2052 {
2053 // peek all WM_COMMANDs (it will always return WM_QUIT too but we don't
2054 // want to process it here)
2055 MSG msg;
2056 while ( ::PeekMessage(&msg, (HWND)0, WM_COMMAND, WM_COMMAND, PM_REMOVE) )
2057 {
2058 if ( msg.message == WM_QUIT )
2059 {
2060 // if we retrieved a WM_QUIT, insert back into the message queue.
2061 ::PostQuitMessage(0);
2062 break;
2063 }
2064
2065 // luckily (as we don't have access to wxEventLoopImpl method from here
2066 // anyhow...) we don't need to pre process WM_COMMANDs so dispatch it
2067 // immediately
2068 ::TranslateMessage(&msg);
2069 ::DispatchMessage(&msg);
2070 }
2071 }
2072
2073 bool wxWindowMSW::DoPopupMenu(wxMenu *menu, int x, int y)
2074 {
2075 menu->SetInvokingWindow(this);
2076 menu->UpdateUI();
2077
2078 if ( x == wxDefaultCoord && y == wxDefaultCoord )
2079 {
2080 wxPoint mouse = ScreenToClient(wxGetMousePosition());
2081 x = mouse.x; y = mouse.y;
2082 }
2083
2084 HWND hWnd = GetHwnd();
2085 HMENU hMenu = GetHmenuOf(menu);
2086 POINT point;
2087 point.x = x;
2088 point.y = y;
2089 ::ClientToScreen(hWnd, &point);
2090 wxCurrentPopupMenu = menu;
2091 #if defined(__WXWINCE__)
2092 static const UINT flags = 0;
2093 #else // !__WXWINCE__
2094 UINT flags = TPM_RIGHTBUTTON;
2095 // NT4 doesn't support TPM_RECURSE and simply doesn't show the menu at all
2096 // when it's use, I'm not sure about Win95/98 but prefer to err on the safe
2097 // side and not to use it there neither -- modify the test if it does work
2098 // on these systems
2099 if ( wxGetWinVersion() >= wxWinVersion_5 )
2100 {
2101 // using TPM_RECURSE allows us to show a popup menu while another menu
2102 // is opened which can be useful and is supported by the other
2103 // platforms, so allow it under Windows too
2104 flags |= TPM_RECURSE;
2105 }
2106 #endif // __WXWINCE__/!__WXWINCE__
2107
2108 ::TrackPopupMenu(hMenu, flags, point.x, point.y, 0, hWnd, NULL);
2109
2110 // we need to do it right now as otherwise the events are never going to be
2111 // sent to wxCurrentPopupMenu from HandleCommand()
2112 //
2113 // note that even eliminating (ugly) wxCurrentPopupMenu global wouldn't
2114 // help and we'd still need wxYieldForCommandsOnly() as the menu may be
2115 // destroyed as soon as we return (it can be a local variable in the caller
2116 // for example) and so we do need to process the event immediately
2117 wxYieldForCommandsOnly();
2118
2119 wxCurrentPopupMenu = NULL;
2120
2121 menu->SetInvokingWindow(NULL);
2122
2123 return true;
2124 }
2125
2126 #endif // wxUSE_MENUS_NATIVE
2127
2128 // ===========================================================================
2129 // pre/post message processing
2130 // ===========================================================================
2131
2132 WXLRESULT wxWindowMSW::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2133 {
2134 if ( m_oldWndProc )
2135 return ::CallWindowProc(CASTWNDPROC m_oldWndProc, GetHwnd(), (UINT) nMsg, (WPARAM) wParam, (LPARAM) lParam);
2136 else
2137 return ::DefWindowProc(GetHwnd(), nMsg, wParam, lParam);
2138 }
2139
2140 bool wxWindowMSW::MSWProcessMessage(WXMSG* pMsg)
2141 {
2142 // wxUniversal implements tab traversal itself
2143 #ifndef __WXUNIVERSAL__
2144 if ( m_hWnd != 0 && (GetWindowStyleFlag() & wxTAB_TRAVERSAL) )
2145 {
2146 // intercept dialog navigation keys
2147 MSG *msg = (MSG *)pMsg;
2148
2149 // here we try to do all the job which ::IsDialogMessage() usually does
2150 // internally
2151 if ( msg->message == WM_KEYDOWN )
2152 {
2153 bool bCtrlDown = wxIsCtrlDown();
2154 bool bShiftDown = wxIsShiftDown();
2155
2156 // WM_GETDLGCODE: ask the control if it wants the key for itself,
2157 // don't process it if it's the case (except for Ctrl-Tab/Enter
2158 // combinations which are always processed)
2159 LONG lDlgCode = ::SendMessage(msg->hwnd, WM_GETDLGCODE, 0, 0);
2160
2161 // surprizingly, DLGC_WANTALLKEYS bit mask doesn't contain the
2162 // DLGC_WANTTAB nor DLGC_WANTARROWS bits although, logically,
2163 // it, of course, implies them
2164 if ( lDlgCode & DLGC_WANTALLKEYS )
2165 {
2166 lDlgCode |= DLGC_WANTTAB | DLGC_WANTARROWS;
2167 }
2168
2169 bool bForward = true,
2170 bWindowChange = false,
2171 bFromTab = false;
2172
2173 // should we process this message specially?
2174 bool bProcess = true;
2175 switch ( msg->wParam )
2176 {
2177 case VK_TAB:
2178 if ( (lDlgCode & DLGC_WANTTAB) && !bCtrlDown )
2179 {
2180 // let the control have the TAB
2181 bProcess = false;
2182 }
2183 else // use it for navigation
2184 {
2185 // Ctrl-Tab cycles thru notebook pages
2186 bWindowChange = bCtrlDown;
2187 bForward = !bShiftDown;
2188 bFromTab = true;
2189 }
2190 break;
2191
2192 case VK_UP:
2193 case VK_LEFT:
2194 if ( (lDlgCode & DLGC_WANTARROWS) || bCtrlDown )
2195 bProcess = false;
2196 else
2197 bForward = false;
2198 break;
2199
2200 case VK_DOWN:
2201 case VK_RIGHT:
2202 if ( (lDlgCode & DLGC_WANTARROWS) || bCtrlDown )
2203 bProcess = false;
2204 break;
2205
2206 case VK_PRIOR:
2207 bForward = false;
2208 // fall through
2209
2210 case VK_NEXT:
2211 // we treat PageUp/Dn as arrows because chances are that
2212 // a control which needs arrows also needs them for
2213 // navigation (e.g. wxTextCtrl, wxListCtrl, ...)
2214 if ( (lDlgCode & DLGC_WANTARROWS) && !bCtrlDown )
2215 bProcess = false;
2216 else // OTOH Ctrl-PageUp/Dn works as [Shift-]Ctrl-Tab
2217 bWindowChange = true;
2218 break;
2219
2220 case VK_RETURN:
2221 {
2222 if ( (lDlgCode & DLGC_WANTMESSAGE) && !bCtrlDown )
2223 {
2224 // control wants to process Enter itself, don't
2225 // call IsDialogMessage() which would consume it
2226 return false;
2227 }
2228
2229 #if wxUSE_BUTTON
2230 // currently active button should get enter press even
2231 // if there is a default button elsewhere so check if
2232 // this window is a button first
2233 wxWindow *btn = NULL;
2234 if ( lDlgCode & DLGC_DEFPUSHBUTTON )
2235 {
2236 // let IsDialogMessage() handle this for all
2237 // buttons except the owner-drawn ones which it
2238 // just seems to ignore
2239 long style = ::GetWindowLong(msg->hwnd, GWL_STYLE);
2240 if ( (style & BS_OWNERDRAW) == BS_OWNERDRAW )
2241 {
2242 // emulate the button click
2243 btn = wxFindWinFromHandle((WXHWND)msg->hwnd);
2244 }
2245
2246 bProcess = false;
2247 }
2248 else // not a button itself, do we have default button?
2249 {
2250 wxTopLevelWindow *
2251 tlw = wxDynamicCast(wxGetTopLevelParent(this),
2252 wxTopLevelWindow);
2253 if ( tlw )
2254 {
2255 btn = wxDynamicCast(tlw->GetDefaultItem(),
2256 wxButton);
2257 }
2258 }
2259
2260 if ( btn && btn->IsEnabled() )
2261 {
2262 btn->MSWCommand(BN_CLICKED, 0 /* unused */);
2263 return true;
2264 }
2265
2266 #endif // wxUSE_BUTTON
2267
2268 #ifdef __WXWINCE__
2269 // map Enter presses into button presses on PDAs
2270 wxJoystickEvent event(wxEVT_JOY_BUTTON_DOWN);
2271 event.SetEventObject(this);
2272 if ( GetEventHandler()->ProcessEvent(event) )
2273 return true;
2274 #endif // __WXWINCE__
2275 }
2276 break;
2277
2278 default:
2279 bProcess = false;
2280 }
2281
2282 if ( bProcess )
2283 {
2284 wxNavigationKeyEvent event;
2285 event.SetDirection(bForward);
2286 event.SetWindowChange(bWindowChange);
2287 event.SetFromTab(bFromTab);
2288 event.SetEventObject(this);
2289
2290 if ( GetEventHandler()->ProcessEvent(event) )
2291 {
2292 // as we don't call IsDialogMessage(), which would take of
2293 // this by default, we need to manually send this message
2294 // so that controls can change their UI state if needed
2295 MSWUpdateUIState(UIS_CLEAR, UISF_HIDEFOCUS);
2296
2297 return true;
2298 }
2299 }
2300 }
2301
2302 if ( ::IsDialogMessage(GetHwnd(), msg) )
2303 {
2304 // IsDialogMessage() did something...
2305 return true;
2306 }
2307 }
2308 #endif // __WXUNIVERSAL__
2309
2310 #if wxUSE_TOOLTIPS
2311 if ( m_tooltip )
2312 {
2313 // relay mouse move events to the tooltip control
2314 MSG *msg = (MSG *)pMsg;
2315 if ( msg->message == WM_MOUSEMOVE )
2316 wxToolTip::RelayEvent(pMsg);
2317 }
2318 #endif // wxUSE_TOOLTIPS
2319
2320 return false;
2321 }
2322
2323 bool wxWindowMSW::MSWTranslateMessage(WXMSG* pMsg)
2324 {
2325 #if wxUSE_ACCEL && !defined(__WXUNIVERSAL__)
2326 return m_acceleratorTable.Translate(this, pMsg);
2327 #else
2328 (void) pMsg;
2329 return false;
2330 #endif // wxUSE_ACCEL
2331 }
2332
2333 bool wxWindowMSW::MSWShouldPreProcessMessage(WXMSG* msg)
2334 {
2335 // all tests below have to deal with various bugs/misfeatures of
2336 // IsDialogMessage(): we have to prevent it from being called from our
2337 // MSWProcessMessage() in some situations
2338
2339 // don't let IsDialogMessage() get VK_ESCAPE as it _always_ eats the
2340 // message even when there is no cancel button and when the message is
2341 // needed by the control itself: in particular, it prevents the tree in
2342 // place edit control from being closed with Escape in a dialog
2343 if ( msg->message == WM_KEYDOWN && msg->wParam == VK_ESCAPE )
2344 {
2345 return false;
2346 }
2347
2348 // ::IsDialogMessage() is broken and may sometimes hang the application by
2349 // going into an infinite loop when it tries to find the control to give
2350 // focus to when Alt-<key> is pressed, so we try to detect [some of] the
2351 // situations when this may happen and not call it then
2352 if ( msg->message != WM_SYSCHAR )
2353 return true;
2354
2355 // assume we can call it by default
2356 bool canSafelyCallIsDlgMsg = true;
2357
2358 HWND hwndFocus = ::GetFocus();
2359
2360 // if the currently focused window itself has WS_EX_CONTROLPARENT style,
2361 // ::IsDialogMessage() will also enter an infinite loop, because it will
2362 // recursively check the child windows but not the window itself and so if
2363 // none of the children accepts focus it loops forever (as it only stops
2364 // when it gets back to the window it started from)
2365 //
2366 // while it is very unusual that a window with WS_EX_CONTROLPARENT
2367 // style has the focus, it can happen. One such possibility is if
2368 // all windows are either toplevel, wxDialog, wxPanel or static
2369 // controls and no window can actually accept keyboard input.
2370 #if !defined(__WXWINCE__)
2371 if ( ::GetWindowLong(hwndFocus, GWL_EXSTYLE) & WS_EX_CONTROLPARENT )
2372 {
2373 // pessimistic by default
2374 canSafelyCallIsDlgMsg = false;
2375 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
2376 node;
2377 node = node->GetNext() )
2378 {
2379 wxWindow * const win = node->GetData();
2380 if ( win->CanAcceptFocus() &&
2381 !(::GetWindowLong(GetHwndOf(win), GWL_EXSTYLE) &
2382 WS_EX_CONTROLPARENT) )
2383 {
2384 // it shouldn't hang...
2385 canSafelyCallIsDlgMsg = true;
2386
2387 break;
2388 }
2389 }
2390 }
2391 #endif // !__WXWINCE__
2392
2393 if ( canSafelyCallIsDlgMsg )
2394 {
2395 // ::IsDialogMessage() can enter in an infinite loop when the
2396 // currently focused window is disabled or hidden and its
2397 // parent has WS_EX_CONTROLPARENT style, so don't call it in
2398 // this case
2399 while ( hwndFocus )
2400 {
2401 if ( !::IsWindowEnabled(hwndFocus) ||
2402 !::IsWindowVisible(hwndFocus) )
2403 {
2404 // it would enter an infinite loop if we do this!
2405 canSafelyCallIsDlgMsg = false;
2406
2407 break;
2408 }
2409
2410 if ( !(::GetWindowLong(hwndFocus, GWL_STYLE) & WS_CHILD) )
2411 {
2412 // it's a top level window, don't go further -- e.g. even
2413 // if the parent of a dialog is disabled, this doesn't
2414 // break navigation inside the dialog
2415 break;
2416 }
2417
2418 hwndFocus = ::GetParent(hwndFocus);
2419 }
2420 }
2421
2422 return canSafelyCallIsDlgMsg;
2423 }
2424
2425 // ---------------------------------------------------------------------------
2426 // message params unpackers
2427 // ---------------------------------------------------------------------------
2428
2429 void wxWindowMSW::UnpackCommand(WXWPARAM wParam, WXLPARAM lParam,
2430 WORD *id, WXHWND *hwnd, WORD *cmd)
2431 {
2432 *id = LOWORD(wParam);
2433 *hwnd = (WXHWND)lParam;
2434 *cmd = HIWORD(wParam);
2435 }
2436
2437 void wxWindowMSW::UnpackActivate(WXWPARAM wParam, WXLPARAM lParam,
2438 WXWORD *state, WXWORD *minimized, WXHWND *hwnd)
2439 {
2440 *state = LOWORD(wParam);
2441 *minimized = HIWORD(wParam);
2442 *hwnd = (WXHWND)lParam;
2443 }
2444
2445 void wxWindowMSW::UnpackScroll(WXWPARAM wParam, WXLPARAM lParam,
2446 WXWORD *code, WXWORD *pos, WXHWND *hwnd)
2447 {
2448 *code = LOWORD(wParam);
2449 *pos = HIWORD(wParam);
2450 *hwnd = (WXHWND)lParam;
2451 }
2452
2453 void wxWindowMSW::UnpackCtlColor(WXWPARAM wParam, WXLPARAM lParam,
2454 WXHDC *hdc, WXHWND *hwnd)
2455 {
2456 *hwnd = (WXHWND)lParam;
2457 *hdc = (WXHDC)wParam;
2458 }
2459
2460 void wxWindowMSW::UnpackMenuSelect(WXWPARAM wParam, WXLPARAM lParam,
2461 WXWORD *item, WXWORD *flags, WXHMENU *hmenu)
2462 {
2463 *item = (WXWORD)wParam;
2464 *flags = HIWORD(wParam);
2465 *hmenu = (WXHMENU)lParam;
2466 }
2467
2468 // ---------------------------------------------------------------------------
2469 // Main wxWidgets window proc and the window proc for wxWindow
2470 // ---------------------------------------------------------------------------
2471
2472 // Hook for new window just as it's being created, when the window isn't yet
2473 // associated with the handle
2474 static wxWindowMSW *gs_winBeingCreated = NULL;
2475
2476 // implementation of wxWindowCreationHook class: it just sets gs_winBeingCreated to the
2477 // window being created and insures that it's always unset back later
2478 wxWindowCreationHook::wxWindowCreationHook(wxWindowMSW *winBeingCreated)
2479 {
2480 gs_winBeingCreated = winBeingCreated;
2481 }
2482
2483 wxWindowCreationHook::~wxWindowCreationHook()
2484 {
2485 gs_winBeingCreated = NULL;
2486 }
2487
2488 // Main window proc
2489 LRESULT WXDLLEXPORT APIENTRY _EXPORT wxWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
2490 {
2491 // trace all messages - useful for the debugging
2492 #ifdef __WXDEBUG__
2493 wxLogTrace(wxTraceMessages,
2494 wxT("Processing %s(hWnd=%08lx, wParam=%8lx, lParam=%8lx)"),
2495 wxGetMessageName(message), (long)hWnd, (long)wParam, lParam);
2496 #endif // __WXDEBUG__
2497
2498 wxWindowMSW *wnd = wxFindWinFromHandle((WXHWND) hWnd);
2499
2500 // when we get the first message for the HWND we just created, we associate
2501 // it with wxWindow stored in gs_winBeingCreated
2502 if ( !wnd && gs_winBeingCreated )
2503 {
2504 wxAssociateWinWithHandle(hWnd, gs_winBeingCreated);
2505 wnd = gs_winBeingCreated;
2506 gs_winBeingCreated = NULL;
2507 wnd->SetHWND((WXHWND)hWnd);
2508 }
2509
2510 LRESULT rc;
2511
2512 if ( wnd && wxGUIEventLoop::AllowProcessing(wnd) )
2513 rc = wnd->MSWWindowProc(message, wParam, lParam);
2514 else
2515 rc = ::DefWindowProc(hWnd, message, wParam, lParam);
2516
2517 return rc;
2518 }
2519
2520 WXLRESULT wxWindowMSW::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM lParam)
2521 {
2522 // did we process the message?
2523 bool processed = false;
2524
2525 // the return value
2526 union
2527 {
2528 bool allow;
2529 WXLRESULT result;
2530 WXHBRUSH hBrush;
2531 } rc;
2532
2533 // for most messages we should return 0 when we do process the message
2534 rc.result = 0;
2535
2536 switch ( message )
2537 {
2538 case WM_CREATE:
2539 {
2540 bool mayCreate;
2541 processed = HandleCreate((WXLPCREATESTRUCT)lParam, &mayCreate);
2542 if ( processed )
2543 {
2544 // return 0 to allow window creation
2545 rc.result = mayCreate ? 0 : -1;
2546 }
2547 }
2548 break;
2549
2550 case WM_DESTROY:
2551 // never set processed to true and *always* pass WM_DESTROY to
2552 // DefWindowProc() as Windows may do some internal cleanup when
2553 // processing it and failing to pass the message along may cause
2554 // memory and resource leaks!
2555 (void)HandleDestroy();
2556 break;
2557
2558 case WM_SIZE:
2559 processed = HandleSize(LOWORD(lParam), HIWORD(lParam), wParam);
2560 break;
2561
2562 case WM_MOVE:
2563 processed = HandleMove(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
2564 break;
2565
2566 #if !defined(__WXWINCE__)
2567 case WM_MOVING:
2568 {
2569 LPRECT pRect = (LPRECT)lParam;
2570 wxRect rc;
2571 rc.SetLeft(pRect->left);
2572 rc.SetTop(pRect->top);
2573 rc.SetRight(pRect->right);
2574 rc.SetBottom(pRect->bottom);
2575 processed = HandleMoving(rc);
2576 if (processed) {
2577 pRect->left = rc.GetLeft();
2578 pRect->top = rc.GetTop();
2579 pRect->right = rc.GetRight();
2580 pRect->bottom = rc.GetBottom();
2581 }
2582 }
2583 break;
2584 #if 0
2585 case WM_ENTERSIZEMOVE:
2586 {
2587 processed = HandleEnterSizeMove();
2588 }
2589 break;
2590
2591 case WM_EXITSIZEMOVE:
2592 {
2593 processed = HandleExitSizeMove();
2594 }
2595 break;
2596 #endif
2597 case WM_SIZING:
2598 {
2599 LPRECT pRect = (LPRECT)lParam;
2600 wxRect rc;
2601 rc.SetLeft(pRect->left);
2602 rc.SetTop(pRect->top);
2603 rc.SetRight(pRect->right);
2604 rc.SetBottom(pRect->bottom);
2605 processed = HandleSizing(rc);
2606 if (processed) {
2607 pRect->left = rc.GetLeft();
2608 pRect->top = rc.GetTop();
2609 pRect->right = rc.GetRight();
2610 pRect->bottom = rc.GetBottom();
2611 }
2612 }
2613 break;
2614 #endif // !__WXWINCE__
2615
2616 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
2617 case WM_ACTIVATEAPP:
2618 // This implicitly sends a wxEVT_ACTIVATE_APP event
2619 wxTheApp->SetActive(wParam != 0, FindFocus());
2620 break;
2621 #endif
2622
2623 case WM_ACTIVATE:
2624 {
2625 WXWORD state, minimized;
2626 WXHWND hwnd;
2627 UnpackActivate(wParam, lParam, &state, &minimized, &hwnd);
2628
2629 processed = HandleActivate(state, minimized != 0, (WXHWND)hwnd);
2630 }
2631 break;
2632
2633 case WM_SETFOCUS:
2634 processed = HandleSetFocus((WXHWND)(HWND)wParam);
2635 break;
2636
2637 case WM_KILLFOCUS:
2638 processed = HandleKillFocus((WXHWND)(HWND)wParam);
2639 break;
2640
2641 case WM_PRINTCLIENT:
2642 processed = HandlePrintClient((WXHDC)wParam);
2643 break;
2644
2645 case WM_PAINT:
2646 if ( wParam )
2647 {
2648 wxPaintDCEx dc((wxWindow *)this, (WXHDC)wParam);
2649
2650 processed = HandlePaint();
2651 }
2652 else // no DC given
2653 {
2654 processed = HandlePaint();
2655 }
2656 break;
2657
2658 case WM_CLOSE:
2659 #ifdef __WXUNIVERSAL__
2660 // Universal uses its own wxFrame/wxDialog, so we don't receive
2661 // close events unless we have this.
2662 Close();
2663 #endif // __WXUNIVERSAL__
2664
2665 // don't let the DefWindowProc() destroy our window - we'll do it
2666 // ourselves in ~wxWindow
2667 processed = true;
2668 rc.result = TRUE;
2669 break;
2670
2671 case WM_SHOWWINDOW:
2672 processed = HandleShow(wParam != 0, (int)lParam);
2673 break;
2674
2675 case WM_MOUSEMOVE:
2676 processed = HandleMouseMove(GET_X_LPARAM(lParam),
2677 GET_Y_LPARAM(lParam),
2678 wParam);
2679 break;
2680
2681 #ifdef HAVE_TRACKMOUSEEVENT
2682 case WM_MOUSELEAVE:
2683 // filter out excess WM_MOUSELEAVE events sent after PopupMenu()
2684 // (on XP at least)
2685 if ( m_mouseInWindow )
2686 {
2687 GenerateMouseLeave();
2688 }
2689
2690 // always pass processed back as false, this allows the window
2691 // manager to process the message too. This is needed to
2692 // ensure windows XP themes work properly as the mouse moves
2693 // over widgets like buttons. So don't set processed to true here.
2694 break;
2695 #endif // HAVE_TRACKMOUSEEVENT
2696
2697 #if wxUSE_MOUSEWHEEL
2698 case WM_MOUSEWHEEL:
2699 processed = HandleMouseWheel(wParam, lParam);
2700 break;
2701 #endif
2702
2703 case WM_LBUTTONDOWN:
2704 case WM_LBUTTONUP:
2705 case WM_LBUTTONDBLCLK:
2706 case WM_RBUTTONDOWN:
2707 case WM_RBUTTONUP:
2708 case WM_RBUTTONDBLCLK:
2709 case WM_MBUTTONDOWN:
2710 case WM_MBUTTONUP:
2711 case WM_MBUTTONDBLCLK:
2712 #ifdef wxHAS_XBUTTON
2713 case WM_XBUTTONDOWN:
2714 case WM_XBUTTONUP:
2715 case WM_XBUTTONDBLCLK:
2716 #endif // wxHAS_XBUTTON
2717 {
2718 #ifdef __WXMICROWIN__
2719 // MicroWindows seems to ignore the fact that a window is
2720 // disabled. So catch mouse events and throw them away if
2721 // necessary.
2722 wxWindowMSW* win = this;
2723 for ( ;; )
2724 {
2725 if (!win->IsEnabled())
2726 {
2727 processed = true;
2728 break;
2729 }
2730
2731 win = win->GetParent();
2732 if ( !win || win->IsTopLevel() )
2733 break;
2734 }
2735
2736 if ( processed )
2737 break;
2738
2739 #endif // __WXMICROWIN__
2740 int x = GET_X_LPARAM(lParam),
2741 y = GET_Y_LPARAM(lParam);
2742
2743 #ifdef __WXWINCE__
2744 // redirect the event to a static control if necessary by
2745 // finding one under mouse because under CE the static controls
2746 // don't generate mouse events (even with SS_NOTIFY)
2747 wxWindowMSW *win;
2748 if ( GetCapture() == this )
2749 {
2750 // but don't do it if the mouse is captured by this window
2751 // because then it should really get this event itself
2752 win = this;
2753 }
2754 else
2755 {
2756 win = FindWindowForMouseEvent(this, &x, &y);
2757
2758 // this should never happen
2759 wxCHECK_MSG( win, 0,
2760 _T("FindWindowForMouseEvent() returned NULL") );
2761 }
2762 #ifdef __POCKETPC__
2763 if (IsContextMenuEnabled() && message == WM_LBUTTONDOWN)
2764 {
2765 SHRGINFO shrgi = {0};
2766
2767 shrgi.cbSize = sizeof(SHRGINFO);
2768 shrgi.hwndClient = (HWND) GetHWND();
2769 shrgi.ptDown.x = x;
2770 shrgi.ptDown.y = y;
2771
2772 shrgi.dwFlags = SHRG_RETURNCMD;
2773 // shrgi.dwFlags = SHRG_NOTIFYPARENT;
2774
2775 if (GN_CONTEXTMENU == ::SHRecognizeGesture(&shrgi))
2776 {
2777 wxPoint pt(x, y);
2778 pt = ClientToScreen(pt);
2779
2780 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, GetId(), pt);
2781
2782 evtCtx.SetEventObject(this);
2783 if (GetEventHandler()->ProcessEvent(evtCtx))
2784 {
2785 processed = true;
2786 return true;
2787 }
2788 }
2789 }
2790 #endif
2791
2792 #else // !__WXWINCE__
2793 wxWindowMSW *win = this;
2794 #endif // __WXWINCE__/!__WXWINCE__
2795
2796 processed = win->HandleMouseEvent(message, x, y, wParam);
2797
2798 // if the app didn't eat the event, handle it in the default
2799 // way, that is by giving this window the focus
2800 if ( !processed )
2801 {
2802 // for the standard classes their WndProc sets the focus to
2803 // them anyhow and doing it from here results in some weird
2804 // problems, so don't do it for them (unnecessary anyhow)
2805 if ( !win->IsOfStandardClass() )
2806 {
2807 if ( message == WM_LBUTTONDOWN && win->IsFocusable() )
2808 win->SetFocus();
2809 }
2810 }
2811 }
2812 break;
2813
2814 #ifdef MM_JOY1MOVE
2815 case MM_JOY1MOVE:
2816 case MM_JOY2MOVE:
2817 case MM_JOY1ZMOVE:
2818 case MM_JOY2ZMOVE:
2819 case MM_JOY1BUTTONDOWN:
2820 case MM_JOY2BUTTONDOWN:
2821 case MM_JOY1BUTTONUP:
2822 case MM_JOY2BUTTONUP:
2823 processed = HandleJoystickEvent(message,
2824 GET_X_LPARAM(lParam),
2825 GET_Y_LPARAM(lParam),
2826 wParam);
2827 break;
2828 #endif // __WXMICROWIN__
2829
2830 case WM_COMMAND:
2831 {
2832 WORD id, cmd;
2833 WXHWND hwnd;
2834 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
2835
2836 processed = HandleCommand(id, cmd, hwnd);
2837 }
2838 break;
2839
2840 case WM_NOTIFY:
2841 processed = HandleNotify((int)wParam, lParam, &rc.result);
2842 break;
2843
2844 // we only need to reply to WM_NOTIFYFORMAT manually when using MSLU,
2845 // otherwise DefWindowProc() does it perfectly fine for us, but MSLU
2846 // apparently doesn't always behave properly and needs some help
2847 #if wxUSE_UNICODE_MSLU && defined(NF_QUERY)
2848 case WM_NOTIFYFORMAT:
2849 if ( lParam == NF_QUERY )
2850 {
2851 processed = true;
2852 rc.result = NFR_UNICODE;
2853 }
2854 break;
2855 #endif // wxUSE_UNICODE_MSLU
2856
2857 // for these messages we must return true if process the message
2858 #ifdef WM_DRAWITEM
2859 case WM_DRAWITEM:
2860 case WM_MEASUREITEM:
2861 {
2862 int idCtrl = (UINT)wParam;
2863 if ( message == WM_DRAWITEM )
2864 {
2865 processed = MSWOnDrawItem(idCtrl,
2866 (WXDRAWITEMSTRUCT *)lParam);
2867 }
2868 else
2869 {
2870 processed = MSWOnMeasureItem(idCtrl,
2871 (WXMEASUREITEMSTRUCT *)lParam);
2872 }
2873
2874 if ( processed )
2875 rc.result = TRUE;
2876 }
2877 break;
2878 #endif // defined(WM_DRAWITEM)
2879
2880 case WM_GETDLGCODE:
2881 if ( !IsOfStandardClass() || HasFlag(wxWANTS_CHARS) )
2882 {
2883 // we always want to get the char events
2884 rc.result = DLGC_WANTCHARS;
2885
2886 if ( HasFlag(wxWANTS_CHARS) )
2887 {
2888 // in fact, we want everything
2889 rc.result |= DLGC_WANTARROWS |
2890 DLGC_WANTTAB |
2891 DLGC_WANTALLKEYS;
2892 }
2893
2894 processed = true;
2895 }
2896 //else: get the dlg code from the DefWindowProc()
2897 break;
2898
2899 case WM_SYSKEYDOWN:
2900 case WM_KEYDOWN:
2901 // If this has been processed by an event handler, return 0 now
2902 // (we've handled it).
2903 m_lastKeydownProcessed = HandleKeyDown((WORD) wParam, lParam);
2904 if ( m_lastKeydownProcessed )
2905 {
2906 processed = true;
2907 }
2908
2909 if ( !processed )
2910 {
2911 switch ( wParam )
2912 {
2913 // we consider these messages "not interesting" to OnChar, so
2914 // just don't do anything more with them
2915 case VK_SHIFT:
2916 case VK_CONTROL:
2917 case VK_MENU:
2918 case VK_CAPITAL:
2919 case VK_NUMLOCK:
2920 case VK_SCROLL:
2921 processed = true;
2922 break;
2923
2924 // avoid duplicate messages to OnChar for these ASCII keys:
2925 // they will be translated by TranslateMessage() and received
2926 // in WM_CHAR
2927 case VK_ESCAPE:
2928 case VK_SPACE:
2929 case VK_RETURN:
2930 case VK_BACK:
2931 case VK_TAB:
2932 case VK_ADD:
2933 case VK_SUBTRACT:
2934 case VK_MULTIPLY:
2935 case VK_DIVIDE:
2936 case VK_NUMPAD0:
2937 case VK_NUMPAD1:
2938 case VK_NUMPAD2:
2939 case VK_NUMPAD3:
2940 case VK_NUMPAD4:
2941 case VK_NUMPAD5:
2942 case VK_NUMPAD6:
2943 case VK_NUMPAD7:
2944 case VK_NUMPAD8:
2945 case VK_NUMPAD9:
2946 case VK_OEM_1:
2947 case VK_OEM_2:
2948 case VK_OEM_3:
2949 case VK_OEM_4:
2950 case VK_OEM_5:
2951 case VK_OEM_6:
2952 case VK_OEM_7:
2953 case VK_OEM_PLUS:
2954 case VK_OEM_COMMA:
2955 case VK_OEM_MINUS:
2956 case VK_OEM_PERIOD:
2957 // but set processed to false, not true to still pass them
2958 // to the control's default window proc - otherwise
2959 // built-in keyboard handling won't work
2960 processed = false;
2961 break;
2962
2963 #ifdef VK_APPS
2964 // special case of VK_APPS: treat it the same as right mouse
2965 // click because both usually pop up a context menu
2966 case VK_APPS:
2967 processed = HandleMouseEvent(WM_RBUTTONDOWN, -1, -1, 0);
2968 break;
2969 #endif // VK_APPS
2970
2971 default:
2972 // do generate a CHAR event
2973 processed = HandleChar((WORD)wParam, lParam);
2974 }
2975 }
2976 if (message == WM_SYSKEYDOWN) // Let Windows still handle the SYSKEYs
2977 processed = false;
2978 break;
2979
2980 case WM_SYSKEYUP:
2981 case WM_KEYUP:
2982 #ifdef VK_APPS
2983 // special case of VK_APPS: treat it the same as right mouse button
2984 if ( wParam == VK_APPS )
2985 {
2986 processed = HandleMouseEvent(WM_RBUTTONUP, -1, -1, 0);
2987 }
2988 else
2989 #endif // VK_APPS
2990 {
2991 processed = HandleKeyUp((WORD) wParam, lParam);
2992 }
2993 break;
2994
2995 case WM_SYSCHAR:
2996 case WM_CHAR: // Always an ASCII character
2997 if ( m_lastKeydownProcessed )
2998 {
2999 // The key was handled in the EVT_KEY_DOWN and handling
3000 // a key in an EVT_KEY_DOWN handler is meant, by
3001 // design, to prevent EVT_CHARs from happening
3002 m_lastKeydownProcessed = false;
3003 processed = true;
3004 }
3005 else
3006 {
3007 processed = HandleChar((WORD)wParam, lParam, true);
3008 }
3009 break;
3010
3011 #if wxUSE_HOTKEY
3012 case WM_HOTKEY:
3013 processed = HandleHotKey((WORD)wParam, lParam);
3014 break;
3015 #endif // wxUSE_HOTKEY
3016
3017 case WM_HSCROLL:
3018 case WM_VSCROLL:
3019 {
3020 WXWORD code, pos;
3021 WXHWND hwnd;
3022 UnpackScroll(wParam, lParam, &code, &pos, &hwnd);
3023
3024 processed = MSWOnScroll(message == WM_HSCROLL ? wxHORIZONTAL
3025 : wxVERTICAL,
3026 code, pos, hwnd);
3027 }
3028 break;
3029
3030 // CTLCOLOR messages are sent by children to query the parent for their
3031 // colors
3032 #ifndef __WXMICROWIN__
3033 case WM_CTLCOLORMSGBOX:
3034 case WM_CTLCOLOREDIT:
3035 case WM_CTLCOLORLISTBOX:
3036 case WM_CTLCOLORBTN:
3037 case WM_CTLCOLORDLG:
3038 case WM_CTLCOLORSCROLLBAR:
3039 case WM_CTLCOLORSTATIC:
3040 {
3041 WXHDC hdc;
3042 WXHWND hwnd;
3043 UnpackCtlColor(wParam, lParam, &hdc, &hwnd);
3044
3045 processed = HandleCtlColor(&rc.hBrush, (WXHDC)hdc, (WXHWND)hwnd);
3046 }
3047 break;
3048 #endif // !__WXMICROWIN__
3049
3050 case WM_SYSCOLORCHANGE:
3051 // the return value for this message is ignored
3052 processed = HandleSysColorChange();
3053 break;
3054
3055 #if !defined(__WXWINCE__)
3056 case WM_DISPLAYCHANGE:
3057 processed = HandleDisplayChange();
3058 break;
3059 #endif
3060
3061 case WM_PALETTECHANGED:
3062 processed = HandlePaletteChanged((WXHWND) (HWND) wParam);
3063 break;
3064
3065 case WM_CAPTURECHANGED:
3066 processed = HandleCaptureChanged((WXHWND) (HWND) lParam);
3067 break;
3068
3069 case WM_SETTINGCHANGE:
3070 processed = HandleSettingChange(wParam, lParam);
3071 break;
3072
3073 case WM_QUERYNEWPALETTE:
3074 processed = HandleQueryNewPalette();
3075 break;
3076
3077 case WM_ERASEBKGND:
3078 processed = HandleEraseBkgnd((WXHDC)(HDC)wParam);
3079 if ( processed )
3080 {
3081 // we processed the message, i.e. erased the background
3082 rc.result = TRUE;
3083 }
3084 break;
3085
3086 #if !defined(__WXWINCE__)
3087 case WM_DROPFILES:
3088 processed = HandleDropFiles(wParam);
3089 break;
3090 #endif
3091
3092 case WM_INITDIALOG:
3093 processed = HandleInitDialog((WXHWND)(HWND)wParam);
3094
3095 if ( processed )
3096 {
3097 // we never set focus from here
3098 rc.result = FALSE;
3099 }
3100 break;
3101
3102 #if !defined(__WXWINCE__)
3103 case WM_QUERYENDSESSION:
3104 processed = HandleQueryEndSession(lParam, &rc.allow);
3105 break;
3106
3107 case WM_ENDSESSION:
3108 processed = HandleEndSession(wParam != 0, lParam);
3109 break;
3110
3111 case WM_GETMINMAXINFO:
3112 processed = HandleGetMinMaxInfo((MINMAXINFO*)lParam);
3113 break;
3114 #endif
3115
3116 case WM_SETCURSOR:
3117 processed = HandleSetCursor((WXHWND)(HWND)wParam,
3118 LOWORD(lParam), // hit test
3119 HIWORD(lParam)); // mouse msg
3120
3121 if ( processed )
3122 {
3123 // returning TRUE stops the DefWindowProc() from further
3124 // processing this message - exactly what we need because we've
3125 // just set the cursor.
3126 rc.result = TRUE;
3127 }
3128 break;
3129
3130 #if wxUSE_ACCESSIBILITY
3131 case WM_GETOBJECT:
3132 {
3133 //WPARAM dwFlags = (WPARAM) (DWORD) wParam;
3134 LPARAM dwObjId = (LPARAM) (DWORD) lParam;
3135
3136 if (dwObjId == (LPARAM)OBJID_CLIENT && GetOrCreateAccessible())
3137 {
3138 return LresultFromObject(IID_IAccessible, wParam, (IUnknown*) GetAccessible()->GetIAccessible());
3139 }
3140 break;
3141 }
3142 #endif
3143
3144 #if defined(WM_HELP)
3145 case WM_HELP:
3146 {
3147 // by default, WM_HELP is propagated by DefWindowProc() upwards
3148 // to the window parent but as we do it ourselves already
3149 // (wxHelpEvent is derived from wxCommandEvent), we don't want
3150 // to get the other events if we process this message at all
3151 processed = true;
3152
3153 // WM_HELP doesn't use lParam under CE
3154 #ifndef __WXWINCE__
3155 HELPINFO* info = (HELPINFO*) lParam;
3156 if ( info->iContextType == HELPINFO_WINDOW )
3157 {
3158 #endif // !__WXWINCE__
3159 wxHelpEvent helpEvent
3160 (
3161 wxEVT_HELP,
3162 GetId(),
3163 #ifdef __WXWINCE__
3164 wxGetMousePosition() // what else?
3165 #else
3166 wxPoint(info->MousePos.x, info->MousePos.y)
3167 #endif
3168 );
3169
3170 helpEvent.SetEventObject(this);
3171 GetEventHandler()->ProcessEvent(helpEvent);
3172 #ifndef __WXWINCE__
3173 }
3174 else if ( info->iContextType == HELPINFO_MENUITEM )
3175 {
3176 wxHelpEvent helpEvent(wxEVT_HELP, info->iCtrlId);
3177 helpEvent.SetEventObject(this);
3178 GetEventHandler()->ProcessEvent(helpEvent);
3179
3180 }
3181 else // unknown help event?
3182 {
3183 processed = false;
3184 }
3185 #endif // !__WXWINCE__
3186 }
3187 break;
3188 #endif // WM_HELP
3189
3190 #if !defined(__WXWINCE__)
3191 case WM_CONTEXTMENU:
3192 {
3193 // we don't convert from screen to client coordinates as
3194 // the event may be handled by a parent window
3195 wxPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
3196
3197 wxContextMenuEvent evtCtx(wxEVT_CONTEXT_MENU, GetId(), pt);
3198
3199 // we could have got an event from our child, reflect it back
3200 // to it if this is the case
3201 wxWindowMSW *win = NULL;
3202 if ( (WXHWND)wParam != m_hWnd )
3203 {
3204 win = FindItemByHWND((WXHWND)wParam);
3205 }
3206
3207 if ( !win )
3208 win = this;
3209
3210 evtCtx.SetEventObject(win);
3211 processed = win->GetEventHandler()->ProcessEvent(evtCtx);
3212 }
3213 break;
3214 #endif
3215
3216 #if wxUSE_MENUS
3217 case WM_MENUCHAR:
3218 // we're only interested in our own menus, not MF_SYSMENU
3219 if ( HIWORD(wParam) == MF_POPUP )
3220 {
3221 // handle menu chars for ownerdrawn menu items
3222 int i = HandleMenuChar(toupper(LOWORD(wParam)), lParam);
3223 if ( i != wxNOT_FOUND )
3224 {
3225 rc.result = MAKELRESULT(i, MNC_EXECUTE);
3226 processed = true;
3227 }
3228 }
3229 break;
3230 #endif // wxUSE_MENUS
3231
3232 #ifndef __WXWINCE__
3233 case WM_POWERBROADCAST:
3234 {
3235 bool vetoed;
3236 processed = HandlePower(wParam, lParam, &vetoed);
3237 rc.result = processed && vetoed ? BROADCAST_QUERY_DENY : TRUE;
3238 }
3239 break;
3240 #endif // __WXWINCE__
3241
3242 #if wxUSE_UXTHEME
3243 // If we want the default themed border then we need to draw it ourselves
3244 case WM_NCCALCSIZE:
3245 {
3246 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
3247 if (theme && GetBorder() == wxBORDER_THEME)
3248 {
3249 // first ask the widget to calculate the border size
3250 rc.result = MSWDefWindowProc(message, wParam, lParam);
3251 processed = true;
3252
3253 // now alter the client size making room for drawing a themed border
3254 NCCALCSIZE_PARAMS *csparam = NULL;
3255 RECT rect;
3256 if (wParam)
3257 {
3258 csparam = (NCCALCSIZE_PARAMS*)lParam;
3259 rect = csparam->rgrc[0];
3260 }
3261 else
3262 {
3263 rect = *((RECT*)lParam);
3264 }
3265 wxUxThemeHandle hTheme(this, L"EDIT");
3266 RECT rcClient = { 0, 0, 0, 0 };
3267 wxClientDC dc(this);
3268
3269 if (theme->GetThemeBackgroundContentRect(
3270 hTheme, GetHdcOf(dc), EP_EDITTEXT, ETS_NORMAL,
3271 &rect, &rcClient) == S_OK)
3272 {
3273 InflateRect(&rcClient, -1, -1);
3274 if (wParam)
3275 csparam->rgrc[0] = rcClient;
3276 else
3277 *((RECT*)lParam) = rcClient;
3278 rc.result = WVR_REDRAW;
3279 }
3280 }
3281 }
3282 break;
3283
3284 case WM_NCPAINT:
3285 {
3286 wxUxThemeEngine* theme = wxUxThemeEngine::GetIfActive();
3287 if (theme && GetBorder() == wxBORDER_THEME)
3288 {
3289 // first ask the widget to paint its non-client area, such as scrollbars, etc.
3290 rc.result = MSWDefWindowProc(message, wParam, lParam);
3291 processed = true;
3292
3293 wxUxThemeHandle hTheme(this, L"EDIT");
3294 wxWindowDC dc(this);
3295
3296 // Clip the DC so that you only draw on the non-client area
3297 RECT rcBorder;
3298 wxCopyRectToRECT(GetSize(), rcBorder);
3299
3300 RECT rcClient;
3301 theme->GetThemeBackgroundContentRect(
3302 hTheme, GetHdcOf(dc), EP_EDITTEXT, ETS_NORMAL, &rcBorder, &rcClient);
3303 InflateRect(&rcClient, -1, -1);
3304
3305 ::ExcludeClipRect(GetHdcOf(dc), rcClient.left, rcClient.top,
3306 rcClient.right, rcClient.bottom);
3307
3308 // Make sure the background is in a proper state
3309 if (theme->IsThemeBackgroundPartiallyTransparent(hTheme, EP_EDITTEXT, ETS_NORMAL))
3310 {
3311 theme->DrawThemeParentBackground(GetHwnd(), GetHdcOf(dc), &rcBorder);
3312 }
3313
3314 // Draw the border
3315 int nState;
3316 if ( !IsEnabled() )
3317 nState = ETS_DISABLED;
3318 // should we check this?
3319 //else if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & ES_READONLY)
3320 // nState = ETS_READONLY;
3321 else
3322 nState = ETS_NORMAL;
3323 theme->DrawThemeBackground(hTheme, GetHdcOf(dc), EP_EDITTEXT, nState, &rcBorder, NULL);
3324 }
3325 }
3326 break;
3327
3328 #endif // wxUSE_UXTHEME
3329
3330 default:
3331 // try a custom message handler
3332 const MSWMessageHandlers::const_iterator
3333 i = gs_messageHandlers.find(message);
3334 if ( i != gs_messageHandlers.end() )
3335 {
3336 processed = (*i->second)(this, message, wParam, lParam);
3337 }
3338 }
3339
3340 if ( !processed )
3341 {
3342 #ifdef __WXDEBUG__
3343 wxLogTrace(wxTraceMessages, wxT("Forwarding %s to DefWindowProc."),
3344 wxGetMessageName(message));
3345 #endif // __WXDEBUG__
3346 rc.result = MSWDefWindowProc(message, wParam, lParam);
3347 }
3348
3349 return rc.result;
3350 }
3351
3352 // ----------------------------------------------------------------------------
3353 // wxWindow <-> HWND map
3354 // ----------------------------------------------------------------------------
3355
3356 wxWinHashTable *wxWinHandleHash = NULL;
3357
3358 wxWindow *wxFindWinFromHandle(WXHWND hWnd)
3359 {
3360 return (wxWindow*)wxWinHandleHash->Get((long)hWnd);
3361 }
3362
3363 void wxAssociateWinWithHandle(HWND hWnd, wxWindowMSW *win)
3364 {
3365 // adding NULL hWnd is (first) surely a result of an error and
3366 // (secondly) breaks menu command processing
3367 wxCHECK_RET( hWnd != (HWND)NULL,
3368 wxT("attempt to add a NULL hWnd to window list ignored") );
3369
3370 wxWindow *oldWin = wxFindWinFromHandle((WXHWND) hWnd);
3371 #ifdef __WXDEBUG__
3372 if ( oldWin && (oldWin != win) )
3373 {
3374 wxLogDebug(wxT("HWND %X already associated with another window (%s)"),
3375 (int) hWnd, win->GetClassInfo()->GetClassName());
3376 }
3377 else
3378 #endif // __WXDEBUG__
3379 if (!oldWin)
3380 {
3381 wxWinHandleHash->Put((long)hWnd, (wxWindow *)win);
3382 }
3383 }
3384
3385 void wxRemoveHandleAssociation(wxWindowMSW *win)
3386 {
3387 wxWinHandleHash->Delete((long)win->GetHWND());
3388 }
3389
3390 // ----------------------------------------------------------------------------
3391 // various MSW speciic class dependent functions
3392 // ----------------------------------------------------------------------------
3393
3394 // Default destroyer - override if you destroy it in some other way
3395 // (e.g. with MDI child windows)
3396 void wxWindowMSW::MSWDestroyWindow()
3397 {
3398 }
3399
3400 bool wxWindowMSW::MSWGetCreateWindowCoords(const wxPoint& pos,
3401 const wxSize& size,
3402 int& x, int& y,
3403 int& w, int& h) const
3404 {
3405 // yes, those are just some arbitrary hardcoded numbers
3406 static const int DEFAULT_Y = 200;
3407
3408 bool nonDefault = false;
3409
3410 if ( pos.x == wxDefaultCoord )
3411 {
3412 // if x is set to CW_USEDEFAULT, y parameter is ignored anyhow so we
3413 // can just as well set it to CW_USEDEFAULT as well
3414 x =
3415 y = CW_USEDEFAULT;
3416 }
3417 else
3418 {
3419 // OTOH, if x is not set to CW_USEDEFAULT, y shouldn't be set to it
3420 // neither because it is not handled as a special value by Windows then
3421 // and so we have to choose some default value for it
3422 x = pos.x;
3423 y = pos.y == wxDefaultCoord ? DEFAULT_Y : pos.y;
3424
3425 nonDefault = true;
3426 }
3427
3428 /*
3429 NB: there used to be some code here which set the initial size of the
3430 window to the client size of the parent if no explicit size was
3431 specified. This was wrong because wxWidgets programs often assume
3432 that they get a WM_SIZE (EVT_SIZE) upon creation, however this broke
3433 it. To see why, you should understand that Windows sends WM_SIZE from
3434 inside ::CreateWindow() anyhow. However, ::CreateWindow() is called
3435 from some base class ctor and so this WM_SIZE is not processed in the
3436 real class' OnSize() (because it's not fully constructed yet and the
3437 event goes to some base class OnSize() instead). So the WM_SIZE we
3438 rely on is the one sent when the parent frame resizes its children
3439 but here is the problem: if the child already has just the right
3440 size, nothing will happen as both wxWidgets and Windows check for
3441 this and ignore any attempts to change the window size to the size it
3442 already has - so no WM_SIZE would be sent.
3443 */
3444
3445
3446 // we don't use CW_USEDEFAULT here for several reasons:
3447 //
3448 // 1. it results in huge frames on modern screens (1000*800 is not
3449 // uncommon on my 1280*1024 screen) which is way too big for a half
3450 // empty frame of most of wxWidgets samples for example)
3451 //
3452 // 2. it is buggy for frames with wxFRAME_TOOL_WINDOW style for which
3453 // the default is for whatever reason 8*8 which breaks client <->
3454 // window size calculations (it would be nice if it didn't, but it
3455 // does and the simplest way to fix it seemed to change the broken
3456 // default size anyhow)
3457 //
3458 // 3. there is just no advantage in doing it: with x and y it is
3459 // possible that [future versions of] Windows position the new top
3460 // level window in some smart way which we can't do, but we can
3461 // guess a reasonably good size for a new window just as well
3462 // ourselves
3463
3464 // However, on PocketPC devices, we must use the default
3465 // size if possible.
3466 #ifdef _WIN32_WCE
3467 if (size.x == wxDefaultCoord)
3468 w = CW_USEDEFAULT;
3469 else
3470 w = size.x;
3471 if (size.y == wxDefaultCoord)
3472 h = CW_USEDEFAULT;
3473 else
3474 h = size.y;
3475 #else
3476 if ( size.x == wxDefaultCoord || size.y == wxDefaultCoord)
3477 {
3478 nonDefault = true;
3479 }
3480 w = WidthDefault(size.x);
3481 h = HeightDefault(size.y);
3482 #endif
3483
3484 AdjustForParentClientOrigin(x, y);
3485
3486 return nonDefault;
3487 }
3488
3489 WXHWND wxWindowMSW::MSWGetParent() const
3490 {
3491 return m_parent ? m_parent->GetHWND() : WXHWND(NULL);
3492 }
3493
3494 bool wxWindowMSW::MSWCreate(const wxChar *wclass,
3495 const wxChar *title,
3496 const wxPoint& pos,
3497 const wxSize& size,
3498 WXDWORD style,
3499 WXDWORD extendedStyle)
3500 {
3501 // choose the position/size for the new window
3502 int x, y, w, h;
3503 (void)MSWGetCreateWindowCoords(pos, size, x, y, w, h);
3504
3505 // controlId is menu handle for the top level windows, so set it to 0
3506 // unless we're creating a child window
3507 int controlId = style & WS_CHILD ? GetId() : 0;
3508
3509 // for each class "Foo" we have we also have "FooNR" ("no repaint") class
3510 // which is the same but without CS_[HV]REDRAW class styles so using it
3511 // ensures that the window is not fully repainted on each resize
3512 wxString className(wclass);
3513 if ( !HasFlag(wxFULL_REPAINT_ON_RESIZE) )
3514 {
3515 className += wxT("NR");
3516 }
3517
3518 // do create the window
3519 wxWindowCreationHook hook(this);
3520
3521 m_hWnd = (WXHWND)::CreateWindowEx
3522 (
3523 extendedStyle,
3524 className.wx_str(),
3525 title ? title : m_windowName.wx_str(),
3526 style,
3527 x, y, w, h,
3528 (HWND)MSWGetParent(),
3529 (HMENU)controlId,
3530 wxGetInstance(),
3531 NULL // no extra data
3532 );
3533
3534 if ( !m_hWnd )
3535 {
3536 wxLogSysError(_("Can't create window of class %s"), className.c_str());
3537
3538 return false;
3539 }
3540
3541 SubclassWin(m_hWnd);
3542
3543 return true;
3544 }
3545
3546 // ===========================================================================
3547 // MSW message handlers
3548 // ===========================================================================
3549
3550 // ---------------------------------------------------------------------------
3551 // WM_NOTIFY
3552 // ---------------------------------------------------------------------------
3553
3554 bool wxWindowMSW::HandleNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
3555 {
3556 #ifndef __WXMICROWIN__
3557 LPNMHDR hdr = (LPNMHDR)lParam;
3558 HWND hWnd = hdr->hwndFrom;
3559 wxWindow *win = wxFindWinFromHandle((WXHWND)hWnd);
3560
3561 // if the control is one of our windows, let it handle the message itself
3562 if ( win )
3563 {
3564 return win->MSWOnNotify(idCtrl, lParam, result);
3565 }
3566
3567 // VZ: why did we do it? normally this is unnecessary and, besides, it
3568 // breaks the message processing for the toolbars because the tooltip
3569 // notifications were being forwarded to the toolbar child controls
3570 // (if it had any) before being passed to the toolbar itself, so in my
3571 // example the tooltip for the combobox was always shown instead of the
3572 // correct button tooltips
3573 #if 0
3574 // try all our children
3575 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
3576 while ( node )
3577 {
3578 wxWindow *child = node->GetData();
3579 if ( child->MSWOnNotify(idCtrl, lParam, result) )
3580 {
3581 return true;
3582 }
3583
3584 node = node->GetNext();
3585 }
3586 #endif // 0
3587
3588 // by default, handle it ourselves
3589 return MSWOnNotify(idCtrl, lParam, result);
3590 #else // __WXMICROWIN__
3591 return false;
3592 #endif
3593 }
3594
3595 #if wxUSE_TOOLTIPS
3596
3597 bool wxWindowMSW::HandleTooltipNotify(WXUINT code,
3598 WXLPARAM lParam,
3599 const wxString& ttip)
3600 {
3601 // I don't know why it happens, but the versions of comctl32.dll starting
3602 // from 4.70 sometimes send TTN_NEEDTEXTW even to ANSI programs (normally,
3603 // this message is supposed to be sent to Unicode programs only) -- hence
3604 // we need to handle it as well, otherwise no tooltips will be shown in
3605 // this case
3606 #ifndef __WXWINCE__
3607 if ( !(code == (WXUINT) TTN_NEEDTEXTA || code == (WXUINT) TTN_NEEDTEXTW)
3608 || ttip.empty() )
3609 {
3610 // not a tooltip message or no tooltip to show anyhow
3611 return false;
3612 }
3613 #endif
3614
3615 LPTOOLTIPTEXT ttText = (LPTOOLTIPTEXT)lParam;
3616
3617 // We don't want to use the szText buffer because it has a limit of 80
3618 // bytes and this is not enough, especially for Unicode build where it
3619 // limits the tooltip string length to only 40 characters
3620 //
3621 // The best would be, of course, to not impose any length limitations at
3622 // all but then the buffer would have to be dynamic and someone would have
3623 // to free it and we don't have the tooltip owner object here any more, so
3624 // for now use our own static buffer with a higher fixed max length.
3625 //
3626 // Note that using a static buffer should not be a problem as only a single
3627 // tooltip can be shown at the same time anyhow.
3628 #if !wxUSE_UNICODE
3629 if ( code == (WXUINT) TTN_NEEDTEXTW )
3630 {
3631 // We need to convert tooltip from multi byte to Unicode on the fly.
3632 static wchar_t buf[513];
3633
3634 // Truncate tooltip length if needed as otherwise we might not have
3635 // enough space for it in the buffer and MultiByteToWideChar() would
3636 // return an error
3637 size_t tipLength = wxMin(ttip.length(), WXSIZEOF(buf) - 1);
3638
3639 // Convert to WideChar without adding the NULL character. The NULL
3640 // character is added afterwards (this is more efficient).
3641 int len = ::MultiByteToWideChar
3642 (
3643 CP_ACP,
3644 0, // no flags
3645 ttip.wx_str(),
3646 tipLength,
3647 buf,
3648 WXSIZEOF(buf) - 1
3649 );
3650
3651 if ( !len )
3652 {
3653 wxLogLastError(_T("MultiByteToWideChar()"));
3654 }
3655
3656 buf[len] = L'\0';
3657 ttText->lpszText = (LPSTR) buf;
3658 }
3659 else // TTN_NEEDTEXTA
3660 #endif // !wxUSE_UNICODE
3661 {
3662 // we get here if we got TTN_NEEDTEXTA (only happens in ANSI build) or
3663 // if we got TTN_NEEDTEXTW in Unicode build: in this case we just have
3664 // to copy the string we have into the buffer
3665 static wxChar buf[513];
3666 wxStrncpy(buf, ttip.c_str(), WXSIZEOF(buf) - 1);
3667 buf[WXSIZEOF(buf) - 1] = _T('\0');
3668 ttText->lpszText = buf;
3669 }
3670
3671 return true;
3672 }
3673
3674 #endif // wxUSE_TOOLTIPS
3675
3676 bool wxWindowMSW::MSWOnNotify(int WXUNUSED(idCtrl),
3677 WXLPARAM lParam,
3678 WXLPARAM* WXUNUSED(result))
3679 {
3680 #if wxUSE_TOOLTIPS
3681 if ( m_tooltip )
3682 {
3683 NMHDR* hdr = (NMHDR *)lParam;
3684 if ( HandleTooltipNotify(hdr->code, lParam, m_tooltip->GetTip()))
3685 {
3686 // processed
3687 return true;
3688 }
3689 }
3690 #else
3691 wxUnusedVar(lParam);
3692 #endif // wxUSE_TOOLTIPS
3693
3694 return false;
3695 }
3696
3697 // ---------------------------------------------------------------------------
3698 // end session messages
3699 // ---------------------------------------------------------------------------
3700
3701 bool wxWindowMSW::HandleQueryEndSession(long logOff, bool *mayEnd)
3702 {
3703 #ifdef ENDSESSION_LOGOFF
3704 wxCloseEvent event(wxEVT_QUERY_END_SESSION, wxID_ANY);
3705 event.SetEventObject(wxTheApp);
3706 event.SetCanVeto(true);
3707 event.SetLoggingOff(logOff == (long)ENDSESSION_LOGOFF);
3708
3709 bool rc = wxTheApp->ProcessEvent(event);
3710
3711 if ( rc )
3712 {
3713 // we may end only if the app didn't veto session closing (double
3714 // negation...)
3715 *mayEnd = !event.GetVeto();
3716 }
3717
3718 return rc;
3719 #else
3720 wxUnusedVar(logOff);
3721 wxUnusedVar(mayEnd);
3722 return false;
3723 #endif
3724 }
3725
3726 bool wxWindowMSW::HandleEndSession(bool endSession, long logOff)
3727 {
3728 #ifdef ENDSESSION_LOGOFF
3729 // do nothing if the session isn't ending
3730 if ( !endSession )
3731 return false;
3732
3733 // only send once
3734 if ( (this != wxTheApp->GetTopWindow()) )
3735 return false;
3736
3737 wxCloseEvent event(wxEVT_END_SESSION, wxID_ANY);
3738 event.SetEventObject(wxTheApp);
3739 event.SetCanVeto(false);
3740 event.SetLoggingOff( (logOff == (long)ENDSESSION_LOGOFF) );
3741
3742 return wxTheApp->ProcessEvent(event);
3743 #else
3744 wxUnusedVar(endSession);
3745 wxUnusedVar(logOff);
3746 return false;
3747 #endif
3748 }
3749
3750 // ---------------------------------------------------------------------------
3751 // window creation/destruction
3752 // ---------------------------------------------------------------------------
3753
3754 bool wxWindowMSW::HandleCreate(WXLPCREATESTRUCT WXUNUSED_IN_WINCE(cs),
3755 bool *mayCreate)
3756 {
3757 // VZ: why is this commented out for WinCE? If it doesn't support
3758 // WS_EX_CONTROLPARENT at all it should be somehow handled globally,
3759 // not with multiple #ifdef's!
3760 #ifndef __WXWINCE__
3761 if ( ((CREATESTRUCT *)cs)->dwExStyle & WS_EX_CONTROLPARENT )
3762 EnsureParentHasControlParentStyle(GetParent());
3763 #endif // !__WXWINCE__
3764
3765 *mayCreate = true;
3766
3767 return true;
3768 }
3769
3770 bool wxWindowMSW::HandleDestroy()
3771 {
3772 SendDestroyEvent();
3773
3774 // delete our drop target if we've got one
3775 #if wxUSE_DRAG_AND_DROP
3776 if ( m_dropTarget != NULL )
3777 {
3778 m_dropTarget->Revoke(m_hWnd);
3779
3780 delete m_dropTarget;
3781 m_dropTarget = NULL;
3782 }
3783 #endif // wxUSE_DRAG_AND_DROP
3784
3785 // WM_DESTROY handled
3786 return true;
3787 }
3788
3789 // ---------------------------------------------------------------------------
3790 // activation/focus
3791 // ---------------------------------------------------------------------------
3792
3793 bool wxWindowMSW::HandleActivate(int state,
3794 bool WXUNUSED(minimized),
3795 WXHWND WXUNUSED(activate))
3796 {
3797 wxActivateEvent event(wxEVT_ACTIVATE,
3798 (state == WA_ACTIVE) || (state == WA_CLICKACTIVE),
3799 m_windowId);
3800 event.SetEventObject(this);
3801
3802 return GetEventHandler()->ProcessEvent(event);
3803 }
3804
3805 bool wxWindowMSW::HandleSetFocus(WXHWND hwnd)
3806 {
3807 // Strangly enough, some controls get set focus events when they are being
3808 // deleted, even if they already had focus before.
3809 if ( m_isBeingDeleted )
3810 {
3811 return false;
3812 }
3813
3814 // notify the parent keeping track of focus for the kbd navigation
3815 // purposes that we got it
3816 wxChildFocusEvent eventFocus((wxWindow *)this);
3817 (void)GetEventHandler()->ProcessEvent(eventFocus);
3818
3819 #if wxUSE_CARET
3820 // Deal with caret
3821 if ( m_caret )
3822 {
3823 m_caret->OnSetFocus();
3824 }
3825 #endif // wxUSE_CARET
3826
3827 #if wxUSE_TEXTCTRL
3828 // If it's a wxTextCtrl don't send the event as it will be done
3829 // after the control gets to process it from EN_FOCUS handler
3830 if ( wxDynamicCastThis(wxTextCtrl) )
3831 {
3832 return false;
3833 }
3834 #endif // wxUSE_TEXTCTRL
3835
3836 wxFocusEvent event(wxEVT_SET_FOCUS, m_windowId);
3837 event.SetEventObject(this);
3838
3839 // wxFindWinFromHandle() may return NULL, it is ok
3840 event.SetWindow(wxFindWinFromHandle(hwnd));
3841
3842 return GetEventHandler()->ProcessEvent(event);
3843 }
3844
3845 bool wxWindowMSW::HandleKillFocus(WXHWND hwnd)
3846 {
3847 #if wxUSE_CARET
3848 // Deal with caret
3849 if ( m_caret )
3850 {
3851 m_caret->OnKillFocus();
3852 }
3853 #endif // wxUSE_CARET
3854
3855 #if wxUSE_TEXTCTRL
3856 // If it's a wxTextCtrl don't send the event as it will be done
3857 // after the control gets to process it.
3858 wxTextCtrl *ctrl = wxDynamicCastThis(wxTextCtrl);
3859 if ( ctrl )
3860 {
3861 return false;
3862 }
3863 #endif
3864
3865 // Don't send the event when in the process of being deleted. This can
3866 // only cause problems if the event handler tries to access the object.
3867 if ( m_isBeingDeleted )
3868 {
3869 return false;
3870 }
3871
3872 wxFocusEvent event(wxEVT_KILL_FOCUS, m_windowId);
3873 event.SetEventObject(this);
3874
3875 // wxFindWinFromHandle() may return NULL, it is ok
3876 event.SetWindow(wxFindWinFromHandle(hwnd));
3877
3878 return GetEventHandler()->ProcessEvent(event);
3879 }
3880
3881 // ---------------------------------------------------------------------------
3882 // labels
3883 // ---------------------------------------------------------------------------
3884
3885 void wxWindowMSW::SetLabel( const wxString& label)
3886 {
3887 SetWindowText(GetHwnd(), label.c_str());
3888 }
3889
3890 wxString wxWindowMSW::GetLabel() const
3891 {
3892 return wxGetWindowText(GetHWND());
3893 }
3894
3895 // ---------------------------------------------------------------------------
3896 // miscellaneous
3897 // ---------------------------------------------------------------------------
3898
3899 bool wxWindowMSW::HandleShow(bool show, int WXUNUSED(status))
3900 {
3901 wxShowEvent event(GetId(), show);
3902 event.SetEventObject(this);
3903
3904 return GetEventHandler()->ProcessEvent(event);
3905 }
3906
3907 bool wxWindowMSW::HandleInitDialog(WXHWND WXUNUSED(hWndFocus))
3908 {
3909 wxInitDialogEvent event(GetId());
3910 event.SetEventObject(this);
3911
3912 return GetEventHandler()->ProcessEvent(event);
3913 }
3914
3915 bool wxWindowMSW::HandleDropFiles(WXWPARAM wParam)
3916 {
3917 #if defined (__WXMICROWIN__) || defined(__WXWINCE__)
3918 wxUnusedVar(wParam);
3919 return false;
3920 #else // __WXMICROWIN__
3921 HDROP hFilesInfo = (HDROP) wParam;
3922
3923 // Get the total number of files dropped
3924 UINT gwFilesDropped = ::DragQueryFile
3925 (
3926 (HDROP)hFilesInfo,
3927 (UINT)-1,
3928 (LPTSTR)0,
3929 (UINT)0
3930 );
3931
3932 wxString *files = new wxString[gwFilesDropped];
3933 for ( UINT wIndex = 0; wIndex < gwFilesDropped; wIndex++ )
3934 {
3935 // first get the needed buffer length (+1 for terminating NUL)
3936 size_t len = ::DragQueryFile(hFilesInfo, wIndex, NULL, 0) + 1;
3937
3938 // and now get the file name
3939 ::DragQueryFile(hFilesInfo, wIndex,
3940 wxStringBuffer(files[wIndex], len), len);
3941 }
3942 DragFinish (hFilesInfo);
3943
3944 wxDropFilesEvent event(wxEVT_DROP_FILES, gwFilesDropped, files);
3945 event.SetEventObject(this);
3946
3947 POINT dropPoint;
3948 DragQueryPoint(hFilesInfo, (LPPOINT) &dropPoint);
3949 event.m_pos.x = dropPoint.x;
3950 event.m_pos.y = dropPoint.y;
3951
3952 return GetEventHandler()->ProcessEvent(event);
3953 #endif
3954 }
3955
3956
3957 bool wxWindowMSW::HandleSetCursor(WXHWND WXUNUSED(hWnd),
3958 short nHitTest,
3959 int WXUNUSED(mouseMsg))
3960 {
3961 #ifndef __WXMICROWIN__
3962 // the logic is as follows:
3963 // 0. if we're busy, set the busy cursor (even for non client elements)
3964 // 1. don't set custom cursor for non client area of enabled windows
3965 // 2. ask user EVT_SET_CURSOR handler for the cursor
3966 // 3. if still no cursor but we're in a TLW, set the global cursor
3967
3968 HCURSOR hcursor = 0;
3969 if ( wxIsBusy() )
3970 {
3971 hcursor = wxGetCurrentBusyCursor();
3972 }
3973 else // not busy
3974 {
3975 if ( nHitTest != HTCLIENT )
3976 return false;
3977
3978 // first ask the user code - it may wish to set the cursor in some very
3979 // specific way (for example, depending on the current position)
3980 POINT pt;
3981 #ifdef __WXWINCE__
3982 if ( !::GetCursorPosWinCE(&pt))
3983 #else
3984 if ( !::GetCursorPos(&pt) )
3985 #endif
3986 {
3987 wxLogLastError(wxT("GetCursorPos"));
3988 }
3989
3990 int x = pt.x,
3991 y = pt.y;
3992 ScreenToClient(&x, &y);
3993 wxSetCursorEvent event(x, y);
3994
3995 bool processedEvtSetCursor = GetEventHandler()->ProcessEvent(event);
3996 if ( processedEvtSetCursor && event.HasCursor() )
3997 {
3998 hcursor = GetHcursorOf(event.GetCursor());
3999 }
4000
4001 if ( !hcursor )
4002 {
4003 // the test for processedEvtSetCursor is here to prevent using
4004 // m_cursor if the user code caught EVT_SET_CURSOR() and returned
4005 // nothing from it - this is a way to say that our cursor shouldn't
4006 // be used for this point
4007 if ( !processedEvtSetCursor && m_cursor.Ok() )
4008 {
4009 hcursor = GetHcursorOf(m_cursor);
4010 }
4011
4012 if ( !hcursor && !GetParent() )
4013 {
4014 const wxCursor *cursor = wxGetGlobalCursor();
4015 if ( cursor && cursor->Ok() )
4016 {
4017 hcursor = GetHcursorOf(*cursor);
4018 }
4019 }
4020 }
4021 }
4022
4023
4024 if ( hcursor )
4025 {
4026 ::SetCursor(hcursor);
4027
4028 // cursor set, stop here
4029 return true;
4030 }
4031 #endif // __WXMICROWIN__
4032
4033 // pass up the window chain
4034 return false;
4035 }
4036
4037 bool wxWindowMSW::HandlePower(WXWPARAM WXUNUSED_IN_WINCE(wParam),
4038 WXLPARAM WXUNUSED(lParam),
4039 bool *WXUNUSED_IN_WINCE(vetoed))
4040 {
4041 #ifdef __WXWINCE__
4042 // FIXME
4043 return false;
4044 #else
4045 wxEventType evtType;
4046 switch ( wParam )
4047 {
4048 case PBT_APMQUERYSUSPEND:
4049 evtType = wxEVT_POWER_SUSPENDING;
4050 break;
4051
4052 case PBT_APMQUERYSUSPENDFAILED:
4053 evtType = wxEVT_POWER_SUSPEND_CANCEL;
4054 break;
4055
4056 case PBT_APMSUSPEND:
4057 evtType = wxEVT_POWER_SUSPENDED;
4058 break;
4059
4060 case PBT_APMRESUMESUSPEND:
4061 #ifdef PBT_APMRESUMEAUTOMATIC
4062 case PBT_APMRESUMEAUTOMATIC:
4063 #endif
4064 evtType = wxEVT_POWER_RESUME;
4065 break;
4066
4067 default:
4068 wxLogDebug(_T("Unknown WM_POWERBROADCAST(%d) event"), wParam);
4069 // fall through
4070
4071 // these messages are currently not mapped to wx events
4072 case PBT_APMQUERYSTANDBY:
4073 case PBT_APMQUERYSTANDBYFAILED:
4074 case PBT_APMSTANDBY:
4075 case PBT_APMRESUMESTANDBY:
4076 case PBT_APMBATTERYLOW:
4077 case PBT_APMPOWERSTATUSCHANGE:
4078 case PBT_APMOEMEVENT:
4079 case PBT_APMRESUMECRITICAL:
4080 evtType = wxEVT_NULL;
4081 break;
4082 }
4083
4084 // don't handle unknown messages
4085 if ( evtType == wxEVT_NULL )
4086 return false;
4087
4088 // TODO: notify about PBTF_APMRESUMEFROMFAILURE in case of resume events?
4089
4090 wxPowerEvent event(evtType);
4091 if ( !GetEventHandler()->ProcessEvent(event) )
4092 return false;
4093
4094 *vetoed = event.IsVetoed();
4095
4096 return true;
4097 #endif
4098 }
4099
4100 bool wxWindowMSW::IsDoubleBuffered() const
4101 {
4102 for ( const wxWindowMSW *wnd = this;
4103 wnd && !wnd->IsTopLevel(); wnd =
4104 wnd->GetParent() )
4105 {
4106 if ( ::GetWindowLong(GetHwndOf(wnd), GWL_EXSTYLE) & WS_EX_COMPOSITED )
4107 return true;
4108 }
4109
4110 return false;
4111 }
4112
4113 // ---------------------------------------------------------------------------
4114 // owner drawn stuff
4115 // ---------------------------------------------------------------------------
4116
4117 #if (wxUSE_OWNER_DRAWN && wxUSE_MENUS_NATIVE) || \
4118 (wxUSE_CONTROLS && !defined(__WXUNIVERSAL__))
4119 #define WXUNUSED_UNLESS_ODRAWN(param) param
4120 #else
4121 #define WXUNUSED_UNLESS_ODRAWN(param)
4122 #endif
4123
4124 bool
4125 wxWindowMSW::MSWOnDrawItem(int WXUNUSED_UNLESS_ODRAWN(id),
4126 WXDRAWITEMSTRUCT * WXUNUSED_UNLESS_ODRAWN(itemStruct))
4127 {
4128 #if wxUSE_OWNER_DRAWN
4129
4130 #if wxUSE_MENUS_NATIVE
4131 // is it a menu item?
4132 DRAWITEMSTRUCT *pDrawStruct = (DRAWITEMSTRUCT *)itemStruct;
4133 if ( id == 0 && pDrawStruct->CtlType == ODT_MENU )
4134 {
4135 wxMenuItem *pMenuItem = (wxMenuItem *)(pDrawStruct->itemData);
4136
4137 // see comment before the same test in MSWOnMeasureItem() below
4138 if ( !pMenuItem )
4139 return false;
4140
4141 wxCHECK_MSG( wxDynamicCast(pMenuItem, wxMenuItem),
4142 false, _T("MSWOnDrawItem: bad wxMenuItem pointer") );
4143
4144 // prepare to call OnDrawItem(): notice using of wxDCTemp to prevent
4145 // the DC from being released
4146 wxDCTemp dc((WXHDC)pDrawStruct->hDC);
4147 wxRect rect(pDrawStruct->rcItem.left, pDrawStruct->rcItem.top,
4148 pDrawStruct->rcItem.right - pDrawStruct->rcItem.left,
4149 pDrawStruct->rcItem.bottom - pDrawStruct->rcItem.top);
4150
4151 return pMenuItem->OnDrawItem
4152 (
4153 dc,
4154 rect,
4155 (wxOwnerDrawn::wxODAction)pDrawStruct->itemAction,
4156 (wxOwnerDrawn::wxODStatus)pDrawStruct->itemState
4157 );
4158 }
4159 #endif // wxUSE_MENUS_NATIVE
4160
4161 #endif // USE_OWNER_DRAWN
4162
4163 #if wxUSE_CONTROLS && !defined(__WXUNIVERSAL__)
4164
4165 #if wxUSE_OWNER_DRAWN
4166 wxControl *item = wxDynamicCast(FindItem(id), wxControl);
4167 #else // !wxUSE_OWNER_DRAWN
4168 // we may still have owner-drawn buttons internally because we have to make
4169 // them owner-drawn to support colour change
4170 wxControl *item =
4171 # if wxUSE_BUTTON
4172 wxDynamicCast(FindItem(id), wxButton)
4173 # else
4174 NULL
4175 # endif
4176 ;
4177 #endif // USE_OWNER_DRAWN
4178
4179 if ( item )
4180 {
4181 return item->MSWOnDraw(itemStruct);
4182 }
4183
4184 #endif // wxUSE_CONTROLS
4185
4186 return false;
4187 }
4188
4189 bool
4190 wxWindowMSW::MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *itemStruct)
4191 {
4192 #if wxUSE_OWNER_DRAWN && wxUSE_MENUS_NATIVE
4193 // is it a menu item?
4194 MEASUREITEMSTRUCT *pMeasureStruct = (MEASUREITEMSTRUCT *)itemStruct;
4195 if ( id == 0 && pMeasureStruct->CtlType == ODT_MENU )
4196 {
4197 wxMenuItem *pMenuItem = (wxMenuItem *)(pMeasureStruct->itemData);
4198
4199 // according to Carsten Fuchs the pointer may be NULL under XP if an
4200 // MDI child frame is initially maximized, see this for more info:
4201 // http://article.gmane.org/gmane.comp.lib.wxwidgets.general/27745
4202 //
4203 // so silently ignore it instead of asserting
4204 if ( !pMenuItem )
4205 return false;
4206
4207 wxCHECK_MSG( wxDynamicCast(pMenuItem, wxMenuItem),
4208 false, _T("MSWOnMeasureItem: bad wxMenuItem pointer") );
4209
4210 size_t w, h;
4211 bool rc = pMenuItem->OnMeasureItem(&w, &h);
4212
4213 pMeasureStruct->itemWidth = w;
4214 pMeasureStruct->itemHeight = h;
4215
4216 return rc;
4217 }
4218
4219 wxControl *item = wxDynamicCast(FindItem(id), wxControl);
4220 if ( item )
4221 {
4222 return item->MSWOnMeasure(itemStruct);
4223 }
4224 #else
4225 wxUnusedVar(id);
4226 wxUnusedVar(itemStruct);
4227 #endif // wxUSE_OWNER_DRAWN && wxUSE_MENUS_NATIVE
4228
4229 return false;
4230 }
4231
4232 // ---------------------------------------------------------------------------
4233 // colours and palettes
4234 // ---------------------------------------------------------------------------
4235
4236 bool wxWindowMSW::HandleSysColorChange()
4237 {
4238 wxSysColourChangedEvent event;
4239 event.SetEventObject(this);
4240
4241 (void)GetEventHandler()->ProcessEvent(event);
4242
4243 // always let the system carry on the default processing to allow the
4244 // native controls to react to the colours update
4245 return false;
4246 }
4247
4248 bool wxWindowMSW::HandleDisplayChange()
4249 {
4250 wxDisplayChangedEvent event;
4251 event.SetEventObject(this);
4252
4253 return GetEventHandler()->ProcessEvent(event);
4254 }
4255
4256 #ifndef __WXMICROWIN__
4257
4258 bool wxWindowMSW::HandleCtlColor(WXHBRUSH *brush, WXHDC hDC, WXHWND hWnd)
4259 {
4260 #if !wxUSE_CONTROLS || defined(__WXUNIVERSAL__)
4261 wxUnusedVar(hDC);
4262 wxUnusedVar(hWnd);
4263 #else
4264 wxControl *item = wxDynamicCast(FindItemByHWND(hWnd, true), wxControl);
4265
4266 if ( item )
4267 *brush = item->MSWControlColor(hDC, hWnd);
4268 else
4269 #endif // wxUSE_CONTROLS
4270 *brush = NULL;
4271
4272 return *brush != NULL;
4273 }
4274
4275 #endif // __WXMICROWIN__
4276
4277 bool wxWindowMSW::HandlePaletteChanged(WXHWND hWndPalChange)
4278 {
4279 #if wxUSE_PALETTE
4280 // same as below except we don't respond to our own messages
4281 if ( hWndPalChange != GetHWND() )
4282 {
4283 // check to see if we our our parents have a custom palette
4284 wxWindowMSW *win = this;
4285 while ( win && !win->HasCustomPalette() )
4286 {
4287 win = win->GetParent();
4288 }
4289
4290 if ( win && win->HasCustomPalette() )
4291 {
4292 // realize the palette to see whether redrawing is needed
4293 HDC hdc = ::GetDC((HWND) hWndPalChange);
4294 win->m_palette.SetHPALETTE((WXHPALETTE)
4295 ::SelectPalette(hdc, GetHpaletteOf(win->m_palette), FALSE));
4296
4297 int result = ::RealizePalette(hdc);
4298
4299 // restore the palette (before releasing the DC)
4300 win->m_palette.SetHPALETTE((WXHPALETTE)
4301 ::SelectPalette(hdc, GetHpaletteOf(win->m_palette), FALSE));
4302 ::RealizePalette(hdc);
4303 ::ReleaseDC((HWND) hWndPalChange, hdc);
4304
4305 // now check for the need to redraw
4306 if (result > 0)
4307 ::InvalidateRect((HWND) hWndPalChange, NULL, TRUE);
4308 }
4309
4310 }
4311 #endif // wxUSE_PALETTE
4312
4313 wxPaletteChangedEvent event(GetId());
4314 event.SetEventObject(this);
4315 event.SetChangedWindow(wxFindWinFromHandle(hWndPalChange));
4316
4317 return GetEventHandler()->ProcessEvent(event);
4318 }
4319
4320 bool wxWindowMSW::HandleCaptureChanged(WXHWND hWndGainedCapture)
4321 {
4322 // notify windows on the capture stack about lost capture
4323 // (see http://sourceforge.net/tracker/index.php?func=detail&aid=1153662&group_id=9863&atid=109863):
4324 wxWindowBase::NotifyCaptureLost();
4325
4326 wxWindow *win = wxFindWinFromHandle(hWndGainedCapture);
4327 wxMouseCaptureChangedEvent event(GetId(), win);
4328 event.SetEventObject(this);
4329 return GetEventHandler()->ProcessEvent(event);
4330 }
4331
4332 bool wxWindowMSW::HandleSettingChange(WXWPARAM wParam, WXLPARAM lParam)
4333 {
4334 // despite MSDN saying "(This message cannot be sent directly to a window.)"
4335 // we need to send this to child windows (it is only sent to top-level
4336 // windows) so {list,tree}ctrls can adjust their font size if necessary
4337 // this is exactly how explorer does it to enable the font size changes
4338
4339 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
4340 while ( node )
4341 {
4342 // top-level windows already get this message from the system
4343 wxWindow *win = node->GetData();
4344 if ( !win->IsTopLevel() )
4345 {
4346 ::SendMessage(GetHwndOf(win), WM_SETTINGCHANGE, wParam, lParam);
4347 }
4348
4349 node = node->GetNext();
4350 }
4351
4352 // let the system handle it
4353 return false;
4354 }
4355
4356 bool wxWindowMSW::HandleQueryNewPalette()
4357 {
4358
4359 #if wxUSE_PALETTE
4360 // check to see if we our our parents have a custom palette
4361 wxWindowMSW *win = this;
4362 while (!win->HasCustomPalette() && win->GetParent()) win = win->GetParent();
4363 if (win->HasCustomPalette()) {
4364 /* realize the palette to see whether redrawing is needed */
4365 HDC hdc = ::GetDC((HWND) GetHWND());
4366 win->m_palette.SetHPALETTE( (WXHPALETTE)
4367 ::SelectPalette(hdc, (HPALETTE) win->m_palette.GetHPALETTE(), FALSE) );
4368
4369 int result = ::RealizePalette(hdc);
4370 /* restore the palette (before releasing the DC) */
4371 win->m_palette.SetHPALETTE( (WXHPALETTE)
4372 ::SelectPalette(hdc, (HPALETTE) win->m_palette.GetHPALETTE(), TRUE) );
4373 ::RealizePalette(hdc);
4374 ::ReleaseDC((HWND) GetHWND(), hdc);
4375 /* now check for the need to redraw */
4376 if (result > 0)
4377 ::InvalidateRect((HWND) GetHWND(), NULL, TRUE);
4378 }
4379 #endif // wxUSE_PALETTE
4380
4381 wxQueryNewPaletteEvent event(GetId());
4382 event.SetEventObject(this);
4383
4384 return GetEventHandler()->ProcessEvent(event) && event.GetPaletteRealized();
4385 }
4386
4387 // Responds to colour changes: passes event on to children.
4388 void wxWindowMSW::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
4389 {
4390 // the top level window also reset the standard colour map as it might have
4391 // changed (there is no need to do it for the non top level windows as we
4392 // only have to do it once)
4393 if ( IsTopLevel() )
4394 {
4395 // FIXME-MT
4396 gs_hasStdCmap = false;
4397 }
4398 wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
4399 while ( node )
4400 {
4401 // Only propagate to non-top-level windows because Windows already
4402 // sends this event to all top-level ones
4403 wxWindow *win = node->GetData();
4404 if ( !win->IsTopLevel() )
4405 {
4406 // we need to send the real WM_SYSCOLORCHANGE and not just trigger
4407 // EVT_SYS_COLOUR_CHANGED call because the latter wouldn't work for
4408 // the standard controls
4409 ::SendMessage(GetHwndOf(win), WM_SYSCOLORCHANGE, 0, 0);
4410 }
4411
4412 node = node->GetNext();
4413 }
4414 }
4415
4416 extern wxCOLORMAP *wxGetStdColourMap()
4417 {
4418 static COLORREF s_stdColours[wxSTD_COL_MAX];
4419 static wxCOLORMAP s_cmap[wxSTD_COL_MAX];
4420
4421 if ( !gs_hasStdCmap )
4422 {
4423 static bool s_coloursInit = false;
4424
4425 if ( !s_coloursInit )
4426 {
4427 // When a bitmap is loaded, the RGB values can change (apparently
4428 // because Windows adjusts them to care for the old programs always
4429 // using 0xc0c0c0 while the transparent colour for the new Windows
4430 // versions is different). But we do this adjustment ourselves so
4431 // we want to avoid Windows' "help" and for this we need to have a
4432 // reference bitmap which can tell us what the RGB values change
4433 // to.
4434 wxLogNull logNo; // suppress error if we couldn't load the bitmap
4435 wxBitmap stdColourBitmap(_T("wxBITMAP_STD_COLOURS"));
4436 if ( stdColourBitmap.Ok() )
4437 {
4438 // the pixels in the bitmap must correspond to wxSTD_COL_XXX!
4439 wxASSERT_MSG( stdColourBitmap.GetWidth() == wxSTD_COL_MAX,
4440 _T("forgot to update wxBITMAP_STD_COLOURS!") );
4441
4442 wxMemoryDC memDC;
4443 memDC.SelectObject(stdColourBitmap);
4444
4445 wxColour colour;
4446 for ( size_t i = 0; i < WXSIZEOF(s_stdColours); i++ )
4447 {
4448 memDC.GetPixel(i, 0, &colour);
4449 s_stdColours[i] = wxColourToRGB(colour);
4450 }
4451 }
4452 else // wxBITMAP_STD_COLOURS couldn't be loaded
4453 {
4454 s_stdColours[0] = RGB(000,000,000); // black
4455 s_stdColours[1] = RGB(128,128,128); // dark grey
4456 s_stdColours[2] = RGB(192,192,192); // light grey
4457 s_stdColours[3] = RGB(255,255,255); // white
4458 //s_stdColours[4] = RGB(000,000,255); // blue
4459 //s_stdColours[5] = RGB(255,000,255); // magenta
4460 }
4461
4462 s_coloursInit = true;
4463 }
4464
4465 gs_hasStdCmap = true;
4466
4467 // create the colour map
4468 #define INIT_CMAP_ENTRY(col) \
4469 s_cmap[wxSTD_COL_##col].from = s_stdColours[wxSTD_COL_##col]; \
4470 s_cmap[wxSTD_COL_##col].to = ::GetSysColor(COLOR_##col)
4471
4472 INIT_CMAP_ENTRY(BTNTEXT);
4473 INIT_CMAP_ENTRY(BTNSHADOW);
4474 INIT_CMAP_ENTRY(BTNFACE);
4475 INIT_CMAP_ENTRY(BTNHIGHLIGHT);
4476
4477 #undef INIT_CMAP_ENTRY
4478 }
4479
4480 return s_cmap;
4481 }
4482
4483 // ---------------------------------------------------------------------------
4484 // painting
4485 // ---------------------------------------------------------------------------
4486
4487 bool wxWindowMSW::HandlePaint()
4488 {
4489 HRGN hRegion = ::CreateRectRgn(0, 0, 0, 0); // Dummy call to get a handle
4490 if ( !hRegion )
4491 wxLogLastError(wxT("CreateRectRgn"));
4492 if ( ::GetUpdateRgn(GetHwnd(), hRegion, FALSE) == ERROR )
4493 wxLogLastError(wxT("GetUpdateRgn"));
4494
4495 m_updateRegion = wxRegion((WXHRGN) hRegion);
4496
4497 wxPaintEvent event(m_windowId);
4498 event.SetEventObject(this);
4499
4500 bool processed = GetEventHandler()->ProcessEvent(event);
4501
4502 // note that we must generate NC event after the normal one as otherwise
4503 // BeginPaint() will happily overwrite our decorations with the background
4504 // colour
4505 wxNcPaintEvent eventNc(m_windowId);
4506 eventNc.SetEventObject(this);
4507 GetEventHandler()->ProcessEvent(eventNc);
4508
4509 return processed;
4510 }
4511
4512 // Can be called from an application's OnPaint handler
4513 void wxWindowMSW::OnPaint(wxPaintEvent& event)
4514 {
4515 #ifdef __WXUNIVERSAL__
4516 event.Skip();
4517 #else
4518 HDC hDC = (HDC) wxPaintDC::FindDCInCache((wxWindow*) event.GetEventObject());
4519 if (hDC != 0)
4520 {
4521 MSWDefWindowProc(WM_PAINT, (WPARAM) hDC, 0);
4522 }
4523 #endif
4524 }
4525
4526 bool wxWindowMSW::HandleEraseBkgnd(WXHDC hdc)
4527 {
4528 wxDCTemp dc(hdc, GetClientSize());
4529
4530 dc.SetHDC(hdc);
4531 dc.SetWindow((wxWindow *)this);
4532
4533 wxEraseEvent event(m_windowId, &dc);
4534 event.SetEventObject(this);
4535 bool rc = GetEventHandler()->ProcessEvent(event);
4536
4537 // must be called manually as ~wxDC doesn't do anything for wxDCTemp
4538 dc.SelectOldObjects(hdc);
4539
4540 return rc;
4541 }
4542
4543 void wxWindowMSW::OnEraseBackground(wxEraseEvent& event)
4544 {
4545 // standard non top level controls (i.e. except the dialogs) always erase
4546 // their background themselves in HandleCtlColor() or have some control-
4547 // specific ways to set the colours (common controls)
4548 if ( IsOfStandardClass() && !IsTopLevel() )
4549 {
4550 event.Skip();
4551 return;
4552 }
4553
4554 if ( GetBackgroundStyle() == wxBG_STYLE_CUSTOM )
4555 {
4556 // don't skip the event here, custom background means that the app
4557 // is drawing it itself in its OnPaint(), so don't draw it at all
4558 // now to avoid flicker
4559 return;
4560 }
4561
4562
4563 // do default background painting
4564 if ( !DoEraseBackground(GetHdcOf(*event.GetDC())) )
4565 {
4566 // let the system paint the background
4567 event.Skip();
4568 }
4569 }
4570
4571 bool wxWindowMSW::DoEraseBackground(WXHDC hDC)
4572 {
4573 HBRUSH hbr = (HBRUSH)MSWGetBgBrush(hDC);
4574 if ( !hbr )
4575 return false;
4576
4577 wxFillRect(GetHwnd(), (HDC)hDC, hbr);
4578
4579 return true;
4580 }
4581
4582 WXHBRUSH
4583 wxWindowMSW::MSWGetBgBrushForChild(WXHDC WXUNUSED(hDC), WXHWND hWnd)
4584 {
4585 if ( m_hasBgCol )
4586 {
4587 // our background colour applies to:
4588 // 1. this window itself, always
4589 // 2. all children unless the colour is "not inheritable"
4590 // 3. even if it is not inheritable, our immediate transparent
4591 // children should still inherit it -- but not any transparent
4592 // children because it would look wrong if a child of non
4593 // transparent child would show our bg colour when the child itself
4594 // does not
4595 wxWindow *win = wxFindWinFromHandle(hWnd);
4596 if ( win == this ||
4597 m_inheritBgCol ||
4598 (win && win->HasTransparentBackground() &&
4599 win->GetParent() == this) )
4600 {
4601 // draw children with the same colour as the parent
4602 wxBrush *
4603 brush = wxTheBrushList->FindOrCreateBrush(GetBackgroundColour());
4604
4605 return (WXHBRUSH)GetHbrushOf(*brush);
4606 }
4607 }
4608
4609 return 0;
4610 }
4611
4612 WXHBRUSH wxWindowMSW::MSWGetBgBrush(WXHDC hDC, WXHWND hWndToPaint)
4613 {
4614 if ( !hWndToPaint )
4615 hWndToPaint = GetHWND();
4616
4617 for ( wxWindowMSW *win = this; win; win = win->GetParent() )
4618 {
4619 WXHBRUSH hBrush = win->MSWGetBgBrushForChild(hDC, hWndToPaint);
4620 if ( hBrush )
4621 return hBrush;
4622
4623 // background is not inherited beyond top level windows
4624 if ( win->IsTopLevel() )
4625 break;
4626 }
4627
4628 return 0;
4629 }
4630
4631 bool wxWindowMSW::HandlePrintClient(WXHDC hDC)
4632 {
4633 // we receive this message when DrawThemeParentBackground() is
4634 // called from def window proc of several controls under XP and we
4635 // must draw properly themed background here
4636 //
4637 // note that naively I'd expect filling the client rect with the
4638 // brush returned by MSWGetBgBrush() work -- but for some reason it
4639 // doesn't and we have to call parents MSWPrintChild() which is
4640 // supposed to call DrawThemeBackground() with appropriate params
4641 //
4642 // also note that in this case lParam == PRF_CLIENT but we're
4643 // clearly expected to paint the background and nothing else!
4644
4645 if ( IsTopLevel() || InheritsBackgroundColour() )
4646 return false;
4647
4648 // sometimes we don't want the parent to handle it at all, instead
4649 // return whatever value this window wants
4650 if ( !MSWShouldPropagatePrintChild() )
4651 return MSWPrintChild(hDC, (wxWindow *)this);
4652
4653 for ( wxWindow *win = GetParent(); win; win = win->GetParent() )
4654 {
4655 if ( win->MSWPrintChild(hDC, (wxWindow *)this) )
4656 return true;
4657
4658 if ( win->IsTopLevel() || win->InheritsBackgroundColour() )
4659 break;
4660 }
4661
4662 return false;
4663 }
4664
4665 // ---------------------------------------------------------------------------
4666 // moving and resizing
4667 // ---------------------------------------------------------------------------
4668
4669 bool wxWindowMSW::HandleMinimize()
4670 {
4671 wxIconizeEvent event(m_windowId);
4672 event.SetEventObject(this);
4673
4674 return GetEventHandler()->ProcessEvent(event);
4675 }
4676
4677 bool wxWindowMSW::HandleMaximize()
4678 {
4679 wxMaximizeEvent event(m_windowId);
4680 event.SetEventObject(this);
4681
4682 return GetEventHandler()->ProcessEvent(event);
4683 }
4684
4685 bool wxWindowMSW::HandleMove(int x, int y)
4686 {
4687 wxPoint point(x,y);
4688 wxMoveEvent event(point, m_windowId);
4689 event.SetEventObject(this);
4690
4691 return GetEventHandler()->ProcessEvent(event);
4692 }
4693
4694 bool wxWindowMSW::HandleMoving(wxRect& rect)
4695 {
4696 wxMoveEvent event(rect, m_windowId);
4697 event.SetEventObject(this);
4698
4699 bool rc = GetEventHandler()->ProcessEvent(event);
4700 if (rc)
4701 rect = event.GetRect();
4702 return rc;
4703 }
4704
4705 bool wxWindowMSW::HandleEnterSizeMove()
4706 {
4707 wxMoveEvent event(wxPoint(0,0), m_windowId);
4708 event.SetEventType(wxEVT_MOVE_START);
4709 event.SetEventObject(this);
4710
4711 return GetEventHandler()->ProcessEvent(event);
4712 }
4713
4714 bool wxWindowMSW::HandleExitSizeMove()
4715 {
4716 wxMoveEvent event(wxPoint(0,0), m_windowId);
4717 event.SetEventType(wxEVT_MOVE_END);
4718 event.SetEventObject(this);
4719
4720 return GetEventHandler()->ProcessEvent(event);
4721 }
4722
4723 bool wxWindowMSW::HandleSize(int WXUNUSED(w), int WXUNUSED(h), WXUINT wParam)
4724 {
4725 #if USE_DEFERRED_SIZING
4726 // when we resize this window, its children are probably going to be
4727 // repositioned as well, prepare to use DeferWindowPos() for them
4728 int numChildren = 0;
4729 for ( HWND child = ::GetWindow(GetHwndOf(this), GW_CHILD);
4730 child;
4731 child = ::GetWindow(child, GW_HWNDNEXT) )
4732 {
4733 numChildren ++;
4734 }
4735
4736 // Protect against valid m_hDWP being overwritten
4737 bool useDefer = false;
4738
4739 if ( numChildren > 1 )
4740 {
4741 if (!m_hDWP)
4742 {
4743 m_hDWP = (WXHANDLE)::BeginDeferWindowPos(numChildren);
4744 if ( !m_hDWP )
4745 {
4746 wxLogLastError(_T("BeginDeferWindowPos"));
4747 }
4748 if (m_hDWP)
4749 useDefer = true;
4750 }
4751 }
4752 #endif // USE_DEFERRED_SIZING
4753
4754 // update this window size
4755 bool processed = false;
4756 switch ( wParam )
4757 {
4758 default:
4759 wxFAIL_MSG( _T("unexpected WM_SIZE parameter") );
4760 // fall through nevertheless
4761
4762 case SIZE_MAXHIDE:
4763 case SIZE_MAXSHOW:
4764 // we're not interested in these messages at all
4765 break;
4766
4767 case SIZE_MINIMIZED:
4768 processed = HandleMinimize();
4769 break;
4770
4771 case SIZE_MAXIMIZED:
4772 /* processed = */ HandleMaximize();
4773 // fall through to send a normal size event as well
4774
4775 case SIZE_RESTORED:
4776 // don't use w and h parameters as they specify the client size
4777 // while according to the docs EVT_SIZE handler is supposed to
4778 // receive the total size
4779 wxSizeEvent event(GetSize(), m_windowId);
4780 event.SetEventObject(this);
4781
4782 processed = GetEventHandler()->ProcessEvent(event);
4783 }
4784
4785 #if USE_DEFERRED_SIZING
4786 // and finally change the positions of all child windows at once
4787 if ( useDefer && m_hDWP )
4788 {
4789 // reset m_hDWP to NULL so that child windows don't try to use our
4790 // m_hDWP after we call EndDeferWindowPos() on it (this shouldn't
4791 // happen anyhow normally but who knows what weird flow of control we
4792 // may have depending on what the users EVT_SIZE handler does...)
4793 HDWP hDWP = (HDWP)m_hDWP;
4794 m_hDWP = NULL;
4795
4796 // do put all child controls in place at once
4797 if ( !::EndDeferWindowPos(hDWP) )
4798 {
4799 wxLogLastError(_T("EndDeferWindowPos"));
4800 }
4801
4802 // Reset our children's pending pos/size values.
4803 for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst();
4804 node;
4805 node = node->GetNext() )
4806 {
4807 wxWindowMSW *child = node->GetData();
4808 child->m_pendingPosition = wxDefaultPosition;
4809 child->m_pendingSize = wxDefaultSize;
4810 }
4811 }
4812 #endif // USE_DEFERRED_SIZING
4813
4814 return processed;
4815 }
4816
4817 bool wxWindowMSW::HandleSizing(wxRect& rect)
4818 {
4819 wxSizeEvent event(rect, m_windowId);
4820 event.SetEventObject(this);
4821
4822 bool rc = GetEventHandler()->ProcessEvent(event);
4823 if (rc)
4824 rect = event.GetRect();
4825 return rc;
4826 }
4827
4828 bool wxWindowMSW::HandleGetMinMaxInfo(void *WXUNUSED_IN_WINCE(mmInfo))
4829 {
4830 #ifdef __WXWINCE__
4831 return false;
4832 #else
4833 MINMAXINFO *info = (MINMAXINFO *)mmInfo;
4834
4835 bool rc = false;
4836
4837 int minWidth = GetMinWidth(),
4838 minHeight = GetMinHeight(),
4839 maxWidth = GetMaxWidth(),
4840 maxHeight = GetMaxHeight();
4841
4842 if ( minWidth != wxDefaultCoord )
4843 {
4844 info->ptMinTrackSize.x = minWidth;
4845 rc = true;
4846 }
4847
4848 if ( minHeight != wxDefaultCoord )
4849 {
4850 info->ptMinTrackSize.y = minHeight;
4851 rc = true;
4852 }
4853
4854 if ( maxWidth != wxDefaultCoord )
4855 {
4856 info->ptMaxTrackSize.x = maxWidth;
4857 rc = true;
4858 }
4859
4860 if ( maxHeight != wxDefaultCoord )
4861 {
4862 info->ptMaxTrackSize.y = maxHeight;
4863 rc = true;
4864 }
4865
4866 return rc;
4867 #endif
4868 }
4869
4870 // ---------------------------------------------------------------------------
4871 // command messages
4872 // ---------------------------------------------------------------------------
4873
4874 bool wxWindowMSW::HandleCommand(WXWORD id_, WXWORD cmd, WXHWND control)
4875 {
4876 // sign extend to int from short before comparing with the other int ids
4877 int id = (signed short)id_;
4878
4879 #if wxUSE_MENUS_NATIVE
4880 if ( !cmd && wxCurrentPopupMenu )
4881 {
4882 wxMenu *popupMenu = wxCurrentPopupMenu;
4883 wxCurrentPopupMenu = NULL;
4884
4885 return popupMenu->MSWCommand(cmd, id);
4886 }
4887 #endif // wxUSE_MENUS_NATIVE
4888
4889 wxWindow *win = NULL;
4890
4891 // first try to find it from HWND - this works even with the broken
4892 // programs using the same ids for different controls
4893 if ( control )
4894 {
4895 win = wxFindWinFromHandle(control);
4896 }
4897
4898 // try the id
4899 if ( !win )
4900 {
4901 win = FindItem(id);
4902 }
4903
4904 if ( win )
4905 {
4906 return win->MSWCommand(cmd, id);
4907 }
4908
4909 // the messages sent from the in-place edit control used by the treectrl
4910 // for label editing have id == 0, but they should _not_ be treated as menu
4911 // messages (they are EN_XXX ones, in fact) so don't translate anything
4912 // coming from a control to wxEVT_COMMAND_MENU_SELECTED
4913 if ( !control )
4914 {
4915 // If no child window, it may be an accelerator, e.g. for a popup menu
4916 // command
4917
4918 wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED);
4919 event.SetEventObject(this);
4920 event.SetId(id);
4921 event.SetInt(id);
4922
4923 return GetEventHandler()->ProcessEvent(event);
4924 }
4925 else
4926 {
4927 #if wxUSE_SPINCTRL && !defined(__WXUNIVERSAL__)
4928 // the text ctrl which is logically part of wxSpinCtrl sends WM_COMMAND
4929 // notifications to its parent which we want to reflect back to
4930 // wxSpinCtrl
4931 wxSpinCtrl *spin = wxSpinCtrl::GetSpinForTextCtrl(control);
4932 if ( spin && spin->ProcessTextCommand(cmd, id) )
4933 return true;
4934 #endif // wxUSE_SPINCTRL
4935
4936 #if wxUSE_CHOICE && defined(__SMARTPHONE__)
4937 // the listbox ctrl which is logically part of wxChoice sends WM_COMMAND
4938 // notifications to its parent which we want to reflect back to
4939 // wxChoice
4940 wxChoice *choice = wxChoice::GetChoiceForListBox(control);
4941 if ( choice && choice->MSWCommand(cmd, id) )
4942 return true;
4943 #endif
4944 }
4945
4946 return false;
4947 }
4948
4949 // ---------------------------------------------------------------------------
4950 // mouse events
4951 // ---------------------------------------------------------------------------
4952
4953 void wxWindowMSW::InitMouseEvent(wxMouseEvent& event,
4954 int x, int y,
4955 WXUINT flags)
4956 {
4957 // our client coords are not quite the same as Windows ones
4958 wxPoint pt = GetClientAreaOrigin();
4959 event.m_x = x - pt.x;
4960 event.m_y = y - pt.y;
4961
4962 event.m_shiftDown = (flags & MK_SHIFT) != 0;
4963 event.m_controlDown = (flags & MK_CONTROL) != 0;
4964 event.m_leftDown = (flags & MK_LBUTTON) != 0;
4965 event.m_middleDown = (flags & MK_MBUTTON) != 0;
4966 event.m_rightDown = (flags & MK_RBUTTON) != 0;
4967 #ifdef wxHAS_XBUTTON
4968 event.m_aux1Down = (flags & MK_XBUTTON1) != 0;
4969 event.m_aux2Down = (flags & MK_XBUTTON2) != 0;
4970 #endif // wxHAS_XBUTTON
4971 event.m_altDown = ::GetKeyState(VK_MENU) < 0;
4972
4973 #ifndef __WXWINCE__
4974 event.SetTimestamp(::GetMessageTime());
4975 #endif
4976
4977 event.SetEventObject(this);
4978 event.SetId(GetId());
4979
4980 #if wxUSE_MOUSEEVENT_HACK
4981 gs_lastMouseEvent.pos = ClientToScreen(wxPoint(x, y));
4982 gs_lastMouseEvent.type = event.GetEventType();
4983 #endif // wxUSE_MOUSEEVENT_HACK
4984 }
4985
4986 #ifdef __WXWINCE__
4987 // Windows doesn't send the mouse events to the static controls (which are
4988 // transparent in the sense that their WM_NCHITTEST handler returns
4989 // HTTRANSPARENT) at all but we want all controls to receive the mouse events
4990 // and so we manually check if we don't have a child window under mouse and if
4991 // we do, send the event to it instead of the window Windows had sent WM_XXX
4992 // to.
4993 //
4994 // Notice that this is not done for the mouse move events because this could
4995 // (would?) be too slow, but only for clicks which means that the static texts
4996 // still don't get move, enter nor leave events.
4997 static wxWindowMSW *FindWindowForMouseEvent(wxWindowMSW *win, int *x, int *y)
4998 {
4999 wxCHECK_MSG( x && y, win, _T("NULL pointer in FindWindowForMouseEvent") );
5000
5001 // first try to find a non transparent child: this allows us to send events
5002 // to a static text which is inside a static box, for example
5003 POINT pt = { *x, *y };
5004 HWND hwnd = GetHwndOf(win),
5005 hwndUnderMouse;
5006
5007 #ifdef __WXWINCE__
5008 hwndUnderMouse = ::ChildWindowFromPoint
5009 (
5010 hwnd,
5011 pt
5012 );
5013 #else
5014 hwndUnderMouse = ::ChildWindowFromPointEx
5015 (
5016 hwnd,
5017 pt,
5018 CWP_SKIPINVISIBLE |
5019 CWP_SKIPDISABLED |
5020 CWP_SKIPTRANSPARENT
5021 );
5022 #endif
5023
5024 if ( !hwndUnderMouse || hwndUnderMouse == hwnd )
5025 {
5026 // now try any child window at all
5027 hwndUnderMouse = ::ChildWindowFromPoint(hwnd, pt);
5028 }
5029
5030 // check that we have a child window which is susceptible to receive mouse
5031 // events: for this it must be shown and enabled
5032 if ( hwndUnderMouse &&
5033 hwndUnderMouse != hwnd &&
5034 ::IsWindowVisible(hwndUnderMouse) &&
5035 ::IsWindowEnabled(hwndUnderMouse) )
5036 {
5037 wxWindow *winUnderMouse = wxFindWinFromHandle((WXHWND)hwndUnderMouse);
5038 if ( winUnderMouse )
5039 {
5040 // translate the mouse coords to the other window coords
5041 win->ClientToScreen(x, y);
5042 winUnderMouse->ScreenToClient(x, y);
5043
5044 win = winUnderMouse;
5045 }
5046 }
5047
5048 return win;
5049 }
5050 #endif // __WXWINCE__
5051
5052 bool wxWindowMSW::HandleMouseEvent(WXUINT msg, int x, int y, WXUINT flags)
5053 {
5054 // the mouse events take consecutive IDs from WM_MOUSEFIRST to
5055 // WM_MOUSELAST, so it's enough to subtract WM_MOUSEMOVE == WM_MOUSEFIRST
5056 // from the message id and take the value in the table to get wxWin event
5057 // id
5058 static const wxEventType eventsMouse[] =
5059 {
5060 wxEVT_MOTION,
5061 wxEVT_LEFT_DOWN,
5062 wxEVT_LEFT_UP,
5063 wxEVT_LEFT_DCLICK,
5064 wxEVT_RIGHT_DOWN,
5065 wxEVT_RIGHT_UP,
5066 wxEVT_RIGHT_DCLICK,
5067 wxEVT_MIDDLE_DOWN,
5068 wxEVT_MIDDLE_UP,
5069 wxEVT_MIDDLE_DCLICK,
5070 0, // this one is for wxEVT_MOTION which is not used here
5071 wxEVT_AUX1_DOWN,
5072 wxEVT_AUX1_UP,
5073 wxEVT_AUX1_DCLICK,
5074 wxEVT_AUX2_DOWN,
5075 wxEVT_AUX2_UP,
5076 wxEVT_AUX2_DCLICK
5077 };
5078
5079 #ifdef wxHAS_XBUTTON
5080 // the same messages are used for both auxillary mouse buttons so we need
5081 // to adjust the index manually
5082 switch ( msg )
5083 {
5084 case WM_XBUTTONDOWN:
5085 case WM_XBUTTONUP:
5086 case WM_XBUTTONDBLCLK:
5087 if ( flags & MK_XBUTTON2 )
5088 msg += wxEVT_AUX2_DOWN - wxEVT_AUX1_DOWN;
5089 }
5090 #endif // wxHAS_XBUTTON
5091
5092 wxMouseEvent event(eventsMouse[msg - WM_MOUSEMOVE]);
5093 InitMouseEvent(event, x, y, flags);
5094
5095 return GetEventHandler()->ProcessEvent(event);
5096 }
5097
5098 bool wxWindowMSW::HandleMouseMove(int x, int y, WXUINT flags)
5099 {
5100 if ( !m_mouseInWindow )
5101 {
5102 // it would be wrong to assume that just because we get a mouse move
5103 // event that the mouse is inside the window: although this is usually
5104 // true, it is not if we had captured the mouse, so we need to check
5105 // the mouse coordinates here
5106 if ( !HasCapture() || IsMouseInWindow() )
5107 {
5108 // Generate an ENTER event
5109 m_mouseInWindow = true;
5110
5111 #ifdef HAVE_TRACKMOUSEEVENT
5112 typedef BOOL (WINAPI *_TrackMouseEvent_t)(LPTRACKMOUSEEVENT);
5113 #ifdef __WXWINCE__
5114 static const _TrackMouseEvent_t
5115 s_pfn_TrackMouseEvent = _TrackMouseEvent;
5116 #else // !__WXWINCE__
5117 static _TrackMouseEvent_t s_pfn_TrackMouseEvent;
5118 static bool s_initDone = false;
5119 if ( !s_initDone )
5120 {
5121 wxLogNull noLog;
5122
5123 wxDynamicLibrary dllComCtl32(_T("comctl32.dll"), wxDL_VERBATIM);
5124 if ( dllComCtl32.IsLoaded() )
5125 {
5126 s_pfn_TrackMouseEvent = (_TrackMouseEvent_t)
5127 dllComCtl32.GetSymbol(_T("_TrackMouseEvent"));
5128 }
5129
5130 s_initDone = true;
5131
5132 // notice that it's ok to unload comctl32.dll here as it won't
5133 // be really unloaded, being still in use because we link to it
5134 // statically too
5135 }
5136
5137 if ( s_pfn_TrackMouseEvent )
5138 #endif // __WXWINCE__/!__WXWINCE__
5139 {
5140 WinStruct<TRACKMOUSEEVENT> trackinfo;
5141
5142 trackinfo.dwFlags = TME_LEAVE;
5143 trackinfo.hwndTrack = GetHwnd();
5144
5145 (*s_pfn_TrackMouseEvent)(&trackinfo);
5146 }
5147 #endif // HAVE_TRACKMOUSEEVENT
5148
5149 wxMouseEvent event(wxEVT_ENTER_WINDOW);
5150 InitMouseEvent(event, x, y, flags);
5151
5152 (void)GetEventHandler()->ProcessEvent(event);
5153 }
5154 }
5155 #ifdef HAVE_TRACKMOUSEEVENT
5156 else // mouse not in window
5157 {
5158 // Check if we need to send a LEAVE event
5159 // Windows doesn't send WM_MOUSELEAVE if the mouse has been captured so
5160 // send it here if we are using native mouse leave tracking
5161 if ( HasCapture() && !IsMouseInWindow() )
5162 {
5163 GenerateMouseLeave();
5164 }
5165 }
5166 #endif // HAVE_TRACKMOUSEEVENT
5167
5168 #if wxUSE_MOUSEEVENT_HACK
5169 // Windows often generates mouse events even if mouse position hasn't
5170 // changed (http://article.gmane.org/gmane.comp.lib.wxwidgets.devel/66576)
5171 //
5172 // Filter this out as it can result in unexpected behaviour compared to
5173 // other platforms
5174 if ( gs_lastMouseEvent.type == wxEVT_RIGHT_DOWN ||
5175 gs_lastMouseEvent.type == wxEVT_LEFT_DOWN ||
5176 gs_lastMouseEvent.type == wxEVT_MIDDLE_DOWN ||
5177 gs_lastMouseEvent.type == wxEVT_MOTION )
5178 {
5179 if ( ClientToScreen(wxPoint(x, y)) == gs_lastMouseEvent.pos )
5180 {
5181 gs_lastMouseEvent.type = wxEVT_MOTION;
5182
5183 return false;
5184 }
5185 }
5186 #endif // wxUSE_MOUSEEVENT_HACK
5187
5188 return HandleMouseEvent(WM_MOUSEMOVE, x, y, flags);
5189 }
5190
5191
5192 bool wxWindowMSW::HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam)
5193 {
5194 #if wxUSE_MOUSEWHEEL
5195 // notice that WM_MOUSEWHEEL position is in screen coords (as it's
5196 // forwarded up to the parent by DefWindowProc()) and not in the client
5197 // ones as all the other messages, translate them to the client coords for
5198 // consistency
5199 const wxPoint
5200 pt = ScreenToClient(wxPoint(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
5201 wxMouseEvent event(wxEVT_MOUSEWHEEL);
5202 InitMouseEvent(event, pt.x, pt.y, LOWORD(wParam));
5203 event.m_wheelRotation = (short)HIWORD(wParam);
5204 event.m_wheelDelta = WHEEL_DELTA;
5205
5206 static int s_linesPerRotation = -1;
5207 if ( s_linesPerRotation == -1 )
5208 {
5209 if ( !::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0,
5210 &s_linesPerRotation, 0))
5211 {
5212 // this is not supposed to happen
5213 wxLogLastError(_T("SystemParametersInfo(GETWHEELSCROLLLINES)"));
5214
5215 // the default is 3, so use it if SystemParametersInfo() failed
5216 s_linesPerRotation = 3;
5217 }
5218 }
5219
5220 event.m_linesPerAction = s_linesPerRotation;
5221 return GetEventHandler()->ProcessEvent(event);
5222
5223 #else // !wxUSE_MOUSEWHEEL
5224 wxUnusedVar(wParam);
5225 wxUnusedVar(lParam);
5226
5227 return false;
5228 #endif // wxUSE_MOUSEWHEEL/!wxUSE_MOUSEWHEEL
5229 }
5230
5231 void wxWindowMSW::GenerateMouseLeave()
5232 {
5233 m_mouseInWindow = false;
5234
5235 int state = 0;
5236 if ( wxIsShiftDown() )
5237 state |= MK_SHIFT;
5238 if ( wxIsCtrlDown() )
5239 state |= MK_CONTROL;
5240
5241 // Only the high-order bit should be tested
5242 if ( GetKeyState( VK_LBUTTON ) & (1<<15) )
5243 state |= MK_LBUTTON;
5244 if ( GetKeyState( VK_MBUTTON ) & (1<<15) )
5245 state |= MK_MBUTTON;
5246 if ( GetKeyState( VK_RBUTTON ) & (1<<15) )
5247 state |= MK_RBUTTON;
5248
5249 POINT pt;
5250 #ifdef __WXWINCE__
5251 if ( !::GetCursorPosWinCE(&pt) )
5252 #else
5253 if ( !::GetCursorPos(&pt) )
5254 #endif
5255 {
5256 wxLogLastError(_T("GetCursorPos"));
5257 }
5258
5259 // we need to have client coordinates here for symmetry with
5260 // wxEVT_ENTER_WINDOW
5261 RECT rect = wxGetWindowRect(GetHwnd());
5262 pt.x -= rect.left;
5263 pt.y -= rect.top;
5264
5265 wxMouseEvent event(wxEVT_LEAVE_WINDOW);
5266 InitMouseEvent(event, pt.x, pt.y, state);
5267
5268 (void)GetEventHandler()->ProcessEvent(event);
5269 }
5270
5271 // ---------------------------------------------------------------------------
5272 // keyboard handling
5273 // ---------------------------------------------------------------------------
5274
5275 // create the key event of the given type for the given key - used by
5276 // HandleChar and HandleKeyDown/Up
5277 wxKeyEvent wxWindowMSW::CreateKeyEvent(wxEventType evType,
5278 int id,
5279 WXLPARAM lParam,
5280 WXWPARAM wParam) const
5281 {
5282 wxKeyEvent event(evType);
5283 event.SetId(GetId());
5284 event.m_shiftDown = wxIsShiftDown();
5285 event.m_controlDown = wxIsCtrlDown();
5286 event.m_altDown = (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN;
5287
5288 event.SetEventObject((wxWindow *)this); // const_cast
5289 event.m_keyCode = id;
5290 #if wxUSE_UNICODE
5291 event.m_uniChar = (wxChar) wParam;
5292 #endif
5293 event.m_rawCode = (wxUint32) wParam;
5294 event.m_rawFlags = (wxUint32) lParam;
5295 #ifndef __WXWINCE__
5296 event.SetTimestamp(::GetMessageTime());
5297 #endif
5298
5299 // translate the position to client coords
5300 POINT pt;
5301 #ifdef __WXWINCE__
5302 GetCursorPosWinCE(&pt);
5303 #else
5304 GetCursorPos(&pt);
5305 #endif
5306 RECT rect;
5307 GetWindowRect(GetHwnd(),&rect);
5308 pt.x -= rect.left;
5309 pt.y -= rect.top;
5310
5311 event.m_x = pt.x;
5312 event.m_y = pt.y;
5313
5314 return event;
5315 }
5316
5317 // isASCII is true only when we're called from WM_CHAR handler and not from
5318 // WM_KEYDOWN one
5319 bool wxWindowMSW::HandleChar(WXWPARAM wParam, WXLPARAM lParam, bool isASCII)
5320 {
5321 int id;
5322 if ( isASCII )
5323 {
5324 id = wParam;
5325 }
5326 else // we're called from WM_KEYDOWN
5327 {
5328 // don't pass lParam to wxCharCodeMSWToWX() here because we don't want
5329 // to get numpad key codes: CHAR events should use the logical keys
5330 // such as WXK_HOME instead of WXK_NUMPAD_HOME which is for KEY events
5331 id = wxCharCodeMSWToWX(wParam);
5332 if ( id == 0 )
5333 {
5334 // it's ASCII and will be processed here only when called from
5335 // WM_CHAR (i.e. when isASCII = true), don't process it now
5336 return false;
5337 }
5338 }
5339
5340 wxKeyEvent event(CreateKeyEvent(wxEVT_CHAR, id, lParam, wParam));
5341
5342 // the alphanumeric keys produced by pressing AltGr+something on European
5343 // keyboards have both Ctrl and Alt modifiers which may confuse the user
5344 // code as, normally, keys with Ctrl and/or Alt don't result in anything
5345 // alphanumeric, so pretend that there are no modifiers at all (the
5346 // KEY_DOWN event would still have the correct modifiers if they're really
5347 // needed)
5348 if ( event.m_controlDown && event.m_altDown &&
5349 (id >= 32 && id < 256) )
5350 {
5351 event.m_controlDown =
5352 event.m_altDown = false;
5353 }
5354
5355 return GetEventHandler()->ProcessEvent(event);
5356 }
5357
5358 bool wxWindowMSW::HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam)
5359 {
5360 int id = wxCharCodeMSWToWX(wParam, lParam);
5361
5362 if ( !id )
5363 {
5364 // normal ASCII char
5365 id = wParam;
5366 }
5367
5368 wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_DOWN, id, lParam, wParam));
5369 return GetEventHandler()->ProcessEvent(event);
5370 }
5371
5372 bool wxWindowMSW::HandleKeyUp(WXWPARAM wParam, WXLPARAM lParam)
5373 {
5374 int id = wxCharCodeMSWToWX(wParam, lParam);
5375
5376 if ( !id )
5377 {
5378 // normal ASCII char
5379 id = wParam;
5380 }
5381
5382 wxKeyEvent event(CreateKeyEvent(wxEVT_KEY_UP, id, lParam, wParam));
5383 return GetEventHandler()->ProcessEvent(event);
5384 }
5385
5386 #if wxUSE_MENUS
5387 int wxWindowMSW::HandleMenuChar(int WXUNUSED_IN_WINCE(chAccel),
5388 WXLPARAM WXUNUSED_IN_WINCE(lParam))
5389 {
5390 // FIXME: implement GetMenuItemCount for WinCE, possibly
5391 // in terms of GetMenuItemInfo
5392 #ifndef __WXWINCE__
5393 const HMENU hmenu = (HMENU)lParam;
5394
5395 MENUITEMINFO mii;
5396 wxZeroMemory(mii);
5397 mii.cbSize = sizeof(MENUITEMINFO);
5398
5399 // we could use MIIM_FTYPE here as we only need to know if the item is
5400 // ownerdrawn or not and not dwTypeData which MIIM_TYPE also returns, but
5401 // MIIM_FTYPE is not supported under Win95
5402 mii.fMask = MIIM_TYPE | MIIM_DATA;
5403
5404 // find if we have this letter in any owner drawn item
5405 const int count = ::GetMenuItemCount(hmenu);
5406 for ( int i = 0; i < count; i++ )
5407 {
5408 // previous loop iteration could modify it, reset it back before
5409 // calling GetMenuItemInfo() to prevent it from overflowing dwTypeData
5410 mii.cch = 0;
5411
5412 if ( ::GetMenuItemInfo(hmenu, i, TRUE, &mii) )
5413 {
5414 if ( mii.fType == MFT_OWNERDRAW )
5415 {
5416 // dwItemData member of the MENUITEMINFO is a
5417 // pointer to the associated wxMenuItem -- see the
5418 // menu creation code
5419 wxMenuItem *item = (wxMenuItem*)mii.dwItemData;
5420
5421 const wxChar *p = wxStrchr(item->GetItemLabel().wx_str(), _T('&'));
5422 while ( p++ )
5423 {
5424 if ( *p == _T('&') )
5425 {
5426 // this is not the accel char, find the real one
5427 p = wxStrchr(p + 1, _T('&'));
5428 }
5429 else // got the accel char
5430 {
5431 // FIXME-UNICODE: this comparison doesn't risk to work
5432 // for non ASCII accelerator characters I'm afraid, but
5433 // what can we do?
5434 if ( (wchar_t)wxToupper(*p) == (wchar_t)chAccel )
5435 {
5436 return i;
5437 }
5438 else
5439 {
5440 // this one doesn't match
5441 break;
5442 }
5443 }
5444 }
5445 }
5446 }
5447 else // failed to get the menu text?
5448 {
5449 // it's not fatal, so don't show error, but still log it
5450 wxLogLastError(_T("GetMenuItemInfo"));
5451 }
5452 }
5453 #endif
5454 return wxNOT_FOUND;
5455 }
5456
5457 bool wxWindowMSW::HandleClipboardEvent( WXUINT nMsg )
5458 {
5459 const wxEventType type = ( nMsg == WM_CUT ) ? wxEVT_COMMAND_TEXT_CUT :
5460 ( nMsg == WM_COPY ) ? wxEVT_COMMAND_TEXT_COPY :
5461 /*( nMsg == WM_PASTE ) ? */ wxEVT_COMMAND_TEXT_PASTE;
5462 wxClipboardTextEvent evt(type, GetId());
5463
5464 evt.SetEventObject(this);
5465
5466 return GetEventHandler()->ProcessEvent(evt);
5467 }
5468 #endif // wxUSE_MENUS
5469
5470 // ---------------------------------------------------------------------------
5471 // joystick
5472 // ---------------------------------------------------------------------------
5473
5474 bool wxWindowMSW::HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags)
5475 {
5476 #ifdef JOY_BUTTON1
5477 int change = 0;
5478 if ( flags & JOY_BUTTON1CHG )
5479 change = wxJOY_BUTTON1;
5480 if ( flags & JOY_BUTTON2CHG )
5481 change = wxJOY_BUTTON2;
5482 if ( flags & JOY_BUTTON3CHG )
5483 change = wxJOY_BUTTON3;
5484 if ( flags & JOY_BUTTON4CHG )
5485 change = wxJOY_BUTTON4;
5486
5487 int buttons = 0;
5488 if ( flags & JOY_BUTTON1 )
5489 buttons |= wxJOY_BUTTON1;
5490 if ( flags & JOY_BUTTON2 )
5491 buttons |= wxJOY_BUTTON2;
5492 if ( flags & JOY_BUTTON3 )
5493 buttons |= wxJOY_BUTTON3;
5494 if ( flags & JOY_BUTTON4 )
5495 buttons |= wxJOY_BUTTON4;
5496
5497 // the event ids aren't consecutive so we can't use table based lookup
5498 int joystick;
5499 wxEventType eventType;
5500 switch ( msg )
5501 {
5502 case MM_JOY1MOVE:
5503 joystick = 1;
5504 eventType = wxEVT_JOY_MOVE;
5505 break;
5506
5507 case MM_JOY2MOVE:
5508 joystick = 2;
5509 eventType = wxEVT_JOY_MOVE;
5510 break;
5511
5512 case MM_JOY1ZMOVE:
5513 joystick = 1;
5514 eventType = wxEVT_JOY_ZMOVE;
5515 break;
5516
5517 case MM_JOY2ZMOVE:
5518 joystick = 2;
5519 eventType = wxEVT_JOY_ZMOVE;
5520 break;
5521
5522 case MM_JOY1BUTTONDOWN:
5523 joystick = 1;
5524 eventType = wxEVT_JOY_BUTTON_DOWN;
5525 break;
5526
5527 case MM_JOY2BUTTONDOWN:
5528 joystick = 2;
5529 eventType = wxEVT_JOY_BUTTON_DOWN;
5530 break;
5531
5532 case MM_JOY1BUTTONUP:
5533 joystick = 1;
5534 eventType = wxEVT_JOY_BUTTON_UP;
5535 break;
5536
5537 case MM_JOY2BUTTONUP:
5538 joystick = 2;
5539 eventType = wxEVT_JOY_BUTTON_UP;
5540 break;
5541
5542 default:
5543 wxFAIL_MSG(wxT("no such joystick event"));
5544
5545 return false;
5546 }
5547
5548 wxJoystickEvent event(eventType, buttons, joystick, change);
5549 event.SetPosition(wxPoint(x, y));
5550 event.SetEventObject(this);
5551
5552 return GetEventHandler()->ProcessEvent(event);
5553 #else
5554 wxUnusedVar(msg);
5555 wxUnusedVar(x);
5556 wxUnusedVar(y);
5557 wxUnusedVar(flags);
5558 return false;
5559 #endif
5560 }
5561
5562 // ---------------------------------------------------------------------------
5563 // scrolling
5564 // ---------------------------------------------------------------------------
5565
5566 bool wxWindowMSW::MSWOnScroll(int orientation, WXWORD wParam,
5567 WXWORD pos, WXHWND control)
5568 {
5569 if ( control && control != m_hWnd ) // Prevent infinite recursion
5570 {
5571 wxWindow *child = wxFindWinFromHandle(control);
5572 if ( child )
5573 return child->MSWOnScroll(orientation, wParam, pos, control);
5574 }
5575
5576 wxScrollWinEvent event;
5577 event.SetPosition(pos);
5578 event.SetOrientation(orientation);
5579 event.SetEventObject(this);
5580
5581 switch ( wParam )
5582 {
5583 case SB_TOP:
5584 event.SetEventType(wxEVT_SCROLLWIN_TOP);
5585 break;
5586
5587 case SB_BOTTOM:
5588 event.SetEventType(wxEVT_SCROLLWIN_BOTTOM);
5589 break;
5590
5591 case SB_LINEUP:
5592 event.SetEventType(wxEVT_SCROLLWIN_LINEUP);
5593 break;
5594
5595 case SB_LINEDOWN:
5596 event.SetEventType(wxEVT_SCROLLWIN_LINEDOWN);
5597 break;
5598
5599 case SB_PAGEUP:
5600 event.SetEventType(wxEVT_SCROLLWIN_PAGEUP);
5601 break;
5602
5603 case SB_PAGEDOWN:
5604 event.SetEventType(wxEVT_SCROLLWIN_PAGEDOWN);
5605 break;
5606
5607 case SB_THUMBPOSITION:
5608 case SB_THUMBTRACK:
5609 // under Win32, the scrollbar range and position are 32 bit integers,
5610 // but WM_[HV]SCROLL only carry the low 16 bits of them, so we must
5611 // explicitly query the scrollbar for the correct position (this must
5612 // be done only for these two SB_ events as they are the only one
5613 // carrying the scrollbar position)
5614 {
5615 WinStruct<SCROLLINFO> scrollInfo;
5616 scrollInfo.fMask = SIF_TRACKPOS;
5617
5618 if ( !::GetScrollInfo(GetHwnd(),
5619 orientation == wxHORIZONTAL ? SB_HORZ
5620 : SB_VERT,
5621 &scrollInfo) )
5622 {
5623 // Not necessarily an error, if there are no scrollbars yet.
5624 // wxLogLastError(_T("GetScrollInfo"));
5625 }
5626
5627 event.SetPosition(scrollInfo.nTrackPos);
5628 }
5629
5630 event.SetEventType( wParam == SB_THUMBPOSITION
5631 ? wxEVT_SCROLLWIN_THUMBRELEASE
5632 : wxEVT_SCROLLWIN_THUMBTRACK );
5633 break;
5634
5635 default:
5636 return false;
5637 }
5638
5639 return GetEventHandler()->ProcessEvent(event);
5640 }
5641
5642 // ----------------------------------------------------------------------------
5643 // custom message handlers
5644 // ----------------------------------------------------------------------------
5645
5646 /* static */ bool
5647 wxWindowMSW::MSWRegisterMessageHandler(int msg, MSWMessageHandler handler)
5648 {
5649 wxCHECK_MSG( gs_messageHandlers.find(msg) == gs_messageHandlers.end(),
5650 false, _T("registering handler for the same message twice") );
5651
5652 gs_messageHandlers[msg] = handler;
5653 return true;
5654 }
5655
5656 /* static */ void
5657 wxWindowMSW::MSWUnregisterMessageHandler(int msg, MSWMessageHandler handler)
5658 {
5659 const MSWMessageHandlers::iterator i = gs_messageHandlers.find(msg);
5660 wxCHECK_RET( i != gs_messageHandlers.end() && i->second == handler,
5661 _T("unregistering non-registered handler?") );
5662
5663 gs_messageHandlers.erase(i);
5664 }
5665
5666 // ===========================================================================
5667 // global functions
5668 // ===========================================================================
5669
5670 void wxGetCharSize(WXHWND wnd, int *x, int *y, const wxFont& the_font)
5671 {
5672 TEXTMETRIC tm;
5673 HDC dc = ::GetDC((HWND) wnd);
5674 HFONT was = 0;
5675
5676 // the_font.UseResource();
5677 // the_font.RealizeResource();
5678 HFONT fnt = (HFONT)the_font.GetResourceHandle(); // const_cast
5679 if ( fnt )
5680 was = (HFONT) SelectObject(dc,fnt);
5681
5682 GetTextMetrics(dc, &tm);
5683 if ( fnt && was )
5684 {
5685 SelectObject(dc,was);
5686 }
5687 ReleaseDC((HWND)wnd, dc);
5688
5689 if ( x )
5690 *x = tm.tmAveCharWidth;
5691 if ( y )
5692 *y = tm.tmHeight + tm.tmExternalLeading;
5693
5694 // the_font.ReleaseResource();
5695 }
5696
5697 // use the "extended" bit (24) of lParam to distinguish extended keys
5698 // from normal keys as the same key is sent
5699 static inline
5700 int ChooseNormalOrExtended(int lParam, int keyNormal, int keyExtended)
5701 {
5702 // except that if lParam is 0, it means we don't have real lParam from
5703 // WM_KEYDOWN but are just translating just a VK constant (e.g. done from
5704 // msw/treectrl.cpp when processing TVN_KEYDOWN) -- then assume this is a
5705 // non-numpad (hence extended) key as this is a more common case
5706 return !lParam || (lParam & (1 << 24)) ? keyExtended : keyNormal;
5707 }
5708
5709 // this array contains the Windows virtual key codes which map one to one to
5710 // WXK_xxx constants and is used in wxCharCodeMSWToWX/WXToMSW() below
5711 //
5712 // note that keys having a normal and numpad version (e.g. WXK_HOME and
5713 // WXK_NUMPAD_HOME) are not included in this table as the mapping is not 1-to-1
5714 static const struct wxKeyMapping
5715 {
5716 int vk;
5717 wxKeyCode wxk;
5718 } gs_specialKeys[] =
5719 {
5720 { VK_CANCEL, WXK_CANCEL },
5721 { VK_BACK, WXK_BACK },
5722 { VK_TAB, WXK_TAB },
5723 { VK_CLEAR, WXK_CLEAR },
5724 { VK_SHIFT, WXK_SHIFT },
5725 { VK_CONTROL, WXK_CONTROL },
5726 { VK_MENU , WXK_ALT },
5727 { VK_PAUSE, WXK_PAUSE },
5728 { VK_CAPITAL, WXK_CAPITAL },
5729 { VK_SPACE, WXK_SPACE },
5730 { VK_ESCAPE, WXK_ESCAPE },
5731 { VK_SELECT, WXK_SELECT },
5732 { VK_PRINT, WXK_PRINT },
5733 { VK_EXECUTE, WXK_EXECUTE },
5734 { VK_SNAPSHOT, WXK_SNAPSHOT },
5735 { VK_HELP, WXK_HELP },
5736
5737 { VK_NUMPAD0, WXK_NUMPAD0 },
5738 { VK_NUMPAD1, WXK_NUMPAD1 },
5739 { VK_NUMPAD2, WXK_NUMPAD2 },
5740 { VK_NUMPAD3, WXK_NUMPAD3 },
5741 { VK_NUMPAD4, WXK_NUMPAD4 },
5742 { VK_NUMPAD5, WXK_NUMPAD5 },
5743 { VK_NUMPAD6, WXK_NUMPAD6 },
5744 { VK_NUMPAD7, WXK_NUMPAD7 },
5745 { VK_NUMPAD8, WXK_NUMPAD8 },
5746 { VK_NUMPAD9, WXK_NUMPAD9 },
5747 { VK_MULTIPLY, WXK_NUMPAD_MULTIPLY },
5748 { VK_ADD, WXK_NUMPAD_ADD },
5749 { VK_SUBTRACT, WXK_NUMPAD_SUBTRACT },
5750 { VK_DECIMAL, WXK_NUMPAD_DECIMAL },
5751 { VK_DIVIDE, WXK_NUMPAD_DIVIDE },
5752
5753 { VK_F1, WXK_F1 },
5754 { VK_F2, WXK_F2 },
5755 { VK_F3, WXK_F3 },
5756 { VK_F4, WXK_F4 },
5757 { VK_F5, WXK_F5 },
5758 { VK_F6, WXK_F6 },
5759 { VK_F7, WXK_F7 },
5760 { VK_F8, WXK_F8 },
5761 { VK_F9, WXK_F9 },
5762 { VK_F10, WXK_F10 },
5763 { VK_F11, WXK_F11 },
5764 { VK_F12, WXK_F12 },
5765 { VK_F13, WXK_F13 },
5766 { VK_F14, WXK_F14 },
5767 { VK_F15, WXK_F15 },
5768 { VK_F16, WXK_F16 },
5769 { VK_F17, WXK_F17 },
5770 { VK_F18, WXK_F18 },
5771 { VK_F19, WXK_F19 },
5772 { VK_F20, WXK_F20 },
5773 { VK_F21, WXK_F21 },
5774 { VK_F22, WXK_F22 },
5775 { VK_F23, WXK_F23 },
5776 { VK_F24, WXK_F24 },
5777
5778 { VK_NUMLOCK, WXK_NUMLOCK },
5779 { VK_SCROLL, WXK_SCROLL },
5780
5781 #ifdef VK_APPS
5782 { VK_LWIN, WXK_WINDOWS_LEFT },
5783 { VK_RWIN, WXK_WINDOWS_RIGHT },
5784 { VK_APPS, WXK_WINDOWS_MENU },
5785 #endif // VK_APPS defined
5786 };
5787
5788 // Returns 0 if was a normal ASCII value, not a special key. This indicates that
5789 // the key should be ignored by WM_KEYDOWN and processed by WM_CHAR instead.
5790 int wxCharCodeMSWToWX(int vk, WXLPARAM lParam)
5791 {
5792 // check the table first
5793 for ( size_t n = 0; n < WXSIZEOF(gs_specialKeys); n++ )
5794 {
5795 if ( gs_specialKeys[n].vk == vk )
5796 return gs_specialKeys[n].wxk;
5797 }
5798
5799 // keys requiring special handling
5800 int wxk;
5801 switch ( vk )
5802 {
5803 // the mapping for these keys may be incorrect on non-US keyboards so
5804 // maybe we shouldn't map them to ASCII values at all
5805 case VK_OEM_1: wxk = ';'; break;
5806 case VK_OEM_PLUS: wxk = '+'; break;
5807 case VK_OEM_COMMA: wxk = ','; break;
5808 case VK_OEM_MINUS: wxk = '-'; break;
5809 case VK_OEM_PERIOD: wxk = '.'; break;
5810 case VK_OEM_2: wxk = '/'; break;
5811 case VK_OEM_3: wxk = '~'; break;
5812 case VK_OEM_4: wxk = '['; break;
5813 case VK_OEM_5: wxk = '\\'; break;
5814 case VK_OEM_6: wxk = ']'; break;
5815 case VK_OEM_7: wxk = '\''; break;
5816
5817 // handle extended keys
5818 case VK_PRIOR:
5819 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_PAGEUP, WXK_PAGEUP);
5820 break;
5821
5822 case VK_NEXT:
5823 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_PAGEDOWN, WXK_PAGEDOWN);
5824 break;
5825
5826 case VK_END:
5827 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_END, WXK_END);
5828 break;
5829
5830 case VK_HOME:
5831 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_HOME, WXK_HOME);
5832 break;
5833
5834 case VK_LEFT:
5835 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_LEFT, WXK_LEFT);
5836 break;
5837
5838 case VK_UP:
5839 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_UP, WXK_UP);
5840 break;
5841
5842 case VK_RIGHT:
5843 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_RIGHT, WXK_RIGHT);
5844 break;
5845
5846 case VK_DOWN:
5847 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_DOWN, WXK_DOWN);
5848 break;
5849
5850 case VK_INSERT:
5851 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_INSERT, WXK_INSERT);
5852 break;
5853
5854 case VK_DELETE:
5855 wxk = ChooseNormalOrExtended(lParam, WXK_NUMPAD_DELETE, WXK_DELETE);
5856 break;
5857
5858 case VK_RETURN:
5859 // don't use ChooseNormalOrExtended() here as the keys are reversed
5860 // here: numpad enter is the extended one
5861 wxk = lParam && (lParam & (1 << 24)) ? WXK_NUMPAD_ENTER : WXK_RETURN;
5862 break;
5863
5864 default:
5865 wxk = 0;
5866 }
5867
5868 return wxk;
5869 }
5870
5871 WXWORD wxCharCodeWXToMSW(int wxk, bool *isVirtual)
5872 {
5873 if ( isVirtual )
5874 *isVirtual = true;
5875
5876 // check the table first
5877 for ( size_t n = 0; n < WXSIZEOF(gs_specialKeys); n++ )
5878 {
5879 if ( gs_specialKeys[n].wxk == wxk )
5880 return gs_specialKeys[n].vk;
5881 }
5882
5883 // and then check for special keys not included in the table
5884 WXWORD vk;
5885 switch ( wxk )
5886 {
5887 case WXK_PAGEUP:
5888 case WXK_NUMPAD_PAGEUP:
5889 vk = VK_PRIOR;
5890 break;
5891
5892 case WXK_PAGEDOWN:
5893 case WXK_NUMPAD_PAGEDOWN:
5894 vk = VK_NEXT;
5895 break;
5896
5897 case WXK_END:
5898 case WXK_NUMPAD_END:
5899 vk = VK_END;
5900 break;
5901
5902 case WXK_HOME:
5903 case WXK_NUMPAD_HOME:
5904 vk = VK_HOME;
5905 break;
5906
5907 case WXK_LEFT:
5908 case WXK_NUMPAD_LEFT:
5909 vk = VK_LEFT;
5910 break;
5911
5912 case WXK_UP:
5913 case WXK_NUMPAD_UP:
5914 vk = VK_UP;
5915 break;
5916
5917 case WXK_RIGHT:
5918 case WXK_NUMPAD_RIGHT:
5919 vk = VK_RIGHT;
5920 break;
5921
5922 case WXK_DOWN:
5923 case WXK_NUMPAD_DOWN:
5924 vk = VK_DOWN;
5925 break;
5926
5927 case WXK_INSERT:
5928 case WXK_NUMPAD_INSERT:
5929 vk = VK_INSERT;
5930 break;
5931
5932 case WXK_DELETE:
5933 case WXK_NUMPAD_DELETE:
5934 vk = VK_DELETE;
5935 break;
5936
5937 default:
5938 if ( isVirtual )
5939 *isVirtual = false;
5940 vk = (WXWORD)wxk;
5941 break;
5942 }
5943
5944 return vk;
5945 }
5946
5947 #ifndef SM_SWAPBUTTON
5948 #define SM_SWAPBUTTON 23
5949 #endif
5950
5951 // small helper for wxGetKeyState() and wxGetMouseState()
5952 static inline bool wxIsKeyDown(WXWORD vk)
5953 {
5954 switch (vk)
5955 {
5956 case VK_LBUTTON:
5957 if (GetSystemMetrics(SM_SWAPBUTTON)) vk = VK_RBUTTON;
5958 break;
5959 case VK_RBUTTON:
5960 if (GetSystemMetrics(SM_SWAPBUTTON)) vk = VK_LBUTTON;
5961 break;
5962 }
5963 // the low order bit indicates whether the key was pressed since the last
5964 // call and the high order one indicates whether it is down right now and
5965 // we only want that one
5966 return (GetAsyncKeyState(vk) & (1<<15)) != 0;
5967 }
5968
5969 bool wxGetKeyState(wxKeyCode key)
5970 {
5971 // although this does work under Windows, it is not supported under other
5972 // platforms so don't allow it, you must use wxGetMouseState() instead
5973 wxASSERT_MSG( key != VK_LBUTTON &&
5974 key != VK_RBUTTON &&
5975 key != VK_MBUTTON,
5976 wxT("can't use wxGetKeyState() for mouse buttons") );
5977
5978 const WXWORD vk = wxCharCodeWXToMSW(key);
5979
5980 // if the requested key is a LED key, return true if the led is pressed
5981 if ( key == WXK_NUMLOCK || key == WXK_CAPITAL || key == WXK_SCROLL )
5982 {
5983 // low order bit means LED is highlighted and high order one means the
5984 // key is down; for compatibility with the other ports return true if
5985 // either one is set
5986 return GetKeyState(vk) != 0;
5987
5988 }
5989 else // normal key
5990 {
5991 return wxIsKeyDown(vk);
5992 }
5993 }
5994
5995
5996 wxMouseState wxGetMouseState()
5997 {
5998 wxMouseState ms;
5999 POINT pt;
6000 GetCursorPos( &pt );
6001
6002 ms.SetX(pt.x);
6003 ms.SetY(pt.y);
6004 ms.SetLeftDown(wxIsKeyDown(VK_LBUTTON));
6005 ms.SetMiddleDown(wxIsKeyDown(VK_MBUTTON));
6006 ms.SetRightDown(wxIsKeyDown(VK_RBUTTON));
6007 #ifdef wxHAS_XBUTTON
6008 ms.SetAux1Down(wxIsKeyDown(VK_XBUTTON1));
6009 ms.SetAux2Down(wxIsKeyDown(VK_XBUTTON2));
6010 #endif // wxHAS_XBUTTON
6011
6012 ms.SetControlDown(wxIsKeyDown(VK_CONTROL));
6013 ms.SetShiftDown(wxIsKeyDown(VK_SHIFT));
6014 ms.SetAltDown(wxIsKeyDown(VK_MENU));
6015 // ms.SetMetaDown();
6016
6017 return ms;
6018 }
6019
6020
6021 wxWindow *wxGetActiveWindow()
6022 {
6023 HWND hWnd = GetActiveWindow();
6024 if ( hWnd != 0 )
6025 {
6026 return wxFindWinFromHandle((WXHWND) hWnd);
6027 }
6028 return NULL;
6029 }
6030
6031 extern wxWindow *wxGetWindowFromHWND(WXHWND hWnd)
6032 {
6033 HWND hwnd = (HWND)hWnd;
6034
6035 // For a radiobutton, we get the radiobox from GWL_USERDATA (which is set
6036 // by code in msw/radiobox.cpp), for all the others we just search up the
6037 // window hierarchy
6038 wxWindow *win = (wxWindow *)NULL;
6039 if ( hwnd )
6040 {
6041 win = wxFindWinFromHandle((WXHWND)hwnd);
6042 if ( !win )
6043 {
6044 #if wxUSE_RADIOBOX
6045 // native radiobuttons return DLGC_RADIOBUTTON here and for any
6046 // wxWindow class which overrides WM_GETDLGCODE processing to
6047 // do it as well, win would be already non NULL
6048 if ( ::SendMessage(hwnd, WM_GETDLGCODE, 0, 0) & DLGC_RADIOBUTTON )
6049 {
6050 win = (wxWindow *)wxGetWindowUserData(hwnd);
6051 }
6052 //else: it's a wxRadioButton, not a radiobutton from wxRadioBox
6053 #endif // wxUSE_RADIOBOX
6054
6055 // spin control text buddy window should be mapped to spin ctrl
6056 // itself so try it too
6057 #if wxUSE_SPINCTRL && !defined(__WXUNIVERSAL__)
6058 if ( !win )
6059 {
6060 win = wxSpinCtrl::GetSpinForTextCtrl((WXHWND)hwnd);
6061 }
6062 #endif // wxUSE_SPINCTRL
6063 }
6064 }
6065
6066 while ( hwnd && !win )
6067 {
6068 // this is a really ugly hack needed to avoid mistakenly returning the
6069 // parent frame wxWindow for the find/replace modeless dialog HWND -
6070 // this, in turn, is needed to call IsDialogMessage() from
6071 // wxApp::ProcessMessage() as for this we must return NULL from here
6072 //
6073 // FIXME: this is clearly not the best way to do it but I think we'll
6074 // need to change HWND <-> wxWindow code more heavily than I can
6075 // do it now to fix it
6076 #ifndef __WXMICROWIN__
6077 if ( ::GetWindow(hwnd, GW_OWNER) )
6078 {
6079 // it's a dialog box, don't go upwards
6080 break;
6081 }
6082 #endif
6083
6084 hwnd = ::GetParent(hwnd);
6085 win = wxFindWinFromHandle((WXHWND)hwnd);
6086 }
6087
6088 return win;
6089 }
6090
6091 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
6092
6093 // Windows keyboard hook. Allows interception of e.g. F1, ESCAPE
6094 // in active frames and dialogs, regardless of where the focus is.
6095 static HHOOK wxTheKeyboardHook = 0;
6096 static FARPROC wxTheKeyboardHookProc = 0;
6097 int APIENTRY _EXPORT
6098 wxKeyboardHook(int nCode, WORD wParam, DWORD lParam);
6099
6100 void wxSetKeyboardHook(bool doIt)
6101 {
6102 if ( doIt )
6103 {
6104 wxTheKeyboardHookProc = MakeProcInstance((FARPROC) wxKeyboardHook, wxGetInstance());
6105 wxTheKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC) wxTheKeyboardHookProc, wxGetInstance(),
6106
6107 GetCurrentThreadId()
6108 // (DWORD)GetCurrentProcess()); // This is another possibility. Which is right?
6109 );
6110 }
6111 else
6112 {
6113 UnhookWindowsHookEx(wxTheKeyboardHook);
6114 }
6115 }
6116
6117 int APIENTRY _EXPORT
6118 wxKeyboardHook(int nCode, WORD wParam, DWORD lParam)
6119 {
6120 DWORD hiWord = HIWORD(lParam);
6121 if ( nCode != HC_NOREMOVE && ((hiWord & KF_UP) == 0) )
6122 {
6123 int id = wxCharCodeMSWToWX(wParam, lParam);
6124 if ( id != 0 )
6125 {
6126 wxKeyEvent event(wxEVT_CHAR_HOOK);
6127 if ( (HIWORD(lParam) & KF_ALTDOWN) == KF_ALTDOWN )
6128 event.m_altDown = true;
6129
6130 event.SetEventObject(NULL);
6131 event.m_keyCode = id;
6132 event.m_shiftDown = wxIsShiftDown();
6133 event.m_controlDown = wxIsCtrlDown();
6134 #ifndef __WXWINCE__
6135 event.SetTimestamp(::GetMessageTime());
6136 #endif
6137 wxWindow *win = wxGetActiveWindow();
6138 wxEvtHandler *handler;
6139 if ( win )
6140 {
6141 handler = win->GetEventHandler();
6142 event.SetId(win->GetId());
6143 }
6144 else
6145 {
6146 handler = wxTheApp;
6147 event.SetId(wxID_ANY);
6148 }
6149
6150 if ( handler && handler->ProcessEvent(event) )
6151 {
6152 // processed
6153 return 1;
6154 }
6155 }
6156 }
6157
6158 return (int)CallNextHookEx(wxTheKeyboardHook, nCode, wParam, lParam);
6159 }
6160
6161 #endif // !__WXMICROWIN__
6162
6163 #ifdef __WXDEBUG__
6164 const wxChar *wxGetMessageName(int message)
6165 {
6166 switch ( message )
6167 {
6168 case 0x0000: return wxT("WM_NULL");
6169 case 0x0001: return wxT("WM_CREATE");
6170 case 0x0002: return wxT("WM_DESTROY");
6171 case 0x0003: return wxT("WM_MOVE");
6172 case 0x0005: return wxT("WM_SIZE");
6173 case 0x0006: return wxT("WM_ACTIVATE");
6174 case 0x0007: return wxT("WM_SETFOCUS");
6175 case 0x0008: return wxT("WM_KILLFOCUS");
6176 case 0x000A: return wxT("WM_ENABLE");
6177 case 0x000B: return wxT("WM_SETREDRAW");
6178 case 0x000C: return wxT("WM_SETTEXT");
6179 case 0x000D: return wxT("WM_GETTEXT");
6180 case 0x000E: return wxT("WM_GETTEXTLENGTH");
6181 case 0x000F: return wxT("WM_PAINT");
6182 case 0x0010: return wxT("WM_CLOSE");
6183 case 0x0011: return wxT("WM_QUERYENDSESSION");
6184 case 0x0012: return wxT("WM_QUIT");
6185 case 0x0013: return wxT("WM_QUERYOPEN");
6186 case 0x0014: return wxT("WM_ERASEBKGND");
6187 case 0x0015: return wxT("WM_SYSCOLORCHANGE");
6188 case 0x0016: return wxT("WM_ENDSESSION");
6189 case 0x0017: return wxT("WM_SYSTEMERROR");
6190 case 0x0018: return wxT("WM_SHOWWINDOW");
6191 case 0x0019: return wxT("WM_CTLCOLOR");
6192 case 0x001A: return wxT("WM_WININICHANGE");
6193 case 0x001B: return wxT("WM_DEVMODECHANGE");
6194 case 0x001C: return wxT("WM_ACTIVATEAPP");
6195 case 0x001D: return wxT("WM_FONTCHANGE");
6196 case 0x001E: return wxT("WM_TIMECHANGE");
6197 case 0x001F: return wxT("WM_CANCELMODE");
6198 case 0x0020: return wxT("WM_SETCURSOR");
6199 case 0x0021: return wxT("WM_MOUSEACTIVATE");
6200 case 0x0022: return wxT("WM_CHILDACTIVATE");
6201 case 0x0023: return wxT("WM_QUEUESYNC");
6202 case 0x0024: return wxT("WM_GETMINMAXINFO");
6203 case 0x0026: return wxT("WM_PAINTICON");
6204 case 0x0027: return wxT("WM_ICONERASEBKGND");
6205 case 0x0028: return wxT("WM_NEXTDLGCTL");
6206 case 0x002A: return wxT("WM_SPOOLERSTATUS");
6207 case 0x002B: return wxT("WM_DRAWITEM");
6208 case 0x002C: return wxT("WM_MEASUREITEM");
6209 case 0x002D: return wxT("WM_DELETEITEM");
6210 case 0x002E: return wxT("WM_VKEYTOITEM");
6211 case 0x002F: return wxT("WM_CHARTOITEM");
6212 case 0x0030: return wxT("WM_SETFONT");
6213 case 0x0031: return wxT("WM_GETFONT");
6214 case 0x0037: return wxT("WM_QUERYDRAGICON");
6215 case 0x0039: return wxT("WM_COMPAREITEM");
6216 case 0x0041: return wxT("WM_COMPACTING");
6217 case 0x0044: return wxT("WM_COMMNOTIFY");
6218 case 0x0046: return wxT("WM_WINDOWPOSCHANGING");
6219 case 0x0047: return wxT("WM_WINDOWPOSCHANGED");
6220 case 0x0048: return wxT("WM_POWER");
6221
6222 case 0x004A: return wxT("WM_COPYDATA");
6223 case 0x004B: return wxT("WM_CANCELJOURNAL");
6224 case 0x004E: return wxT("WM_NOTIFY");
6225 case 0x0050: return wxT("WM_INPUTLANGCHANGEREQUEST");
6226 case 0x0051: return wxT("WM_INPUTLANGCHANGE");
6227 case 0x0052: return wxT("WM_TCARD");
6228 case 0x0053: return wxT("WM_HELP");
6229 case 0x0054: return wxT("WM_USERCHANGED");
6230 case 0x0055: return wxT("WM_NOTIFYFORMAT");
6231 case 0x007B: return wxT("WM_CONTEXTMENU");
6232 case 0x007C: return wxT("WM_STYLECHANGING");
6233 case 0x007D: return wxT("WM_STYLECHANGED");
6234 case 0x007E: return wxT("WM_DISPLAYCHANGE");
6235 case 0x007F: return wxT("WM_GETICON");
6236 case 0x0080: return wxT("WM_SETICON");
6237
6238 case 0x0081: return wxT("WM_NCCREATE");
6239 case 0x0082: return wxT("WM_NCDESTROY");
6240 case 0x0083: return wxT("WM_NCCALCSIZE");
6241 case 0x0084: return wxT("WM_NCHITTEST");
6242 case 0x0085: return wxT("WM_NCPAINT");
6243 case 0x0086: return wxT("WM_NCACTIVATE");
6244 case 0x0087: return wxT("WM_GETDLGCODE");
6245 case 0x00A0: return wxT("WM_NCMOUSEMOVE");
6246 case 0x00A1: return wxT("WM_NCLBUTTONDOWN");
6247 case 0x00A2: return wxT("WM_NCLBUTTONUP");
6248 case 0x00A3: return wxT("WM_NCLBUTTONDBLCLK");
6249 case 0x00A4: return wxT("WM_NCRBUTTONDOWN");
6250 case 0x00A5: return wxT("WM_NCRBUTTONUP");
6251 case 0x00A6: return wxT("WM_NCRBUTTONDBLCLK");
6252 case 0x00A7: return wxT("WM_NCMBUTTONDOWN");
6253 case 0x00A8: return wxT("WM_NCMBUTTONUP");
6254 case 0x00A9: return wxT("WM_NCMBUTTONDBLCLK");
6255
6256 case 0x00B0: return wxT("EM_GETSEL");
6257 case 0x00B1: return wxT("EM_SETSEL");
6258 case 0x00B2: return wxT("EM_GETRECT");
6259 case 0x00B3: return wxT("EM_SETRECT");
6260 case 0x00B4: return wxT("EM_SETRECTNP");
6261 case 0x00B5: return wxT("EM_SCROLL");
6262 case 0x00B6: return wxT("EM_LINESCROLL");
6263 case 0x00B7: return wxT("EM_SCROLLCARET");
6264 case 0x00B8: return wxT("EM_GETMODIFY");
6265 case 0x00B9: return wxT("EM_SETMODIFY");
6266 case 0x00BA: return wxT("EM_GETLINECOUNT");
6267 case 0x00BB: return wxT("EM_LINEINDEX");
6268 case 0x00BC: return wxT("EM_SETHANDLE");
6269 case 0x00BD: return wxT("EM_GETHANDLE");
6270 case 0x00BE: return wxT("EM_GETTHUMB");
6271 case 0x00C1: return wxT("EM_LINELENGTH");
6272 case 0x00C2: return wxT("EM_REPLACESEL");
6273 case 0x00C4: return wxT("EM_GETLINE");
6274 case 0x00C5: return wxT("EM_LIMITTEXT/EM_SETLIMITTEXT"); /* ;win40 Name change */
6275 case 0x00C6: return wxT("EM_CANUNDO");
6276 case 0x00C7: return wxT("EM_UNDO");
6277 case 0x00C8: return wxT("EM_FMTLINES");
6278 case 0x00C9: return wxT("EM_LINEFROMCHAR");
6279 case 0x00CB: return wxT("EM_SETTABSTOPS");
6280 case 0x00CC: return wxT("EM_SETPASSWORDCHAR");
6281 case 0x00CD: return wxT("EM_EMPTYUNDOBUFFER");
6282 case 0x00CE: return wxT("EM_GETFIRSTVISIBLELINE");
6283 case 0x00CF: return wxT("EM_SETREADONLY");
6284 case 0x00D0: return wxT("EM_SETWORDBREAKPROC");
6285 case 0x00D1: return wxT("EM_GETWORDBREAKPROC");
6286 case 0x00D2: return wxT("EM_GETPASSWORDCHAR");
6287 case 0x00D3: return wxT("EM_SETMARGINS");
6288 case 0x00D4: return wxT("EM_GETMARGINS");
6289 case 0x00D5: return wxT("EM_GETLIMITTEXT");
6290 case 0x00D6: return wxT("EM_POSFROMCHAR");
6291 case 0x00D7: return wxT("EM_CHARFROMPOS");
6292 case 0x00D8: return wxT("EM_SETIMESTATUS");
6293 case 0x00D9: return wxT("EM_GETIMESTATUS");
6294
6295 case 0x0100: return wxT("WM_KEYDOWN");
6296 case 0x0101: return wxT("WM_KEYUP");
6297 case 0x0102: return wxT("WM_CHAR");
6298 case 0x0103: return wxT("WM_DEADCHAR");
6299 case 0x0104: return wxT("WM_SYSKEYDOWN");
6300 case 0x0105: return wxT("WM_SYSKEYUP");
6301 case 0x0106: return wxT("WM_SYSCHAR");
6302 case 0x0107: return wxT("WM_SYSDEADCHAR");
6303 case 0x0108: return wxT("WM_KEYLAST");
6304
6305 case 0x010D: return wxT("WM_IME_STARTCOMPOSITION");
6306 case 0x010E: return wxT("WM_IME_ENDCOMPOSITION");
6307 case 0x010F: return wxT("WM_IME_COMPOSITION");
6308
6309 case 0x0110: return wxT("WM_INITDIALOG");
6310 case 0x0111: return wxT("WM_COMMAND");
6311 case 0x0112: return wxT("WM_SYSCOMMAND");
6312 case 0x0113: return wxT("WM_TIMER");
6313 case 0x0114: return wxT("WM_HSCROLL");
6314 case 0x0115: return wxT("WM_VSCROLL");
6315 case 0x0116: return wxT("WM_INITMENU");
6316 case 0x0117: return wxT("WM_INITMENUPOPUP");
6317 case 0x011F: return wxT("WM_MENUSELECT");
6318 case 0x0120: return wxT("WM_MENUCHAR");
6319 case 0x0121: return wxT("WM_ENTERIDLE");
6320
6321 case 0x0132: return wxT("WM_CTLCOLORMSGBOX");
6322 case 0x0133: return wxT("WM_CTLCOLOREDIT");
6323 case 0x0134: return wxT("WM_CTLCOLORLISTBOX");
6324 case 0x0135: return wxT("WM_CTLCOLORBTN");
6325 case 0x0136: return wxT("WM_CTLCOLORDLG");
6326 case 0x0137: return wxT("WM_CTLCOLORSCROLLBAR");
6327 case 0x0138: return wxT("WM_CTLCOLORSTATIC");
6328 case 0x01E1: return wxT("MN_GETHMENU");
6329
6330 case 0x0200: return wxT("WM_MOUSEMOVE");
6331 case 0x0201: return wxT("WM_LBUTTONDOWN");
6332 case 0x0202: return wxT("WM_LBUTTONUP");
6333 case 0x0203: return wxT("WM_LBUTTONDBLCLK");
6334 case 0x0204: return wxT("WM_RBUTTONDOWN");
6335 case 0x0205: return wxT("WM_RBUTTONUP");
6336 case 0x0206: return wxT("WM_RBUTTONDBLCLK");
6337 case 0x0207: return wxT("WM_MBUTTONDOWN");
6338 case 0x0208: return wxT("WM_MBUTTONUP");
6339 case 0x0209: return wxT("WM_MBUTTONDBLCLK");
6340 case 0x020A: return wxT("WM_MOUSEWHEEL");
6341 case 0x020B: return wxT("WM_XBUTTONDOWN");
6342 case 0x020C: return wxT("WM_XBUTTONUP");
6343 case 0x020D: return wxT("WM_XBUTTONDBLCLK");
6344 case 0x0210: return wxT("WM_PARENTNOTIFY");
6345 case 0x0211: return wxT("WM_ENTERMENULOOP");
6346 case 0x0212: return wxT("WM_EXITMENULOOP");
6347
6348 case 0x0213: return wxT("WM_NEXTMENU");
6349 case 0x0214: return wxT("WM_SIZING");
6350 case 0x0215: return wxT("WM_CAPTURECHANGED");
6351 case 0x0216: return wxT("WM_MOVING");
6352 case 0x0218: return wxT("WM_POWERBROADCAST");
6353 case 0x0219: return wxT("WM_DEVICECHANGE");
6354
6355 case 0x0220: return wxT("WM_MDICREATE");
6356 case 0x0221: return wxT("WM_MDIDESTROY");
6357 case 0x0222: return wxT("WM_MDIACTIVATE");
6358 case 0x0223: return wxT("WM_MDIRESTORE");
6359 case 0x0224: return wxT("WM_MDINEXT");
6360 case 0x0225: return wxT("WM_MDIMAXIMIZE");
6361 case 0x0226: return wxT("WM_MDITILE");
6362 case 0x0227: return wxT("WM_MDICASCADE");
6363 case 0x0228: return wxT("WM_MDIICONARRANGE");
6364 case 0x0229: return wxT("WM_MDIGETACTIVE");
6365 case 0x0230: return wxT("WM_MDISETMENU");
6366 case 0x0233: return wxT("WM_DROPFILES");
6367
6368 case 0x0281: return wxT("WM_IME_SETCONTEXT");
6369 case 0x0282: return wxT("WM_IME_NOTIFY");
6370 case 0x0283: return wxT("WM_IME_CONTROL");
6371 case 0x0284: return wxT("WM_IME_COMPOSITIONFULL");
6372 case 0x0285: return wxT("WM_IME_SELECT");
6373 case 0x0286: return wxT("WM_IME_CHAR");
6374 case 0x0290: return wxT("WM_IME_KEYDOWN");
6375 case 0x0291: return wxT("WM_IME_KEYUP");
6376
6377 case 0x02A0: return wxT("WM_NCMOUSEHOVER");
6378 case 0x02A1: return wxT("WM_MOUSEHOVER");
6379 case 0x02A2: return wxT("WM_NCMOUSELEAVE");
6380 case 0x02A3: return wxT("WM_MOUSELEAVE");
6381
6382 case 0x0300: return wxT("WM_CUT");
6383 case 0x0301: return wxT("WM_COPY");
6384 case 0x0302: return wxT("WM_PASTE");
6385 case 0x0303: return wxT("WM_CLEAR");
6386 case 0x0304: return wxT("WM_UNDO");
6387 case 0x0305: return wxT("WM_RENDERFORMAT");
6388 case 0x0306: return wxT("WM_RENDERALLFORMATS");
6389 case 0x0307: return wxT("WM_DESTROYCLIPBOARD");
6390 case 0x0308: return wxT("WM_DRAWCLIPBOARD");
6391 case 0x0309: return wxT("WM_PAINTCLIPBOARD");
6392 case 0x030A: return wxT("WM_VSCROLLCLIPBOARD");
6393 case 0x030B: return wxT("WM_SIZECLIPBOARD");
6394 case 0x030C: return wxT("WM_ASKCBFORMATNAME");
6395 case 0x030D: return wxT("WM_CHANGECBCHAIN");
6396 case 0x030E: return wxT("WM_HSCROLLCLIPBOARD");
6397 case 0x030F: return wxT("WM_QUERYNEWPALETTE");
6398 case 0x0310: return wxT("WM_PALETTEISCHANGING");
6399 case 0x0311: return wxT("WM_PALETTECHANGED");
6400 case 0x0312: return wxT("WM_HOTKEY");
6401
6402 case 0x0317: return wxT("WM_PRINT");
6403 case 0x0318: return wxT("WM_PRINTCLIENT");
6404
6405 // common controls messages - although they're not strictly speaking
6406 // standard, it's nice to decode them nevertheless
6407
6408 // listview
6409 case 0x1000 + 0: return wxT("LVM_GETBKCOLOR");
6410 case 0x1000 + 1: return wxT("LVM_SETBKCOLOR");
6411 case 0x1000 + 2: return wxT("LVM_GETIMAGELIST");
6412 case 0x1000 + 3: return wxT("LVM_SETIMAGELIST");
6413 case 0x1000 + 4: return wxT("LVM_GETITEMCOUNT");
6414 case 0x1000 + 5: return wxT("LVM_GETITEMA");
6415 case 0x1000 + 75: return wxT("LVM_GETITEMW");
6416 case 0x1000 + 6: return wxT("LVM_SETITEMA");
6417 case 0x1000 + 76: return wxT("LVM_SETITEMW");
6418 case 0x1000 + 7: return wxT("LVM_INSERTITEMA");
6419 case 0x1000 + 77: return wxT("LVM_INSERTITEMW");
6420 case 0x1000 + 8: return wxT("LVM_DELETEITEM");
6421 case 0x1000 + 9: return wxT("LVM_DELETEALLITEMS");
6422 case 0x1000 + 10: return wxT("LVM_GETCALLBACKMASK");
6423 case 0x1000 + 11: return wxT("LVM_SETCALLBACKMASK");
6424 case 0x1000 + 12: return wxT("LVM_GETNEXTITEM");
6425 case 0x1000 + 13: return wxT("LVM_FINDITEMA");
6426 case 0x1000 + 83: return wxT("LVM_FINDITEMW");
6427 case 0x1000 + 14: return wxT("LVM_GETITEMRECT");
6428 case 0x1000 + 15: return wxT("LVM_SETITEMPOSITION");
6429 case 0x1000 + 16: return wxT("LVM_GETITEMPOSITION");
6430 case 0x1000 + 17: return wxT("LVM_GETSTRINGWIDTHA");
6431 case 0x1000 + 87: return wxT("LVM_GETSTRINGWIDTHW");
6432 case 0x1000 + 18: return wxT("LVM_HITTEST");
6433 case 0x1000 + 19: return wxT("LVM_ENSUREVISIBLE");
6434 case 0x1000 + 20: return wxT("LVM_SCROLL");
6435 case 0x1000 + 21: return wxT("LVM_REDRAWITEMS");
6436 case 0x1000 + 22: return wxT("LVM_ARRANGE");
6437 case 0x1000 + 23: return wxT("LVM_EDITLABELA");
6438 case 0x1000 + 118: return wxT("LVM_EDITLABELW");
6439 case 0x1000 + 24: return wxT("LVM_GETEDITCONTROL");
6440 case 0x1000 + 25: return wxT("LVM_GETCOLUMNA");
6441 case 0x1000 + 95: return wxT("LVM_GETCOLUMNW");
6442 case 0x1000 + 26: return wxT("LVM_SETCOLUMNA");
6443 case 0x1000 + 96: return wxT("LVM_SETCOLUMNW");
6444 case 0x1000 + 27: return wxT("LVM_INSERTCOLUMNA");
6445 case 0x1000 + 97: return wxT("LVM_INSERTCOLUMNW");
6446 case 0x1000 + 28: return wxT("LVM_DELETECOLUMN");
6447 case 0x1000 + 29: return wxT("LVM_GETCOLUMNWIDTH");
6448 case 0x1000 + 30: return wxT("LVM_SETCOLUMNWIDTH");
6449 case 0x1000 + 31: return wxT("LVM_GETHEADER");
6450 case 0x1000 + 33: return wxT("LVM_CREATEDRAGIMAGE");
6451 case 0x1000 + 34: return wxT("LVM_GETVIEWRECT");
6452 case 0x1000 + 35: return wxT("LVM_GETTEXTCOLOR");
6453 case 0x1000 + 36: return wxT("LVM_SETTEXTCOLOR");
6454 case 0x1000 + 37: return wxT("LVM_GETTEXTBKCOLOR");
6455 case 0x1000 + 38: return wxT("LVM_SETTEXTBKCOLOR");
6456 case 0x1000 + 39: return wxT("LVM_GETTOPINDEX");
6457 case 0x1000 + 40: return wxT("LVM_GETCOUNTPERPAGE");
6458 case 0x1000 + 41: return wxT("LVM_GETORIGIN");
6459 case 0x1000 + 42: return wxT("LVM_UPDATE");
6460 case 0x1000 + 43: return wxT("LVM_SETITEMSTATE");
6461 case 0x1000 + 44: return wxT("LVM_GETITEMSTATE");
6462 case 0x1000 + 45: return wxT("LVM_GETITEMTEXTA");
6463 case 0x1000 + 115: return wxT("LVM_GETITEMTEXTW");
6464 case 0x1000 + 46: return wxT("LVM_SETITEMTEXTA");
6465 case 0x1000 + 116: return wxT("LVM_SETITEMTEXTW");
6466 case 0x1000 + 47: return wxT("LVM_SETITEMCOUNT");
6467 case 0x1000 + 48: return wxT("LVM_SORTITEMS");
6468 case 0x1000 + 49: return wxT("LVM_SETITEMPOSITION32");
6469 case 0x1000 + 50: return wxT("LVM_GETSELECTEDCOUNT");
6470 case 0x1000 + 51: return wxT("LVM_GETITEMSPACING");
6471 case 0x1000 + 52: return wxT("LVM_GETISEARCHSTRINGA");
6472 case 0x1000 + 117: return wxT("LVM_GETISEARCHSTRINGW");
6473 case 0x1000 + 53: return wxT("LVM_SETICONSPACING");
6474 case 0x1000 + 54: return wxT("LVM_SETEXTENDEDLISTVIEWSTYLE");
6475 case 0x1000 + 55: return wxT("LVM_GETEXTENDEDLISTVIEWSTYLE");
6476 case 0x1000 + 56: return wxT("LVM_GETSUBITEMRECT");
6477 case 0x1000 + 57: return wxT("LVM_SUBITEMHITTEST");
6478 case 0x1000 + 58: return wxT("LVM_SETCOLUMNORDERARRAY");
6479 case 0x1000 + 59: return wxT("LVM_GETCOLUMNORDERARRAY");
6480 case 0x1000 + 60: return wxT("LVM_SETHOTITEM");
6481 case 0x1000 + 61: return wxT("LVM_GETHOTITEM");
6482 case 0x1000 + 62: return wxT("LVM_SETHOTCURSOR");
6483 case 0x1000 + 63: return wxT("LVM_GETHOTCURSOR");
6484 case 0x1000 + 64: return wxT("LVM_APPROXIMATEVIEWRECT");
6485 case 0x1000 + 65: return wxT("LVM_SETWORKAREA");
6486
6487 // tree view
6488 case 0x1100 + 0: return wxT("TVM_INSERTITEMA");
6489 case 0x1100 + 50: return wxT("TVM_INSERTITEMW");
6490 case 0x1100 + 1: return wxT("TVM_DELETEITEM");
6491 case 0x1100 + 2: return wxT("TVM_EXPAND");
6492 case 0x1100 + 4: return wxT("TVM_GETITEMRECT");
6493 case 0x1100 + 5: return wxT("TVM_GETCOUNT");
6494 case 0x1100 + 6: return wxT("TVM_GETINDENT");
6495 case 0x1100 + 7: return wxT("TVM_SETINDENT");
6496 case 0x1100 + 8: return wxT("TVM_GETIMAGELIST");
6497 case 0x1100 + 9: return wxT("TVM_SETIMAGELIST");
6498 case 0x1100 + 10: return wxT("TVM_GETNEXTITEM");
6499 case 0x1100 + 11: return wxT("TVM_SELECTITEM");
6500 case 0x1100 + 12: return wxT("TVM_GETITEMA");
6501 case 0x1100 + 62: return wxT("TVM_GETITEMW");
6502 case 0x1100 + 13: return wxT("TVM_SETITEMA");
6503 case 0x1100 + 63: return wxT("TVM_SETITEMW");
6504 case 0x1100 + 14: return wxT("TVM_EDITLABELA");
6505 case 0x1100 + 65: return wxT("TVM_EDITLABELW");
6506 case 0x1100 + 15: return wxT("TVM_GETEDITCONTROL");
6507 case 0x1100 + 16: return wxT("TVM_GETVISIBLECOUNT");
6508 case 0x1100 + 17: return wxT("TVM_HITTEST");
6509 case 0x1100 + 18: return wxT("TVM_CREATEDRAGIMAGE");
6510 case 0x1100 + 19: return wxT("TVM_SORTCHILDREN");
6511 case 0x1100 + 20: return wxT("TVM_ENSUREVISIBLE");
6512 case 0x1100 + 21: return wxT("TVM_SORTCHILDRENCB");
6513 case 0x1100 + 22: return wxT("TVM_ENDEDITLABELNOW");
6514 case 0x1100 + 23: return wxT("TVM_GETISEARCHSTRINGA");
6515 case 0x1100 + 64: return wxT("TVM_GETISEARCHSTRINGW");
6516 case 0x1100 + 24: return wxT("TVM_SETTOOLTIPS");
6517 case 0x1100 + 25: return wxT("TVM_GETTOOLTIPS");
6518
6519 // header
6520 case 0x1200 + 0: return wxT("HDM_GETITEMCOUNT");
6521 case 0x1200 + 1: return wxT("HDM_INSERTITEMA");
6522 case 0x1200 + 10: return wxT("HDM_INSERTITEMW");
6523 case 0x1200 + 2: return wxT("HDM_DELETEITEM");
6524 case 0x1200 + 3: return wxT("HDM_GETITEMA");
6525 case 0x1200 + 11: return wxT("HDM_GETITEMW");
6526 case 0x1200 + 4: return wxT("HDM_SETITEMA");
6527 case 0x1200 + 12: return wxT("HDM_SETITEMW");
6528 case 0x1200 + 5: return wxT("HDM_LAYOUT");
6529 case 0x1200 + 6: return wxT("HDM_HITTEST");
6530 case 0x1200 + 7: return wxT("HDM_GETITEMRECT");
6531 case 0x1200 + 8: return wxT("HDM_SETIMAGELIST");
6532 case 0x1200 + 9: return wxT("HDM_GETIMAGELIST");
6533 case 0x1200 + 15: return wxT("HDM_ORDERTOINDEX");
6534 case 0x1200 + 16: return wxT("HDM_CREATEDRAGIMAGE");
6535 case 0x1200 + 17: return wxT("HDM_GETORDERARRAY");
6536 case 0x1200 + 18: return wxT("HDM_SETORDERARRAY");
6537 case 0x1200 + 19: return wxT("HDM_SETHOTDIVIDER");
6538
6539 // tab control
6540 case 0x1300 + 2: return wxT("TCM_GETIMAGELIST");
6541 case 0x1300 + 3: return wxT("TCM_SETIMAGELIST");
6542 case 0x1300 + 4: return wxT("TCM_GETITEMCOUNT");
6543 case 0x1300 + 5: return wxT("TCM_GETITEMA");
6544 case 0x1300 + 60: return wxT("TCM_GETITEMW");
6545 case 0x1300 + 6: return wxT("TCM_SETITEMA");
6546 case 0x1300 + 61: return wxT("TCM_SETITEMW");
6547 case 0x1300 + 7: return wxT("TCM_INSERTITEMA");
6548 case 0x1300 + 62: return wxT("TCM_INSERTITEMW");
6549 case 0x1300 + 8: return wxT("TCM_DELETEITEM");
6550 case 0x1300 + 9: return wxT("TCM_DELETEALLITEMS");
6551 case 0x1300 + 10: return wxT("TCM_GETITEMRECT");
6552 case 0x1300 + 11: return wxT("TCM_GETCURSEL");
6553 case 0x1300 + 12: return wxT("TCM_SETCURSEL");
6554 case 0x1300 + 13: return wxT("TCM_HITTEST");
6555 case 0x1300 + 14: return wxT("TCM_SETITEMEXTRA");
6556 case 0x1300 + 40: return wxT("TCM_ADJUSTRECT");
6557 case 0x1300 + 41: return wxT("TCM_SETITEMSIZE");
6558 case 0x1300 + 42: return wxT("TCM_REMOVEIMAGE");
6559 case 0x1300 + 43: return wxT("TCM_SETPADDING");
6560 case 0x1300 + 44: return wxT("TCM_GETROWCOUNT");
6561 case 0x1300 + 45: return wxT("TCM_GETTOOLTIPS");
6562 case 0x1300 + 46: return wxT("TCM_SETTOOLTIPS");
6563 case 0x1300 + 47: return wxT("TCM_GETCURFOCUS");
6564 case 0x1300 + 48: return wxT("TCM_SETCURFOCUS");
6565 case 0x1300 + 49: return wxT("TCM_SETMINTABWIDTH");
6566 case 0x1300 + 50: return wxT("TCM_DESELECTALL");
6567
6568 // toolbar
6569 case WM_USER+1: return wxT("TB_ENABLEBUTTON");
6570 case WM_USER+2: return wxT("TB_CHECKBUTTON");
6571 case WM_USER+3: return wxT("TB_PRESSBUTTON");
6572 case WM_USER+4: return wxT("TB_HIDEBUTTON");
6573 case WM_USER+5: return wxT("TB_INDETERMINATE");
6574 case WM_USER+9: return wxT("TB_ISBUTTONENABLED");
6575 case WM_USER+10: return wxT("TB_ISBUTTONCHECKED");
6576 case WM_USER+11: return wxT("TB_ISBUTTONPRESSED");
6577 case WM_USER+12: return wxT("TB_ISBUTTONHIDDEN");
6578 case WM_USER+13: return wxT("TB_ISBUTTONINDETERMINATE");
6579 case WM_USER+17: return wxT("TB_SETSTATE");
6580 case WM_USER+18: return wxT("TB_GETSTATE");
6581 case WM_USER+19: return wxT("TB_ADDBITMAP");
6582 case WM_USER+20: return wxT("TB_ADDBUTTONS");
6583 case WM_USER+21: return wxT("TB_INSERTBUTTON");
6584 case WM_USER+22: return wxT("TB_DELETEBUTTON");
6585 case WM_USER+23: return wxT("TB_GETBUTTON");
6586 case WM_USER+24: return wxT("TB_BUTTONCOUNT");
6587 case WM_USER+25: return wxT("TB_COMMANDTOINDEX");
6588 case WM_USER+26: return wxT("TB_SAVERESTOREA");
6589 case WM_USER+76: return wxT("TB_SAVERESTOREW");
6590 case WM_USER+27: return wxT("TB_CUSTOMIZE");
6591 case WM_USER+28: return wxT("TB_ADDSTRINGA");
6592 case WM_USER+77: return wxT("TB_ADDSTRINGW");
6593 case WM_USER+29: return wxT("TB_GETITEMRECT");
6594 case WM_USER+30: return wxT("TB_BUTTONSTRUCTSIZE");
6595 case WM_USER+31: return wxT("TB_SETBUTTONSIZE");
6596 case WM_USER+32: return wxT("TB_SETBITMAPSIZE");
6597 case WM_USER+33: return wxT("TB_AUTOSIZE");
6598 case WM_USER+35: return wxT("TB_GETTOOLTIPS");
6599 case WM_USER+36: return wxT("TB_SETTOOLTIPS");
6600 case WM_USER+37: return wxT("TB_SETPARENT");
6601 case WM_USER+39: return wxT("TB_SETROWS");
6602 case WM_USER+40: return wxT("TB_GETROWS");
6603 case WM_USER+42: return wxT("TB_SETCMDID");
6604 case WM_USER+43: return wxT("TB_CHANGEBITMAP");
6605 case WM_USER+44: return wxT("TB_GETBITMAP");
6606 case WM_USER+45: return wxT("TB_GETBUTTONTEXTA");
6607 case WM_USER+75: return wxT("TB_GETBUTTONTEXTW");
6608 case WM_USER+46: return wxT("TB_REPLACEBITMAP");
6609 case WM_USER+47: return wxT("TB_SETINDENT");
6610 case WM_USER+48: return wxT("TB_SETIMAGELIST");
6611 case WM_USER+49: return wxT("TB_GETIMAGELIST");
6612 case WM_USER+50: return wxT("TB_LOADIMAGES");
6613 case WM_USER+51: return wxT("TB_GETRECT");
6614 case WM_USER+52: return wxT("TB_SETHOTIMAGELIST");
6615 case WM_USER+53: return wxT("TB_GETHOTIMAGELIST");
6616 case WM_USER+54: return wxT("TB_SETDISABLEDIMAGELIST");
6617 case WM_USER+55: return wxT("TB_GETDISABLEDIMAGELIST");
6618 case WM_USER+56: return wxT("TB_SETSTYLE");
6619 case WM_USER+57: return wxT("TB_GETSTYLE");
6620 case WM_USER+58: return wxT("TB_GETBUTTONSIZE");
6621 case WM_USER+59: return wxT("TB_SETBUTTONWIDTH");
6622 case WM_USER+60: return wxT("TB_SETMAXTEXTROWS");
6623 case WM_USER+61: return wxT("TB_GETTEXTROWS");
6624 case WM_USER+41: return wxT("TB_GETBITMAPFLAGS");
6625
6626 default:
6627 static wxString s_szBuf;
6628 s_szBuf.Printf(wxT("<unknown message = %d>"), message);
6629 return s_szBuf.c_str();
6630 }
6631 }
6632 #endif //__WXDEBUG__
6633
6634 static TEXTMETRIC wxGetTextMetrics(const wxWindowMSW *win)
6635 {
6636 // prepare the DC
6637 TEXTMETRIC tm;
6638 HWND hwnd = GetHwndOf(win);
6639 HDC hdc = ::GetDC(hwnd);
6640
6641 #if !wxDIALOG_UNIT_COMPATIBILITY
6642 // and select the current font into it
6643 HFONT hfont = GetHfontOf(win->GetFont());
6644 if ( hfont )
6645 {
6646 hfont = (HFONT)::SelectObject(hdc, hfont);
6647 }
6648 #endif
6649
6650 // finally retrieve the text metrics from it
6651 GetTextMetrics(hdc, &tm);
6652
6653 #if !wxDIALOG_UNIT_COMPATIBILITY
6654 // and clean up
6655 if ( hfont )
6656 {
6657 (void)::SelectObject(hdc, hfont);
6658 }
6659 #endif
6660
6661 ::ReleaseDC(hwnd, hdc);
6662
6663 return tm;
6664 }
6665
6666 // Find the wxWindow at the current mouse position, returning the mouse
6667 // position.
6668 wxWindow* wxFindWindowAtPointer(wxPoint& pt)
6669 {
6670 pt = wxGetMousePosition();
6671 return wxFindWindowAtPoint(pt);
6672 }
6673
6674 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
6675 {
6676 POINT pt2;
6677 pt2.x = pt.x;
6678 pt2.y = pt.y;
6679
6680 HWND hWnd = ::WindowFromPoint(pt2);
6681
6682 return wxGetWindowFromHWND((WXHWND)hWnd);
6683 }
6684
6685 // Get the current mouse position.
6686 wxPoint wxGetMousePosition()
6687 {
6688 POINT pt;
6689 #ifdef __WXWINCE__
6690 GetCursorPosWinCE(&pt);
6691 #else
6692 GetCursorPos( & pt );
6693 #endif
6694
6695 return wxPoint(pt.x, pt.y);
6696 }
6697
6698 #if wxUSE_HOTKEY
6699
6700 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
6701 static void WinCEUnregisterHotKey(int modifiers, int id)
6702 {
6703 // Register hotkeys for the hardware buttons
6704 HINSTANCE hCoreDll;
6705 typedef BOOL (WINAPI *UnregisterFunc1Proc)(UINT, UINT);
6706
6707 UnregisterFunc1Proc procUnregisterFunc;
6708 hCoreDll = LoadLibrary(_T("coredll.dll"));
6709 if (hCoreDll)
6710 {
6711 procUnregisterFunc = (UnregisterFunc1Proc)GetProcAddress(hCoreDll, _T("UnregisterFunc1"));
6712 if (procUnregisterFunc)
6713 procUnregisterFunc(modifiers, id);
6714 FreeLibrary(hCoreDll);
6715 }
6716 }
6717 #endif
6718
6719 bool wxWindowMSW::RegisterHotKey(int hotkeyId, int modifiers, int keycode)
6720 {
6721 UINT win_modifiers=0;
6722 if ( modifiers & wxMOD_ALT )
6723 win_modifiers |= MOD_ALT;
6724 if ( modifiers & wxMOD_SHIFT )
6725 win_modifiers |= MOD_SHIFT;
6726 if ( modifiers & wxMOD_CONTROL )
6727 win_modifiers |= MOD_CONTROL;
6728 if ( modifiers & wxMOD_WIN )
6729 win_modifiers |= MOD_WIN;
6730
6731 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
6732 // Required for PPC and Smartphone hardware buttons
6733 if (keycode >= WXK_SPECIAL1 && keycode <= WXK_SPECIAL20)
6734 WinCEUnregisterHotKey(win_modifiers, hotkeyId);
6735 #endif
6736
6737 if ( !::RegisterHotKey(GetHwnd(), hotkeyId, win_modifiers, keycode) )
6738 {
6739 wxLogLastError(_T("RegisterHotKey"));
6740
6741 return false;
6742 }
6743
6744 return true;
6745 }
6746
6747 bool wxWindowMSW::UnregisterHotKey(int hotkeyId)
6748 {
6749 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
6750 WinCEUnregisterHotKey(MOD_WIN, hotkeyId);
6751 #endif
6752
6753 if ( !::UnregisterHotKey(GetHwnd(), hotkeyId) )
6754 {
6755 wxLogLastError(_T("UnregisterHotKey"));
6756
6757 return false;
6758 }
6759
6760 return true;
6761 }
6762
6763 #if wxUSE_ACCEL
6764
6765 bool wxWindowMSW::HandleHotKey(WXWPARAM wParam, WXLPARAM lParam)
6766 {
6767 int hotkeyId = wParam;
6768 int virtualKey = HIWORD(lParam);
6769 int win_modifiers = LOWORD(lParam);
6770
6771 wxKeyEvent event(CreateKeyEvent(wxEVT_HOTKEY, virtualKey, wParam, lParam));
6772 event.SetId(hotkeyId);
6773 event.m_shiftDown = (win_modifiers & MOD_SHIFT) != 0;
6774 event.m_controlDown = (win_modifiers & MOD_CONTROL) != 0;
6775 event.m_altDown = (win_modifiers & MOD_ALT) != 0;
6776 event.m_metaDown = (win_modifiers & MOD_WIN) != 0;
6777
6778 return GetEventHandler()->ProcessEvent(event);
6779 }
6780
6781 #endif // wxUSE_ACCEL
6782
6783 #endif // wxUSE_HOTKEY
6784
6785 // Not tested under WinCE
6786 #ifndef __WXWINCE__
6787
6788 // this class installs a message hook which really wakes up our idle processing
6789 // each time a WM_NULL is received (wxWakeUpIdle does this), even if we're
6790 // sitting inside a local modal loop (e.g. a menu is opened or scrollbar is
6791 // being dragged or even inside ::MessageBox()) and so don't control message
6792 // dispatching otherwise
6793 class wxIdleWakeUpModule : public wxModule
6794 {
6795 public:
6796 virtual bool OnInit()
6797 {
6798 ms_hMsgHookProc = ::SetWindowsHookEx
6799 (
6800 WH_GETMESSAGE,
6801 &wxIdleWakeUpModule::MsgHookProc,
6802 NULL,
6803 GetCurrentThreadId()
6804 );
6805
6806 if ( !ms_hMsgHookProc )
6807 {
6808 wxLogLastError(_T("SetWindowsHookEx(WH_GETMESSAGE)"));
6809
6810 return false;
6811 }
6812
6813 return true;
6814 }
6815
6816 virtual void OnExit()
6817 {
6818 ::UnhookWindowsHookEx(wxIdleWakeUpModule::ms_hMsgHookProc);
6819 }
6820
6821 static LRESULT CALLBACK MsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
6822 {
6823 MSG *msg = (MSG*)lParam;
6824
6825 // only process the message if it is actually going to be removed from
6826 // the message queue, this prevents that the same event from being
6827 // processed multiple times if now someone just called PeekMessage()
6828 if ( msg->message == WM_NULL && wParam == PM_REMOVE )
6829 {
6830 wxTheApp->ProcessPendingEvents();
6831 }
6832
6833 return CallNextHookEx(ms_hMsgHookProc, nCode, wParam, lParam);
6834 }
6835
6836 private:
6837 static HHOOK ms_hMsgHookProc;
6838
6839 DECLARE_DYNAMIC_CLASS(wxIdleWakeUpModule)
6840 };
6841
6842 HHOOK wxIdleWakeUpModule::ms_hMsgHookProc = 0;
6843
6844 IMPLEMENT_DYNAMIC_CLASS(wxIdleWakeUpModule, wxModule)
6845
6846 #endif // __WXWINCE__
6847
6848 #ifdef __WXWINCE__
6849
6850 #if wxUSE_STATBOX
6851 static void wxAdjustZOrder(wxWindow* parent)
6852 {
6853 if (parent->IsKindOf(CLASSINFO(wxStaticBox)))
6854 {
6855 // Set the z-order correctly
6856 SetWindowPos((HWND) parent->GetHWND(), HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
6857 }
6858
6859 wxWindowList::compatibility_iterator current = parent->GetChildren().GetFirst();
6860 while (current)
6861 {
6862 wxWindow *childWin = current->GetData();
6863 wxAdjustZOrder(childWin);
6864 current = current->GetNext();
6865 }
6866 }
6867 #endif
6868
6869 // We need to adjust the z-order of static boxes in WinCE, to
6870 // make 'contained' controls visible
6871 void wxWindowMSW::OnInitDialog( wxInitDialogEvent& event )
6872 {
6873 #if wxUSE_STATBOX
6874 wxAdjustZOrder(this);
6875 #endif
6876
6877 event.Skip();
6878 }
6879 #endif