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