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;
98 virtual void GetTextExtent(const wxString
& string
,
101 int *externalLeading
= NULL
,
102 const wxFont
*theFont
= (const wxFont
*) NULL
)
105 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
106 int range
, bool refresh
= true );
107 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
108 virtual int GetScrollPos( int orient
) const;
109 virtual int GetScrollThumb( int orient
) const;
110 virtual int GetScrollRange( int orient
) const;
111 virtual void ScrollWindow( int dx
, int dy
,
112 const wxRect
* rect
= NULL
);
114 virtual bool ScrollLines(int lines
);
115 virtual bool ScrollPages(int pages
);
117 virtual void SetLayoutDirection(wxLayoutDirection dir
);
118 virtual wxLayoutDirection
GetLayoutDirection() const;
119 virtual wxCoord
AdjustForLayoutDirection(wxCoord x
,
121 wxCoord widthTotal
) const;
123 #if wxUSE_DRAG_AND_DROP
124 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
125 #endif // wxUSE_DRAG_AND_DROP
127 // Accept files for dragging
128 virtual void DragAcceptFiles(bool accept
);
130 #ifndef __WXUNIVERSAL__
131 // Native resource loading (implemented in src/msw/nativdlg.cpp)
132 // FIXME: should they really be all virtual?
133 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
134 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
135 wxWindow
* GetWindowChild1(wxWindowID id
);
136 wxWindow
* GetWindowChild(wxWindowID id
);
137 #endif // __WXUNIVERSAL__
140 // install and deinstall a system wide hotkey
141 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
142 virtual bool UnregisterHotKey(int hotkeyId
);
143 #endif // wxUSE_HOTKEY
146 bool IsContextMenuEnabled() const { return m_contextMenuEnabled
; }
147 void EnableContextMenu(bool enable
= true) { m_contextMenuEnabled
= enable
; }
150 // window handle stuff
151 // -------------------
153 WXHWND
GetHWND() const { return m_hWnd
; }
154 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
155 virtual WXWidget
GetHandle() const { return GetHWND(); }
157 void AssociateHandle(WXWidget handle
);
158 void DissociateHandle();
160 // does this window have deferred position and/or size?
161 bool IsSizeDeferred() const;
163 // these functions allow to register a global handler for the given Windows
164 // message: it will be called from MSWWindowProc() of any window which gets
165 // this event if it's not processed before (i.e. unlike a hook procedure it
166 // does not override the normal processing)
168 // notice that if you want to process a message for a given window only you
169 // should override its MSWWindowProc() instead
171 // type of the handler: it is called with the message parameters (except
172 // that the window object is passed instead of window handle) and should
173 // return true if it handled the message or false if it should be passed to
175 typedef bool (*MSWMessageHandler
)(wxWindowMSW
*win
,
180 // install a handler, shouldn't be called more than one for the same message
181 static bool MSWRegisterMessageHandler(int msg
, MSWMessageHandler handler
);
183 // unregister a previously registered handler
184 static void MSWUnregisterMessageHandler(int msg
, MSWMessageHandler handler
);
187 // implementation from now on
188 // ==========================
193 void OnPaint(wxPaintEvent
& event
);
194 void OnEraseBackground(wxEraseEvent
& event
);
196 void OnInitDialog(wxInitDialogEvent
& event
);
200 // Windows subclassing
201 void SubclassWin(WXHWND hWnd
);
202 void UnsubclassWin();
204 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
205 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
207 // return true if the window is of a standard (i.e. not wxWidgets') class
209 // to understand why does it work, look at SubclassWin() code and comments
210 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
212 wxWindow
*FindItem(long id
) const;
213 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
215 // MSW only: true if this control is part of the main control
216 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; }
219 // MSW only: true if this window or any of its children have a tooltip
220 virtual bool HasToolTips() const { return GetToolTip() != NULL
; }
221 #endif // wxUSE_TOOLTIPS
223 // translate wxWidgets style flags for this control into the Windows style
224 // and optional extended style for the corresponding native control
226 // this is the function that should be overridden in the derived classes,
227 // but you will mostly use MSWGetCreateWindowFlags() below
228 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
230 // get the MSW window flags corresponding to wxWidgets ones
232 // the functions returns the flags (WS_XXX) directly and puts the ext
233 // (WS_EX_XXX) flags into the provided pointer if not NULL
234 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
235 { return MSWGetStyle(GetWindowStyle(), exflags
); }
237 // update the real underlying window style flags to correspond to the
238 // current wxWindow object style (safe to call even if window isn't fully
240 void MSWUpdateStyle(long flagsOld
, long exflagsOld
);
242 // translate wxWidgets coords into Windows ones suitable to be passed to
245 // returns true if non default coords are returned, false otherwise
246 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
249 int& w
, int& h
) const;
251 // get the HWND to be used as parent of this window with CreateWindow()
252 virtual WXHWND
MSWGetParent() const;
254 // get the Win32 window class name used by all wxWindow objects by default
255 static const wxChar
*MSWGetRegisteredClassName();
257 // creates the window of specified Windows class with given style, extended
258 // style, title and geometry (default values
260 // returns true if the window has been created, false if creation failed
261 bool MSWCreate(const wxChar
*wclass
,
262 const wxChar
*title
= NULL
,
263 const wxPoint
& pos
= wxDefaultPosition
,
264 const wxSize
& size
= wxDefaultSize
,
266 WXDWORD exendedStyle
= 0);
268 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
270 #ifndef __WXUNIVERSAL__
271 // Create an appropriate wxWindow from a HWND
272 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
274 // Make sure the window style reflects the HWND style (roughly)
275 virtual void AdoptAttributesFromHWND();
276 #endif // __WXUNIVERSAL__
278 // Setup background and foreground colours correctly
279 virtual void SetupColours();
281 // ------------------------------------------------------------------------
282 // helpers for message handlers: these perform the same function as the
283 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
284 // the correct parameters
285 // ------------------------------------------------------------------------
287 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
288 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
289 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
290 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
291 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
292 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
293 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
294 WXHDC
*hdc
, WXHWND
*hwnd
);
295 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
296 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
298 // ------------------------------------------------------------------------
299 // internal handlers for MSW messages: all handlers return a boolean value:
300 // true means that the handler processed the event and false that it didn't
301 // ------------------------------------------------------------------------
303 // there are several cases where we have virtual functions for Windows
304 // message processing: this is because these messages often require to be
305 // processed in a different manner in the derived classes. For all other
306 // messages, however, we do *not* have corresponding MSWOnXXX() function
307 // and if the derived class wants to process them, it should override
308 // MSWWindowProc() directly.
310 // scroll event (both horizontal and vertical)
311 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
312 WXWORD pos
, WXHWND control
);
314 // child control notifications
315 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
317 // owner-drawn controls need to process these messages
318 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
319 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
321 // the rest are not virtual
322 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
323 bool HandleInitDialog(WXHWND hWndFocus
);
324 bool HandleDestroy();
327 bool HandlePrintClient(WXHDC hDC
);
328 bool HandleEraseBkgnd(WXHDC hDC
);
330 bool HandleMinimize();
331 bool HandleMaximize();
332 bool HandleSize(int x
, int y
, WXUINT flag
);
333 bool HandleSizing(wxRect
& rect
);
334 bool HandleGetMinMaxInfo(void *mmInfo
);
335 bool HandleEnterSizeMove();
336 bool HandleExitSizeMove();
338 bool HandleShow(bool show
, int status
);
339 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
341 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
343 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
345 bool HandlePaletteChanged(WXHWND hWndPalChange
);
346 bool HandleQueryNewPalette();
347 bool HandleSysColorChange();
348 bool HandleDisplayChange();
349 bool HandleCaptureChanged(WXHWND gainedCapture
);
350 virtual bool HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
);
352 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
353 bool HandleEndSession(bool endSession
, long logOff
);
355 bool HandleSetFocus(WXHWND wnd
);
356 bool HandleKillFocus(WXHWND wnd
);
358 bool HandleDropFiles(WXWPARAM wParam
);
360 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
361 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
362 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
364 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
365 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
366 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
368 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
371 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
373 // Create and process a clipboard event specified by type.
374 bool HandleClipboardEvent( WXUINT nMsg
);
376 bool HandleQueryDragIcon(WXHICON
*hIcon
);
378 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
380 bool HandlePower(WXWPARAM wParam
, WXLPARAM lParam
, bool *vetoed
);
384 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
386 // Calls an appropriate default window procedure
387 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
389 // message processing helpers
391 // return false if the message shouldn't be translated/preprocessed but
392 // dispatched normally
393 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
395 // return true if the message was preprocessed and shouldn't be dispatched
396 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
398 // return true if the message was translated and shouldn't be dispatched
399 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
401 // called when the window is about to be destroyed
402 virtual void MSWDestroyWindow();
405 // this function should return the brush to paint the children controls
406 // background or 0 if this window doesn't impose any particular background
409 // the base class version returns a solid brush if we have a non default
410 // background colour or 0 otherwise
411 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
413 // return the background brush to use for painting the given window by
414 // quering the parent windows via their MSWGetBgBrushForChild() recursively
416 // hWndToPaint is normally NULL meaning this window itself, but it can also
417 // be a child of this window which is used by the static box and could be
418 // potentially useful for other transparent controls
419 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
421 // gives the parent the possibility to draw its children background, e.g.
422 // this is used by wxNotebook to do it using DrawThemeBackground()
424 // return true if background was drawn, false otherwise
425 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
430 // some controls (e.g. wxListBox) need to set the return value themselves
432 // return true to let parent handle it if we don't, false otherwise
433 virtual bool MSWShouldPropagatePrintChild()
438 // common part of Show/HideWithEffect()
439 bool MSWShowWithEffect(bool show
,
443 // Responds to colour changes: passes event on to children.
444 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
446 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
447 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
449 // check if mouse is in the window
450 bool IsMouseInWindow() const;
452 // check if a native double-buffering applies for this window
453 virtual bool IsDoubleBuffered() const;
455 void SetDoubleBuffered(bool on
);
457 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
458 void GenerateMouseLeave();
460 // virtual function for implementing internal idle
462 virtual void OnInternalIdle();
465 // this allows you to implement standard control borders without
466 // repeating the code in different classes that are not derived from
468 virtual wxBorder
GetDefaultBorderForControl() const;
470 // choose the default border for this window
471 virtual wxBorder
GetDefaultBorder() const;
473 // Translate wxBORDER_THEME (and other border styles if necessary to the value
474 // that makes most sense for this Windows environment
475 virtual wxBorder
TranslateBorder(wxBorder border
) const;
477 #if wxUSE_MENUS_NATIVE
478 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
479 #endif // wxUSE_MENUS_NATIVE
484 // the old window proc (we subclass all windows)
485 WXFARPROC m_oldWndProc
;
487 // additional (MSW specific) flags
488 bool m_mouseInWindow
:1;
489 bool m_lastKeydownProcessed
:1;
491 // the size of one page for scrolling
495 // implement the base class pure virtuals
496 virtual void DoClientToScreen( int *x
, int *y
) const;
497 virtual void DoScreenToClient( int *x
, int *y
) const;
498 virtual void DoGetPosition( int *x
, int *y
) const;
499 virtual void DoGetSize( int *width
, int *height
) const;
500 virtual void DoGetClientSize( int *width
, int *height
) const;
501 virtual void DoSetSize(int x
, int y
,
502 int width
, int height
,
503 int sizeFlags
= wxSIZE_AUTO
);
504 virtual void DoSetClientSize(int width
, int height
);
506 virtual void DoCaptureMouse();
507 virtual void DoReleaseMouse();
509 virtual void DoEnable(bool enable
);
511 virtual void DoFreeze();
512 virtual void DoThaw();
514 // this simply moves/resizes the given HWND which is supposed to be our
515 // sibling (this is useful for controls which are composite at MSW level
516 // and for which DoMoveWindow() is not enough)
518 // returns true if the window move was deferred, false if it was moved
519 // immediately (no error return)
520 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
522 // move the window to the specified location and resize it: this is called
523 // from both DoSetSize() and DoSetClientSize() and would usually just call
524 // ::MoveWindow() except for composite controls which will want to arrange
525 // themselves inside the given rectangle
526 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
529 virtual void DoSetToolTip( wxToolTip
*tip
);
531 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
532 // comctl32.dll in our code -- see the function body for more info)
533 bool HandleTooltipNotify(WXUINT code
,
535 const wxString
& ttip
);
536 #endif // wxUSE_TOOLTIPS
538 // the helper functions used by HandleChar/KeyXXX methods
539 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
540 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
543 // default OnEraseBackground() implementation, return true if we did erase
544 // the background, false otherwise (i.e. the system should erase it)
545 bool DoEraseBackground(WXHDC hDC
);
547 // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
549 // action should be one of the UIS_XXX constants
550 // state should be one or more of the UISF_XXX constants
551 // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
552 // for state as the system will decide for us what needs to be set
553 void MSWUpdateUIState(int action
, int state
= 0);
556 // common part of all ctors
559 // the (non-virtual) handlers for the events
560 bool HandleMove(int x
, int y
);
561 bool HandleMoving(wxRect
& rect
);
562 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
563 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
565 #if wxUSE_DEFERRED_SIZING
567 // this function is called after the window was resized to its new size
568 virtual void MSWEndDeferWindowPos()
570 m_pendingPosition
= wxDefaultPosition
;
571 m_pendingSize
= wxDefaultSize
;
574 // current defer window position operation handle (may be NULL)
577 // When deferred positioning is done these hold the pending changes, and
578 // are used for the default values if another size/pos changes is done on
579 // this window before the group of deferred changes is completed.
580 wxPoint m_pendingPosition
;
581 wxSize m_pendingSize
;
582 #endif // wxUSE_DEFERRED_SIZING
586 bool m_contextMenuEnabled
;
589 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
590 wxDECLARE_NO_COPY_CLASS(wxWindowMSW
);
591 DECLARE_EVENT_TABLE()
594 // ----------------------------------------------------------------------------
596 // ----------------------------------------------------------------------------
598 // ---------------------------------------------------------------------------
600 // ---------------------------------------------------------------------------
602 // kbd code translation
603 WXDLLIMPEXP_CORE
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
604 WXDLLIMPEXP_CORE WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
= NULL
);
606 // window creation helper class: before creating a new HWND, instantiate an
607 // object of this class on stack - this allows to process the messages sent to
608 // the window even before CreateWindow() returns
609 class wxWindowCreationHook
612 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
613 ~wxWindowCreationHook();
616 #endif // _WX_WINDOW_H_