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(), ...
7// Created:     01/02/97
8// RCS-ID:      $Id: window.h 53929 2008-06-02 18:27:16Z RD $
9// Copyright:   (c) Julian Smart
10// Licence:     wxWindows licence
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef _WX_WINDOW_H_
14#define _WX_WINDOW_H_
15
16// ---------------------------------------------------------------------------
17// constants
18// ---------------------------------------------------------------------------
19
20#if WXWIN_COMPATIBILITY_2_4
21// they're unused by wxWidgets...
22enum
23{
24    wxKEY_SHIFT = 1,
25    wxKEY_CTRL  = 2
26};
27#endif
28
29// ---------------------------------------------------------------------------
30// wxWindow declaration for MSW
31// ---------------------------------------------------------------------------
32
33class WXDLLEXPORT wxWindowMSW : public wxWindowBase
34{
35    friend class wxSpinCtrl;
36    friend class wxSlider;
37    friend class wxRadioBox;
38#if defined __VISUALC__ && __VISUALC__ <= 1200
39    friend class wxWindowMSW;
40#endif
41public:
42    wxWindowMSW() { Init(); }
43
44    wxWindowMSW(wxWindow *parent,
45                wxWindowID id,
46                const wxPoint& pos = wxDefaultPosition,
47                const wxSize& size = wxDefaultSize,
48                long style = 0,
49                const wxString& name = wxPanelNameStr)
50    {
51        Init();
52        Create(parent, id, pos, size, style, name);
53    }
54
55    virtual ~wxWindowMSW();
56
57    bool Create(wxWindow *parent,
58                wxWindowID id,
59                const wxPoint& pos = wxDefaultPosition,
60                const wxSize& size = wxDefaultSize,
61                long style = 0,
62                const wxString& name = wxPanelNameStr);
63
64    // implement base class pure virtuals
65    virtual void SetLabel(const wxString& label);
66    virtual wxString GetLabel() const;
67
68    virtual void Raise();
69    virtual void Lower();
70
71    virtual bool Show( bool show = true );
72    virtual bool Enable( bool enable = true );
73
74    virtual void SetFocus();
75    virtual void SetFocusFromKbd();
76
77    virtual bool Reparent(wxWindowBase *newParent);
78
79    virtual void WarpPointer(int x, int y);
80
81    virtual void Refresh( bool eraseBackground = true,
82                          const wxRect *rect = (const wxRect *) NULL );
83    virtual void Update();
84    virtual void Freeze();
85    virtual void Thaw();
86    virtual bool IsFrozen() const { return m_frozenness > 0; }
87
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 );
92
93    virtual int GetCharHeight() const;
94    virtual int GetCharWidth() const;
95    virtual void GetTextExtent(const wxString& string,
96                               int *x, int *y,
97                               int *descent = (int *) NULL,
98                               int *externalLeading = (int *) NULL,
99                               const wxFont *theFont = (const wxFont *) NULL)
100                               const;
101
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 );
110
111    virtual bool ScrollLines(int lines);
112    virtual bool ScrollPages(int pages);
113
114    virtual void SetLayoutDirection(wxLayoutDirection dir);
115    virtual wxLayoutDirection GetLayoutDirection() const;
116    virtual wxCoord AdjustForLayoutDirection(wxCoord x,
117                                             wxCoord width,
118                                             wxCoord widthTotal) const;
119
120#if wxUSE_DRAG_AND_DROP
121    virtual void SetDropTarget( wxDropTarget *dropTarget );
122#endif // wxUSE_DRAG_AND_DROP
123
124    // Accept files for dragging
125    virtual void DragAcceptFiles(bool accept);
126
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
132
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__
141
142#if wxUSE_HOTKEY
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
147
148#ifdef __POCKETPC__
149    bool IsContextMenuEnabled() const { return m_contextMenuEnabled; }
150    void EnableContextMenu(bool enable = true) { m_contextMenuEnabled = enable; }
151#endif
152
153    // window handle stuff
154    // -------------------
155
156    WXHWND GetHWND() const { return m_hWnd; }
157    void SetHWND(WXHWND hWnd) { m_hWnd = hWnd; }
158    virtual WXWidget GetHandle() const { return GetHWND(); }
159
160    void AssociateHandle(WXWidget handle);
161    void DissociateHandle();
162
163    // does this window have deferred position and/or size?
164    bool IsSizeDeferred() const;
165
166
167    // implementation from now on
168    // ==========================
169
170    // event handlers
171    // --------------
172
173    void OnPaint(wxPaintEvent& event);
174    void OnEraseBackground(wxEraseEvent& event);
175#ifdef __WXWINCE__
176    void OnInitDialog(wxInitDialogEvent& event);
177#endif
178
179public:
180    // Windows subclassing
181    void SubclassWin(WXHWND hWnd);
182    void UnsubclassWin();
183
184    WXFARPROC MSWGetOldWndProc() const { return m_oldWndProc; }
185    void MSWSetOldWndProc(WXFARPROC proc) { m_oldWndProc = proc; }
186
187    // return true if the window is of a standard (i.e. not wxWidgets') class
188    //
189    // to understand why does it work, look at SubclassWin() code and comments
190    bool IsOfStandardClass() const { return m_oldWndProc != NULL; }
191
192    wxWindow *FindItem(long id) const;
193    wxWindow *FindItemByHWND(WXHWND hWnd, bool controlOnly = false) const;
194
195    // MSW only: true if this control is part of the main control
196    virtual bool ContainsHWND(WXHWND WXUNUSED(hWnd)) const { return false; }
197
198#if wxUSE_TOOLTIPS
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
202
203    // translate wxWidgets style flags for this control into the Windows style
204    // and optional extended style for the corresponding native control
205    //
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 ;
209
210    // get the MSW window flags corresponding to wxWidgets ones
211    //
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); }
216
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
219    // created yet)
220    void MSWUpdateStyle(long flagsOld, long exflagsOld);
221
222#if wxABI_VERSION >= 20805
223    // Helper for getting an appropriate theme style for the application. Unnecessary in
224    // 2.9 and above.
225	wxBorder GetThemedBorderStyle() const;
226#endif
227
228    // translate wxWidgets coords into Windows ones suitable to be passed to
229    // ::CreateWindow()
230    //
231    // returns true if non default coords are returned, false otherwise
232    bool MSWGetCreateWindowCoords(const wxPoint& pos,
233                                  const wxSize& size,
234                                  int& x, int& y,
235                                  int& w, int& h) const;
236
237    // get the HWND to be used as parent of this window with CreateWindow()
238    virtual WXHWND MSWGetParent() const;
239
240    // creates the window of specified Windows class with given style, extended
241    // style, title and geometry (default values
242    //
243    // returns true if the window has been created, false if creation failed
244    bool MSWCreate(const wxChar *wclass,
245                   const wxChar *title = NULL,
246                   const wxPoint& pos = wxDefaultPosition,
247                   const wxSize& size = wxDefaultSize,
248                   WXDWORD style = 0,
249                   WXDWORD exendedStyle = 0);
250
251    virtual bool MSWCommand(WXUINT param, WXWORD id);
252
253#ifndef __WXUNIVERSAL__
254    // Create an appropriate wxWindow from a HWND
255    virtual wxWindow* CreateWindowFromHWND(wxWindow* parent, WXHWND hWnd);
256
257    // Make sure the window style reflects the HWND style (roughly)
258    virtual void AdoptAttributesFromHWND();
259#endif // __WXUNIVERSAL__
260
261    // Setup background and foreground colours correctly
262    virtual void SetupColours();
263
264    // ------------------------------------------------------------------------
265    // helpers for message handlers: these perform the same function as the
266    // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
267    // the correct parameters
268    // ------------------------------------------------------------------------
269
270    void UnpackCommand(WXWPARAM wParam, WXLPARAM lParam,
271                       WXWORD *id, WXHWND *hwnd, WXWORD *cmd);
272    void UnpackActivate(WXWPARAM wParam, WXLPARAM lParam,
273                        WXWORD *state, WXWORD *minimized, WXHWND *hwnd);
274    void UnpackScroll(WXWPARAM wParam, WXLPARAM lParam,
275                      WXWORD *code, WXWORD *pos, WXHWND *hwnd);
276    void UnpackCtlColor(WXWPARAM wParam, WXLPARAM lParam,
277                        WXHDC *hdc, WXHWND *hwnd);
278    void UnpackMenuSelect(WXWPARAM wParam, WXLPARAM lParam,
279                          WXWORD *item, WXWORD *flags, WXHMENU *hmenu);
280
281    // ------------------------------------------------------------------------
282    // internal handlers for MSW messages: all handlers return a boolean value:
283    // true means that the handler processed the event and false that it didn't
284    // ------------------------------------------------------------------------
285
286    // there are several cases where we have virtual functions for Windows
287    // message processing: this is because these messages often require to be
288    // processed in a different manner in the derived classes. For all other
289    // messages, however, we do *not* have corresponding MSWOnXXX() function
290    // and if the derived class wants to process them, it should override
291    // MSWWindowProc() directly.
292
293    // scroll event (both horizontal and vertical)
294    virtual bool MSWOnScroll(int orientation, WXWORD nSBCode,
295                             WXWORD pos, WXHWND control);
296
297    // child control notifications
298    virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
299
300    // owner-drawn controls need to process these messages
301    virtual bool MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *item);
302    virtual bool MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *item);
303
304    // the rest are not virtual
305    bool HandleCreate(WXLPCREATESTRUCT cs, bool *mayCreate);
306    bool HandleInitDialog(WXHWND hWndFocus);
307    bool HandleDestroy();
308
309    bool HandlePaint();
310    bool HandlePrintClient(WXHDC hDC);
311    bool HandleEraseBkgnd(WXHDC hDC);
312
313    bool HandleMinimize();
314    bool HandleMaximize();
315    bool HandleSize(int x, int y, WXUINT flag);
316    bool HandleSizing(wxRect& rect);
317    bool HandleGetMinMaxInfo(void *mmInfo);
318
319    bool HandleShow(bool show, int status);
320    bool HandleActivate(int flag, bool minimized, WXHWND activate);
321
322    bool HandleCommand(WXWORD id, WXWORD cmd, WXHWND control);
323
324    bool HandleCtlColor(WXHBRUSH *hBrush, WXHDC hdc, WXHWND hWnd);
325
326    bool HandlePaletteChanged(WXHWND hWndPalChange);
327    bool HandleQueryNewPalette();
328    bool HandleSysColorChange();
329    bool HandleDisplayChange();
330    bool HandleCaptureChanged(WXHWND gainedCapture);
331    virtual bool HandleSettingChange(WXWPARAM wParam, WXLPARAM lParam);
332
333    bool HandleQueryEndSession(long logOff, bool *mayEnd);
334    bool HandleEndSession(bool endSession, long logOff);
335
336    bool HandleSetFocus(WXHWND wnd);
337    bool HandleKillFocus(WXHWND wnd);
338
339    bool HandleDropFiles(WXWPARAM wParam);
340
341    bool HandleMouseEvent(WXUINT msg, int x, int y, WXUINT flags);
342    bool HandleMouseMove(int x, int y, WXUINT flags);
343    bool HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam);
344
345    bool HandleChar(WXWPARAM wParam, WXLPARAM lParam, bool isASCII = false);
346    bool HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam);
347    bool HandleKeyUp(WXWPARAM wParam, WXLPARAM lParam);
348#if wxUSE_ACCEL
349    bool HandleHotKey(WXWPARAM wParam, WXLPARAM lParam);
350#endif
351#ifdef __WIN32__
352    int HandleMenuChar(int chAccel, WXLPARAM lParam);
353#endif
354    // Create and process a clipboard event specified by type.
355    bool HandleClipboardEvent( WXUINT nMsg );
356
357    bool HandleQueryDragIcon(WXHICON *hIcon);
358
359    bool HandleSetCursor(WXHWND hWnd, short nHitTest, int mouseMsg);
360
361    bool HandlePower(WXWPARAM wParam, WXLPARAM lParam, bool *vetoed);
362
363
364    // Window procedure
365    virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
366
367    // Calls an appropriate default window procedure
368    virtual WXLRESULT MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
369
370    // message processing helpers
371
372    // return false if the message shouldn't be translated/preprocessed but
373    // dispatched normally
374    virtual bool MSWShouldPreProcessMessage(WXMSG* pMsg);
375
376    // return true if the message was preprocessed and shouldn't be dispatched
377    virtual bool MSWProcessMessage(WXMSG* pMsg);
378
379    // return true if the message was translated and shouldn't be dispatched
380    virtual bool MSWTranslateMessage(WXMSG* pMsg);
381
382    // called when the window is about to be destroyed
383    virtual void MSWDestroyWindow();
384
385
386    // this function should return the brush to paint the children controls
387    // background or 0 if this window doesn't impose any particular background
388    // on its children
389    //
390    // the base class version returns a solid brush if we have a non default
391    // background colour or 0 otherwise
392    virtual WXHBRUSH MSWGetBgBrushForChild(WXHDC hDC, WXHWND hWnd);
393
394    // return the background brush to use for painting the given window by
395    // quering the parent windows via their MSWGetBgBrushForChild() recursively
396    //
397    // hWndToPaint is normally NULL meaning this window itself, but it can also
398    // be a child of this window which is used by the static box and could be
399    // potentially useful for other transparent controls
400    WXHBRUSH MSWGetBgBrush(WXHDC hDC, WXHWND hWndToPaint = NULL);
401
402    // gives the parent the possibility to draw its children background, e.g.
403    // this is used by wxNotebook to do it using DrawThemeBackground()
404    //
405    // return true if background was drawn, false otherwise
406    virtual bool MSWPrintChild(WXHDC WXUNUSED(hDC), wxWindow * WXUNUSED(child))
407    {
408        return false;
409    }
410
411    // some controls (e.g. wxListBox) need to set the return value themselves
412    //
413    // return true to let parent handle it if we don't, false otherwise
414    virtual bool MSWShouldPropagatePrintChild()
415    {
416        return true;
417    }
418
419
420    // Responds to colour changes: passes event on to children.
421    void OnSysColourChanged(wxSysColourChangedEvent& event);
422
423    // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
424    void InitMouseEvent(wxMouseEvent& event, int x, int y, WXUINT flags);
425
426    // check if mouse is in the window
427    bool IsMouseInWindow() const;
428
429    // check if a native double-buffering applies for this window
430    virtual bool IsDoubleBuffered() const;
431
432#if wxABI_VERSION >= 20808
433    void SetDoubleBuffered(bool on);
434#endif
435
436    // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
437    void GenerateMouseLeave();
438
439    // virtual function for implementing internal idle
440    // behaviour
441    virtual void OnInternalIdle();
442
443protected:
444
445#if wxUSE_MENUS_NATIVE
446    virtual bool DoPopupMenu( wxMenu *menu, int x, int y );
447#endif // wxUSE_MENUS_NATIVE
448
449    // the window handle
450    WXHWND                m_hWnd;
451
452    // the old window proc (we subclass all windows)
453    WXFARPROC             m_oldWndProc;
454
455    // additional (MSW specific) flags
456    bool                  m_mouseInWindow:1;
457    bool                  m_lastKeydownProcessed:1;
458
459    // the size of one page for scrolling
460    int                   m_xThumbSize;
461    int                   m_yThumbSize;
462
463    // implement the base class pure virtuals
464    virtual void DoClientToScreen( int *x, int *y ) const;
465    virtual void DoScreenToClient( int *x, int *y ) const;
466    virtual void DoGetPosition( int *x, int *y ) const;
467    virtual void DoGetSize( int *width, int *height ) const;
468    virtual void DoGetClientSize( int *width, int *height ) const;
469    virtual void DoSetSize(int x, int y,
470                           int width, int height,
471                           int sizeFlags = wxSIZE_AUTO);
472    virtual void DoSetClientSize(int width, int height);
473
474    virtual void DoCaptureMouse();
475    virtual void DoReleaseMouse();
476
477    // this simply moves/resizes the given HWND which is supposed to be our
478    // sibling (this is useful for controls which are composite at MSW level
479    // and for which DoMoveWindow() is not enough)
480    //
481    // returns true if the window move was deferred, false if it was moved
482    // immediately (no error return)
483    bool DoMoveSibling(WXHWND hwnd, int x, int y, int width, int height);
484
485    // move the window to the specified location and resize it: this is called
486    // from both DoSetSize() and DoSetClientSize() and would usually just call
487    // ::MoveWindow() except for composite controls which will want to arrange
488    // themselves inside the given rectangle
489    virtual void DoMoveWindow(int x, int y, int width, int height);
490
491#if wxUSE_TOOLTIPS
492    virtual void DoSetToolTip( wxToolTip *tip );
493
494    // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
495    // comctl32.dll in our code -- see the function body for more info)
496    bool HandleTooltipNotify(WXUINT code,
497                             WXLPARAM lParam,
498                             const wxString& ttip);
499#endif // wxUSE_TOOLTIPS
500
501    // the helper functions used by HandleChar/KeyXXX methods
502    wxKeyEvent CreateKeyEvent(wxEventType evType, int id,
503                              WXLPARAM lParam = 0, WXWPARAM wParam = 0) const;
504
505
506    // default OnEraseBackground() implementation, return true if we did erase
507    // the background, false otherwise (i.e. the system should erase it)
508    bool DoEraseBackground(WXHDC hDC);
509
510    // generate WM_CHANGEUISTATE if it's needed for the OS we're running under
511    //
512    // action should be one of the UIS_XXX constants
513    // state should be one or more of the UISF_XXX constants
514    // if action == UIS_INITIALIZE then it doesn't seem to matter what we use
515    // for state as the system will decide for us what needs to be set
516    void MSWUpdateUIState(int action, int state = 0);
517
518private:
519    // common part of all ctors
520    void Init();
521
522    // the (non-virtual) handlers for the events
523    bool HandleMove(int x, int y);
524    bool HandleMoving(wxRect& rect);
525    bool HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags);
526    bool HandleNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
527
528    // list of disabled children before last call to our Disable()
529    wxWindowList *m_childrenDisabled;
530
531    // number of calls to Freeze() minus number of calls to Thaw()
532    unsigned int m_frozenness;
533
534    // current defer window position operation handle (may be NULL)
535    WXHANDLE m_hDWP;
536
537protected:
538    // When deferred positioning is done these hold the pending changes, and
539    // are used for the default values if another size/pos changes is done on
540    // this window before the group of deferred changes is completed.
541    wxPoint     m_pendingPosition;
542    wxSize      m_pendingSize;
543
544private:
545#ifdef __POCKETPC__
546    bool        m_contextMenuEnabled;
547#endif
548
549    DECLARE_DYNAMIC_CLASS(wxWindowMSW)
550    DECLARE_NO_COPY_CLASS(wxWindowMSW)
551    DECLARE_EVENT_TABLE()
552};
553
554// ----------------------------------------------------------------------------
555// inline functions
556// ----------------------------------------------------------------------------
557
558#if WXWIN_COMPATIBILITY_2_4
559
560inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
561inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
562inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t)) { }
563
564#endif // WXWIN_COMPATIBILITY_2_4
565
566// ---------------------------------------------------------------------------
567// global functions
568// ---------------------------------------------------------------------------
569
570// kbd code translation
571WXDLLEXPORT int wxCharCodeMSWToWX(int keySym, WXLPARAM lParam = 0);
572WXDLLEXPORT WXWORD wxCharCodeWXToMSW(int id, bool *IsVirtual = NULL);
573
574// window creation helper class: before creating a new HWND, instantiate an
575// object of this class on stack - this allows to process the messages sent to
576// the window even before CreateWindow() returns
577class wxWindowCreationHook
578{
579public:
580    wxWindowCreationHook(wxWindowMSW *winBeingCreated);
581    ~wxWindowCreationHook();
582};
583
584// ----------------------------------------------------------------------------
585// global objects
586// ----------------------------------------------------------------------------
587
588// notice that this hash must be defined after wxWindow declaration as it
589// needs to "see" its dtor and not just forward declaration
590#include "wx/hash.h"
591
592// pseudo-template HWND <-> wxWindow hash table
593#if WXWIN_COMPATIBILITY_2_4
594WX_DECLARE_HASH(wxWindow, wxWindowList, wxWinHashTable);
595#else
596WX_DECLARE_HASH(wxWindowMSW, wxWindowList, wxWinHashTable);
597#endif
598
599extern wxWinHashTable *wxWinHandleHash;
600
601#endif // _WX_WINDOW_H_
602