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 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // ---------------------------------------------------------------------------
21 // wxWindow declaration for MSW
22 // ---------------------------------------------------------------------------
24 class WXDLLEXPORT wxWindowMSW
: public wxWindowBase
26 friend class wxSpinCtrl
;
27 friend class wxSlider
;
28 friend class wxRadioBox
;
29 #if defined __VISUALC__ && __VISUALC__ <= 1200
30 friend class wxWindowMSW
;
33 wxWindowMSW() { Init(); }
35 wxWindowMSW(wxWindow
*parent
,
37 const wxPoint
& pos
= wxDefaultPosition
,
38 const wxSize
& size
= wxDefaultSize
,
40 const wxString
& name
= wxPanelNameStr
)
43 Create(parent
, id
, pos
, size
, style
, name
);
46 virtual ~wxWindowMSW();
48 bool Create(wxWindow
*parent
,
50 const wxPoint
& pos
= wxDefaultPosition
,
51 const wxSize
& size
= wxDefaultSize
,
53 const wxString
& name
= wxPanelNameStr
);
55 // implement base class pure virtuals
56 virtual void SetLabel(const wxString
& label
);
57 virtual wxString
GetLabel() const;
62 virtual bool Show(bool show
= true);
63 virtual bool ShowWithEffect(wxShowEffect effect
,
65 wxDirection dir
= wxBOTTOM
)
67 return MSWShowWithEffect(true, effect
, timeout
, dir
);
69 virtual bool HideWithEffect(wxShowEffect effect
,
71 wxDirection dir
= wxBOTTOM
)
73 return MSWShowWithEffect(false, effect
, timeout
, dir
);
76 virtual void SetFocus();
77 virtual void SetFocusFromKbd();
79 virtual bool Reparent(wxWindowBase
*newParent
);
81 virtual void WarpPointer(int x
, int y
);
83 virtual void Refresh( bool eraseBackground
= true,
84 const wxRect
*rect
= (const wxRect
*) NULL
);
85 virtual void Update();
86 virtual void Freeze();
88 virtual bool IsFrozen() const { return m_frozenness
> 0; }
90 virtual void SetWindowStyleFlag(long style
);
91 virtual void SetExtraStyle(long exStyle
);
92 virtual bool SetCursor( const wxCursor
&cursor
);
93 virtual bool SetFont( const wxFont
&font
);
95 virtual int GetCharHeight() const;
96 virtual int GetCharWidth() const;
97 virtual void GetTextExtent(const wxString
& string
,
99 int *descent
= (int *) NULL
,
100 int *externalLeading
= (int *) NULL
,
101 const wxFont
*theFont
= (const wxFont
*) NULL
)
104 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
105 int range
, bool refresh
= true );
106 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
107 virtual int GetScrollPos( int orient
) const;
108 virtual int GetScrollThumb( int orient
) const;
109 virtual int GetScrollRange( int orient
) const;
110 virtual void ScrollWindow( int dx
, int dy
,
111 const wxRect
* rect
= (wxRect
*) NULL
);
113 virtual bool ScrollLines(int lines
);
114 virtual bool ScrollPages(int pages
);
116 virtual void SetLayoutDirection(wxLayoutDirection dir
);
117 virtual wxLayoutDirection
GetLayoutDirection() const;
118 virtual wxCoord
AdjustForLayoutDirection(wxCoord x
,
120 wxCoord widthTotal
) const;
122 #if wxUSE_DRAG_AND_DROP
123 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
124 #endif // wxUSE_DRAG_AND_DROP
126 // Accept files for dragging
127 virtual void DragAcceptFiles(bool accept
);
129 #ifndef __WXUNIVERSAL__
130 // Native resource loading (implemented in src/msw/nativdlg.cpp)
131 // FIXME: should they really be all virtual?
132 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
133 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
134 wxWindow
* GetWindowChild1(wxWindowID id
);
135 wxWindow
* GetWindowChild(wxWindowID id
);
136 #endif // __WXUNIVERSAL__
139 // install and deinstall a system wide hotkey
140 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
141 virtual bool UnregisterHotKey(int hotkeyId
);
142 #endif // wxUSE_HOTKEY
145 bool IsContextMenuEnabled() const { return m_contextMenuEnabled
; }
146 void EnableContextMenu(bool enable
= true) { m_contextMenuEnabled
= enable
; }
149 // window handle stuff
150 // -------------------
152 WXHWND
GetHWND() const { return m_hWnd
; }
153 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
154 virtual WXWidget
GetHandle() const { return GetHWND(); }
156 void AssociateHandle(WXWidget handle
);
157 void DissociateHandle();
159 // does this window have deferred position and/or size?
160 bool IsSizeDeferred() const;
162 // these functions allow to register a global handler for the given Windows
163 // message: it will be called from MSWWindowProc() of any window which gets
164 // this event if it's not processed before (i.e. unlike a hook procedure it
165 // does not override the normal processing)
167 // notice that if you want to process a message for a given window only you
168 // should override its MSWWindowProc() instead
170 // type of the handler: it is called with the message parameters (except
171 // that the window object is passed instead of window handle) and should
172 // return true if it handled the message or false if it should be passed to
174 typedef bool (*MSWMessageHandler
)(wxWindowMSW
*win
,
179 // install a handler, shouldn't be called more than one for the same message
180 static bool MSWRegisterMessageHandler(int msg
, MSWMessageHandler handler
);
182 // unregister a previously registered handler
183 static void MSWUnregisterMessageHandler(int msg
, MSWMessageHandler handler
);
186 // implementation from now on
187 // ==========================
192 void OnPaint(wxPaintEvent
& event
);
193 void OnEraseBackground(wxEraseEvent
& event
);
195 void OnInitDialog(wxInitDialogEvent
& event
);
199 // Windows subclassing
200 void SubclassWin(WXHWND hWnd
);
201 void UnsubclassWin();
203 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
204 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
206 // return true if the window is of a standard (i.e. not wxWidgets') class
208 // to understand why does it work, look at SubclassWin() code and comments
209 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
211 wxWindow
*FindItem(long id
) const;
212 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
214 // MSW only: true if this control is part of the main control
215 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; }
218 // MSW only: true if this window or any of its children have a tooltip
219 virtual bool HasToolTips() const { return GetToolTip() != NULL
; }
220 #endif // wxUSE_TOOLTIPS
222 // translate wxWidgets style flags for this control into the Windows style
223 // and optional extended style for the corresponding native control
225 // this is the function that should be overridden in the derived classes,
226 // but you will mostly use MSWGetCreateWindowFlags() below
227 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
229 // get the MSW window flags corresponding to wxWidgets ones
231 // the functions returns the flags (WS_XXX) directly and puts the ext
232 // (WS_EX_XXX) flags into the provided pointer if not NULL
233 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
234 { return MSWGetStyle(GetWindowStyle(), exflags
); }
236 // update the real underlying window style flags to correspond to the
237 // current wxWindow object style (safe to call even if window isn't fully
239 void MSWUpdateStyle(long flagsOld
, long exflagsOld
);
241 // translate wxWidgets coords into Windows ones suitable to be passed to
244 // returns true if non default coords are returned, false otherwise
245 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
248 int& w
, int& h
) const;
250 // get the HWND to be used as parent of this window with CreateWindow()
251 virtual WXHWND
MSWGetParent() const;
253 // creates the window of specified Windows class with given style, extended
254 // style, title and geometry (default values
256 // returns true if the window has been created, false if creation failed
257 bool MSWCreate(const wxChar
*wclass
,
258 const wxChar
*title
= NULL
,
259 const wxPoint
& pos
= wxDefaultPosition
,
260 const wxSize
& size
= wxDefaultSize
,
262 WXDWORD exendedStyle
= 0);
264 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
266 #ifndef __WXUNIVERSAL__
267 // Create an appropriate wxWindow from a HWND
268 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
270 // Make sure the window style reflects the HWND style (roughly)
271 virtual void AdoptAttributesFromHWND();
272 #endif // __WXUNIVERSAL__
274 // Setup background and foreground colours correctly
275 virtual void SetupColours();
277 // ------------------------------------------------------------------------
278 // helpers for message handlers: these perform the same function as the
279 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
280 // the correct parameters
281 // ------------------------------------------------------------------------
283 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
284 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
285 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
286 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
287 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
288 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
289 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
290 WXHDC
*hdc
, WXHWND
*hwnd
);
291 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
292 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
294 // ------------------------------------------------------------------------
295 // internal handlers for MSW messages: all handlers return a boolean value:
296 // true means that the handler processed the event and false that it didn't
297 // ------------------------------------------------------------------------
299 // there are several cases where we have virtual functions for Windows
300 // message processing: this is because these messages often require to be
301 // processed in a different manner in the derived classes. For all other
302 // messages, however, we do *not* have corresponding MSWOnXXX() function
303 // and if the derived class wants to process them, it should override
304 // MSWWindowProc() directly.
306 // scroll event (both horizontal and vertical)
307 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
308 WXWORD pos
, WXHWND control
);
310 // child control notifications
311 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
313 // owner-drawn controls need to process these messages
314 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
315 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
317 // the rest are not virtual
318 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
319 bool HandleInitDialog(WXHWND hWndFocus
);
320 bool HandleDestroy();
323 bool HandlePrintClient(WXHDC hDC
);
324 bool HandleEraseBkgnd(WXHDC hDC
);
326 bool HandleMinimize();
327 bool HandleMaximize();
328 bool HandleSize(int x
, int y
, WXUINT flag
);
329 bool HandleSizing(wxRect
& rect
);
330 bool HandleGetMinMaxInfo(void *mmInfo
);
331 bool HandleEnterSizeMove();
332 bool HandleExitSizeMove();
334 bool HandleShow(bool show
, int status
);
335 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
337 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
339 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
341 bool HandlePaletteChanged(WXHWND hWndPalChange
);
342 bool HandleQueryNewPalette();
343 bool HandleSysColorChange();
344 bool HandleDisplayChange();
345 bool HandleCaptureChanged(WXHWND gainedCapture
);
346 virtual bool HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
);
348 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
349 bool HandleEndSession(bool endSession
, long logOff
);
351 bool HandleSetFocus(WXHWND wnd
);
352 bool HandleKillFocus(WXHWND wnd
);
354 bool HandleDropFiles(WXWPARAM wParam
);
356 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
357 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
358 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
360 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
361 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
362 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
364 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
367 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
369 // Create and process a clipboard event specified by type.
370 bool HandleClipboardEvent( WXUINT nMsg
);
372 bool HandleQueryDragIcon(WXHICON
*hIcon
);
374 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
376 bool HandlePower(WXWPARAM wParam
, WXLPARAM lParam
, bool *vetoed
);
380 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
382 // Calls an appropriate default window procedure
383 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
385 // message processing helpers
387 // return false if the message shouldn't be translated/preprocessed but
388 // dispatched normally
389 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
391 // return true if the message was preprocessed and shouldn't be dispatched
392 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
394 // return true if the message was translated and shouldn't be dispatched
395 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
397 // called when the window is about to be destroyed
398 virtual void MSWDestroyWindow();
401 // this function should return the brush to paint the children controls
402 // background or 0 if this window doesn't impose any particular background
405 // the base class version returns a solid brush if we have a non default
406 // background colour or 0 otherwise
407 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
409 // return the background brush to use for painting the given window by
410 // quering the parent windows via their MSWGetBgBrushForChild() recursively
412 // hWndToPaint is normally NULL meaning this window itself, but it can also
413 // be a child of this window which is used by the static box and could be
414 // potentially useful for other transparent controls
415 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
417 // gives the parent the possibility to draw its children background, e.g.
418 // this is used by wxNotebook to do it using DrawThemeBackground()
420 // return true if background was drawn, false otherwise
421 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
426 // some controls (e.g. wxListBox) need to set the return value themselves
428 // return true to let parent handle it if we don't, false otherwise
429 virtual bool MSWShouldPropagatePrintChild()
434 // common part of Show/HideWithEffect()
435 bool MSWShowWithEffect(bool show
,
440 // Responds to colour changes: passes event on to children.
441 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
443 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
444 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
446 // check if mouse is in the window
447 bool IsMouseInWindow() const;
449 // check if a native double-buffering applies for this window
450 virtual bool IsDoubleBuffered() const;
452 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
453 void GenerateMouseLeave();
455 // virtual function for implementing internal idle
457 virtual void OnInternalIdle();
460 // this allows you to implement standard control borders without
461 // repeating the code in different classes that are not derived from
463 virtual wxBorder
GetDefaultBorderForControl() const;
465 // choose the default border for this window
466 virtual wxBorder
GetDefaultBorder() const;
468 // Translate wxBORDER_THEME (and other border styles if necessary to the value
469 // that makes most sense for this Windows environment
470 virtual wxBorder
TranslateBorder(wxBorder border
) const;
472 #if wxUSE_MENUS_NATIVE
473 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
474 #endif // wxUSE_MENUS_NATIVE
479 // the old window proc (we subclass all windows)
480 WXFARPROC m_oldWndProc
;
482 // additional (MSW specific) flags
483 bool m_mouseInWindow
:1;
484 bool m_lastKeydownProcessed
:1;
486 // the size of one page for scrolling
490 // implement the base class pure virtuals
491 virtual void DoClientToScreen( int *x
, int *y
) const;
492 virtual void DoScreenToClient( int *x
, int *y
) const;
493 virtual void DoGetPosition( int *x
, int *y
) const;
494 virtual void DoGetSize( int *width
, int *height
) const;
495 virtual void DoGetClientSize( int *width
, int *height
) const;
496 virtual void DoSetSize(int x
, int y
,
497 int width
, int height
,
498 int sizeFlags
= wxSIZE_AUTO
);
499 virtual void DoSetClientSize(int width
, int height
);
501 virtual void DoCaptureMouse();
502 virtual void DoReleaseMouse();
504 virtual void DoEnable(bool enable
);
506 // this simply moves/resizes the given HWND which is supposed to be our
507 // sibling (this is useful for controls which are composite at MSW level
508 // and for which DoMoveWindow() is not enough)
510 // returns true if the window move was deferred, false if it was moved
511 // immediately (no error return)
512 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
514 // move the window to the specified location and resize it: this is called
515 // from both DoSetSize() and DoSetClientSize() and would usually just call
516 // ::MoveWindow() except for composite controls which will want to arrange
517 // themselves inside the given rectangle
518 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
521 virtual void DoSetToolTip( wxToolTip
*tip
);
523 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
524 // comctl32.dll in our code -- see the function body for more info)
525 bool HandleTooltipNotify(WXUINT code
,
527 const wxString
& ttip
);
528 #endif // wxUSE_TOOLTIPS
530 // the helper functions used by HandleChar/KeyXXX methods
531 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
532 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
535 // default OnEraseBackground() implementation, return true if we did erase
536 // the background, false otherwise (i.e. the system should erase it)
537 bool DoEraseBackground(WXHDC hDC
);
539 // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
541 // action should be one of the UIS_XXX constants
542 // state should be one or more of the UISF_XXX constants
543 // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
544 // for state as the system will decide for us what needs to be set
545 void MSWUpdateUIState(int action
, int state
= 0);
548 // common part of all ctors
551 // the (non-virtual) handlers for the events
552 bool HandleMove(int x
, int y
);
553 bool HandleMoving(wxRect
& rect
);
554 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
555 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
558 // number of calls to Freeze() minus number of calls to Thaw()
559 unsigned int m_frozenness
;
561 // current defer window position operation handle (may be NULL)
565 // When deferred positioning is done these hold the pending changes, and
566 // are used for the default values if another size/pos changes is done on
567 // this window before the group of deferred changes is completed.
568 wxPoint m_pendingPosition
;
569 wxSize m_pendingSize
;
573 bool m_contextMenuEnabled
;
576 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
577 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
578 DECLARE_EVENT_TABLE()
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 // ---------------------------------------------------------------------------
587 // ---------------------------------------------------------------------------
589 // kbd code translation
590 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
591 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
= NULL
);
593 // window creation helper class: before creating a new HWND, instantiate an
594 // object of this class on stack - this allows to process the messages sent to
595 // the window even before CreateWindow() returns
596 class wxWindowCreationHook
599 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
600 ~wxWindowCreationHook();
603 // ----------------------------------------------------------------------------
605 // ----------------------------------------------------------------------------
607 // notice that this hash must be defined after wxWindow declaration as it
608 // needs to "see" its dtor and not just forward declaration
611 // pseudo-template HWND <-> wxWindow hash table
612 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
614 extern wxWinHashTable
*wxWinHandleHash
;
616 #endif // _WX_WINDOW_H_