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