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