added IsSizeDeferred() (part of patch 1199639)
[wxWidgets.git] / include / wx / msw / window.h
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(), ...
7 // Created: 01/02/97
8 // RCS-ID: $Id$
9 // Copyright: (c) Julian Smart
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef _WX_WINDOW_H_
14 #define _WX_WINDOW_H_
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma interface "window.h"
22 #endif
23
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
26 // define below to 1
27 //
28 // a better solution should be found later...
29 #define wxUSE_MOUSEEVENT_HACK 0
30
31 // ---------------------------------------------------------------------------
32 // constants
33 // ---------------------------------------------------------------------------
34
35 #if WXWIN_COMPATIBILITY_2_4
36 // they're unused by wxWidgets...
37 enum
38 {
39 wxKEY_SHIFT = 1,
40 wxKEY_CTRL = 2
41 };
42 #endif
43
44 // ---------------------------------------------------------------------------
45 // wxWindow declaration for MSW
46 // ---------------------------------------------------------------------------
47
48 class WXDLLEXPORT wxWindowMSW : public wxWindowBase
49 {
50 friend class wxSpinCtrl;
51 friend class wxSlider;
52 friend class wxRadioBox;
53 #if defined __VISUALC__ && __VISUALC__ <= 1200
54 friend class wxWindowMSW;
55 #endif
56 public:
57 wxWindowMSW() { Init(); }
58
59 wxWindowMSW(wxWindow *parent,
60 wxWindowID id,
61 const wxPoint& pos = wxDefaultPosition,
62 const wxSize& size = wxDefaultSize,
63 long style = 0,
64 const wxString& name = wxPanelNameStr)
65 {
66 Init();
67 Create(parent, id, pos, size, style, name);
68 }
69
70 virtual ~wxWindowMSW();
71
72 bool Create(wxWindow *parent,
73 wxWindowID id,
74 const wxPoint& pos = wxDefaultPosition,
75 const wxSize& size = wxDefaultSize,
76 long style = 0,
77 const wxString& name = wxPanelNameStr);
78
79 // implement base class pure virtuals
80 virtual void SetTitle( const wxString& title);
81 virtual wxString GetTitle() const;
82
83 virtual void Raise();
84 virtual void Lower();
85
86 virtual bool Show( bool show = true );
87 virtual bool Enable( bool enable = true );
88
89 virtual void SetFocus();
90 virtual void SetFocusFromKbd();
91
92 virtual bool Reparent(wxWindowBase *newParent);
93
94 virtual void WarpPointer(int x, int y);
95
96 virtual void Refresh( bool eraseBackground = true,
97 const wxRect *rect = (const wxRect *) NULL );
98 virtual void Update();
99 virtual void Freeze();
100 virtual void Thaw();
101
102 virtual void SetWindowStyleFlag( long style );
103 virtual bool SetCursor( const wxCursor &cursor );
104 virtual bool SetFont( const wxFont &font );
105
106 virtual int GetCharHeight() const;
107 virtual int GetCharWidth() const;
108 virtual void GetTextExtent(const wxString& string,
109 int *x, int *y,
110 int *descent = (int *) NULL,
111 int *externalLeading = (int *) NULL,
112 const wxFont *theFont = (const wxFont *) NULL)
113 const;
114
115 #if wxUSE_MENUS_NATIVE
116 virtual bool DoPopupMenu( wxMenu *menu, int x, int y );
117 #endif // wxUSE_MENUS_NATIVE
118
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 );
127
128 virtual bool ScrollLines(int lines);
129 virtual bool ScrollPages(int pages);
130
131 #if wxUSE_DRAG_AND_DROP
132 virtual void SetDropTarget( wxDropTarget *dropTarget );
133 #endif // wxUSE_DRAG_AND_DROP
134
135 // Accept files for dragging
136 virtual void DragAcceptFiles(bool accept);
137
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
143
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__
152
153 #if wxUSE_HOTKEY
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
158
159 // window handle stuff
160 // -------------------
161
162 WXHWND GetHWND() const { return m_hWnd; }
163 void SetHWND(WXHWND hWnd) { m_hWnd = hWnd; }
164 virtual WXWidget GetHandle() const { return GetHWND(); }
165
166 void AssociateHandle(WXWidget handle);
167 void DissociateHandle();
168
169 // does this window have deferred position and/or size?
170 bool IsSizeDeferred() const;
171
172
173 // implementation from now on
174 // ==========================
175
176 // event handlers
177 // --------------
178
179 void OnPaint(wxPaintEvent& event);
180 void OnEraseBackground(wxEraseEvent& event);
181 #ifdef __WXWINCE__
182 void OnInitDialog(wxInitDialogEvent& event);
183 #endif
184
185 public:
186 // Windows subclassing
187 void SubclassWin(WXHWND hWnd);
188 void UnsubclassWin();
189
190 WXFARPROC MSWGetOldWndProc() const { return m_oldWndProc; }
191 void MSWSetOldWndProc(WXFARPROC proc) { m_oldWndProc = proc; }
192
193 // return true if the window is of a standard (i.e. not wxWidgets') class
194 //
195 // to understand why does it work, look at SubclassWin() code and comments
196 bool IsOfStandardClass() const { return m_oldWndProc != NULL; }
197
198 wxWindow *FindItem(long id) const;
199 wxWindow *FindItemByHWND(WXHWND hWnd, bool controlOnly = false) const;
200
201 // MSW only: true if this control is part of the main control
202 virtual bool ContainsHWND(WXHWND WXUNUSED(hWnd)) const { return false; };
203
204 // translate wxWidgets style flags for this control into the Windows style
205 // and optional extended style for the corresponding native control
206 //
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 ;
210
211 // get the MSW window flags corresponding to wxWidgets ones
212 //
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); }
217
218 // translate wxWidgets coords into Windows ones suitable to be passed to
219 // ::CreateWindow()
220 //
221 // returns true if non default coords are returned, false otherwise
222 bool MSWGetCreateWindowCoords(const wxPoint& pos,
223 const wxSize& size,
224 int& x, int& y,
225 int& w, int& h) const;
226
227 // get the HWND to be used as parent of this window with CreateWindow()
228 virtual WXHWND MSWGetParent() const;
229
230 // creates the window of specified Windows class with given style, extended
231 // style, title and geometry (default values
232 //
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,
238 WXDWORD style = 0,
239 WXDWORD exendedStyle = 0);
240
241 virtual bool MSWCommand(WXUINT param, WXWORD id);
242
243 #ifndef __WXUNIVERSAL__
244 // Create an appropriate wxWindow from a HWND
245 virtual wxWindow* CreateWindowFromHWND(wxWindow* parent, WXHWND hWnd);
246
247 // Make sure the window style reflects the HWND style (roughly)
248 virtual void AdoptAttributesFromHWND();
249 #endif // __WXUNIVERSAL__
250
251 // Setup background and foreground colours correctly
252 virtual void SetupColours();
253
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 // ------------------------------------------------------------------------
259
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);
270
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 // ------------------------------------------------------------------------
275
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.
282
283 // scroll event (both horizontal and vertical)
284 virtual bool MSWOnScroll(int orientation, WXWORD nSBCode,
285 WXWORD pos, WXHWND control);
286
287 // child control notifications
288 #ifdef __WIN95__
289 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
290 #endif // __WIN95__
291
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);
295
296 // the rest are not virtual
297 bool HandleCreate(WXLPCREATESTRUCT cs, bool *mayCreate);
298 bool HandleInitDialog(WXHWND hWndFocus);
299 bool HandleDestroy();
300
301 bool HandlePaint();
302 bool HandlePrintClient(WXHDC hDC);
303 bool HandleEraseBkgnd(WXHDC hDC);
304
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);
310
311 bool HandleShow(bool show, int status);
312 bool HandleActivate(int flag, bool minimized, WXHWND activate);
313
314 bool HandleCommand(WXWORD id, WXWORD cmd, WXHWND control);
315
316 bool HandleCtlColor(WXHBRUSH *hBrush, WXHDC hdc, WXHWND hWnd);
317
318 bool HandlePaletteChanged(WXHWND hWndPalChange);
319 bool HandleQueryNewPalette();
320 bool HandleSysColorChange();
321 bool HandleDisplayChange();
322 bool HandleCaptureChanged(WXHWND gainedCapture);
323
324 bool HandleQueryEndSession(long logOff, bool *mayEnd);
325 bool HandleEndSession(bool endSession, long logOff);
326
327 bool HandleSetFocus(WXHWND wnd);
328 bool HandleKillFocus(WXHWND wnd);
329
330 bool HandleDropFiles(WXWPARAM wParam);
331
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);
335
336 bool HandleChar(WXWPARAM wParam, WXLPARAM lParam, bool isASCII = false);
337 bool HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam);
338 bool HandleKeyUp(WXWPARAM wParam, WXLPARAM lParam);
339 #if wxUSE_ACCEL
340 bool HandleHotKey(WXWPARAM wParam, WXLPARAM lParam);
341 #endif
342 #ifdef __WIN32__
343 int HandleMenuChar(int chAccel, WXLPARAM lParam);
344 #endif
345
346 bool HandleQueryDragIcon(WXHICON *hIcon);
347
348 bool HandleSetCursor(WXHWND hWnd, short nHitTest, int mouseMsg);
349
350 // Window procedure
351 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
352
353 // Calls an appropriate default window procedure
354 virtual WXLRESULT MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
355
356 // message processing helpers
357
358 // return false if the message shouldn't be translated/preprocessed but
359 // dispatched normally
360 virtual bool MSWShouldPreProcessMessage(WXMSG* pMsg);
361
362 // return true if the message was preprocessed and shouldn't be dispatched
363 virtual bool MSWProcessMessage(WXMSG* pMsg);
364
365 // return true if the message was translated and shouldn't be dispatched
366 virtual bool MSWTranslateMessage(WXMSG* pMsg);
367
368 // called when the window is about to be destroyed
369 virtual void MSWDestroyWindow();
370
371
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
374 // on its children
375 //
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);
379
380 // return the background brush to use for painting the given window by
381 // quering the parent windows via their MSWGetBgBrushForChild() recursively
382 //
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);
387
388 // gives the parent the possibility to draw its children background, e.g.
389 // this is used by wxNotebook to do it using DrawThemeBackground()
390 //
391 // return true if background was drawn, false otherwise
392 virtual bool MSWPrintChild(WXHDC WXUNUSED(hDC), wxWindow * WXUNUSED(child))
393 {
394 return false;
395 }
396
397 // some controls (e.g. wxListBox) need to set the return value themselves
398 //
399 // return true to let parent handle it if we don't, false otherwise
400 virtual bool MSWShouldPropagatePrintChild()
401 {
402 return true;
403 }
404
405
406 // Responds to colour changes: passes event on to children.
407 void OnSysColourChanged(wxSysColourChangedEvent& event);
408
409 // initialize various fields of wxMouseEvent (common part of MSWOnMouseXXX)
410 void InitMouseEvent(wxMouseEvent& event, int x, int y, WXUINT flags);
411
412 // check if mouse is in the window
413 bool IsMouseInWindow() const;
414
415 // synthesize a wxEVT_LEAVE_WINDOW event and set m_mouseInWindow to false
416 void GenerateMouseLeave();
417
418 // virtual function for implementing internal idle
419 // behaviour
420 virtual void OnInternalIdle();
421
422 protected:
423 // the window handle
424 WXHWND m_hWnd;
425
426 // the old window proc (we subclass all windows)
427 WXFARPROC m_oldWndProc;
428
429 // additional (MSW specific) flags
430 bool m_mouseInWindow:1;
431 bool m_lastKeydownProcessed:1;
432
433 // the size of one page for scrolling
434 int m_xThumbSize;
435 int m_yThumbSize;
436
437 #if wxUSE_MOUSEEVENT_HACK
438 // the coordinates of the last mouse event and the type of it
439 long m_lastMouseX,
440 m_lastMouseY;
441 int m_lastMouseEvent;
442 #endif // wxUSE_MOUSEEVENT_HACK
443
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);
454
455 virtual void DoCaptureMouse();
456 virtual void DoReleaseMouse();
457
458 // has the window been frozen by Freeze()?
459 bool IsFrozen() const { return m_frozenness > 0; }
460
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)
464 //
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);
468
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);
474
475 #if wxUSE_TOOLTIPS
476 virtual void DoSetToolTip( wxToolTip *tip );
477
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,
481 WXLPARAM lParam,
482 const wxString& ttip);
483 #endif // wxUSE_TOOLTIPS
484
485 // the helper functions used by HandleChar/KeyXXX methods
486 wxKeyEvent CreateKeyEvent(wxEventType evType, int id,
487 WXLPARAM lParam = 0, WXWPARAM wParam = 0) const;
488
489
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);
493
494 private:
495 // common part of all ctors
496 void Init();
497
498 // the (non-virtual) handlers for the events
499 bool HandleMove(int x, int y);
500 bool HandleMoving(wxRect& rect);
501 bool HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags);
502
503 #ifdef __WIN95__
504 bool HandleNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
505 #endif // __WIN95__
506
507 // list of disabled children before last call to our Disable()
508 wxWindowList *m_childrenDisabled;
509
510 // number of calls to Freeze() minus number of calls to Thaw()
511 unsigned int m_frozenness;
512
513 // current defer window position operation handle (may be NULL)
514 WXHANDLE m_hDWP;
515
516 // When deferred positioning is done these hold the pending changes, and
517 // are used for the default values if another size/pos changes is done on
518 // this window before the group of deferred changes is completed.
519 wxPoint m_pendingPosition;
520 wxSize m_pendingSize;
521
522 DECLARE_DYNAMIC_CLASS(wxWindowMSW)
523 DECLARE_NO_COPY_CLASS(wxWindowMSW)
524 DECLARE_EVENT_TABLE()
525 };
526
527 // ----------------------------------------------------------------------------
528 // inline functions
529 // ----------------------------------------------------------------------------
530
531 #if WXWIN_COMPATIBILITY_2_4
532
533 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
534 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
535 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t)) { }
536
537 #endif // WXWIN_COMPATIBILITY_2_4
538
539 // ---------------------------------------------------------------------------
540 // global functions
541 // ---------------------------------------------------------------------------
542
543 // kbd code translation
544 WXDLLEXPORT int wxCharCodeMSWToWX(int keySym, WXLPARAM lParam = 0);
545 WXDLLEXPORT WXWORD wxCharCodeWXToMSW(int id, bool *IsVirtual);
546
547 // window creation helper class: before creating a new HWND, instantiate an
548 // object of this class on stack - this allows to process the messages sent to
549 // the window even before CreateWindow() returns
550 class wxWindowCreationHook
551 {
552 public:
553 wxWindowCreationHook(wxWindowMSW *winBeingCreated);
554 ~wxWindowCreationHook();
555 };
556
557 // ----------------------------------------------------------------------------
558 // global objects
559 // ----------------------------------------------------------------------------
560
561 // notice that this hash must be defined after wxWindow declaration as it
562 // needs to "see" its dtor and not just forward declaration
563 #include "wx/hash.h"
564
565 // pseudo-template HWND <-> wxWindow hash table
566 #if WXWIN_COMPATIBILITY_2_4
567 WX_DECLARE_HASH(wxWindow, wxWindowList, wxWinHashTable);
568 #else
569 WX_DECLARE_HASH(wxWindowMSW, wxWindowList, wxWinHashTable);
570 #endif
571
572 extern wxWinHashTable *wxWinHandleHash;
573
574 #endif
575 // _WX_WINDOW_H_