1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindow class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
16 #pragma interface "window.h"
19 #include "wx/gdicmn.h"
21 #include "wx/cursor.h"
24 #include "wx/validate.h"
26 #include "wx/string.h"
28 #include "wx/region.h"
35 * Base class for frame, panel, canvas, panel items, dialog box.
40 * Event handler: windows have themselves as their event handlers
41 * by default, but their event handlers could be set to another
42 * object entirely. This separation can reduce the amount of
43 * derivation required, and allow alteration of a window's functionality
44 * (e.g. by a resource editor that temporarily switches event handlers).
47 class WXDLLEXPORT wxWindow
;
48 class WXDLLEXPORT wxEvent
;
49 class WXDLLEXPORT wxCommandEvent
;
50 class WXDLLEXPORT wxKeyEvent
;
51 class WXDLLEXPORT wxControl
;
52 class WXDLLEXPORT wxCursor
;
53 class WXDLLEXPORT wxColourMap
;
54 class WXDLLEXPORT wxFont
;
55 class WXDLLEXPORT wxMenu
;
56 class WXDLLEXPORT wxRectangle
;
57 class WXDLLEXPORT wxBitmap
;
58 class WXDLLEXPORT wxSizer
;
59 class WXDLLEXPORT wxList
;
60 class WXDLLEXPORT wxLayoutConstraints
;
61 class WXDLLEXPORT wxMouseEvent
;
62 class WXDLLEXPORT wxButton
;
63 class WXDLLEXPORT wxColour
;
64 class WXDLLEXPORT wxBrush
;
65 class WXDLLEXPORT wxPen
;
66 class WXDLLEXPORT wxIcon
;
67 class WXDLLEXPORT wxDC
;
68 class WXDLLEXPORT wxValidator
;
71 class WXDLLEXPORT wxDropTarget
;
75 class WXDLLEXPORT wxResourceTable
;
76 class WXDLLEXPORT wxItemResource
;
79 WXDLLEXPORT_DATA(extern const char*) wxPanelNameStr
;
81 WXDLLEXPORT_DATA(extern const wxSize
) wxDefaultSize
;
82 WXDLLEXPORT_DATA(extern const wxPoint
) wxDefaultPosition
;
84 class WXDLLEXPORT wxWindow
: public wxEvtHandler
86 DECLARE_ABSTRACT_CLASS(wxWindow
)
89 friend class wxPaintDC
;
93 inline wxWindow(wxWindow
*parent
, wxWindowID id
,
94 const wxPoint
& pos
= wxDefaultPosition
,
95 const wxSize
& size
= wxDefaultSize
,
97 const wxString
& name
= wxPanelNameStr
)
99 m_children
= new wxList
;
100 Create(parent
, id
, pos
, size
, style
, name
);
105 bool Create(wxWindow
*parent
, wxWindowID id
,
106 const wxPoint
& pos
= wxDefaultPosition
,
107 const wxSize
& size
= wxDefaultSize
,
109 const wxString
& name
= wxPanelNameStr
);
111 // Fit the window around the items
114 // Show or hide the window
115 virtual bool Show(bool show
);
117 // Is the window shown?
118 virtual bool IsShown() const;
120 // Raise the window to the top of the Z order
121 virtual void Raise();
123 // Lower the window to the bottom of the Z order
124 virtual void Lower();
126 // Is the window enabled?
127 virtual bool IsEnabled() const;
130 inline bool Enabled() const { return IsEnabled(); }
132 // Dialog support: override these and call
133 // base class members to add functionality
134 // that can't be done using validators.
136 // Transfer values to controls. If returns FALSE,
137 // it's an application error (pops up a dialog)
138 virtual bool TransferDataToWindow();
140 // Transfer values from controls. If returns FALSE,
141 // transfer failed: don't quit
142 virtual bool TransferDataFromWindow();
144 // Validate controls. If returns FALSE,
145 // validation failed: don't quit
146 virtual bool Validate();
148 // Return code for dialogs
149 inline void SetReturnCode(int retCode
);
150 inline int GetReturnCode();
153 virtual void SetCursor(const wxCursor
& cursor
);
154 inline virtual wxCursor
*GetCursor() const { return (wxCursor
*)& m_windowCursor
; };
156 // Get the window with the focus
157 static wxWindow
*FindFocus();
159 // Get character size
160 virtual int GetCharHeight() const;
161 virtual int GetCharWidth() const;
163 // Get overall window size
164 virtual void GetSize(int *width
, int *height
) const;
166 // Get window position, relative to parent (or screen if no parent)
167 virtual void GetPosition(int *x
, int *y
) const;
169 // Get client (application-useable) size
170 virtual void GetClientSize(int *width
, int *height
) const;
172 // Set overall size and position
173 virtual void SetSize(int x
, int y
, int width
, int height
, int sizeFlags
= wxSIZE_AUTO
);
174 inline virtual void SetSize(int width
, int height
) { SetSize(-1, -1, width
, height
, wxSIZE_USE_EXISTING
); }
175 inline virtual void Move(int x
, int y
) { SetSize(x
, y
, -1, -1, wxSIZE_USE_EXISTING
); }
178 virtual void SetClientSize(int width
, int size
);
180 // Convert client to screen coordinates
181 virtual void ClientToScreen(int *x
, int *y
) const;
183 // Convert screen to client coordinates
184 virtual void ScreenToClient(int *x
, int *y
) const;
186 // Set the focus to this window
187 virtual void SetFocus();
189 // Capture/release mouse
190 virtual void CaptureMouse();
191 virtual void ReleaseMouse();
193 // Enable or disable the window
194 virtual void Enable(bool enable
);
196 #if USE_DRAG_AND_DROP
197 // Associate a drop target with this window (if the window already had a drop
198 // target, it's deleted!) and return the current drop target (may be NULL).
199 void SetDropTarget(wxDropTarget
*pDropTarget
);
200 wxDropTarget
*GetDropTarget() const { return m_pDropTarget
; }
203 // Accept files for dragging
204 virtual void DragAcceptFiles(bool accept
);
206 // Update region access
207 virtual wxRegion
GetUpdateRegion() const;
208 virtual bool IsExposed(int x
, int y
, int w
, int h
) const;
209 virtual bool IsExposed(const wxPoint
& pt
) const;
210 virtual bool IsExposed(const wxRect
& rect
) const;
212 // Set/get the window title
213 virtual inline void SetTitle(const wxString
& WXUNUSED(title
)) {};
214 inline virtual wxString
GetTitle() const { return wxString(""); };
215 // Most windows have the concept of a label; for frames, this is the
216 // title; for items, this is the label or button text.
217 inline virtual wxString
GetLabel() const { return GetTitle(); }
219 // Set/get the window name (used for resource setting in X)
220 inline virtual wxString
GetName() const;
221 inline virtual void SetName(const wxString
& name
);
224 virtual void Centre(int direction
) ;
225 inline void Center(int direction
= wxHORIZONTAL
) { Centre(direction
); }
228 virtual bool PopupMenu(wxMenu
*menu
, int x
, int y
);
230 // Send the window a refresh event
231 virtual void Refresh(bool eraseBack
= TRUE
, const wxRectangle
*rect
= NULL
);
233 // New functions that will replace the above.
234 virtual void SetScrollbar(int orient
, int pos
, int thumbVisible
,
235 int range
, bool refresh
= TRUE
);
237 virtual void SetScrollPos(int orient
, int pos
, bool refresh
= TRUE
);
238 virtual int GetScrollPos(int orient
) const;
239 virtual int GetScrollRange(int orient
) const;
240 virtual int GetScrollThumb(int orient
) const;
242 virtual void ScrollWindow(int dx
, int dy
, const wxRectangle
*rect
= NULL
);
244 // Caret manipulation
245 virtual void CreateCaret(int w
, int h
);
246 virtual void CreateCaret(const wxBitmap
*bitmap
);
247 virtual void DestroyCaret();
248 virtual void ShowCaret(bool show
);
249 virtual void SetCaretPos(int x
, int y
);
250 virtual void GetCaretPos(int *x
, int *y
) const;
252 // Tell window how much it can be sized
253 virtual void SetSizeHints(int minW
= -1, int minH
= -1, int maxW
= -1, int maxH
= -1, int incW
= -1, int incH
= -1);
255 // Set/get the window's identifier
256 inline int GetId() const;
257 inline void SetId(int id
);
259 virtual void SetAcceleratorTable(const wxAcceleratorTable
& accel
);
260 inline virtual wxAcceleratorTable
& GetAcceleratorTable() const { return (wxAcceleratorTable
&) m_acceleratorTable
; }
262 // Make the window modal (all other windows unresponsive)
263 virtual void MakeModal(bool modal
);
265 // Get the private handle (platform-dependent)
266 inline void *GetHandle() const;
268 // Set/get the window's relatives
269 inline wxWindow
*GetParent() const;
270 inline void SetParent(wxWindow
*p
) ;
271 inline wxWindow
*GetGrandParent() const;
272 inline wxList
*GetChildren() const;
274 // Set/get the window's font
275 virtual void SetFont(const wxFont
& f
);
276 inline virtual wxFont
*GetFont() const;
278 // Set/get the window's validator
279 void SetValidator(const wxValidator
& validator
);
280 inline wxValidator
*GetValidator() const;
282 // Set/get the window's style
283 inline void SetWindowStyleFlag(long flag
);
284 inline long GetWindowStyleFlag() const;
286 // Set/get event handler
287 inline void SetEventHandler(wxEvtHandler
*handler
);
288 inline wxEvtHandler
*GetEventHandler() const;
290 // Push/pop event handler (i.e. allow a chain of event handlers
292 void PushEventHandler(wxEvtHandler
*handler
) ;
293 wxEvtHandler
*PopEventHandler(bool deleteHandler
= FALSE
) ;
295 // Close the window by calling OnClose, posting a deletion
296 virtual bool Close(bool force
= FALSE
);
298 // Destroy the window (delayed, if a managed window)
299 virtual bool Destroy() ;
301 // Mode for telling default OnSize members to
302 // call Layout(), if not using Sizers, just top-down constraints
303 inline void SetAutoLayout(bool a
);
304 inline bool GetAutoLayout() const;
306 // Set/get constraints
307 inline wxLayoutConstraints
*GetConstraints() const;
308 void SetConstraints(wxLayoutConstraints
*c
);
310 // Set/get window background colour
311 inline virtual void SetBackgroundColour(const wxColour
& col
);
312 inline virtual wxColour
GetBackgroundColour() const;
314 // Set/get window foreground colour
315 inline virtual void SetForegroundColour(const wxColour
& col
);
316 inline virtual wxColour
GetForegroundColour() const;
318 // Set/get window default background colour (for children to inherit).
319 // NOTE: these may be removed in later revisions.
320 inline virtual void SetDefaultBackgroundColour(const wxColour
& col
);
321 inline virtual wxColour
GetDefaultBackgroundColour(void) const;
323 // Set/get window default foreground colour (for children to inherit)
324 inline virtual void SetDefaultForegroundColour(const wxColour
& col
);
325 inline virtual wxColour
GetDefaultForegroundColour(void) const;
327 // Get the default button, if there is one
328 inline virtual wxButton
*GetDefaultItem() const;
329 inline virtual void SetDefaultItem(wxButton
*but
);
331 // Override to define new behaviour for default action (e.g. double clicking
333 virtual void OnDefaultAction(wxControl
*initiatingItem
);
337 virtual bool LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
= NULL
);
338 virtual wxControl
*CreateItem(const wxItemResource
*childResource
, const wxResourceTable
*table
= NULL
);
341 virtual void GetTextExtent(const wxString
& string
, int *x
, int *y
,
343 int *externalLeading
= NULL
,
344 const wxFont
*theFont
= NULL
, bool use16
= FALSE
) const;
346 // Is the window retained?
347 inline bool IsRetained() const;
349 // Warp the pointer the given position
350 virtual void WarpPointer(int x_pos
, int y_pos
) ;
353 virtual void Clear();
355 // Find a window by id or name
356 virtual wxWindow
*FindWindow(long id
);
357 virtual wxWindow
*FindWindow(const wxString
& name
);
359 // Constraint operations
361 void SetSizer(wxSizer
*sizer
); // Adds sizer child to this window
362 inline wxSizer
*GetSizer() const ;
363 inline wxWindow
*GetSizerParent() const ;
364 inline void SetSizerParent(wxWindow
*win
);
366 // Do Update UI processing for controls
367 void UpdateWindowUI();
369 void OnEraseBackground(wxEraseEvent
& event
);
370 void OnChar(wxKeyEvent
& event
);
371 void OnPaint(wxPaintEvent
& event
);
372 void OnIdle(wxIdleEvent
& event
);
374 // Does this window want to accept keyboard focus?
375 virtual bool AcceptsFocus() const;
377 virtual void PrepareDC( wxDC
&dc
) {};
381 ////////////////////////////////////////////////////////////////////////
384 // For implementation purposes - sometimes decorations make the client area
386 virtual wxPoint
GetClientAreaOrigin() const;
388 // Makes an adjustment to the window position (for example, a frame that has
389 // a toolbar that it manages itself).
390 virtual void AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
);
392 // Executes the default message
393 virtual long Default();
395 /* TODO: you may need something like this
396 // Determine whether 3D effects are wanted
397 virtual WXDWORD Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D);
400 virtual void AddChild(wxWindow
*child
); // Adds reference to the child object
401 virtual void RemoveChild(wxWindow
*child
); // Removes reference to child
402 // (but doesn't delete the child object)
403 virtual void DestroyChildren(); // Removes and destroys all children
405 inline bool IsBeingDeleted() const { return FALSE
; } // TODO: Should probably eliminate this
407 // Constraint implementation
408 void UnsetConstraints(wxLayoutConstraints
*c
);
409 inline wxList
*GetConstraintsInvolvedIn() const ;
410 // Back-pointer to other windows we're involved with, so if we delete
411 // this window, we must delete any constraints we're involved with.
412 void AddConstraintReference(wxWindow
*otherWin
);
413 void RemoveConstraintReference(wxWindow
*otherWin
);
414 void DeleteRelatedConstraints();
416 virtual void ResetConstraints();
417 virtual void SetConstraintSizes(bool recurse
= TRUE
);
418 virtual bool LayoutPhase1(int *noChanges
);
419 virtual bool LayoutPhase2(int *noChanges
);
420 virtual bool DoPhase(int);
421 // Transforms from sizer coordinate space to actual
422 // parent coordinate space
423 virtual void TransformSizerToActual(int *x
, int *y
) const ;
425 // Set size with transformation to actual coordinates if nec.
426 virtual void SizerSetSize(int x
, int y
, int w
, int h
);
427 virtual void SizerMove(int x
, int y
);
429 // Only set/get the size/position of the constraint (if any)
430 virtual void SetSizeConstraint(int x
, int y
, int w
, int h
);
431 virtual void MoveConstraint(int x
, int y
);
432 virtual void GetSizeConstraint(int *w
, int *h
) const ;
433 virtual void GetClientSizeConstraint(int *w
, int *h
) const ;
434 virtual void GetPositionConstraint(int *x
, int *y
) const ;
436 wxObject
*GetChild(int number
) const ;
438 // Generates a new id for controls
439 static int NewControlId();
441 // Responds to colour changes: passes event on to children.
442 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
444 // Transfers data to any child controls
445 void OnInitDialog(wxInitDialogEvent
& event
);
447 // Sends an OnInitDialog event, which in turns transfers data to
448 // to the window via validators.
449 virtual void InitDialog();
453 void CanvasGetSize(int* width
, int* height
) const; // If have drawing area
454 void CanvasGetClientSize(int *width
, int *height
) const;
455 void CanvasGetPosition(int *x
, int *y
) const; // If have drawing area
456 void CanvasSetClientSize(int width
, int size
);
457 void CanvasSetSize(int x
, int y
, int width
, int height
, int sizeFlags
= wxSIZE_AUTO
);
459 // Gives window a chance to do something in response to a size
460 // message, e.g. arrange status bar, toolbar etc.
461 virtual bool PreResize() { return TRUE
; }
463 // Get main widget for this window
464 virtual WXWidget
GetMainWidget() const;
465 // Get the client widget for this window (something we can
466 // create other windows on)
467 virtual WXWidget
GetClientWidget() const;
468 virtual void SetMainWidget(WXWidget w
) { m_mainWidget
= w
; }
470 // Get the underlying X window and display
471 virtual WXWindow
GetXWindow() const;
472 virtual WXDisplay
*GetXDisplay() const;
474 // Generates a paint event
475 virtual void DoPaint();
477 ////////////////////////////////////////////////////////////////////////
481 long m_windowStyle
; // Store the window's style
482 wxEvtHandler
* m_windowEventHandler
; // Usually is 'this'
483 wxLayoutConstraints
* m_constraints
; // Constraints for this window
484 wxList
* m_constraintsInvolvedIn
; // List of constraints we're involved in
485 wxSizer
* m_windowSizer
; // Window's top-level sizer (if any)
486 wxWindow
* m_sizerParent
; // Window's parent sizer (if any)
487 bool m_autoLayout
; // Whether to call Layout() in OnSize
488 wxWindow
* m_windowParent
; // Each window always knows its parent
489 wxValidator
* m_windowValidator
;
500 wxFont m_windowFont
; // Window's font
501 wxCursor m_windowCursor
; // Window's cursor
502 wxString m_windowName
; // Window name
504 wxButton
* m_defaultItem
;
506 wxColour m_backgroundColour
;
507 wxColour m_foregroundColour
;
508 wxColour m_defaultBackgroundColour
;
509 wxColour m_defaultForegroundColour
;
510 wxAcceleratorTable m_acceleratorTable
;
512 #if USE_DRAG_AND_DROP
513 wxDropTarget
*m_pDropTarget
; // the current drop target or NULL
514 #endif //USE_DRAG_AND_DROP
517 wxRegion m_updateRegion
;
518 wxList
* m_children
; // Window's children
523 bool m_button1Pressed
;
524 bool m_button2Pressed
;
525 bool m_button3Pressed
;
526 // For double-click detection
527 long m_lastTS
; // last timestamp
528 int m_lastButton
; // last pressed button
529 wxList m_updateRects
; // List of wxRectangles representing damaged region
531 WXWidget m_mainWidget
;
532 WXWidget m_hScrollBar
;
533 WXWidget m_vScrollBar
;
534 WXWidget m_borderWidget
;
535 WXWidget m_scrolledWindow
;
536 WXWidget m_drawingArea
;
541 bool m_hScrollingEnabled
;
542 bool m_vScrollingEnabled
;
543 WXPixmap m_backingPixmap
;
549 DECLARE_EVENT_TABLE()
552 ////////////////////////////////////////////////////////////////////////
555 inline void *wxWindow::GetHandle() const { return (void *)NULL
; }
556 inline int wxWindow::GetId() const { return m_windowId
; }
557 inline void wxWindow::SetId(int id
) { m_windowId
= id
; }
558 inline wxWindow
*wxWindow::GetParent() const { return m_windowParent
; }
559 inline void wxWindow::SetParent(wxWindow
*p
) { m_windowParent
= p
; }
560 inline wxWindow
*wxWindow::GetGrandParent() const { return (m_windowParent
? m_windowParent
->m_windowParent
: (wxWindow
*) NULL
); }
561 inline wxList
*wxWindow::GetChildren() const { return m_children
; }
562 inline wxFont
*wxWindow::GetFont() const { return (wxFont
*) & m_windowFont
; }
563 inline wxString
wxWindow::GetName() const { return m_windowName
; }
564 inline void wxWindow::SetName(const wxString
& name
) { m_windowName
= name
; }
565 inline long wxWindow::GetWindowStyleFlag() const { return m_windowStyle
; }
566 inline void wxWindow::SetWindowStyleFlag(long flag
) { m_windowStyle
= flag
; }
567 inline void wxWindow::SetEventHandler(wxEvtHandler
*handler
) { m_windowEventHandler
= handler
; }
568 inline wxEvtHandler
*wxWindow::GetEventHandler() const { return m_windowEventHandler
; }
569 inline void wxWindow::SetAutoLayout(bool a
) { m_autoLayout
= a
; }
570 inline bool wxWindow::GetAutoLayout() const { return m_autoLayout
; }
571 inline wxLayoutConstraints
*wxWindow::GetConstraints() const { return m_constraints
; }
572 inline void wxWindow::SetBackgroundColour(const wxColour
& col
) { m_backgroundColour
= col
; };
573 inline wxColour
wxWindow::GetBackgroundColour() const { return m_backgroundColour
; };
574 inline void wxWindow::SetForegroundColour(const wxColour
& col
) { m_foregroundColour
= col
; };
575 inline wxColour
wxWindow::GetForegroundColour() const { return m_foregroundColour
; };
576 inline void wxWindow::SetDefaultForegroundColour(const wxColour
& col
) { m_defaultForegroundColour
= col
; };
577 inline wxColour
wxWindow::GetDefaultForegroundColour(void) const { return m_defaultForegroundColour
; };
578 inline void wxWindow::SetDefaultBackgroundColour(const wxColour
& col
) { m_defaultBackgroundColour
= col
; };
579 inline wxColour
wxWindow::GetDefaultBackgroundColour(void) const { return m_defaultBackgroundColour
; };
581 inline wxButton
*wxWindow::GetDefaultItem() const { return m_defaultItem
; }
582 inline void wxWindow::SetDefaultItem(wxButton
*but
) { m_defaultItem
= but
; }
583 inline bool wxWindow::IsRetained() const { return ((m_windowStyle
& wxRETAINED
) == wxRETAINED
); }
585 inline wxList
*wxWindow::GetConstraintsInvolvedIn() const { return m_constraintsInvolvedIn
; }
586 inline wxSizer
*wxWindow::GetSizer() const { return m_windowSizer
; }
587 inline wxWindow
*wxWindow::GetSizerParent() const { return m_sizerParent
; }
588 inline void wxWindow::SetSizerParent(wxWindow
*win
) { m_sizerParent
= win
; }
589 inline wxValidator
*wxWindow::GetValidator() const { return m_windowValidator
; }
590 inline void wxWindow::SetReturnCode(int retCode
) { m_returnCode
= retCode
; }
591 inline int wxWindow::GetReturnCode() { return m_returnCode
; }
593 // Get the active window.
594 wxWindow
* WXDLLEXPORT
wxGetActiveWindow();
596 WXDLLEXPORT_DATA(extern wxList
) wxTopLevelWindows
;