1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/msw/window.h
3 // Purpose: wxWindow 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 // [at least] some version of Windows send extra mouse move messages after
21 // a mouse click or a key press - to temporarily fix this problem, set the
24 // a better solution should be found later...
25 #define wxUSE_MOUSEEVENT_HACK 0
27 // ---------------------------------------------------------------------------
29 // ---------------------------------------------------------------------------
31 #if WXWIN_COMPATIBILITY_2_4
32 // they're unused by wxWidgets...
40 // ---------------------------------------------------------------------------
41 // wxWindow declaration for MSW
42 // ---------------------------------------------------------------------------
44 class WXDLLEXPORT wxWindowMSW
: public wxWindowBase
46 friend class wxSpinCtrl
;
47 friend class wxSlider
;
48 friend class wxRadioBox
;
49 #if defined __VISUALC__ && __VISUALC__ <= 1200
50 friend class wxWindowMSW
;
53 wxWindowMSW() { Init(); }
55 wxWindowMSW(wxWindow
*parent
,
57 const wxPoint
& pos
= wxDefaultPosition
,
58 const wxSize
& size
= wxDefaultSize
,
60 const wxString
& name
= wxPanelNameStr
)
63 Create(parent
, id
, pos
, size
, style
, name
);
66 virtual ~wxWindowMSW();
68 bool Create(wxWindow
*parent
,
70 const wxPoint
& pos
= wxDefaultPosition
,
71 const wxSize
& size
= wxDefaultSize
,
73 const wxString
& name
= wxPanelNameStr
);
75 // implement base class pure virtuals
76 virtual void SetTitle( const wxString
& title
);
77 virtual wxString
GetTitle() const;
82 virtual bool Show( bool show
= true );
83 virtual bool Enable( bool enable
= true );
85 virtual void SetFocus();
86 virtual void SetFocusFromKbd();
88 virtual bool Reparent(wxWindowBase
*newParent
);
90 virtual void WarpPointer(int x
, int y
);
92 virtual void Refresh( bool eraseBackground
= true,
93 const wxRect
*rect
= (const wxRect
*) NULL
);
94 virtual void Update();
95 virtual void Freeze();
98 virtual void SetWindowStyleFlag( long style
);
99 virtual bool SetCursor( const wxCursor
&cursor
);
100 virtual bool SetFont( const wxFont
&font
);
102 virtual int GetCharHeight() const;
103 virtual int GetCharWidth() const;
104 virtual void GetTextExtent(const wxString
& string
,
106 int *descent
= (int *) NULL
,
107 int *externalLeading
= (int *) NULL
,
108 const wxFont
*theFont
= (const wxFont
*) NULL
)
111 #if wxUSE_MENUS_NATIVE
112 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
113 #endif // wxUSE_MENUS_NATIVE
115 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
116 int range
, bool refresh
= true );
117 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
118 virtual int GetScrollPos( int orient
) const;
119 virtual int GetScrollThumb( int orient
) const;
120 virtual int GetScrollRange( int orient
) const;
121 virtual void ScrollWindow( int dx
, int dy
,
122 const wxRect
* rect
= (wxRect
*) NULL
);
124 virtual bool ScrollLines(int lines
);
125 virtual bool ScrollPages(int pages
);
127 #if wxUSE_DRAG_AND_DROP
128 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
129 #endif // wxUSE_DRAG_AND_DROP
131 // Accept files for dragging
132 virtual void DragAcceptFiles(bool accept
);
134 #if WXWIN_COMPATIBILITY_2_4
135 wxDEPRECATED( bool GetUseCtl3D() const );
136 wxDEPRECATED( bool GetTransparentBackground() const );
137 wxDEPRECATED( void SetTransparent(bool t
= true) );
138 #endif // WXWIN_COMPATIBILITY_2_4
140 #ifndef __WXUNIVERSAL__
141 // Native resource loading (implemented in src/msw/nativdlg.cpp)
142 // FIXME: should they really be all virtual?
143 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
144 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
145 wxWindow
* GetWindowChild1(wxWindowID id
);
146 wxWindow
* GetWindowChild(wxWindowID id
);
147 #endif // __WXUNIVERSAL__
150 // install and deinstall a system wide hotkey
151 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
152 virtual bool UnregisterHotKey(int hotkeyId
);
153 #endif // wxUSE_HOTKEY
155 // window handle stuff
156 // -------------------
158 WXHWND
GetHWND() const { return m_hWnd
; }
159 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
160 virtual WXWidget
GetHandle() const { return GetHWND(); }
162 void AssociateHandle(WXWidget handle
);
163 void DissociateHandle();
165 // does this window have deferred position and/or size?
166 bool IsSizeDeferred() const;
169 // implementation from now on
170 // ==========================
175 void OnPaint(wxPaintEvent
& event
);
176 void OnEraseBackground(wxEraseEvent
& event
);
178 void OnInitDialog(wxInitDialogEvent
& event
);
182 // Windows subclassing
183 void SubclassWin(WXHWND hWnd
);
184 void UnsubclassWin();
186 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
187 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
189 // return true if the window is of a standard (i.e. not wxWidgets') class
191 // to understand why does it work, look at SubclassWin() code and comments
192 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
194 wxWindow
*FindItem(long id
) const;
195 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
197 // MSW only: true if this control is part of the main control
198 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; };
200 // translate wxWidgets style flags for this control into the Windows style
201 // and optional extended style for the corresponding native control
203 // this is the function that should be overridden in the derived classes,
204 // but you will mostly use MSWGetCreateWindowFlags() below
205 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
207 // get the MSW window flags corresponding to wxWidgets ones
209 // the functions returns the flags (WS_XXX) directly and puts the ext
210 // (WS_EX_XXX) flags into the provided pointer if not NULL
211 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
212 { return MSWGetStyle(GetWindowStyle(), exflags
); }
214 // translate wxWidgets coords into Windows ones suitable to be passed to
217 // returns true if non default coords are returned, false otherwise
218 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
221 int& w
, int& h
) const;
223 // get the HWND to be used as parent of this window with CreateWindow()
224 virtual WXHWND
MSWGetParent() const;
226 // creates the window of specified Windows class with given style, extended
227 // style, title and geometry (default values
229 // returns true if the window has been created, false if creation failed
230 bool MSWCreate(const wxChar
*wclass
,
231 const wxChar
*title
= NULL
,
232 const wxPoint
& pos
= wxDefaultPosition
,
233 const wxSize
& size
= wxDefaultSize
,
235 WXDWORD exendedStyle
= 0);
237 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
239 #ifndef __WXUNIVERSAL__
240 // Create an appropriate wxWindow from a HWND
241 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
243 // Make sure the window style reflects the HWND style (roughly)
244 virtual void AdoptAttributesFromHWND();
245 #endif // __WXUNIVERSAL__
247 // Setup background and foreground colours correctly
248 virtual void SetupColours();
250 // ------------------------------------------------------------------------
251 // helpers for message handlers: these perform the same function as the
252 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
253 // the correct parameters
254 // ------------------------------------------------------------------------
256 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
257 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
258 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
259 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
260 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
261 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
262 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
263 WXHDC
*hdc
, WXHWND
*hwnd
);
264 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
265 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
267 // ------------------------------------------------------------------------
268 // internal handlers for MSW messages: all handlers return a boolean value:
269 // true means that the handler processed the event and false that it didn't
270 // ------------------------------------------------------------------------
272 // there are several cases where we have virtual functions for Windows
273 // message processing: this is because these messages often require to be
274 // processed in a different manner in the derived classes. For all other
275 // messages, however, we do *not* have corresponding MSWOnXXX() function
276 // and if the derived class wants to process them, it should override
277 // MSWWindowProc() directly.
279 // scroll event (both horizontal and vertical)
280 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
281 WXWORD pos
, WXHWND control
);
283 // child control notifications
285 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
288 // owner-drawn controls need to process these messages
289 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
290 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
292 // the rest are not virtual
293 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
294 bool HandleInitDialog(WXHWND hWndFocus
);
295 bool HandleDestroy();
298 bool HandlePrintClient(WXHDC hDC
);
299 bool HandleEraseBkgnd(WXHDC hDC
);
301 bool HandleMinimize();
302 bool HandleMaximize();
303 bool HandleSize(int x
, int y
, WXUINT flag
);
304 bool HandleSizing(wxRect
& rect
);
305 bool HandleGetMinMaxInfo(void *mmInfo
);
307 bool HandleShow(bool show
, int status
);
308 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
310 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
312 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
314 bool HandlePaletteChanged(WXHWND hWndPalChange
);
315 bool HandleQueryNewPalette();
316 bool HandleSysColorChange();
317 bool HandleDisplayChange();
318 bool HandleCaptureChanged(WXHWND gainedCapture
);
320 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
321 bool HandleEndSession(bool endSession
, long logOff
);
323 bool HandleSetFocus(WXHWND wnd
);
324 bool HandleKillFocus(WXHWND wnd
);
326 bool HandleDropFiles(WXWPARAM wParam
);
328 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
329 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
330 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
332 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
333 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
334 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
336 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
339 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
342 bool HandleQueryDragIcon(WXHICON
*hIcon
);
344 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
347 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
349 // Calls an appropriate default window procedure
350 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
352 // message processing helpers
354 // return false if the message shouldn't be translated/preprocessed but
355 // dispatched normally
356 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
358 // return true if the message was preprocessed and shouldn't be dispatched
359 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
361 // return true if the message was translated and shouldn't be dispatched
362 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
364 // called when the window is about to be destroyed
365 virtual void MSWDestroyWindow();
368 // this function should return the brush to paint the children controls
369 // background or 0 if this window doesn't impose any particular background
372 // the base class version returns a solid brush if we have a non default
373 // background colour or 0 otherwise
374 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
376 // return the background brush to use for painting the given window by
377 // quering the parent windows via their MSWGetBgBrushForChild() recursively
379 // hWndToPaint is normally NULL meaning this window itself, but it can also
380 // be a child of this window which is used by the static box and could be
381 // potentially useful for other transparent controls
382 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
384 // gives the parent the possibility to draw its children background, e.g.
385 // this is used by wxNotebook to do it using DrawThemeBackground()
387 // return true if background was drawn, false otherwise
388 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
393 // some controls (e.g. wxListBox) need to set the return value themselves
395 // return true to let parent handle it if we don't, false otherwise
396 virtual bool MSWShouldPropagatePrintChild()
402 // Responds to colour changes: passes event on to children.
403 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
405 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
406 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
408 // check if mouse is in the window
409 bool IsMouseInWindow() const;
411 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
412 void GenerateMouseLeave();
414 // virtual function for implementing internal idle
416 virtual void OnInternalIdle();
422 // the old window proc (we subclass all windows)
423 WXFARPROC m_oldWndProc
;
425 // additional (MSW specific) flags
426 bool m_mouseInWindow
:1;
427 bool m_lastKeydownProcessed
:1;
429 // the size of one page for scrolling
433 #if wxUSE_MOUSEEVENT_HACK
434 // the coordinates of the last mouse event and the type of it
437 int m_lastMouseEvent
;
438 #endif // wxUSE_MOUSEEVENT_HACK
440 // implement the base class pure virtuals
441 virtual void DoClientToScreen( int *x
, int *y
) const;
442 virtual void DoScreenToClient( int *x
, int *y
) const;
443 virtual void DoGetPosition( int *x
, int *y
) const;
444 virtual void DoGetSize( int *width
, int *height
) const;
445 virtual void DoGetClientSize( int *width
, int *height
) const;
446 virtual void DoSetSize(int x
, int y
,
447 int width
, int height
,
448 int sizeFlags
= wxSIZE_AUTO
);
449 virtual void DoSetClientSize(int width
, int height
);
451 virtual void DoCaptureMouse();
452 virtual void DoReleaseMouse();
454 // has the window been frozen by Freeze()?
455 bool IsFrozen() const { return m_frozenness
> 0; }
457 // this simply moves/resizes the given HWND which is supposed to be our
458 // sibling (this is useful for controls which are composite at MSW level
459 // and for which DoMoveWindow() is not enough)
461 // returns true if the window move was deferred, false if it was moved
462 // immediately (no error return)
463 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
465 // move the window to the specified location and resize it: this is called
466 // from both DoSetSize() and DoSetClientSize() and would usually just call
467 // ::MoveWindow() except for composite controls which will want to arrange
468 // themselves inside the given rectangle
469 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
472 virtual void DoSetToolTip( wxToolTip
*tip
);
474 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
475 // comctl32.dll in our code -- see the function body for more info)
476 bool HandleTooltipNotify(WXUINT code
,
478 const wxString
& ttip
);
479 #endif // wxUSE_TOOLTIPS
481 // the helper functions used by HandleChar/KeyXXX methods
482 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
483 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
486 // default OnEraseBackground() implementation, return true if we did erase
487 // the background, false otherwise (i.e. the system should erase it)
488 bool DoEraseBackground(WXHDC hDC
);
490 // generate WM_UPDATEUISTATE if it's needed for the OS we're running under
491 void MSWUpdateUIState();
494 // common part of all ctors
497 // the (non-virtual) handlers for the events
498 bool HandleMove(int x
, int y
);
499 bool HandleMoving(wxRect
& rect
);
500 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
503 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
506 // list of disabled children before last call to our Disable()
507 wxWindowList
*m_childrenDisabled
;
509 // number of calls to Freeze() minus number of calls to Thaw()
510 unsigned int m_frozenness
;
512 // current defer window position operation handle (may be NULL)
515 // When deferred positioning is done these hold the pending changes, and
516 // are used for the default values if another size/pos changes is done on
517 // this window before the group of deferred changes is completed.
518 wxPoint m_pendingPosition
;
519 wxSize m_pendingSize
;
521 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
522 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
523 DECLARE_EVENT_TABLE()
526 // ----------------------------------------------------------------------------
528 // ----------------------------------------------------------------------------
530 #if WXWIN_COMPATIBILITY_2_4
532 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
533 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
534 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t
)) { }
536 #endif // WXWIN_COMPATIBILITY_2_4
538 // ---------------------------------------------------------------------------
540 // ---------------------------------------------------------------------------
542 // kbd code translation
543 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
544 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
);
546 // window creation helper class: before creating a new HWND, instantiate an
547 // object of this class on stack - this allows to process the messages sent to
548 // the window even before CreateWindow() returns
549 class wxWindowCreationHook
552 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
553 ~wxWindowCreationHook();
556 // ----------------------------------------------------------------------------
558 // ----------------------------------------------------------------------------
560 // notice that this hash must be defined after wxWindow declaration as it
561 // needs to "see" its dtor and not just forward declaration
564 // pseudo-template HWND <-> wxWindow hash table
565 #if WXWIN_COMPATIBILITY_2_4
566 WX_DECLARE_HASH(wxWindow
, wxWindowList
, wxWinHashTable
);
568 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
571 extern wxWinHashTable
*wxWinHandleHash
;