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 #if WXWIN_COMPATIBILITY_2_4
21 // they're unused by wxWidgets...
29 // ---------------------------------------------------------------------------
30 // wxWindow declaration for MSW
31 // ---------------------------------------------------------------------------
33 class WXDLLEXPORT wxWindowMSW
: public wxWindowBase
35 friend class wxSpinCtrl
;
36 friend class wxSlider
;
37 friend class wxRadioBox
;
38 #if defined __VISUALC__ && __VISUALC__ <= 1200
39 friend class wxWindowMSW
;
42 wxWindowMSW() { Init(); }
44 wxWindowMSW(wxWindow
*parent
,
46 const wxPoint
& pos
= wxDefaultPosition
,
47 const wxSize
& size
= wxDefaultSize
,
49 const wxString
& name
= wxPanelNameStr
)
52 Create(parent
, id
, pos
, size
, style
, name
);
55 virtual ~wxWindowMSW();
57 bool Create(wxWindow
*parent
,
59 const wxPoint
& pos
= wxDefaultPosition
,
60 const wxSize
& size
= wxDefaultSize
,
62 const wxString
& name
= wxPanelNameStr
);
64 // implement base class pure virtuals
65 virtual void SetLabel(const wxString
& label
);
66 virtual wxString
GetLabel() const;
71 virtual bool Show( bool show
= true );
72 virtual bool Enable( bool enable
= true );
74 virtual void SetFocus();
75 virtual void SetFocusFromKbd();
77 virtual bool Reparent(wxWindowBase
*newParent
);
79 virtual void WarpPointer(int x
, int y
);
81 virtual void Refresh( bool eraseBackground
= true,
82 const wxRect
*rect
= (const wxRect
*) NULL
);
83 virtual void Update();
84 virtual void Freeze();
86 virtual bool IsFrozen() const { return m_frozenness
> 0; }
88 virtual void SetWindowStyleFlag(long style
);
89 virtual void SetExtraStyle(long exStyle
);
90 virtual bool SetCursor( const wxCursor
&cursor
);
91 virtual bool SetFont( const wxFont
&font
);
93 virtual int GetCharHeight() const;
94 virtual int GetCharWidth() const;
95 virtual void GetTextExtent(const wxString
& string
,
97 int *descent
= (int *) NULL
,
98 int *externalLeading
= (int *) NULL
,
99 const wxFont
*theFont
= (const wxFont
*) NULL
)
102 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
103 int range
, bool refresh
= true );
104 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
105 virtual int GetScrollPos( int orient
) const;
106 virtual int GetScrollThumb( int orient
) const;
107 virtual int GetScrollRange( int orient
) const;
108 virtual void ScrollWindow( int dx
, int dy
,
109 const wxRect
* rect
= (wxRect
*) NULL
);
111 virtual bool ScrollLines(int lines
);
112 virtual bool ScrollPages(int pages
);
114 virtual void SetLayoutDirection(wxLayoutDirection dir
);
115 virtual wxLayoutDirection
GetLayoutDirection() const;
116 virtual wxCoord
AdjustForLayoutDirection(wxCoord x
,
118 wxCoord widthTotal
) const;
120 #if wxUSE_DRAG_AND_DROP
121 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
122 #endif // wxUSE_DRAG_AND_DROP
124 // Accept files for dragging
125 virtual void DragAcceptFiles(bool accept
);
127 #if WXWIN_COMPATIBILITY_2_4
128 wxDEPRECATED( bool GetUseCtl3D() const );
129 wxDEPRECATED( bool GetTransparentBackground() const );
130 wxDEPRECATED( void SetTransparent(bool t
= true) );
131 #endif // WXWIN_COMPATIBILITY_2_4
133 #ifndef __WXUNIVERSAL__
134 // Native resource loading (implemented in src/msw/nativdlg.cpp)
135 // FIXME: should they really be all virtual?
136 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
137 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
138 wxWindow
* GetWindowChild1(wxWindowID id
);
139 wxWindow
* GetWindowChild(wxWindowID id
);
140 #endif // __WXUNIVERSAL__
143 // install and deinstall a system wide hotkey
144 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
145 virtual bool UnregisterHotKey(int hotkeyId
);
146 #endif // wxUSE_HOTKEY
149 bool IsContextMenuEnabled() const { return m_contextMenuEnabled
; }
150 void EnableContextMenu(bool enable
= true) { m_contextMenuEnabled
= enable
; }
153 // window handle stuff
154 // -------------------
156 WXHWND
GetHWND() const { return m_hWnd
; }
157 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
158 virtual WXWidget
GetHandle() const { return GetHWND(); }
160 void AssociateHandle(WXWidget handle
);
161 void DissociateHandle();
163 // does this window have deferred position and/or size?
164 bool IsSizeDeferred() const;
167 // implementation from now on
168 // ==========================
173 void OnPaint(wxPaintEvent
& event
);
174 void OnEraseBackground(wxEraseEvent
& event
);
176 void OnInitDialog(wxInitDialogEvent
& event
);
180 // Windows subclassing
181 void SubclassWin(WXHWND hWnd
);
182 void UnsubclassWin();
184 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
185 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
187 // return true if the window is of a standard (i.e. not wxWidgets') class
189 // to understand why does it work, look at SubclassWin() code and comments
190 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
192 wxWindow
*FindItem(long id
) const;
193 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
195 // MSW only: true if this control is part of the main control
196 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; };
199 // MSW only: true if this window or any of its children have a tooltip
200 virtual bool HasToolTips() const { return GetToolTip() != NULL
; }
201 #endif // wxUSE_TOOLTIPS
203 // translate wxWidgets style flags for this control into the Windows style
204 // and optional extended style for the corresponding native control
206 // this is the function that should be overridden in the derived classes,
207 // but you will mostly use MSWGetCreateWindowFlags() below
208 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
210 // get the MSW window flags corresponding to wxWidgets ones
212 // the functions returns the flags (WS_XXX) directly and puts the ext
213 // (WS_EX_XXX) flags into the provided pointer if not NULL
214 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
215 { return MSWGetStyle(GetWindowStyle(), exflags
); }
217 // update the real underlying window style flags to correspond to the
218 // current wxWindow object style (safe to call even if window isn't fully
220 void MSWUpdateStyle(long flagsOld
, long exflagsOld
);
222 // translate wxWidgets coords into Windows ones suitable to be passed to
225 // returns true if non default coords are returned, false otherwise
226 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
229 int& w
, int& h
) const;
231 // get the HWND to be used as parent of this window with CreateWindow()
232 virtual WXHWND
MSWGetParent() const;
234 // creates the window of specified Windows class with given style, extended
235 // style, title and geometry (default values
237 // returns true if the window has been created, false if creation failed
238 bool MSWCreate(const wxChar
*wclass
,
239 const wxChar
*title
= NULL
,
240 const wxPoint
& pos
= wxDefaultPosition
,
241 const wxSize
& size
= wxDefaultSize
,
243 WXDWORD exendedStyle
= 0);
245 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
247 #ifndef __WXUNIVERSAL__
248 // Create an appropriate wxWindow from a HWND
249 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
251 // Make sure the window style reflects the HWND style (roughly)
252 virtual void AdoptAttributesFromHWND();
253 #endif // __WXUNIVERSAL__
255 // Setup background and foreground colours correctly
256 virtual void SetupColours();
258 // ------------------------------------------------------------------------
259 // helpers for message handlers: these perform the same function as the
260 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
261 // the correct parameters
262 // ------------------------------------------------------------------------
264 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
265 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
266 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
267 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
268 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
269 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
270 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
271 WXHDC
*hdc
, WXHWND
*hwnd
);
272 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
273 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
275 // ------------------------------------------------------------------------
276 // internal handlers for MSW messages: all handlers return a boolean value:
277 // true means that the handler processed the event and false that it didn't
278 // ------------------------------------------------------------------------
280 // there are several cases where we have virtual functions for Windows
281 // message processing: this is because these messages often require to be
282 // processed in a different manner in the derived classes. For all other
283 // messages, however, we do *not* have corresponding MSWOnXXX() function
284 // and if the derived class wants to process them, it should override
285 // MSWWindowProc() directly.
287 // scroll event (both horizontal and vertical)
288 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
289 WXWORD pos
, WXHWND control
);
291 // child control notifications
292 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
294 // owner-drawn controls need to process these messages
295 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
296 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
298 // the rest are not virtual
299 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
300 bool HandleInitDialog(WXHWND hWndFocus
);
301 bool HandleDestroy();
304 bool HandlePrintClient(WXHDC hDC
);
305 bool HandleEraseBkgnd(WXHDC hDC
);
307 bool HandleMinimize();
308 bool HandleMaximize();
309 bool HandleSize(int x
, int y
, WXUINT flag
);
310 bool HandleSizing(wxRect
& rect
);
311 bool HandleGetMinMaxInfo(void *mmInfo
);
313 bool HandleShow(bool show
, int status
);
314 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
316 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
318 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
320 bool HandlePaletteChanged(WXHWND hWndPalChange
);
321 bool HandleQueryNewPalette();
322 bool HandleSysColorChange();
323 bool HandleDisplayChange();
324 bool HandleCaptureChanged(WXHWND gainedCapture
);
325 virtual bool HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
);
327 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
328 bool HandleEndSession(bool endSession
, long logOff
);
330 bool HandleSetFocus(WXHWND wnd
);
331 bool HandleKillFocus(WXHWND wnd
);
333 bool HandleDropFiles(WXWPARAM wParam
);
335 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
336 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
337 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
339 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
340 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
341 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
343 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
346 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
348 // Create and process a clipboard event specified by type.
349 bool HandleClipboardEvent( WXUINT nMsg
);
351 bool HandleQueryDragIcon(WXHICON
*hIcon
);
353 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
355 bool HandlePower(WXWPARAM wParam
, WXLPARAM lParam
, bool *vetoed
);
359 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
361 // Calls an appropriate default window procedure
362 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
364 // message processing helpers
366 // return false if the message shouldn't be translated/preprocessed but
367 // dispatched normally
368 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
370 // return true if the message was preprocessed and shouldn't be dispatched
371 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
373 // return true if the message was translated and shouldn't be dispatched
374 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
376 // called when the window is about to be destroyed
377 virtual void MSWDestroyWindow();
380 // this function should return the brush to paint the children controls
381 // background or 0 if this window doesn't impose any particular background
384 // the base class version returns a solid brush if we have a non default
385 // background colour or 0 otherwise
386 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
388 // return the background brush to use for painting the given window by
389 // quering the parent windows via their MSWGetBgBrushForChild() recursively
391 // hWndToPaint is normally NULL meaning this window itself, but it can also
392 // be a child of this window which is used by the static box and could be
393 // potentially useful for other transparent controls
394 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
396 // gives the parent the possibility to draw its children background, e.g.
397 // this is used by wxNotebook to do it using DrawThemeBackground()
399 // return true if background was drawn, false otherwise
400 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
405 // some controls (e.g. wxListBox) need to set the return value themselves
407 // return true to let parent handle it if we don't, false otherwise
408 virtual bool MSWShouldPropagatePrintChild()
414 // Responds to colour changes: passes event on to children.
415 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
417 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
418 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
420 // check if mouse is in the window
421 bool IsMouseInWindow() const;
423 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
424 void GenerateMouseLeave();
426 // virtual function for implementing internal idle
428 virtual void OnInternalIdle();
432 #if wxUSE_MENUS_NATIVE
433 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
434 #endif // wxUSE_MENUS_NATIVE
439 // the old window proc (we subclass all windows)
440 WXFARPROC m_oldWndProc
;
442 // additional (MSW specific) flags
443 bool m_mouseInWindow
:1;
444 bool m_lastKeydownProcessed
:1;
446 // the size of one page for scrolling
450 // implement the base class pure virtuals
451 virtual void DoClientToScreen( int *x
, int *y
) const;
452 virtual void DoScreenToClient( int *x
, int *y
) const;
453 virtual void DoGetPosition( int *x
, int *y
) const;
454 virtual void DoGetSize( int *width
, int *height
) const;
455 virtual void DoGetClientSize( int *width
, int *height
) const;
456 virtual void DoSetSize(int x
, int y
,
457 int width
, int height
,
458 int sizeFlags
= wxSIZE_AUTO
);
459 virtual void DoSetClientSize(int width
, int height
);
461 virtual void DoCaptureMouse();
462 virtual void DoReleaseMouse();
464 // this simply moves/resizes the given HWND which is supposed to be our
465 // sibling (this is useful for controls which are composite at MSW level
466 // and for which DoMoveWindow() is not enough)
468 // returns true if the window move was deferred, false if it was moved
469 // immediately (no error return)
470 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
472 // move the window to the specified location and resize it: this is called
473 // from both DoSetSize() and DoSetClientSize() and would usually just call
474 // ::MoveWindow() except for composite controls which will want to arrange
475 // themselves inside the given rectangle
476 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
479 virtual void DoSetToolTip( wxToolTip
*tip
);
481 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
482 // comctl32.dll in our code -- see the function body for more info)
483 bool HandleTooltipNotify(WXUINT code
,
485 const wxString
& ttip
);
486 #endif // wxUSE_TOOLTIPS
488 // the helper functions used by HandleChar/KeyXXX methods
489 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
490 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
493 // default OnEraseBackground() implementation, return true if we did erase
494 // the background, false otherwise (i.e. the system should erase it)
495 bool DoEraseBackground(WXHDC hDC
);
497 // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
499 // action should be one of the UIS_XXX constants
500 // state should be one or more of the UISF_XXX constants
501 // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
502 // for state as the system will decide for us what needs to be set
503 void MSWUpdateUIState(int action
, int state
= 0);
506 // common part of all ctors
509 // the (non-virtual) handlers for the events
510 bool HandleMove(int x
, int y
);
511 bool HandleMoving(wxRect
& rect
);
512 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
513 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
515 // list of disabled children before last call to our Disable()
516 wxWindowList
*m_childrenDisabled
;
518 // number of calls to Freeze() minus number of calls to Thaw()
519 unsigned int m_frozenness
;
521 // current defer window position operation handle (may be NULL)
525 // When deferred positioning is done these hold the pending changes, and
526 // are used for the default values if another size/pos changes is done on
527 // this window before the group of deferred changes is completed.
528 wxPoint m_pendingPosition
;
529 wxSize m_pendingSize
;
533 bool m_contextMenuEnabled
;
536 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
537 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
538 DECLARE_EVENT_TABLE()
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
545 #if WXWIN_COMPATIBILITY_2_4
547 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
548 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
549 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t
)) { }
551 #endif // WXWIN_COMPATIBILITY_2_4
553 // ---------------------------------------------------------------------------
555 // ---------------------------------------------------------------------------
557 // kbd code translation
558 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
559 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
= NULL
);
561 // window creation helper class: before creating a new HWND, instantiate an
562 // object of this class on stack - this allows to process the messages sent to
563 // the window even before CreateWindow() returns
564 class wxWindowCreationHook
567 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
568 ~wxWindowCreationHook();
571 // ----------------------------------------------------------------------------
573 // ----------------------------------------------------------------------------
575 // notice that this hash must be defined after wxWindow declaration as it
576 // needs to "see" its dtor and not just forward declaration
579 // pseudo-template HWND <-> wxWindow hash table
580 #if WXWIN_COMPATIBILITY_2_4
581 WX_DECLARE_HASH(wxWindow
, wxWindowList
, wxWinHashTable
);
583 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
586 extern wxWinHashTable
*wxWinHandleHash
;
588 #endif // _WX_WINDOW_H_