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();
87 virtual void SetWindowStyleFlag( long style
);
88 virtual bool SetCursor( const wxCursor
&cursor
);
89 virtual bool SetFont( const wxFont
&font
);
91 virtual int GetCharHeight() const;
92 virtual int GetCharWidth() const;
93 virtual void GetTextExtent(const wxString
& string
,
95 int *descent
= (int *) NULL
,
96 int *externalLeading
= (int *) NULL
,
97 const wxFont
*theFont
= (const wxFont
*) NULL
)
100 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
101 int range
, bool refresh
= true );
102 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
103 virtual int GetScrollPos( int orient
) const;
104 virtual int GetScrollThumb( int orient
) const;
105 virtual int GetScrollRange( int orient
) const;
106 virtual void ScrollWindow( int dx
, int dy
,
107 const wxRect
* rect
= (wxRect
*) NULL
);
109 virtual bool ScrollLines(int lines
);
110 virtual bool ScrollPages(int pages
);
112 #if wxUSE_DRAG_AND_DROP
113 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
114 #endif // wxUSE_DRAG_AND_DROP
116 // Accept files for dragging
117 virtual void DragAcceptFiles(bool accept
);
119 #if WXWIN_COMPATIBILITY_2_4
120 wxDEPRECATED( bool GetUseCtl3D() const );
121 wxDEPRECATED( bool GetTransparentBackground() const );
122 wxDEPRECATED( void SetTransparent(bool t
= true) );
123 #endif // WXWIN_COMPATIBILITY_2_4
125 #ifndef __WXUNIVERSAL__
126 // Native resource loading (implemented in src/msw/nativdlg.cpp)
127 // FIXME: should they really be all virtual?
128 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
129 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
130 wxWindow
* GetWindowChild1(wxWindowID id
);
131 wxWindow
* GetWindowChild(wxWindowID id
);
132 #endif // __WXUNIVERSAL__
135 // install and deinstall a system wide hotkey
136 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
137 virtual bool UnregisterHotKey(int hotkeyId
);
138 #endif // wxUSE_HOTKEY
141 bool IsContextMenuEnabled() const { return m_contextMenuEnabled
; }
142 void EnableContextMenu(bool enable
= true) { m_contextMenuEnabled
= enable
; }
145 // window handle stuff
146 // -------------------
148 WXHWND
GetHWND() const { return m_hWnd
; }
149 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
150 virtual WXWidget
GetHandle() const { return GetHWND(); }
152 void AssociateHandle(WXWidget handle
);
153 void DissociateHandle();
155 // does this window have deferred position and/or size?
156 bool IsSizeDeferred() const;
159 // implementation from now on
160 // ==========================
165 void OnPaint(wxPaintEvent
& event
);
166 void OnEraseBackground(wxEraseEvent
& event
);
168 void OnInitDialog(wxInitDialogEvent
& event
);
172 // Windows subclassing
173 void SubclassWin(WXHWND hWnd
);
174 void UnsubclassWin();
176 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
177 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
179 // return true if the window is of a standard (i.e. not wxWidgets') class
181 // to understand why does it work, look at SubclassWin() code and comments
182 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
184 wxWindow
*FindItem(long id
) const;
185 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
187 // MSW only: true if this control is part of the main control
188 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; };
190 // translate wxWidgets style flags for this control into the Windows style
191 // and optional extended style for the corresponding native control
193 // this is the function that should be overridden in the derived classes,
194 // but you will mostly use MSWGetCreateWindowFlags() below
195 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
197 // get the MSW window flags corresponding to wxWidgets ones
199 // the functions returns the flags (WS_XXX) directly and puts the ext
200 // (WS_EX_XXX) flags into the provided pointer if not NULL
201 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
202 { return MSWGetStyle(GetWindowStyle(), exflags
); }
204 // translate wxWidgets coords into Windows ones suitable to be passed to
207 // returns true if non default coords are returned, false otherwise
208 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
211 int& w
, int& h
) const;
213 // get the HWND to be used as parent of this window with CreateWindow()
214 virtual WXHWND
MSWGetParent() const;
216 // creates the window of specified Windows class with given style, extended
217 // style, title and geometry (default values
219 // returns true if the window has been created, false if creation failed
220 bool MSWCreate(const wxChar
*wclass
,
221 const wxChar
*title
= NULL
,
222 const wxPoint
& pos
= wxDefaultPosition
,
223 const wxSize
& size
= wxDefaultSize
,
225 WXDWORD exendedStyle
= 0);
227 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
229 #ifndef __WXUNIVERSAL__
230 // Create an appropriate wxWindow from a HWND
231 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
233 // Make sure the window style reflects the HWND style (roughly)
234 virtual void AdoptAttributesFromHWND();
235 #endif // __WXUNIVERSAL__
237 // Setup background and foreground colours correctly
238 virtual void SetupColours();
240 // ------------------------------------------------------------------------
241 // helpers for message handlers: these perform the same function as the
242 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
243 // the correct parameters
244 // ------------------------------------------------------------------------
246 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
247 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
248 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
249 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
250 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
251 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
252 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
253 WXHDC
*hdc
, WXHWND
*hwnd
);
254 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
255 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
257 // ------------------------------------------------------------------------
258 // internal handlers for MSW messages: all handlers return a boolean value:
259 // true means that the handler processed the event and false that it didn't
260 // ------------------------------------------------------------------------
262 // there are several cases where we have virtual functions for Windows
263 // message processing: this is because these messages often require to be
264 // processed in a different manner in the derived classes. For all other
265 // messages, however, we do *not* have corresponding MSWOnXXX() function
266 // and if the derived class wants to process them, it should override
267 // MSWWindowProc() directly.
269 // scroll event (both horizontal and vertical)
270 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
271 WXWORD pos
, WXHWND control
);
273 // child control notifications
274 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
276 // owner-drawn controls need to process these messages
277 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
278 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
280 // the rest are not virtual
281 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
282 bool HandleInitDialog(WXHWND hWndFocus
);
283 bool HandleDestroy();
286 bool HandlePrintClient(WXHDC hDC
);
287 bool HandleEraseBkgnd(WXHDC hDC
);
289 bool HandleMinimize();
290 bool HandleMaximize();
291 bool HandleSize(int x
, int y
, WXUINT flag
);
292 bool HandleSizing(wxRect
& rect
);
293 bool HandleGetMinMaxInfo(void *mmInfo
);
295 bool HandleShow(bool show
, int status
);
296 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
298 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
300 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
302 bool HandlePaletteChanged(WXHWND hWndPalChange
);
303 bool HandleQueryNewPalette();
304 bool HandleSysColorChange();
305 bool HandleDisplayChange();
306 bool HandleCaptureChanged(WXHWND gainedCapture
);
307 virtual bool HandleSettingChange(WXWPARAM wParam
, WXLPARAM lParam
);
309 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
310 bool HandleEndSession(bool endSession
, long logOff
);
312 bool HandleSetFocus(WXHWND wnd
);
313 bool HandleKillFocus(WXHWND wnd
);
315 bool HandleDropFiles(WXWPARAM wParam
);
317 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
318 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
319 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
321 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
322 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
323 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
325 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
328 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
331 bool HandleQueryDragIcon(WXHICON
*hIcon
);
333 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
336 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
338 // Calls an appropriate default window procedure
339 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
341 // message processing helpers
343 // return false if the message shouldn't be translated/preprocessed but
344 // dispatched normally
345 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
347 // return true if the message was preprocessed and shouldn't be dispatched
348 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
350 // return true if the message was translated and shouldn't be dispatched
351 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
353 // called when the window is about to be destroyed
354 virtual void MSWDestroyWindow();
357 // this function should return the brush to paint the children controls
358 // background or 0 if this window doesn't impose any particular background
361 // the base class version returns a solid brush if we have a non default
362 // background colour or 0 otherwise
363 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
365 // return the background brush to use for painting the given window by
366 // quering the parent windows via their MSWGetBgBrushForChild() recursively
368 // hWndToPaint is normally NULL meaning this window itself, but it can also
369 // be a child of this window which is used by the static box and could be
370 // potentially useful for other transparent controls
371 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
373 // gives the parent the possibility to draw its children background, e.g.
374 // this is used by wxNotebook to do it using DrawThemeBackground()
376 // return true if background was drawn, false otherwise
377 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
382 // some controls (e.g. wxListBox) need to set the return value themselves
384 // return true to let parent handle it if we don't, false otherwise
385 virtual bool MSWShouldPropagatePrintChild()
391 // Responds to colour changes: passes event on to children.
392 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
394 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
395 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
397 // check if mouse is in the window
398 bool IsMouseInWindow() const;
400 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
401 void GenerateMouseLeave();
403 // virtual function for implementing internal idle
405 virtual void OnInternalIdle();
409 #if wxUSE_MENUS_NATIVE
410 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
411 #endif // wxUSE_MENUS_NATIVE
416 // the old window proc (we subclass all windows)
417 WXFARPROC m_oldWndProc
;
419 // additional (MSW specific) flags
420 bool m_mouseInWindow
:1;
421 bool m_lastKeydownProcessed
:1;
423 // the size of one page for scrolling
427 // implement the base class pure virtuals
428 virtual void DoClientToScreen( int *x
, int *y
) const;
429 virtual void DoScreenToClient( int *x
, int *y
) const;
430 virtual void DoGetPosition( int *x
, int *y
) const;
431 virtual void DoGetSize( int *width
, int *height
) const;
432 virtual void DoGetClientSize( int *width
, int *height
) const;
433 virtual void DoSetSize(int x
, int y
,
434 int width
, int height
,
435 int sizeFlags
= wxSIZE_AUTO
);
436 virtual void DoSetClientSize(int width
, int height
);
438 virtual void DoCaptureMouse();
439 virtual void DoReleaseMouse();
441 // has the window been frozen by Freeze()?
442 bool IsFrozen() const { return m_frozenness
> 0; }
444 // this simply moves/resizes the given HWND which is supposed to be our
445 // sibling (this is useful for controls which are composite at MSW level
446 // and for which DoMoveWindow() is not enough)
448 // returns true if the window move was deferred, false if it was moved
449 // immediately (no error return)
450 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
452 // move the window to the specified location and resize it: this is called
453 // from both DoSetSize() and DoSetClientSize() and would usually just call
454 // ::MoveWindow() except for composite controls which will want to arrange
455 // themselves inside the given rectangle
456 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
459 virtual void DoSetToolTip( wxToolTip
*tip
);
461 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
462 // comctl32.dll in our code -- see the function body for more info)
463 bool HandleTooltipNotify(WXUINT code
,
465 const wxString
& ttip
);
466 #endif // wxUSE_TOOLTIPS
468 // the helper functions used by HandleChar/KeyXXX methods
469 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
470 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
473 // default OnEraseBackground() implementation, return true if we did erase
474 // the background, false otherwise (i.e. the system should erase it)
475 bool DoEraseBackground(WXHDC hDC
);
477 // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
479 // action should be one of the UIS_XXX constants
480 // state should be one or more of the UISF_XXX constants
481 // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
482 // for state as the system will decide for us what needs to be set
483 void MSWUpdateUIState(int action
, int state
= 0);
486 // common part of all ctors
489 // the (non-virtual) handlers for the events
490 bool HandleMove(int x
, int y
);
491 bool HandleMoving(wxRect
& rect
);
492 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
493 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
495 // list of disabled children before last call to our Disable()
496 wxWindowList
*m_childrenDisabled
;
498 // number of calls to Freeze() minus number of calls to Thaw()
499 unsigned int m_frozenness
;
501 // current defer window position operation handle (may be NULL)
504 // When deferred positioning is done these hold the pending changes, and
505 // are used for the default values if another size/pos changes is done on
506 // this window before the group of deferred changes is completed.
507 wxPoint m_pendingPosition
;
508 wxSize m_pendingSize
;
511 bool m_contextMenuEnabled
;
514 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
515 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
516 DECLARE_EVENT_TABLE()
519 // ----------------------------------------------------------------------------
521 // ----------------------------------------------------------------------------
523 #if WXWIN_COMPATIBILITY_2_4
525 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
526 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
527 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t
)) { }
529 #endif // WXWIN_COMPATIBILITY_2_4
531 // ---------------------------------------------------------------------------
533 // ---------------------------------------------------------------------------
535 // kbd code translation
536 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
537 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
);
539 // window creation helper class: before creating a new HWND, instantiate an
540 // object of this class on stack - this allows to process the messages sent to
541 // the window even before CreateWindow() returns
542 class wxWindowCreationHook
545 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
546 ~wxWindowCreationHook();
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 // notice that this hash must be defined after wxWindow declaration as it
554 // needs to "see" its dtor and not just forward declaration
557 // pseudo-template HWND <-> wxWindow hash table
558 #if WXWIN_COMPATIBILITY_2_4
559 WX_DECLARE_HASH(wxWindow
, wxWindowList
, wxWinHashTable
);
561 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
564 extern wxWinHashTable
*wxWinHandleHash
;
566 #endif // _WX_WINDOW_H_