rewrote and virtualized the background erasing logic: each window may now
[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 // FIXME does anybody use those? they're unused by wxWidgets...
36 enum
37 {
38 wxKEY_SHIFT = 1,
39 wxKEY_CTRL = 2
40 };
41
42 // ---------------------------------------------------------------------------
43 // wxWindow declaration for MSW
44 // ---------------------------------------------------------------------------
45
46 class WXDLLEXPORT wxWindowMSW : public wxWindowBase
47 {
48 public:
49 wxWindowMSW() { Init(); }
50
51 wxWindowMSW(wxWindow *parent,
52 wxWindowID id,
53 const wxPoint& pos = wxDefaultPosition,
54 const wxSize& size = wxDefaultSize,
55 long style = 0,
56 const wxString& name = wxPanelNameStr)
57 {
58 Init();
59 Create(parent, id, pos, size, style, name);
60 }
61
62 virtual ~wxWindowMSW();
63
64 bool Create(wxWindow *parent,
65 wxWindowID id,
66 const wxPoint& pos = wxDefaultPosition,
67 const wxSize& size = wxDefaultSize,
68 long style = 0,
69 const wxString& name = wxPanelNameStr);
70
71 // implement base class pure virtuals
72 virtual void SetTitle( const wxString& title);
73 virtual wxString GetTitle() const;
74
75 virtual void Raise();
76 virtual void Lower();
77
78 virtual bool Show( bool show = true );
79 virtual bool Enable( bool enable = true );
80
81 virtual void SetFocus();
82 virtual void SetFocusFromKbd();
83
84 virtual bool Reparent(wxWindowBase *newParent);
85
86 virtual void WarpPointer(int x, int y);
87
88 virtual void Refresh( bool eraseBackground = true,
89 const wxRect *rect = (const wxRect *) NULL );
90 virtual void Update();
91 virtual void Freeze();
92 virtual void Thaw();
93
94 virtual void SetWindowStyleFlag( long style );
95 virtual bool SetCursor( const wxCursor &cursor );
96 virtual bool SetFont( const wxFont &font );
97
98 virtual int GetCharHeight() const;
99 virtual int GetCharWidth() const;
100 virtual void GetTextExtent(const wxString& string,
101 int *x, int *y,
102 int *descent = (int *) NULL,
103 int *externalLeading = (int *) NULL,
104 const wxFont *theFont = (const wxFont *) NULL)
105 const;
106
107 #if wxUSE_MENUS_NATIVE
108 virtual bool DoPopupMenu( wxMenu *menu, int x, int y );
109 #endif // wxUSE_MENUS_NATIVE
110
111 virtual void SetScrollbar( int orient, int pos, int thumbVisible,
112 int range, bool refresh = true );
113 virtual void SetScrollPos( int orient, int pos, bool refresh = true );
114 virtual int GetScrollPos( int orient ) const;
115 virtual int GetScrollThumb( int orient ) const;
116 virtual int GetScrollRange( int orient ) const;
117 virtual void ScrollWindow( int dx, int dy,
118 const wxRect* rect = (wxRect *) NULL );
119
120 virtual bool ScrollLines(int lines);
121 virtual bool ScrollPages(int pages);
122
123 #if wxUSE_DRAG_AND_DROP
124 virtual void SetDropTarget( wxDropTarget *dropTarget );
125 #endif // wxUSE_DRAG_AND_DROP
126
127 // Accept files for dragging
128 virtual void DragAcceptFiles(bool accept);
129
130 #if WXWIN_COMPATIBILITY_2_4
131 wxDEPRECATED( bool GetUseCtl3D() const );
132 wxDEPRECATED( bool GetTransparentBackground() const );
133 wxDEPRECATED( void SetTransparent(bool t = true) );
134 #endif // WXWIN_COMPATIBILITY_2_4
135
136 #ifndef __WXUNIVERSAL__
137 // Native resource loading (implemented in src/msw/nativdlg.cpp)
138 // FIXME: should they really be all virtual?
139 virtual bool LoadNativeDialog(wxWindow* parent, wxWindowID& id);
140 virtual bool LoadNativeDialog(wxWindow* parent, const wxString& name);
141 wxWindow* GetWindowChild1(wxWindowID id);
142 wxWindow* GetWindowChild(wxWindowID id);
143 #endif // __WXUNIVERSAL__
144
145 #if wxUSE_HOTKEY
146 // install and deinstall a system wide hotkey
147 virtual bool RegisterHotKey(int hotkeyId, int modifiers, int keycode);
148 virtual bool UnregisterHotKey(int hotkeyId);
149 #endif // wxUSE_HOTKEY
150
151 // window handle stuff
152 // -------------------
153
154 WXHWND GetHWND() const { return m_hWnd; }
155 void SetHWND(WXHWND hWnd) { m_hWnd = hWnd; }
156 virtual WXWidget GetHandle() const { return GetHWND(); }
157
158 void AssociateHandle(WXWidget handle);
159 void DissociateHandle();
160
161
162 // implementation from now on
163 // ==========================
164
165 // event handlers
166 // --------------
167
168 void OnPaint(wxPaintEvent& event);
169 void OnEraseBackground(wxEraseEvent& event);
170 #ifdef __WXWINCE__
171 void OnInitDialog(wxInitDialogEvent& event);
172 #endif
173
174 public:
175 // For implementation purposes - sometimes decorations make the client area
176 // smaller
177 virtual wxPoint GetClientAreaOrigin() const;
178
179 // Windows subclassing
180 void SubclassWin(WXHWND hWnd);
181 void UnsubclassWin();
182
183 WXFARPROC MSWGetOldWndProc() const { return m_oldWndProc; }
184 void MSWSetOldWndProc(WXFARPROC proc) { m_oldWndProc = proc; }
185
186 // return true if the window is of a standard (i.e. not wxWidgets') class
187 //
188 // to understand why does it work, look at SubclassWin() code and comments
189 bool IsOfStandardClass() const { return m_oldWndProc != NULL; }
190
191 wxWindow *FindItem(long id) const;
192 wxWindow *FindItemByHWND(WXHWND hWnd, bool controlOnly = false) const;
193
194 // MSW only: true if this control is part of the main control
195 virtual bool ContainsHWND(WXHWND WXUNUSED(hWnd)) const { return false; };
196
197 // translate wxWidgets style flags for this control into the Windows style
198 // and optional extended style for the corresponding native control
199 //
200 // this is the function that should be overridden in the derived classes,
201 // but you will mostly use MSWGetCreateWindowFlags() below
202 virtual WXDWORD MSWGetStyle(long flags, WXDWORD *exstyle = NULL) const ;
203
204 // get the MSW window flags corresponding to wxWidgets ones
205 //
206 // the functions returns the flags (WS_XXX) directly and puts the ext
207 // (WS_EX_XXX) flags into the provided pointer if not NULL
208 WXDWORD MSWGetCreateWindowFlags(WXDWORD *exflags = NULL) const
209 { return MSWGetStyle(GetWindowStyle(), exflags); }
210
211 // translate wxWidgets coords into Windows ones suitable to be passed to
212 // ::CreateWindow()
213 //
214 // returns true if non default coords are returned, false otherwise
215 bool MSWGetCreateWindowCoords(const wxPoint& pos,
216 const wxSize& size,
217 int& x, int& y,
218 int& w, int& h) const;
219
220 // get the HWND to be used as parent of this window with CreateWindow()
221 virtual WXHWND MSWGetParent() const;
222
223 // creates the window of specified Windows class with given style, extended
224 // style, title and geometry (default values
225 //
226 // returns true if the window has been created, false if creation failed
227 bool MSWCreate(const wxChar *wclass,
228 const wxChar *title = NULL,
229 const wxPoint& pos = wxDefaultPosition,
230 const wxSize& size = wxDefaultSize,
231 WXDWORD style = 0,
232 WXDWORD exendedStyle = 0);
233
234 virtual bool MSWCommand(WXUINT param, WXWORD id);
235
236 #ifndef __WXUNIVERSAL__
237 // Create an appropriate wxWindow from a HWND
238 virtual wxWindow* CreateWindowFromHWND(wxWindow* parent, WXHWND hWnd);
239
240 // Make sure the window style reflects the HWND style (roughly)
241 virtual void AdoptAttributesFromHWND();
242 #endif // __WXUNIVERSAL__
243
244 // Setup background and foreground colours correctly
245 virtual void SetupColours();
246
247 // ------------------------------------------------------------------------
248 // helpers for message handlers: these perform the same function as the
249 // message crackers from <windowsx.h> - they unpack WPARAM and LPARAM into
250 // the correct parameters
251 // ------------------------------------------------------------------------
252
253 void UnpackCommand(WXWPARAM wParam, WXLPARAM lParam,
254 WXWORD *id, WXHWND *hwnd, WXWORD *cmd);
255 void UnpackActivate(WXWPARAM wParam, WXLPARAM lParam,
256 WXWORD *state, WXWORD *minimized, WXHWND *hwnd);
257 void UnpackScroll(WXWPARAM wParam, WXLPARAM lParam,
258 WXWORD *code, WXWORD *pos, WXHWND *hwnd);
259 void UnpackCtlColor(WXWPARAM wParam, WXLPARAM lParam,
260 WXHDC *hdc, WXHWND *hwnd);
261 void UnpackMenuSelect(WXWPARAM wParam, WXLPARAM lParam,
262 WXWORD *item, WXWORD *flags, WXHMENU *hmenu);
263
264 // ------------------------------------------------------------------------
265 // internal handlers for MSW messages: all handlers return a boolean value:
266 // true means that the handler processed the event and false that it didn't
267 // ------------------------------------------------------------------------
268
269 // there are several cases where we have virtual functions for Windows
270 // message processing: this is because these messages often require to be
271 // processed in a different manner in the derived classes. For all other
272 // messages, however, we do *not* have corresponding MSWOnXXX() function
273 // and if the derived class wants to process them, it should override
274 // MSWWindowProc() directly.
275
276 // scroll event (both horizontal and vertical)
277 virtual bool MSWOnScroll(int orientation, WXWORD nSBCode,
278 WXWORD pos, WXHWND control);
279
280 // child control notifications
281 #ifdef __WIN95__
282 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
283 #endif // __WIN95__
284
285 // owner-drawn controls need to process these messages
286 virtual bool MSWOnDrawItem(int id, WXDRAWITEMSTRUCT *item);
287 virtual bool MSWOnMeasureItem(int id, WXMEASUREITEMSTRUCT *item);
288
289 // the rest are not virtual
290 bool HandleCreate(WXLPCREATESTRUCT cs, bool *mayCreate);
291 bool HandleInitDialog(WXHWND hWndFocus);
292 bool HandleDestroy();
293
294 bool HandlePaint();
295 bool HandleEraseBkgnd(WXHDC pDC);
296
297 bool HandleMinimize();
298 bool HandleMaximize();
299 bool HandleSize(int x, int y, WXUINT flag);
300 bool HandleSizing(wxRect& rect);
301 bool HandleGetMinMaxInfo(void *mmInfo);
302
303 bool HandleShow(bool show, int status);
304 bool HandleActivate(int flag, bool minimized, WXHWND activate);
305
306 bool HandleCommand(WXWORD id, WXWORD cmd, WXHWND control);
307
308 bool HandleCtlColor(WXHBRUSH *hBrush, WXHDC hdc, WXHWND hWnd);
309
310 bool HandlePaletteChanged(WXHWND hWndPalChange);
311 bool HandleQueryNewPalette();
312 bool HandleSysColorChange();
313 bool HandleDisplayChange();
314 bool HandleCaptureChanged(WXHWND gainedCapture);
315
316 bool HandleQueryEndSession(long logOff, bool *mayEnd);
317 bool HandleEndSession(bool endSession, long logOff);
318
319 bool HandleSetFocus(WXHWND wnd);
320 bool HandleKillFocus(WXHWND wnd);
321
322 bool HandleDropFiles(WXWPARAM wParam);
323
324 bool HandleMouseEvent(WXUINT msg, int x, int y, WXUINT flags);
325 bool HandleMouseMove(int x, int y, WXUINT flags);
326 bool HandleMouseWheel(WXWPARAM wParam, WXLPARAM lParam);
327
328 bool HandleChar(WXWPARAM wParam, WXLPARAM lParam, bool isASCII = false);
329 bool HandleKeyDown(WXWPARAM wParam, WXLPARAM lParam);
330 bool HandleKeyUp(WXWPARAM wParam, WXLPARAM lParam);
331 #if wxUSE_ACCEL
332 bool HandleHotKey(WXWPARAM wParam, WXLPARAM lParam);
333 #endif
334 #ifdef __WIN32__
335 int HandleMenuChar(int chAccel, WXLPARAM lParam);
336 #endif
337
338 bool HandleQueryDragIcon(WXHICON *hIcon);
339
340 bool HandleSetCursor(WXHWND hWnd, short nHitTest, int mouseMsg);
341
342 // Window procedure
343 virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
344
345 // Calls an appropriate default window procedure
346 virtual WXLRESULT MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
347
348 // message processing helpers
349
350 // return false if the message shouldn't be translated/preprocessed but
351 // dispatched normally
352 virtual bool MSWShouldPreProcessMessage(WXMSG* pMsg);
353
354 // return true if the message was preprocessed and shouldn't be dispatched
355 virtual bool MSWProcessMessage(WXMSG* pMsg);
356
357 // return true if the message was translated and shouldn't be dispatched
358 virtual bool MSWTranslateMessage(WXMSG* pMsg);
359
360 // called when the window is about to be destroyed
361 virtual void MSWDestroyWindow();
362
363 // this function should return the brush to paint the window background
364 // with or 0 for the default brush
365 virtual WXHBRUSH MSWControlColor(WXHDC hDC);
366
367 // this function should return the brush to paint the children controls
368 // background or 0 if this window doesn't impose any particular background
369 // on its children
370 //
371 // the base class version uses MSWGetBgColourForChild() and returns a solid
372 // brush if we have a non default background colour or 0 otherwise
373 virtual WXHBRUSH MSWGetBgBrushForChild(WXHDC WXUNUSED(hDC), wxWindow *child)
374 {
375 return MSWGetSolidBgBrushForChild(child);
376 }
377
378 // return the background colour of this window under the given child
379 // (possible grand child)
380 //
381 // this is a hack as if the background is themed, there is no single colour
382 // representing it, but sometimes we can't use the pattern brush returned
383 // by MSWGetBgBrushForChild() anyhow and then this function is used as
384 // fallback
385 //
386 // the base class version returns bg colour if it had been explicitely set
387 // or wxNullColour otherwise
388 virtual wxColour MSWGetBgColourForChild(wxWindow *child);
389
390 // convenience function: returns a solid brush of the colour returned by
391 // MSWGetBgColourForChild() or 0
392 WXHBRUSH MSWGetSolidBgBrushForChild(wxWindow *child);
393
394 // normally just calls MSWGetBgBrushForChild() on the parent window but may
395 // be overridden if the default background brush is not suitable for some
396 // reason (e.g. wxStaticBox uses MSWGetSolidBgBrushForChild() instead)
397 virtual WXHBRUSH MSWGetBgBrushForSelf(wxWindow *parent, WXHDC hDC)
398 {
399 return parent->MSWGetBgBrushForChild(hDC, this);
400 }
401
402 // return the background brush to use for this window by quering the parent
403 // windows via their MSWGetBgBrushForChild() recursively
404 WXHBRUSH MSWGetBgBrush(WXHDC hDC);
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 // virtual function for implementing internal idle
416 // behaviour
417 virtual void OnInternalIdle() ;
418
419 protected:
420 // the window handle
421 WXHWND m_hWnd;
422
423 // the old window proc (we subclass all windows)
424 WXFARPROC m_oldWndProc;
425
426 // additional (MSW specific) flags
427 bool m_mouseInWindow:1;
428 bool m_lastKeydownProcessed:1;
429
430 // the size of one page for scrolling
431 int m_xThumbSize;
432 int m_yThumbSize;
433
434 #if wxUSE_MOUSEEVENT_HACK
435 // the coordinates of the last mouse event and the type of it
436 long m_lastMouseX,
437 m_lastMouseY;
438 int m_lastMouseEvent;
439 #endif // wxUSE_MOUSEEVENT_HACK
440
441 WXHMENU m_hMenu; // Menu, if any
442
443 // implement the base class pure virtuals
444 virtual void DoClientToScreen( int *x, int *y ) const;
445 virtual void DoScreenToClient( int *x, int *y ) const;
446 virtual void DoGetPosition( int *x, int *y ) const;
447 virtual void DoGetSize( int *width, int *height ) const;
448 virtual void DoGetClientSize( int *width, int *height ) const;
449 virtual void DoSetSize(int x, int y,
450 int width, int height,
451 int sizeFlags = wxSIZE_AUTO);
452 virtual void DoSetClientSize(int width, int height);
453
454 virtual void DoCaptureMouse();
455 virtual void DoReleaseMouse();
456
457 // has the window been frozen by Freeze()?
458 bool IsFrozen() const { return m_frozenness > 0; }
459
460 // move the window to the specified location and resize it: this is called
461 // from both DoSetSize() and DoSetClientSize() and would usually just call
462 // ::MoveWindow() except for composite controls which will want to arrange
463 // themselves inside the given rectangle
464 virtual void DoMoveWindow(int x, int y, int width, int height);
465
466 #if wxUSE_TOOLTIPS
467 virtual void DoSetToolTip( wxToolTip *tip );
468
469 // process TTN_NEEDTEXT message properly (i.e. fixing the bugs in
470 // comctl32.dll in our code -- see the function body for more info)
471 bool HandleTooltipNotify(WXUINT code,
472 WXLPARAM lParam,
473 const wxString& ttip);
474 #endif // wxUSE_TOOLTIPS
475
476 // the helper functions used by HandleChar/KeyXXX methods
477 wxKeyEvent CreateKeyEvent(wxEventType evType, int id,
478 WXLPARAM lParam = 0, WXWPARAM wParam = 0) const;
479
480 private:
481 // common part of all ctors
482 void Init();
483
484 // the (non-virtual) handlers for the events
485 bool HandleMove(int x, int y);
486 bool HandleMoving(wxRect& rect);
487 bool HandleJoystickEvent(WXUINT msg, int x, int y, WXUINT flags);
488
489 #ifdef __WIN95__
490 bool HandleNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
491 #endif // __WIN95__
492
493 // list of disabled children before last call to our Disable()
494 wxWindowList *m_childrenDisabled;
495
496 // number of calls to Freeze() minus number of calls to Thaw()
497 unsigned int m_frozenness;
498
499 DECLARE_DYNAMIC_CLASS(wxWindowMSW)
500 DECLARE_NO_COPY_CLASS(wxWindowMSW)
501 DECLARE_EVENT_TABLE()
502 };
503
504 // ----------------------------------------------------------------------------
505 // inline functions
506 // ----------------------------------------------------------------------------
507
508 #if WXWIN_COMPATIBILITY_2_4
509
510 inline bool wxWindowMSW::GetUseCtl3D() const { return false; }
511 inline bool wxWindowMSW::GetTransparentBackground() const { return false; }
512 inline void wxWindowMSW::SetTransparent(bool WXUNUSED(t)) { }
513
514 #endif // WXWIN_COMPATIBILITY_2_4
515
516 // ---------------------------------------------------------------------------
517 // global functions
518 // ---------------------------------------------------------------------------
519
520 // kbd code translation
521 WXDLLEXPORT int wxCharCodeMSWToWX(int keySym, WXLPARAM lParam = 0);
522 WXDLLEXPORT WXWORD wxCharCodeWXToMSW(int id, bool *IsVirtual);
523
524 // window creation helper class: before creating a new HWND, instantiate an
525 // object of this class on stack - this allows to process the messages sent to
526 // the window even before CreateWindow() returns
527 class wxWindowCreationHook
528 {
529 public:
530 wxWindowCreationHook(wxWindowMSW *winBeingCreated);
531 ~wxWindowCreationHook();
532 };
533
534 // ----------------------------------------------------------------------------
535 // global objects
536 // ----------------------------------------------------------------------------
537
538 // notice that this hash must be defined after wxWindow declaration as it
539 // needs to "see" its dtor and not just forward declaration
540 #include "wx/hash.h"
541
542 // pseudo-template HWND <-> wxWindow hash table
543 #if WXWIN_COMPATIBILITY_2_4
544 WX_DECLARE_HASH(wxWindow, wxWindowList, wxWinHashTable);
545 #else
546 WX_DECLARE_HASH(wxWindowMSW, wxWindowList, wxWinHashTable);
547 #endif
548
549 extern wxWinHashTable *wxWinHandleHash;
550
551 #endif
552 // _WX_WINDOW_H_