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