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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma interface "window.h"
24 // [at least] some version of Windows send extra mouse move messages after
25 // a mouse click or a key press - to temporarily fix this problem, set the
28 // a better solution should be found later...
29 #define wxUSE_MOUSEEVENT_HACK 0
31 // ---------------------------------------------------------------------------
33 // ---------------------------------------------------------------------------
35 #if WXWIN_COMPATIBILITY_2_4
36 // they're unused by wxWidgets...
44 // ---------------------------------------------------------------------------
45 // wxWindow declaration for MSW
46 // ---------------------------------------------------------------------------
48 class WXDLLEXPORT wxWindowMSW
: public wxWindowBase
50 friend class wxSpinCtrl
;
51 friend class wxSlider
;
52 friend class wxRadioBox
;
53 #if defined __VISUALC__ && __VISUALC__ <= 1200
54 friend class wxWindowMSW
;
57 wxWindowMSW() { Init(); }
59 wxWindowMSW(wxWindow
*parent
,
61 const wxPoint
& pos
= wxDefaultPosition
,
62 const wxSize
& size
= wxDefaultSize
,
64 const wxString
& name
= wxPanelNameStr
)
67 Create(parent
, id
, pos
, size
, style
, name
);
70 virtual ~wxWindowMSW();
72 bool Create(wxWindow
*parent
,
74 const wxPoint
& pos
= wxDefaultPosition
,
75 const wxSize
& size
= wxDefaultSize
,
77 const wxString
& name
= wxPanelNameStr
);
79 // implement base class pure virtuals
80 virtual void SetTitle( const wxString
& title
);
81 virtual wxString
GetTitle() const;
86 virtual bool Show( bool show
= true );
87 virtual bool Enable( bool enable
= true );
89 virtual void SetFocus();
90 virtual void SetFocusFromKbd();
92 virtual bool Reparent(wxWindowBase
*newParent
);
94 virtual void WarpPointer(int x
, int y
);
96 virtual void Refresh( bool eraseBackground
= true,
97 const wxRect
*rect
= (const wxRect
*) NULL
);
98 virtual void Update();
99 virtual void Freeze();
102 virtual void SetWindowStyleFlag( long style
);
103 virtual bool SetCursor( const wxCursor
&cursor
);
104 virtual bool SetFont( const wxFont
&font
);
106 virtual int GetCharHeight() const;
107 virtual int GetCharWidth() const;
108 virtual void GetTextExtent(const wxString
& string
,
110 int *descent
= (int *) NULL
,
111 int *externalLeading
= (int *) NULL
,
112 const wxFont
*theFont
= (const wxFont
*) NULL
)
115 #if wxUSE_MENUS_NATIVE
116 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
117 #endif // wxUSE_MENUS_NATIVE
119 virtual void SetScrollbar( int orient
, int pos
, int thumbVisible
,
120 int range
, bool refresh
= true );
121 virtual void SetScrollPos( int orient
, int pos
, bool refresh
= true );
122 virtual int GetScrollPos( int orient
) const;
123 virtual int GetScrollThumb( int orient
) const;
124 virtual int GetScrollRange( int orient
) const;
125 virtual void ScrollWindow( int dx
, int dy
,
126 const wxRect
* rect
= (wxRect
*) NULL
);
128 virtual bool ScrollLines(int lines
);
129 virtual bool ScrollPages(int pages
);
131 #if wxUSE_DRAG_AND_DROP
132 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
133 #endif // wxUSE_DRAG_AND_DROP
135 // Accept files for dragging
136 virtual void DragAcceptFiles(bool accept
);
138 #if WXWIN_COMPATIBILITY_2_4
139 wxDEPRECATED( bool GetUseCtl3D() const );
140 wxDEPRECATED( bool GetTransparentBackground() const );
141 wxDEPRECATED( void SetTransparent(bool t
= true) );
142 #endif // WXWIN_COMPATIBILITY_2_4
144 #ifndef __WXUNIVERSAL__
145 // Native resource loading (implemented in src/msw/nativdlg.cpp)
146 // FIXME: should they really be all virtual?
147 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
148 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
149 wxWindow
* GetWindowChild1(wxWindowID id
);
150 wxWindow
* GetWindowChild(wxWindowID id
);
151 #endif // __WXUNIVERSAL__
154 // install and deinstall a system wide hotkey
155 virtual bool RegisterHotKey(int hotkeyId
, int modifiers
, int keycode
);
156 virtual bool UnregisterHotKey(int hotkeyId
);
157 #endif // wxUSE_HOTKEY
159 // window handle stuff
160 // -------------------
162 WXHWND
GetHWND() const { return m_hWnd
; }
163 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
164 virtual WXWidget
GetHandle() const { return GetHWND(); }
166 void AssociateHandle(WXWidget handle
);
167 void DissociateHandle();
169 // does this window have deferred position and/or size?
170 bool IsSizeDeferred() const;
173 // implementation from now on
174 // ==========================
179 void OnPaint(wxPaintEvent
& event
);
180 void OnEraseBackground(wxEraseEvent
& event
);
182 void OnInitDialog(wxInitDialogEvent
& event
);
186 // Windows subclassing
187 void SubclassWin(WXHWND hWnd
);
188 void UnsubclassWin();
190 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
191 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
193 // return true if the window is of a standard (i.e. not wxWidgets') class
195 // to understand why does it work, look at SubclassWin() code and comments
196 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
198 wxWindow
*FindItem(long id
) const;
199 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
201 // MSW only: true if this control is part of the main control
202 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; };
204 // translate wxWidgets style flags for this control into the Windows style
205 // and optional extended style for the corresponding native control
207 // this is the function that should be overridden in the derived classes,
208 // but you will mostly use MSWGetCreateWindowFlags() below
209 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
211 // get the MSW window flags corresponding to wxWidgets ones
213 // the functions returns the flags (WS_XXX) directly and puts the ext
214 // (WS_EX_XXX) flags into the provided pointer if not NULL
215 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
216 { return MSWGetStyle(GetWindowStyle(), exflags
); }
218 // translate wxWidgets coords into Windows ones suitable to be passed to
221 // returns true if non default coords are returned, false otherwise
222 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
225 int& w
, int& h
) const;
227 // get the HWND to be used as parent of this window with CreateWindow()
228 virtual WXHWND
MSWGetParent() const;
230 // creates the window of specified Windows class with given style, extended
231 // style, title and geometry (default values
233 // returns true if the window has been created, false if creation failed
234 bool MSWCreate(const wxChar
*wclass
,
235 const wxChar
*title
= NULL
,
236 const wxPoint
& pos
= wxDefaultPosition
,
237 const wxSize
& size
= wxDefaultSize
,
239 WXDWORD exendedStyle
= 0);
241 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
243 #ifndef __WXUNIVERSAL__
244 // Create an appropriate wxWindow from a HWND
245 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
247 // Make sure the window style reflects the HWND style (roughly)
248 virtual void AdoptAttributesFromHWND();
249 #endif // __WXUNIVERSAL__
251 // Setup background and foreground colours correctly
252 virtual void SetupColours();
254 // ------------------------------------------------------------------------
255 // helpers for message handlers: these perform the same function as the
256 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
257 // the correct parameters
258 // ------------------------------------------------------------------------
260 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
261 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
262 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
263 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
264 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
265 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
266 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
267 WXHDC
*hdc
, WXHWND
*hwnd
);
268 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
269 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
271 // ------------------------------------------------------------------------
272 // internal handlers for MSW messages: all handlers return a boolean value:
273 // true means that the handler processed the event and false that it didn't
274 // ------------------------------------------------------------------------
276 // there are several cases where we have virtual functions for Windows
277 // message processing: this is because these messages often require to be
278 // processed in a different manner in the derived classes. For all other
279 // messages, however, we do *not* have corresponding MSWOnXXX() function
280 // and if the derived class wants to process them, it should override
281 // MSWWindowProc() directly.
283 // scroll event (both horizontal and vertical)
284 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
285 WXWORD pos
, WXHWND control
);
287 // child control notifications
289 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
292 // owner-drawn controls need to process these messages
293 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
294 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
296 // the rest are not virtual
297 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
298 bool HandleInitDialog(WXHWND hWndFocus
);
299 bool HandleDestroy();
302 bool HandlePrintClient(WXHDC hDC
);
303 bool HandleEraseBkgnd(WXHDC hDC
);
305 bool HandleMinimize();
306 bool HandleMaximize();
307 bool HandleSize(int x
, int y
, WXUINT flag
);
308 bool HandleSizing(wxRect
& rect
);
309 bool HandleGetMinMaxInfo(void *mmInfo
);
311 bool HandleShow(bool show
, int status
);
312 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
314 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
316 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
318 bool HandlePaletteChanged(WXHWND hWndPalChange
);
319 bool HandleQueryNewPalette();
320 bool HandleSysColorChange();
321 bool HandleDisplayChange();
322 bool HandleCaptureChanged(WXHWND gainedCapture
);
324 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
325 bool HandleEndSession(bool endSession
, long logOff
);
327 bool HandleSetFocus(WXHWND wnd
);
328 bool HandleKillFocus(WXHWND wnd
);
330 bool HandleDropFiles(WXWPARAM wParam
);
332 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
333 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
334 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
336 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
337 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
338 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
340 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
343 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
346 bool HandleQueryDragIcon(WXHICON
*hIcon
);
348 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
351 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
353 // Calls an appropriate default window procedure
354 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
356 // message processing helpers
358 // return false if the message shouldn't be translated/preprocessed but
359 // dispatched normally
360 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
362 // return true if the message was preprocessed and shouldn't be dispatched
363 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
365 // return true if the message was translated and shouldn't be dispatched
366 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
368 // called when the window is about to be destroyed
369 virtual void MSWDestroyWindow();
372 // this function should return the brush to paint the children controls
373 // background or 0 if this window doesn't impose any particular background
376 // the base class version returns a solid brush if we have a non default
377 // background colour or 0 otherwise
378 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
380 // return the background brush to use for painting the given window by
381 // quering the parent windows via their MSWGetBgBrushForChild() recursively
383 // hWndToPaint is normally NULL meaning this window itself, but it can also
384 // be a child of this window which is used by the static box and could be
385 // potentially useful for other transparent controls
386 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
388 // gives the parent the possibility to draw its children background, e.g.
389 // this is used by wxNotebook to do it using DrawThemeBackground()
391 // return true if background was drawn, false otherwise
392 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
397 // some controls (e.g. wxListBox) need to set the return value themselves
399 // return true to let parent handle it if we don't, false otherwise
400 virtual bool MSWShouldPropagatePrintChild()
406 // Responds to colour changes: passes event on to children.
407 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
409 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
410 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
412 // check if mouse is in the window
413 bool IsMouseInWindow() const;
415 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
416 void GenerateMouseLeave();
418 // virtual function for implementing internal idle
420 virtual void OnInternalIdle();
426 // the old window proc (we subclass all windows)
427 WXFARPROC m_oldWndProc
;
429 // additional (MSW specific) flags
430 bool m_mouseInWindow
:1;
431 bool m_lastKeydownProcessed
:1;
433 // the size of one page for scrolling
437 #if wxUSE_MOUSEEVENT_HACK
438 // the coordinates of the last mouse event and the type of it
441 int m_lastMouseEvent
;
442 #endif // wxUSE_MOUSEEVENT_HACK
444 // implement the base class pure virtuals
445 virtual void DoClientToScreen( int *x
, int *y
) const;
446 virtual void DoScreenToClient( int *x
, int *y
) const;
447 virtual void DoGetPosition( int *x
, int *y
) const;
448 virtual void DoGetSize( int *width
, int *height
) const;
449 virtual void DoGetClientSize( int *width
, int *height
) const;
450 virtual void DoSetSize(int x
, int y
,
451 int width
, int height
,
452 int sizeFlags
= wxSIZE_AUTO
);
453 virtual void DoSetClientSize(int width
, int height
);
455 virtual void DoCaptureMouse();
456 virtual void DoReleaseMouse();
458 // has the window been frozen by Freeze()?
459 bool IsFrozen() const { return m_frozenness
> 0; }
461 // this simply moves/resizes the given HWND which is supposed to be our
462 // sibling (this is useful for controls which are composite at MSW level
463 // and for which DoMoveWindow() is not enough)
465 // returns true if the window move was deferred, false if it was moved
466 // immediately (no error return)
467 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
469 // move the window to the specified location and resize it: this is called
470 // from both DoSetSize() and DoSetClientSize() and would usually just call
471 // ::MoveWindow() except for composite controls which will want to arrange
472 // themselves inside the given rectangle
473 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
476 virtual void DoSetToolTip( wxToolTip
*tip
);
478 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
479 // comctl32.dll in our code -- see the function body for more info)
480 bool HandleTooltipNotify(WXUINT code
,
482 const wxString
& ttip
);
483 #endif // wxUSE_TOOLTIPS
485 // the helper functions used by HandleChar/KeyXXX methods
486 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
487 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
490 // default OnEraseBackground() implementation, return true if we did erase
491 // the background, false otherwise (i.e. the system should erase it)
492 bool DoEraseBackground(WXHDC hDC
);
494 // generate WM_UPDATEUISTATE if it's needed for the OS we're running under
495 void MSWUpdateUIState();
498 // common part of all ctors
501 // the (non-virtual) handlers for the events
502 bool HandleMove(int x
, int y
);
503 bool HandleMoving(wxRect
& rect
);
504 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
507 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
510 // list of disabled children before last call to our Disable()
511 wxWindowList
*m_childrenDisabled
;
513 // number of calls to Freeze() minus number of calls to Thaw()
514 unsigned int m_frozenness
;
516 // current defer window position operation handle (may be NULL)
519 // When deferred positioning is done these hold the pending changes, and
520 // are used for the default values if another size/pos changes is done on
521 // this window before the group of deferred changes is completed.
522 wxPoint m_pendingPosition
;
523 wxSize m_pendingSize
;
525 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
526 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
527 DECLARE_EVENT_TABLE()
530 // ----------------------------------------------------------------------------
532 // ----------------------------------------------------------------------------
534 #if WXWIN_COMPATIBILITY_2_4
536 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
537 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
538 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t
)) { }
540 #endif // WXWIN_COMPATIBILITY_2_4
542 // ---------------------------------------------------------------------------
544 // ---------------------------------------------------------------------------
546 // kbd code translation
547 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
548 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
);
550 // window creation helper class: before creating a new HWND, instantiate an
551 // object of this class on stack - this allows to process the messages sent to
552 // the window even before CreateWindow() returns
553 class wxWindowCreationHook
556 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
557 ~wxWindowCreationHook();
560 // ----------------------------------------------------------------------------
562 // ----------------------------------------------------------------------------
564 // notice that this hash must be defined after wxWindow declaration as it
565 // needs to "see" its dtor and not just forward declaration
568 // pseudo-template HWND <-> wxWindow hash table
569 #if WXWIN_COMPATIBILITY_2_4
570 WX_DECLARE_HASH(wxWindow
, wxWindowList
, wxWinHashTable
);
572 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
575 extern wxWinHashTable
*wxWinHandleHash
;