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