1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/window.h
3 // Purpose: wxWindowMSW 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 // if this is set to 1, we use deferred window sizing to reduce flicker when
17 // resizing complicated window hierarchies, but this can in theory result in
18 // different behaviour than the old code so we keep the possibility to use it
19 // by setting this to 0 (in the future this should be removed completely)
21 #define wxUSE_DEFERRED_SIZING 0
23 #define wxUSE_DEFERRED_SIZING 1
26 // ---------------------------------------------------------------------------
27 // wxWindow declaration for MSW
28 // ---------------------------------------------------------------------------
30 class WXDLLIMPEXP_CORE wxWindowMSW
: public wxWindowBase
32 friend class wxSpinCtrl
;
33 friend class wxSlider
;
34 friend class wxRadioBox
;
35 #if defined __VISUALC__ && __VISUALC__ <= 1200
36 friend class wxWindowMSW
;
39 wxWindowMSW() { Init(); }
41 wxWindowMSW(wxWindow
*parent
,
43 const wxPoint
& pos
= wxDefaultPosition
,
44 const wxSize
& size
= wxDefaultSize
,
46 const wxString
& name
= wxPanelNameStr
)
49 Create(parent
, id
, pos
, size
, style
, name
);
52 virtual ~wxWindowMSW();
54 bool Create(wxWindow
*parent
,
56 const wxPoint
& pos
= wxDefaultPosition
,
57 const wxSize
& size
= wxDefaultSize
,
59 const wxString
& name
= wxPanelNameStr
);
61 // implement base class pure virtuals
62 virtual void SetLabel(const wxString
& label
);
63 virtual wxString
GetLabel() const;
68 virtual bool Show(bool show
= true);
69 virtual bool ShowWithEffect(wxShowEffect effect
,
72 return MSWShowWithEffect(true, effect
, timeout
);
74 virtual bool HideWithEffect(wxShowEffect effect
,
77 return MSWShowWithEffect(false, effect
, timeout
);
80 virtual void SetFocus();
81 virtual void SetFocusFromKbd();
83 virtual bool Reparent(wxWindowBase
*newParent
);
85 virtual void WarpPointer(int x
, int y
);
87 virtual void Refresh( bool eraseBackground
= true,
88 const wxRect
*rect
= (const wxRect
*) NULL
);
89 virtual void Update();
91 virtual void SetWindowStyleFlag(long style
);
92 virtual void SetExtraStyle(long exStyle
);
93 virtual bool SetCursor( const wxCursor
&cursor
);
94 virtual bool SetFont( const wxFont
&font
);
96 virtual int GetCharHeight() const;
97 virtual int GetCharWidth() const;
99 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
100 int range
, bool refresh
= true );
101 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
102 virtual int GetScrollPos( int orient
) const;
103 virtual int GetScrollThumb( int orient
) const;
104 virtual int GetScrollRange( int orient
) const;
105 virtual void ScrollWindow( int dx
, int dy
,
106 const wxRect
* rect
= NULL
);
108 virtual bool ScrollLines(int lines
);
109 virtual bool ScrollPages(int pages
);
111 virtual void SetLayoutDirection(wxLayoutDirection dir
);
112 virtual wxLayoutDirection
GetLayoutDirection() const;
113 virtual wxCoord
AdjustForLayoutDirection(wxCoord x
,
115 wxCoord widthTotal
) const;
117 #if wxUSE_DRAG_AND_DROP
118 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
119 #endif // wxUSE_DRAG_AND_DROP
121 // Accept files for dragging
122 virtual void DragAcceptFiles(bool accept
);
124 #ifndef __WXUNIVERSAL__
125 // Native resource loading (implemented in src/msw/nativdlg.cpp)
126 // FIXME: should they really be all virtual?
127 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
128 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
129 wxWindow
* GetWindowChild1(wxWindowID id
);
130 wxWindow
* GetWindowChild(wxWindowID id
);
131 #endif // __WXUNIVERSAL__
134 // install and deinstall a system wide hotkey
135 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
136 virtual bool UnregisterHotKey(int hotkeyId
);
137 #endif // wxUSE_HOTKEY
140 bool IsContextMenuEnabled() const { return m_contextMenuEnabled
; }
141 void EnableContextMenu(bool enable
= true) { m_contextMenuEnabled
= enable
; }
144 // window handle stuff
145 // -------------------
147 WXHWND
GetHWND() const { return m_hWnd
; }
148 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
149 virtual WXWidget
GetHandle() const { return GetHWND(); }
151 void AssociateHandle(WXWidget handle
);
152 void DissociateHandle();
154 // does this window have deferred position and/or size?
155 bool IsSizeDeferred() const;
157 // these functions allow to register a global handler for the given Windows
158 // message: it will be called from MSWWindowProc() of any window which gets
159 // this event if it's not processed before (i.e. unlike a hook procedure it
160 // does not override the normal processing)
162 // notice that if you want to process a message for a given window only you
163 // should override its MSWWindowProc() instead
165 // type of the handler: it is called with the message parameters (except
166 // that the window object is passed instead of window handle) and should
167 // return true if it handled the message or false if it should be passed to
169 typedef bool (*MSWMessageHandler
)(wxWindowMSW
*win
,
174 // install a handler, shouldn't be called more than one for the same message
175 static bool MSWRegisterMessageHandler(int msg
, MSWMessageHandler handler
);
177 // unregister a previously registered handler
178 static void MSWUnregisterMessageHandler(int msg
, MSWMessageHandler handler
);
181 // implementation from now on
182 // ==========================
187 void OnPaint(wxPaintEvent
& event
);
188 void OnEraseBackground(wxEraseEvent
& event
);
190 void OnInitDialog(wxInitDialogEvent
& event
);
194 // Windows subclassing
195 void SubclassWin(WXHWND hWnd
);
196 void UnsubclassWin();
198 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
199 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
201 // return true if the window is of a standard (i.e. not wxWidgets') class
203 // to understand why does it work, look at SubclassWin() code and comments
204 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
206 wxWindow
*FindItem(long id
) const;
207 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
209 // MSW only: true if this control is part of the main control
210 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; }
213 // MSW only: true if this window or any of its children have a tooltip
214 virtual bool HasToolTips() const { return GetToolTip() != NULL
; }
215 #endif // wxUSE_TOOLTIPS
217 // translate wxWidgets style flags for this control into the Windows style
218 // and optional extended style for the corresponding native control
220 // this is the function that should be overridden in the derived classes,
221 // but you will mostly use MSWGetCreateWindowFlags() below
222 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
224 // get the MSW window flags corresponding to wxWidgets ones
226 // the functions returns the flags (WS_XXX) directly and puts the ext
227 // (WS_EX_XXX) flags into the provided pointer if not NULL
228 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
229 { return MSWGetStyle(GetWindowStyle(), exflags
); }
231 // update the real underlying window style flags to correspond to the
232 // current wxWindow object style (safe to call even if window isn't fully
234 void MSWUpdateStyle(long flagsOld
, long exflagsOld
);
236 // translate wxWidgets coords into Windows ones suitable to be passed to
239 // returns true if non default coords are returned, false otherwise
240 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
243 int& w
, int& h
) const;
245 // get the HWND to be used as parent of this window with CreateWindow()
246 virtual WXHWND
MSWGetParent() const;
248 // get the Win32 window class name used by all wxWindow objects by default
249 static const wxChar
*MSWGetRegisteredClassName();
251 // creates the window of specified Windows class with given style, extended
252 // style, title and geometry (default values
254 // returns true if the window has been created, false if creation failed
255 bool MSWCreate(const wxChar
*wclass
,
256 const wxChar
*title
= NULL
,
257 const wxPoint
& pos
= wxDefaultPosition
,
258 const wxSize
& size
= wxDefaultSize
,
260 WXDWORD exendedStyle
= 0);
262 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
264 #ifndef __WXUNIVERSAL__
265 // Create an appropriate wxWindow from a HWND
266 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
268 // Make sure the window style reflects the HWND style (roughly)
269 virtual void AdoptAttributesFromHWND();
270 #endif // __WXUNIVERSAL__
272 // Setup background and foreground colours correctly
273 virtual void SetupColours();
275 // ------------------------------------------------------------------------
276 // helpers for message handlers: these perform the same function as the
277 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
278 // the correct parameters
279 // ------------------------------------------------------------------------
281 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
282 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
283 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
284 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
285 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
286 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
287 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
288 WXHDC
*hdc
, WXHWND
*hwnd
);
289 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
290 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
292 // ------------------------------------------------------------------------
293 // internal handlers for MSW messages: all handlers return a boolean value:
294 // true means that the handler processed the event and false that it didn't
295 // ------------------------------------------------------------------------
297 // there are several cases where we have virtual functions for Windows
298 // message processing: this is because these messages often require to be
299 // processed in a different manner in the derived classes. For all other
300 // messages, however, we do *not* have corresponding MSWOnXXX() function
301 // and if the derived class wants to process them, it should override
302 // MSWWindowProc() directly.
304 // scroll event (both horizontal and vertical)
305 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
306 WXWORD pos
, WXHWND control
);
308 // child control notifications
309 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
311 // owner-drawn controls need to process these messages
312 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
313 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
315 // the rest are not virtual
316 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
317 bool HandleInitDialog(WXHWND hWndFocus
);
318 bool HandleDestroy();
321 bool HandlePrintClient(WXHDC hDC
);
322 bool HandleEraseBkgnd(WXHDC hDC
);
324 bool HandleMinimize();
325 bool HandleMaximize();
326 bool HandleSize(int x
, int y
, WXUINT flag
);
327 bool HandleSizing(wxRect
& rect
);
328 bool HandleGetMinMaxInfo(void *mmInfo
);
329 bool HandleEnterSizeMove();
330 bool HandleExitSizeMove();
332 bool HandleShow(bool show
, int status
);
333 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
335 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
337 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
339 bool HandlePaletteChanged(WXHWND hWndPalChange
);
340 bool HandleQueryNewPalette();
341 bool HandleSysColorChange();
342 bool HandleDisplayChange();
343 bool HandleCaptureChanged(WXHWND gainedCapture
);
344 virtual bool HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
);
346 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
347 bool HandleEndSession(bool endSession
, long logOff
);
349 bool HandleSetFocus(WXHWND wnd
);
350 bool HandleKillFocus(WXHWND wnd
);
352 bool HandleDropFiles(WXWPARAM wParam
);
354 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
355 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
356 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
358 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
359 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
360 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
362 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
365 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
367 // Create and process a clipboard event specified by type.
368 bool HandleClipboardEvent( WXUINT nMsg
);
370 bool HandleQueryDragIcon(WXHICON
*hIcon
);
372 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
374 bool HandlePower(WXWPARAM wParam
, WXLPARAM lParam
, bool *vetoed
);
378 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
380 // Calls an appropriate default window procedure
381 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
383 // message processing helpers
385 // return false if the message shouldn't be translated/preprocessed but
386 // dispatched normally
387 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
389 // return true if the message was preprocessed and shouldn't be dispatched
390 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
392 // return true if the message was translated and shouldn't be dispatched
393 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
395 // called when the window is about to be destroyed
396 virtual void MSWDestroyWindow();
399 // this function should return the brush to paint the children controls
400 // background or 0 if this window doesn't impose any particular background
403 // the base class version returns a solid brush if we have a non default
404 // background colour or 0 otherwise
405 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
407 // return the background brush to use for painting the given window by
408 // quering the parent windows via their MSWGetBgBrushForChild() recursively
410 // hWndToPaint is normally NULL meaning this window itself, but it can also
411 // be a child of this window which is used by the static box and could be
412 // potentially useful for other transparent controls
413 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
415 // gives the parent the possibility to draw its children background, e.g.
416 // this is used by wxNotebook to do it using DrawThemeBackground()
418 // return true if background was drawn, false otherwise
419 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
424 // some controls (e.g. wxListBox) need to set the return value themselves
426 // return true to let parent handle it if we don't, false otherwise
427 virtual bool MSWShouldPropagatePrintChild()
432 // common part of Show/HideWithEffect()
433 bool MSWShowWithEffect(bool show
,
437 // Responds to colour changes: passes event on to children.
438 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
440 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
441 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
443 // check if mouse is in the window
444 bool IsMouseInWindow() const;
446 // check if a native double-buffering applies for this window
447 virtual bool IsDoubleBuffered() const;
449 void SetDoubleBuffered(bool on
);
451 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
452 void GenerateMouseLeave();
454 // virtual function for implementing internal idle
456 virtual void OnInternalIdle();
459 // this allows you to implement standard control borders without
460 // repeating the code in different classes that are not derived from
462 virtual wxBorder
GetDefaultBorderForControl() const;
464 // choose the default border for this window
465 virtual wxBorder
GetDefaultBorder() const;
467 // Translate wxBORDER_THEME (and other border styles if necessary to the value
468 // that makes most sense for this Windows environment
469 virtual wxBorder
TranslateBorder(wxBorder border
) const;
471 #if wxUSE_MENUS_NATIVE
472 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
473 #endif // wxUSE_MENUS_NATIVE
478 // the old window proc (we subclass all windows)
479 WXFARPROC m_oldWndProc
;
481 // additional (MSW specific) flags
482 bool m_mouseInWindow
:1;
483 bool m_lastKeydownProcessed
:1;
485 // the size of one page for scrolling
489 // implement the base class pure virtuals
490 virtual void DoGetTextExtent(const wxString
& string
,
493 int *externalLeading
= NULL
,
494 const wxFont
*font
= NULL
) const;
495 virtual void DoClientToScreen( int *x
, int *y
) const;
496 virtual void DoScreenToClient( int *x
, int *y
) const;
497 virtual void DoGetPosition( int *x
, int *y
) const;
498 virtual void DoGetSize( int *width
, int *height
) const;
499 virtual void DoGetClientSize( int *width
, int *height
) const;
500 virtual void DoSetSize(int x
, int y
,
501 int width
, int height
,
502 int sizeFlags
= wxSIZE_AUTO
);
503 virtual void DoSetClientSize(int width
, int height
);
505 virtual void DoCaptureMouse();
506 virtual void DoReleaseMouse();
508 virtual void DoEnable(bool enable
);
510 virtual void DoFreeze();
511 virtual void DoThaw();
513 // this simply moves/resizes the given HWND which is supposed to be our
514 // sibling (this is useful for controls which are composite at MSW level
515 // and for which DoMoveWindow() is not enough)
517 // returns true if the window move was deferred, false if it was moved
518 // immediately (no error return)
519 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
521 // move the window to the specified location and resize it: this is called
522 // from both DoSetSize() and DoSetClientSize() and would usually just call
523 // ::MoveWindow() except for composite controls which will want to arrange
524 // themselves inside the given rectangle
525 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
528 virtual void DoSetToolTip( wxToolTip
*tip
);
530 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
531 // comctl32.dll in our code -- see the function body for more info)
532 bool HandleTooltipNotify(WXUINT code
,
534 const wxString
& ttip
);
535 #endif // wxUSE_TOOLTIPS
537 // the helper functions used by HandleChar/KeyXXX methods
538 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
539 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
542 // default OnEraseBackground() implementation, return true if we did erase
543 // the background, false otherwise (i.e. the system should erase it)
544 bool DoEraseBackground(WXHDC hDC
);
546 // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
548 // action should be one of the UIS_XXX constants
549 // state should be one or more of the UISF_XXX constants
550 // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
551 // for state as the system will decide for us what needs to be set
552 void MSWUpdateUIState(int action
, int state
= 0);
555 // common part of all ctors
558 // the (non-virtual) handlers for the events
559 bool HandleMove(int x
, int y
);
560 bool HandleMoving(wxRect
& rect
);
561 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
562 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
564 #if wxUSE_DEFERRED_SIZING
566 // this function is called after the window was resized to its new size
567 virtual void MSWEndDeferWindowPos()
569 m_pendingPosition
= wxDefaultPosition
;
570 m_pendingSize
= wxDefaultSize
;
573 // current defer window position operation handle (may be NULL)
576 // When deferred positioning is done these hold the pending changes, and
577 // are used for the default values if another size/pos changes is done on
578 // this window before the group of deferred changes is completed.
579 wxPoint m_pendingPosition
;
580 wxSize m_pendingSize
;
581 #endif // wxUSE_DEFERRED_SIZING
585 bool m_contextMenuEnabled
;
588 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
589 wxDECLARE_NO_COPY_CLASS(wxWindowMSW
);
590 DECLARE_EVENT_TABLE()
593 // ----------------------------------------------------------------------------
595 // ----------------------------------------------------------------------------
597 // ---------------------------------------------------------------------------
599 // ---------------------------------------------------------------------------
601 // kbd code translation
602 WXDLLIMPEXP_CORE
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
603 WXDLLIMPEXP_CORE WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
= NULL
);
605 // window creation helper class: before creating a new HWND, instantiate an
606 // object of this class on stack - this allows to process the messages sent to
607 // the window even before CreateWindow() returns
608 class wxWindowCreationHook
611 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
612 ~wxWindowCreationHook();
615 #endif // _WX_WINDOW_H_