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