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