1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/window.h
3 // Purpose: wxWindow class
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin on 13.05.99: complete refont of message handling,
6 // elimination of Default(), ...
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma interface "window.h"
24 // [at least] some version of Windows send extra mouse move messages after
25 // a mouse click or a key press - to temporarily fix this problem, set the
28 // a better solution should be found later...
29 #define wxUSE_MOUSEEVENT_HACK 0
31 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 #if WXWIN_COMPATIBILITY_2_4
36 // they're unused by wxWidgets...
44 // ---------------------------------------------------------------------------
45 // wxWindow declaration for MSW
46 // ---------------------------------------------------------------------------
48 class WXDLLEXPORT wxWindowMSW
: public wxWindowBase
50 friend class wxSpinCtrl
;
51 friend class wxSlider
;
52 friend class wxRadioBox
;
53 friend class wxWindowMSW
;
55 wxWindowMSW() { Init(); }
57 wxWindowMSW(wxWindow
*parent
,
59 const wxPoint
& pos
= wxDefaultPosition
,
60 const wxSize
& size
= wxDefaultSize
,
62 const wxString
& name
= wxPanelNameStr
)
65 Create(parent
, id
, pos
, size
, style
, name
);
68 virtual ~wxWindowMSW();
70 bool Create(wxWindow
*parent
,
72 const wxPoint
& pos
= wxDefaultPosition
,
73 const wxSize
& size
= wxDefaultSize
,
75 const wxString
& name
= wxPanelNameStr
);
77 // implement base class pure virtuals
78 virtual void SetTitle( const wxString
& title
);
79 virtual wxString
GetTitle() const;
84 virtual bool Show( bool show
= true );
85 virtual bool Enable( bool enable
= true );
87 virtual void SetFocus();
88 virtual void SetFocusFromKbd();
90 virtual bool Reparent(wxWindowBase
*newParent
);
92 virtual void WarpPointer(int x
, int y
);
94 virtual void Refresh( bool eraseBackground
= true,
95 const wxRect
*rect
= (const wxRect
*) NULL
);
96 virtual void Update();
97 virtual void Freeze();
100 virtual void SetWindowStyleFlag( long style
);
101 virtual bool SetCursor( const wxCursor
&cursor
);
102 virtual bool SetFont( const wxFont
&font
);
104 virtual int GetCharHeight() const;
105 virtual int GetCharWidth() const;
106 virtual void GetTextExtent(const wxString
& string
,
108 int *descent
= (int *) NULL
,
109 int *externalLeading
= (int *) NULL
,
110 const wxFont
*theFont
= (const wxFont
*) NULL
)
113 #if wxUSE_MENUS_NATIVE
114 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
115 #endif // wxUSE_MENUS_NATIVE
117 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
118 int range
, bool refresh
= true );
119 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
120 virtual int GetScrollPos( int orient
) const;
121 virtual int GetScrollThumb( int orient
) const;
122 virtual int GetScrollRange( int orient
) const;
123 virtual void ScrollWindow( int dx
, int dy
,
124 const wxRect
* rect
= (wxRect
*) NULL
);
126 virtual bool ScrollLines(int lines
);
127 virtual bool ScrollPages(int pages
);
129 #if wxUSE_DRAG_AND_DROP
130 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
131 #endif // wxUSE_DRAG_AND_DROP
133 // Accept files for dragging
134 virtual void DragAcceptFiles(bool accept
);
136 #if WXWIN_COMPATIBILITY_2_4
137 wxDEPRECATED( bool GetUseCtl3D() const );
138 wxDEPRECATED( bool GetTransparentBackground() const );
139 wxDEPRECATED( void SetTransparent(bool t
= true) );
140 #endif // WXWIN_COMPATIBILITY_2_4
142 #ifndef __WXUNIVERSAL__
143 // Native resource loading (implemented in src/msw/nativdlg.cpp)
144 // FIXME: should they really be all virtual?
145 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
146 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
147 wxWindow
* GetWindowChild1(wxWindowID id
);
148 wxWindow
* GetWindowChild(wxWindowID id
);
149 #endif // __WXUNIVERSAL__
152 // install and deinstall a system wide hotkey
153 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
154 virtual bool UnregisterHotKey(int hotkeyId
);
155 #endif // wxUSE_HOTKEY
157 // window handle stuff
158 // -------------------
160 WXHWND
GetHWND() const { return m_hWnd
; }
161 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
162 virtual WXWidget
GetHandle() const { return GetHWND(); }
164 void AssociateHandle(WXWidget handle
);
165 void DissociateHandle();
168 // implementation from now on
169 // ==========================
174 void OnPaint(wxPaintEvent
& event
);
175 void OnEraseBackground(wxEraseEvent
& event
);
177 void OnInitDialog(wxInitDialogEvent
& event
);
181 // Windows subclassing
182 void SubclassWin(WXHWND hWnd
);
183 void UnsubclassWin();
185 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
186 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
188 // return true if the window is of a standard (i.e. not wxWidgets') class
190 // to understand why does it work, look at SubclassWin() code and comments
191 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
193 wxWindow
*FindItem(long id
) const;
194 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
196 // MSW only: true if this control is part of the main control
197 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; };
199 // translate wxWidgets style flags for this control into the Windows style
200 // and optional extended style for the corresponding native control
202 // this is the function that should be overridden in the derived classes,
203 // but you will mostly use MSWGetCreateWindowFlags() below
204 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
206 // get the MSW window flags corresponding to wxWidgets ones
208 // the functions returns the flags (WS_XXX) directly and puts the ext
209 // (WS_EX_XXX) flags into the provided pointer if not NULL
210 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
211 { return MSWGetStyle(GetWindowStyle(), exflags
); }
213 // translate wxWidgets coords into Windows ones suitable to be passed to
216 // returns true if non default coords are returned, false otherwise
217 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
220 int& w
, int& h
) const;
222 // get the HWND to be used as parent of this window with CreateWindow()
223 virtual WXHWND
MSWGetParent() const;
225 // creates the window of specified Windows class with given style, extended
226 // style, title and geometry (default values
228 // returns true if the window has been created, false if creation failed
229 bool MSWCreate(const wxChar
*wclass
,
230 const wxChar
*title
= NULL
,
231 const wxPoint
& pos
= wxDefaultPosition
,
232 const wxSize
& size
= wxDefaultSize
,
234 WXDWORD exendedStyle
= 0);
236 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
238 #ifndef __WXUNIVERSAL__
239 // Create an appropriate wxWindow from a HWND
240 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
242 // Make sure the window style reflects the HWND style (roughly)
243 virtual void AdoptAttributesFromHWND();
244 #endif // __WXUNIVERSAL__
246 // Setup background and foreground colours correctly
247 virtual void SetupColours();
249 // ------------------------------------------------------------------------
250 // helpers for message handlers: these perform the same function as the
251 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
252 // the correct parameters
253 // ------------------------------------------------------------------------
255 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
256 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
257 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
258 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
259 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
260 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
261 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
262 WXHDC
*hdc
, WXHWND
*hwnd
);
263 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
264 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
266 // ------------------------------------------------------------------------
267 // internal handlers for MSW messages: all handlers return a boolean value:
268 // true means that the handler processed the event and false that it didn't
269 // ------------------------------------------------------------------------
271 // there are several cases where we have virtual functions for Windows
272 // message processing: this is because these messages often require to be
273 // processed in a different manner in the derived classes. For all other
274 // messages, however, we do *not* have corresponding MSWOnXXX() function
275 // and if the derived class wants to process them, it should override
276 // MSWWindowProc() directly.
278 // scroll event (both horizontal and vertical)
279 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
280 WXWORD pos
, WXHWND control
);
282 // child control notifications
284 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
287 // owner-drawn controls need to process these messages
288 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
289 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
291 // the rest are not virtual
292 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
293 bool HandleInitDialog(WXHWND hWndFocus
);
294 bool HandleDestroy();
297 bool HandlePrintClient(WXHDC hDC
);
298 bool HandleEraseBkgnd(WXHDC hDC
);
300 bool HandleMinimize();
301 bool HandleMaximize();
302 bool HandleSize(int x
, int y
, WXUINT flag
);
303 bool HandleSizing(wxRect
& rect
);
304 bool HandleGetMinMaxInfo(void *mmInfo
);
306 bool HandleShow(bool show
, int status
);
307 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
309 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
311 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
313 bool HandlePaletteChanged(WXHWND hWndPalChange
);
314 bool HandleQueryNewPalette();
315 bool HandleSysColorChange();
316 bool HandleDisplayChange();
317 bool HandleCaptureChanged(WXHWND gainedCapture
);
319 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
320 bool HandleEndSession(bool endSession
, long logOff
);
322 bool HandleSetFocus(WXHWND wnd
);
323 bool HandleKillFocus(WXHWND wnd
);
325 bool HandleDropFiles(WXWPARAM wParam
);
327 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
328 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
329 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
331 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
332 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
333 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
335 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
338 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
341 bool HandleQueryDragIcon(WXHICON
*hIcon
);
343 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
346 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
348 // Calls an appropriate default window procedure
349 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
351 // message processing helpers
353 // return false if the message shouldn't be translated/preprocessed but
354 // dispatched normally
355 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
357 // return true if the message was preprocessed and shouldn't be dispatched
358 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
360 // return true if the message was translated and shouldn't be dispatched
361 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
363 // called when the window is about to be destroyed
364 virtual void MSWDestroyWindow();
367 // this function should return the brush to paint the children controls
368 // background or 0 if this window doesn't impose any particular background
371 // the base class version returns a solid brush if we have a non default
372 // background colour or 0 otherwise
373 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
375 // return the background brush to use for painting the given window by
376 // quering the parent windows via their MSWGetBgBrushForChild() recursively
378 // hWndToPaint is normally NULL meaning this window itself, but it can also
379 // be a child of this window which is used by the static box and could be
380 // potentially useful for other transparent controls
381 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
383 // gives the parent the possibility to draw its children background, e.g.
384 // this is used by wxNotebook to do it using DrawThemeBackground()
386 // return true if background was drawn, false otherwise
387 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
393 // Responds to colour changes: passes event on to children.
394 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
396 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
397 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
399 // check if mouse is in the window
400 bool IsMouseInWindow() const;
402 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
403 void GenerateMouseLeave();
405 // virtual function for implementing internal idle
407 virtual void OnInternalIdle();
413 // the old window proc (we subclass all windows)
414 WXFARPROC m_oldWndProc
;
416 // additional (MSW specific) flags
417 bool m_mouseInWindow
:1;
418 bool m_lastKeydownProcessed
:1;
420 // the size of one page for scrolling
424 #if wxUSE_MOUSEEVENT_HACK
425 // the coordinates of the last mouse event and the type of it
428 int m_lastMouseEvent
;
429 #endif // wxUSE_MOUSEEVENT_HACK
431 // implement the base class pure virtuals
432 virtual void DoClientToScreen( int *x
, int *y
) const;
433 virtual void DoScreenToClient( int *x
, int *y
) const;
434 virtual void DoGetPosition( int *x
, int *y
) const;
435 virtual void DoGetSize( int *width
, int *height
) const;
436 virtual void DoGetClientSize( int *width
, int *height
) const;
437 virtual void DoSetSize(int x
, int y
,
438 int width
, int height
,
439 int sizeFlags
= wxSIZE_AUTO
);
440 virtual void DoSetClientSize(int width
, int height
);
442 virtual void DoCaptureMouse();
443 virtual void DoReleaseMouse();
445 // has the window been frozen by Freeze()?
446 bool IsFrozen() const { return m_frozenness
> 0; }
448 // move the window to the specified location and resize it: this is called
449 // from both DoSetSize() and DoSetClientSize() and would usually just call
450 // ::MoveWindow() except for composite controls which will want to arrange
451 // themselves inside the given rectangle
452 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
455 virtual void DoSetToolTip( wxToolTip
*tip
);
457 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
458 // comctl32.dll in our code -- see the function body for more info)
459 bool HandleTooltipNotify(WXUINT code
,
461 const wxString
& ttip
);
462 #endif // wxUSE_TOOLTIPS
464 // the helper functions used by HandleChar/KeyXXX methods
465 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
466 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
469 // default OnEraseBackground() implementation, return true if we did erase
470 // the background, false otherwise (i.e. the system should erase it)
471 bool DoEraseBackground(WXHDC hDC
);
474 // common part of all ctors
477 // the (non-virtual) handlers for the events
478 bool HandleMove(int x
, int y
);
479 bool HandleMoving(wxRect
& rect
);
480 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
483 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
486 // list of disabled children before last call to our Disable()
487 wxWindowList
*m_childrenDisabled
;
489 // number of calls to Freeze() minus number of calls to Thaw()
490 unsigned int m_frozenness
;
492 // current defer window position operation handle (may be NULL)
496 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
497 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
498 DECLARE_EVENT_TABLE()
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 #if WXWIN_COMPATIBILITY_2_4
507 inline bool wxWindowMSW
::GetUseCtl3D() const { return false; }
508 inline bool wxWindowMSW
::GetTransparentBackground() const { return false; }
509 inline void wxWindowMSW
::SetTransparent(bool WXUNUSED(t
)) { }
511 #endif // WXWIN_COMPATIBILITY_2_4
513 // ---------------------------------------------------------------------------
515 // ---------------------------------------------------------------------------
517 // kbd code translation
518 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
519 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
);
521 // window creation helper class: before creating a new HWND, instantiate an
522 // object of this class on stack - this allows to process the messages sent to
523 // the window even before CreateWindow() returns
524 class wxWindowCreationHook
527 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
528 ~wxWindowCreationHook();
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 // notice that this hash must be defined after wxWindow declaration as it
536 // needs to "see" its dtor and not just forward declaration
539 // pseudo-template HWND <-> wxWindow hash table
540 #if WXWIN_COMPATIBILITY_2_4
541 WX_DECLARE_HASH(wxWindow
, wxWindowList
, wxWinHashTable
);
543 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
546 extern wxWinHashTable
*wxWinHandleHash
;