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