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