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 #if wxUSE_MENUS_NATIVE
101 virtual bool DoPopupMenu( wxMenu
*menu
, int x
, int y
);
102 #endif // wxUSE_MENUS_NATIVE
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 #if wxUSE_DRAG_AND_DROP
117 virtual void SetDropTarget( wxDropTarget
*dropTarget
);
118 #endif // wxUSE_DRAG_AND_DROP
120 // Accept files for dragging
121 virtual void DragAcceptFiles(bool accept
);
123 #if WXWIN_COMPATIBILITY_2_4
124 wxDEPRECATED( bool GetUseCtl3D() const );
125 wxDEPRECATED( bool GetTransparentBackground() const );
126 wxDEPRECATED( void SetTransparent(bool t
= true) );
127 #endif // WXWIN_COMPATIBILITY_2_4
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
144 // window handle stuff
145 // -------------------
147 WXHWND
GetHWND() const { return m_hWnd
; }
148 void SetHWND(WXHWND hWnd
) { m_hWnd
= hWnd
; }
149 virtual WXWidget
GetHandle() const { return GetHWND(); }
151 void AssociateHandle(WXWidget handle
);
152 void DissociateHandle();
154 // does this window have deferred position and/or size?
155 bool IsSizeDeferred() const;
158 // implementation from now on
159 // ==========================
164 void OnPaint(wxPaintEvent
& event
);
165 void OnEraseBackground(wxEraseEvent
& event
);
167 void OnInitDialog(wxInitDialogEvent
& event
);
171 // Windows subclassing
172 void SubclassWin(WXHWND hWnd
);
173 void UnsubclassWin();
175 WXFARPROC
MSWGetOldWndProc() const { return m_oldWndProc
; }
176 void MSWSetOldWndProc(WXFARPROC proc
) { m_oldWndProc
= proc
; }
178 // return true if the window is of a standard (i.e. not wxWidgets') class
180 // to understand why does it work, look at SubclassWin() code and comments
181 bool IsOfStandardClass() const { return m_oldWndProc
!= NULL
; }
183 wxWindow
*FindItem(long id
) const;
184 wxWindow
*FindItemByHWND(WXHWND hWnd
, bool controlOnly
= false) const;
186 // MSW only: true if this control is part of the main control
187 virtual bool ContainsHWND(WXHWND
WXUNUSED(hWnd
)) const { return false; };
189 // translate wxWidgets style flags for this control into the Windows style
190 // and optional extended style for the corresponding native control
192 // this is the function that should be overridden in the derived classes,
193 // but you will mostly use MSWGetCreateWindowFlags() below
194 virtual WXDWORD
MSWGetStyle(long flags
, WXDWORD
*exstyle
= NULL
) const ;
196 // get the MSW window flags corresponding to wxWidgets ones
198 // the functions returns the flags (WS_XXX) directly and puts the ext
199 // (WS_EX_XXX) flags into the provided pointer if not NULL
200 WXDWORD
MSWGetCreateWindowFlags(WXDWORD
*exflags
= NULL
) const
201 { return MSWGetStyle(GetWindowStyle(), exflags
); }
203 // translate wxWidgets coords into Windows ones suitable to be passed to
206 // returns true if non default coords are returned, false otherwise
207 bool MSWGetCreateWindowCoords(const wxPoint
& pos
,
210 int& w
, int& h
) const;
212 // get the HWND to be used as parent of this window with CreateWindow()
213 virtual WXHWND
MSWGetParent() const;
215 // creates the window of specified Windows class with given style, extended
216 // style, title and geometry (default values
218 // returns true if the window has been created, false if creation failed
219 bool MSWCreate(const wxChar
*wclass
,
220 const wxChar
*title
= NULL
,
221 const wxPoint
& pos
= wxDefaultPosition
,
222 const wxSize
& size
= wxDefaultSize
,
224 WXDWORD exendedStyle
= 0);
226 virtual bool MSWCommand(WXUINT param
, WXWORD id
);
228 #ifndef __WXUNIVERSAL__
229 // Create an appropriate wxWindow from a HWND
230 virtual wxWindow
* CreateWindowFromHWND(wxWindow
* parent
, WXHWND hWnd
);
232 // Make sure the window style reflects the HWND style (roughly)
233 virtual void AdoptAttributesFromHWND();
234 #endif // __WXUNIVERSAL__
236 // Setup background and foreground colours correctly
237 virtual void SetupColours();
239 // ------------------------------------------------------------------------
240 // helpers for message handlers: these perform the same function as the
241 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
242 // the correct parameters
243 // ------------------------------------------------------------------------
245 void UnpackCommand(WXWPARAM wParam
, WXLPARAM lParam
,
246 WXWORD
*id
, WXHWND
*hwnd
, WXWORD
*cmd
);
247 void UnpackActivate(WXWPARAM wParam
, WXLPARAM lParam
,
248 WXWORD
*state
, WXWORD
*minimized
, WXHWND
*hwnd
);
249 void UnpackScroll(WXWPARAM wParam
, WXLPARAM lParam
,
250 WXWORD
*code
, WXWORD
*pos
, WXHWND
*hwnd
);
251 void UnpackCtlColor(WXWPARAM wParam
, WXLPARAM lParam
,
252 WXHDC
*hdc
, WXHWND
*hwnd
);
253 void UnpackMenuSelect(WXWPARAM wParam
, WXLPARAM lParam
,
254 WXWORD
*item
, WXWORD
*flags
, WXHMENU
*hmenu
);
256 // ------------------------------------------------------------------------
257 // internal handlers for MSW messages: all handlers return a boolean value:
258 // true means that the handler processed the event and false that it didn't
259 // ------------------------------------------------------------------------
261 // there are several cases where we have virtual functions for Windows
262 // message processing: this is because these messages often require to be
263 // processed in a different manner in the derived classes. For all other
264 // messages, however, we do *not* have corresponding MSWOnXXX() function
265 // and if the derived class wants to process them, it should override
266 // MSWWindowProc() directly.
268 // scroll event (both horizontal and vertical)
269 virtual bool MSWOnScroll(int orientation
, WXWORD nSBCode
,
270 WXWORD pos
, WXHWND control
);
272 // child control notifications
273 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
275 // owner-drawn controls need to process these messages
276 virtual bool MSWOnDrawItem(int id
, WXDRAWITEMSTRUCT
*item
);
277 virtual bool MSWOnMeasureItem(int id
, WXMEASUREITEMSTRUCT
*item
);
279 // the rest are not virtual
280 bool HandleCreate(WXLPCREATESTRUCT cs
, bool *mayCreate
);
281 bool HandleInitDialog(WXHWND hWndFocus
);
282 bool HandleDestroy();
285 bool HandlePrintClient(WXHDC hDC
);
286 bool HandleEraseBkgnd(WXHDC hDC
);
288 bool HandleMinimize();
289 bool HandleMaximize();
290 bool HandleSize(int x
, int y
, WXUINT flag
);
291 bool HandleSizing(wxRect
& rect
);
292 bool HandleGetMinMaxInfo(void *mmInfo
);
294 bool HandleShow(bool show
, int status
);
295 bool HandleActivate(int flag
, bool minimized
, WXHWND activate
);
297 bool HandleCommand(WXWORD id
, WXWORD cmd
, WXHWND control
);
299 bool HandleCtlColor(WXHBRUSH
*hBrush
, WXHDC hdc
, WXHWND hWnd
);
301 bool HandlePaletteChanged(WXHWND hWndPalChange
);
302 bool HandleQueryNewPalette();
303 bool HandleSysColorChange();
304 bool HandleDisplayChange();
305 bool HandleCaptureChanged(WXHWND gainedCapture
);
307 bool HandleQueryEndSession(long logOff
, bool *mayEnd
);
308 bool HandleEndSession(bool endSession
, long logOff
);
310 bool HandleSetFocus(WXHWND wnd
);
311 bool HandleKillFocus(WXHWND wnd
);
313 bool HandleDropFiles(WXWPARAM wParam
);
315 bool HandleMouseEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
316 bool HandleMouseMove(int x
, int y
, WXUINT flags
);
317 bool HandleMouseWheel(WXWPARAM wParam
, WXLPARAM lParam
);
319 bool HandleChar(WXWPARAM wParam
, WXLPARAM lParam
, bool isASCII
= false);
320 bool HandleKeyDown(WXWPARAM wParam
, WXLPARAM lParam
);
321 bool HandleKeyUp(WXWPARAM wParam
, WXLPARAM lParam
);
323 bool HandleHotKey(WXWPARAM wParam
, WXLPARAM lParam
);
326 int HandleMenuChar(int chAccel
, WXLPARAM lParam
);
329 bool HandleQueryDragIcon(WXHICON
*hIcon
);
331 bool HandleSetCursor(WXHWND hWnd
, short nHitTest
, int mouseMsg
);
334 virtual WXLRESULT
MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
336 // Calls an appropriate default window procedure
337 virtual WXLRESULT
MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
);
339 // message processing helpers
341 // return false if the message shouldn't be translated/preprocessed but
342 // dispatched normally
343 virtual bool MSWShouldPreProcessMessage(WXMSG
* pMsg
);
345 // return true if the message was preprocessed and shouldn't be dispatched
346 virtual bool MSWProcessMessage(WXMSG
* pMsg
);
348 // return true if the message was translated and shouldn't be dispatched
349 virtual bool MSWTranslateMessage(WXMSG
* pMsg
);
351 // called when the window is about to be destroyed
352 virtual void MSWDestroyWindow();
355 // this function should return the brush to paint the children controls
356 // background or 0 if this window doesn't impose any particular background
359 // the base class version returns a solid brush if we have a non default
360 // background colour or 0 otherwise
361 virtual WXHBRUSH
MSWGetBgBrushForChild(WXHDC hDC
, WXHWND hWnd
);
363 // return the background brush to use for painting the given window by
364 // quering the parent windows via their MSWGetBgBrushForChild() recursively
366 // hWndToPaint is normally NULL meaning this window itself, but it can also
367 // be a child of this window which is used by the static box and could be
368 // potentially useful for other transparent controls
369 WXHBRUSH
MSWGetBgBrush(WXHDC hDC
, WXHWND hWndToPaint
= NULL
);
371 // gives the parent the possibility to draw its children background, e.g.
372 // this is used by wxNotebook to do it using DrawThemeBackground()
374 // return true if background was drawn, false otherwise
375 virtual bool MSWPrintChild(WXHDC
WXUNUSED(hDC
), wxWindow
* WXUNUSED(child
))
380 // some controls (e.g. wxListBox) need to set the return value themselves
382 // return true to let parent handle it if we don't, false otherwise
383 virtual bool MSWShouldPropagatePrintChild()
389 // Responds to colour changes: passes event on to children.
390 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
392 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
393 void InitMouseEvent(wxMouseEvent
& event
, int x
, int y
, WXUINT flags
);
395 // check if mouse is in the window
396 bool IsMouseInWindow() const;
398 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
399 void GenerateMouseLeave();
401 // virtual function for implementing internal idle
403 virtual void OnInternalIdle();
409 // the old window proc (we subclass all windows)
410 WXFARPROC m_oldWndProc
;
412 // additional (MSW specific) flags
413 bool m_mouseInWindow
:1;
414 bool m_lastKeydownProcessed
:1;
416 // the size of one page for scrolling
420 // implement the base class pure virtuals
421 virtual void DoClientToScreen( int *x
, int *y
) const;
422 virtual void DoScreenToClient( int *x
, int *y
) const;
423 virtual void DoGetPosition( int *x
, int *y
) const;
424 virtual void DoGetSize( int *width
, int *height
) const;
425 virtual void DoGetClientSize( int *width
, int *height
) const;
426 virtual void DoSetSize(int x
, int y
,
427 int width
, int height
,
428 int sizeFlags
= wxSIZE_AUTO
);
429 virtual void DoSetClientSize(int width
, int height
);
431 virtual void DoCaptureMouse();
432 virtual void DoReleaseMouse();
434 // has the window been frozen by Freeze()?
435 bool IsFrozen() const { return m_frozenness
> 0; }
437 // this simply moves/resizes the given HWND which is supposed to be our
438 // sibling (this is useful for controls which are composite at MSW level
439 // and for which DoMoveWindow() is not enough)
441 // returns true if the window move was deferred, false if it was moved
442 // immediately (no error return)
443 bool DoMoveSibling(WXHWND hwnd
, int x
, int y
, int width
, int height
);
445 // move the window to the specified location and resize it: this is called
446 // from both DoSetSize() and DoSetClientSize() and would usually just call
447 // ::MoveWindow() except for composite controls which will want to arrange
448 // themselves inside the given rectangle
449 virtual void DoMoveWindow(int x
, int y
, int width
, int height
);
452 virtual void DoSetToolTip( wxToolTip
*tip
);
454 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
455 // comctl32.dll in our code -- see the function body for more info)
456 bool HandleTooltipNotify(WXUINT code
,
458 const wxString
& ttip
);
459 #endif // wxUSE_TOOLTIPS
461 // the helper functions used by HandleChar/KeyXXX methods
462 wxKeyEvent
CreateKeyEvent(wxEventType evType
, int id
,
463 WXLPARAM lParam
= 0, WXWPARAM wParam
= 0) const;
466 // default OnEraseBackground() implementation, return true if we did erase
467 // the background, false otherwise (i.e. the system should erase it)
468 bool DoEraseBackground(WXHDC hDC
);
470 // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
472 // action should be one of the UIS_XXX constants
473 // state should be one or more of the UISF_XXX constants
474 // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
475 // for state as the system will decide for us what needs to be set
476 void MSWUpdateUIState(int action
, int state
= 0);
479 // common part of all ctors
482 // the (non-virtual) handlers for the events
483 bool HandleMove(int x
, int y
);
484 bool HandleMoving(wxRect
& rect
);
485 bool HandleJoystickEvent(WXUINT msg
, int x
, int y
, WXUINT flags
);
486 bool HandleNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
488 // list of disabled children before last call to our Disable()
489 wxWindowList
*m_childrenDisabled
;
491 // number of calls to Freeze() minus number of calls to Thaw()
492 unsigned int m_frozenness
;
494 // current defer window position operation handle (may be NULL)
497 // When deferred positioning is done these hold the pending changes, and
498 // are used for the default values if another size/pos changes is done on
499 // this window before the group of deferred changes is completed.
500 wxPoint m_pendingPosition
;
501 wxSize m_pendingSize
;
503 DECLARE_DYNAMIC_CLASS(wxWindowMSW
)
504 DECLARE_NO_COPY_CLASS(wxWindowMSW
)
505 DECLARE_EVENT_TABLE()
508 // ----------------------------------------------------------------------------
510 // ----------------------------------------------------------------------------
512 #if WXWIN_COMPATIBILITY_2_4
514 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
515 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
516 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t
)) { }
518 #endif // WXWIN_COMPATIBILITY_2_4
520 // ---------------------------------------------------------------------------
522 // ---------------------------------------------------------------------------
524 // kbd code translation
525 WXDLLEXPORT
int wxCharCodeMSWToWX(int keySym
, WXLPARAM lParam
= 0);
526 WXDLLEXPORT WXWORD
wxCharCodeWXToMSW(int id
, bool *IsVirtual
);
528 // window creation helper class: before creating a new HWND, instantiate an
529 // object of this class on stack - this allows to process the messages sent to
530 // the window even before CreateWindow() returns
531 class wxWindowCreationHook
534 wxWindowCreationHook(wxWindowMSW
*winBeingCreated
);
535 ~wxWindowCreationHook();
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
542 // notice that this hash must be defined after wxWindow declaration as it
543 // needs to "see" its dtor and not just forward declaration
546 // pseudo-template HWND <-> wxWindow hash table
547 #if WXWIN_COMPATIBILITY_2_4
548 WX_DECLARE_HASH(wxWindow
, wxWindowList
, wxWinHashTable
);
550 WX_DECLARE_HASH(wxWindowMSW
, wxWindowList
, wxWinHashTable
);
553 extern wxWinHashTable
*wxWinHandleHash
;
555 #endif // _WX_WINDOW_H_