1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindow class
8 // Copyright: (c) AUTHOR
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"
34 * Base class for frame, panel, canvas, panel items, dialog box.
39 * Event handler: windows have themselves as their event handlers
40 * by default, but their event handlers could be set to another
41 * object entirely. This separation can reduce the amount of
42 * derivation required, and allow alteration of a window's functionality
43 * (e.g. by a resource editor that temporarily switches event handlers).
46 class WXDLLEXPORT wxWindow
;
47 class WXDLLEXPORT wxEvent
;
48 class WXDLLEXPORT wxCommandEvent
;
49 class WXDLLEXPORT wxKeyEvent
;
50 class WXDLLEXPORT wxControl
;
51 class WXDLLEXPORT wxCursor
;
52 class WXDLLEXPORT wxColourMap
;
53 class WXDLLEXPORT wxFont
;
54 class WXDLLEXPORT wxMenu
;
55 class WXDLLEXPORT wxRectangle
;
56 class WXDLLEXPORT wxBitmap
;
57 class WXDLLEXPORT wxSizer
;
58 class WXDLLEXPORT wxList
;
59 class WXDLLEXPORT wxLayoutConstraints
;
60 class WXDLLEXPORT wxMouseEvent
;
61 class WXDLLEXPORT wxButton
;
62 class WXDLLEXPORT wxColour
;
63 class WXDLLEXPORT wxBrush
;
64 class WXDLLEXPORT wxPen
;
65 class WXDLLEXPORT wxIcon
;
66 class WXDLLEXPORT wxDC
;
67 class WXDLLEXPORT wxValidator
;
69 #if wxUSE_DRAG_AND_DROP
70 class WXDLLEXPORT wxDropTarget
;
73 #if wxUSE_WX_RESOURCES
74 class WXDLLEXPORT wxResourceTable
;
75 class WXDLLEXPORT wxItemResource
;
78 WXDLLEXPORT_DATA(extern const char*) wxPanelNameStr
;
80 WXDLLEXPORT_DATA(extern const wxSize
) wxDefaultSize
;
81 WXDLLEXPORT_DATA(extern const wxPoint
) wxDefaultPosition
;
83 class WXDLLEXPORT wxWindow
: public wxEvtHandler
85 DECLARE_ABSTRACT_CLASS(wxWindow
)
88 friend class wxPaintDC
;
92 inline wxWindow(wxWindow
*parent
, wxWindowID id
,
93 const wxPoint
& pos
= wxDefaultPosition
,
94 const wxSize
& size
= wxDefaultSize
,
96 const wxString
& name
= wxPanelNameStr
)
98 m_children
= new wxList
;
99 Create(parent
, id
, pos
, size
, style
, name
);
104 bool Create(wxWindow
*parent
, wxWindowID id
,
105 const wxPoint
& pos
= wxDefaultPosition
,
106 const wxSize
& size
= wxDefaultSize
,
108 const wxString
& name
= wxPanelNameStr
);
110 // Fit the window around the items
113 // Show or hide the window
114 virtual bool Show(bool show
);
116 // Is the window shown?
117 virtual bool IsShown() const;
119 // Raise the window to the top of the Z order
120 virtual void Raise();
122 // Lower the window to the bottom of the Z order
123 virtual void Lower();
125 // Is the window enabled?
126 virtual bool IsEnabled() const;
129 inline bool Enabled() const { return IsEnabled(); }
131 // Dialog support: override these and call
132 // base class members to add functionality
133 // that can't be done using validators.
135 // Transfer values to controls. If returns FALSE,
136 // it's an application error (pops up a dialog)
137 virtual bool TransferDataToWindow();
139 // Transfer values from controls. If returns FALSE,
140 // transfer failed: don't quit
141 virtual bool TransferDataFromWindow();
143 // Validate controls. If returns FALSE,
144 // validation failed: don't quit
145 virtual bool Validate();
147 // Return code for dialogs
148 inline void SetReturnCode(int retCode
);
149 inline int GetReturnCode();
152 virtual void SetCursor(const wxCursor
& cursor
);
153 inline virtual wxCursor
*GetCursor() const { return (wxCursor
*)& m_windowCursor
; };
155 // Get the window with the focus
156 static wxWindow
*FindFocus();
158 // Get character size
159 virtual int GetCharHeight() const;
160 virtual int GetCharWidth() const;
162 // Get overall window size
163 virtual void GetSize(int *width
, int *height
) const;
165 // Get window position, relative to parent (or screen if no parent)
166 virtual void GetPosition(int *x
, int *y
) const;
168 // Get client (application-useable) size
169 virtual void GetClientSize(int *width
, int *height
) const;
171 // Set overall size and position
172 virtual void SetSize(int x
, int y
, int width
, int height
, int sizeFlags
= wxSIZE_AUTO
);
173 inline virtual void SetSize(int width
, int height
) { SetSize(-1, -1, width
, height
, wxSIZE_USE_EXISTING
); }
174 inline virtual void Move(int x
, int y
) { SetSize(x
, y
, -1, -1, wxSIZE_USE_EXISTING
); }
177 virtual void SetClientSize(int width
, int size
);
179 // Convert client to screen coordinates
180 virtual void ClientToScreen(int *x
, int *y
) const;
182 // Convert screen to client coordinates
183 virtual void ScreenToClient(int *x
, int *y
) const;
185 // Set the focus to this window
186 virtual void SetFocus();
188 // Capture/release mouse
189 virtual void CaptureMouse();
190 virtual void ReleaseMouse();
192 // Enable or disable the window
193 virtual void Enable(bool enable
);
195 #if wxUSE_DRAG_AND_DROP
196 // Associate a drop target with this window (if the window already had a drop
197 // target, it's deleted!) and return the current drop target (may be NULL).
198 void SetDropTarget(wxDropTarget
*pDropTarget
);
199 wxDropTarget
*GetDropTarget() const { return m_pDropTarget
; }
202 // Accept files for dragging
203 virtual void DragAcceptFiles(bool accept
);
205 // Update region access
206 virtual wxRegion
GetUpdateRegion() const;
207 virtual bool IsExposed(int x
, int y
, int w
, int h
) const;
208 virtual bool IsExposed(const wxPoint
& pt
) const;
209 virtual bool IsExposed(const wxRect
& rect
) const;
211 // Set/get the window title
212 virtual inline void SetTitle(const wxString
& WXUNUSED(title
)) {};
213 inline virtual wxString
GetTitle() const { return wxString(""); };
214 // Most windows have the concept of a label; for frames, this is the
215 // title; for items, this is the label or button text.
216 inline virtual wxString
GetLabel() const { return GetTitle(); }
218 // Set/get the window name (used for resource setting in X)
219 inline virtual wxString
GetName() const;
220 inline virtual void SetName(const wxString
& name
);
223 virtual void Centre(int direction
) ;
224 inline void Center(int direction
= wxHORIZONTAL
) { Centre(direction
); }
227 virtual bool PopupMenu(wxMenu
*menu
, int x
, int y
);
229 // Send the window a refresh event
230 virtual void Refresh(bool eraseBack
= TRUE
, const wxRectangle
*rect
= NULL
);
232 // New functions that will replace the above.
233 virtual void SetScrollbar(int orient
, int pos
, int thumbVisible
,
234 int range
, bool refresh
= TRUE
);
236 virtual void SetScrollPos(int orient
, int pos
, bool refresh
= TRUE
);
237 virtual int GetScrollPos(int orient
) const;
238 virtual int GetScrollRange(int orient
) const;
239 virtual int GetScrollThumb(int orient
) const;
241 virtual void ScrollWindow(int dx
, int dy
, const wxRectangle
*rect
= NULL
);
243 // Caret manipulation
244 virtual void CreateCaret(int w
, int h
);
245 virtual void CreateCaret(const wxBitmap
*bitmap
);
246 virtual void DestroyCaret();
247 virtual void ShowCaret(bool show
);
248 virtual void SetCaretPos(int x
, int y
);
249 virtual void GetCaretPos(int *x
, int *y
) const;
251 // Tell window how much it can be sized
252 virtual void SetSizeHints(int minW
= -1, int minH
= -1, int maxW
= -1, int maxH
= -1, int incW
= -1, int incH
= -1);
254 // Set/get the window's identifier
255 inline int GetId() const;
256 inline void SetId(int id
);
258 // Make the window modal (all other windows unresponsive)
259 virtual void MakeModal(bool modal
);
261 // Get the private handle (platform-dependent)
262 inline void *GetHandle() const;
264 // Set/get the window's relatives
265 inline wxWindow
*GetParent() const;
266 inline void SetParent(wxWindow
*p
) ;
267 inline wxWindow
*GetGrandParent() const;
268 inline wxList
*GetChildren() const;
270 // Set/get the window's font
271 virtual void SetFont(const wxFont
& f
);
272 inline virtual wxFont
*GetFont() const;
274 // Set/get the window's validator
275 void SetValidator(const wxValidator
& validator
);
276 inline wxValidator
*GetValidator() const;
278 // Set/get the window's style
279 inline void SetWindowStyleFlag(long flag
);
280 inline long GetWindowStyleFlag() const;
282 // Set/get event handler
283 inline void SetEventHandler(wxEvtHandler
*handler
);
284 inline wxEvtHandler
*GetEventHandler() const;
286 // Push/pop event handler (i.e. allow a chain of event handlers
288 void PushEventHandler(wxEvtHandler
*handler
) ;
289 wxEvtHandler
*PopEventHandler(bool deleteHandler
= FALSE
) ;
291 // Close the window by calling OnClose, posting a deletion
292 virtual bool Close(bool force
= FALSE
);
294 // Destroy the window (delayed, if a managed window)
295 virtual bool Destroy() ;
297 // Mode for telling default OnSize members to
298 // call Layout(), if not using Sizers, just top-down constraints
299 inline void SetAutoLayout(bool a
);
300 inline bool GetAutoLayout() const;
302 // Set/get constraints
303 inline wxLayoutConstraints
*GetConstraints() const;
304 void SetConstraints(wxLayoutConstraints
*c
);
306 // Set/get window background colour
307 inline virtual void SetBackgroundColour(const wxColour
& col
);
308 inline virtual wxColour
GetBackgroundColour() const;
310 // Set/get window foreground colour
311 inline virtual void SetForegroundColour(const wxColour
& col
);
312 inline virtual wxColour
GetForegroundColour() const;
314 // Set/get window default background colour (for children to inherit).
315 // NOTE: these may be removed in later revisions.
316 inline virtual void SetDefaultBackgroundColour(const wxColour
& col
);
317 inline virtual wxColour
GetDefaultBackgroundColour(void) const;
319 // Set/get window default foreground colour (for children to inherit)
320 inline virtual void SetDefaultForegroundColour(const wxColour
& col
);
321 inline virtual wxColour
GetDefaultForegroundColour(void) const;
323 // Get the default button, if there is one
324 inline virtual wxButton
*GetDefaultItem() const;
325 inline virtual void SetDefaultItem(wxButton
*but
);
327 // Override to define new behaviour for default action (e.g. double clicking
329 virtual void OnDefaultAction(wxControl
*initiatingItem
);
332 #if wxUSE_WX_RESOURCES
333 virtual bool LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
= NULL
);
334 virtual wxControl
*CreateItem(const wxItemResource
*childResource
, const wxResourceTable
*table
= NULL
);
337 // Native resource loading
338 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
339 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
340 virtual wxWindow
* GetWindowChild1(wxWindowID
& id
);
341 virtual wxWindow
* GetWindowChild(wxWindowID
& id
);
343 virtual void GetTextExtent(const wxString
& string
, int *x
, int *y
,
345 int *externalLeading
= NULL
,
346 const wxFont
*theFont
= NULL
, bool use16
= FALSE
) const;
348 // Is the window retained?
349 inline bool IsRetained() const;
351 // Warp the pointer the given position
352 virtual void WarpPointer(int x_pos
, int y_pos
) ;
355 virtual void Clear();
357 // Find a window by id or name
358 virtual wxWindow
*FindWindow(long id
);
359 virtual wxWindow
*FindWindow(const wxString
& name
);
361 // Constraint operations
363 void SetSizer(wxSizer
*sizer
); // Adds sizer child to this window
364 inline wxSizer
*GetSizer() const ;
365 inline wxWindow
*GetSizerParent() const ;
366 inline void SetSizerParent(wxWindow
*win
);
368 // Do Update UI processing for controls
369 void UpdateWindowUI();
371 void OnEraseBackground(wxEraseEvent
& event
);
372 void OnChar(wxKeyEvent
& event
);
373 void OnPaint(wxPaintEvent
& event
);
374 void OnIdle(wxIdleEvent
& event
);
376 // Does this window want to accept keyboard focus?
377 virtual bool AcceptsFocus() const;
380 ////////////////////////////////////////////////////////////////////////
383 // For implementation purposes - sometimes decorations make the client area
385 virtual wxPoint
GetClientAreaOrigin() const;
387 // Makes an adjustment to the window position (for example, a frame that has
388 // a toolbar that it manages itself).
389 virtual void AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
);
391 // Executes the default message
392 virtual long Default();
394 /* TODO: you may need something like this
395 // Determine whether 3D effects are wanted
396 virtual WXDWORD Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D);
399 virtual void AddChild(wxWindow
*child
); // Adds reference to the child object
400 virtual void RemoveChild(wxWindow
*child
); // Removes reference to child
401 // (but doesn't delete the child object)
402 virtual void DestroyChildren(); // Removes and destroys all children
404 // Constraint implementation
405 void UnsetConstraints(wxLayoutConstraints
*c
);
406 inline wxList
*GetConstraintsInvolvedIn() const ;
407 // Back-pointer to other windows we're involved with, so if we delete
408 // this window, we must delete any constraints we're involved with.
409 void AddConstraintReference(wxWindow
*otherWin
);
410 void RemoveConstraintReference(wxWindow
*otherWin
);
411 void DeleteRelatedConstraints();
413 virtual void ResetConstraints();
414 virtual void SetConstraintSizes(bool recurse
= TRUE
);
415 virtual bool LayoutPhase1(int *noChanges
);
416 virtual bool LayoutPhase2(int *noChanges
);
417 virtual bool DoPhase(int);
418 // Transforms from sizer coordinate space to actual
419 // parent coordinate space
420 virtual void TransformSizerToActual(int *x
, int *y
) const ;
422 // Set size with transformation to actual coordinates if nec.
423 virtual void SizerSetSize(int x
, int y
, int w
, int h
);
424 virtual void SizerMove(int x
, int y
);
426 // Only set/get the size/position of the constraint (if any)
427 virtual void SetSizeConstraint(int x
, int y
, int w
, int h
);
428 virtual void MoveConstraint(int x
, int y
);
429 virtual void GetSizeConstraint(int *w
, int *h
) const ;
430 virtual void GetClientSizeConstraint(int *w
, int *h
) const ;
431 virtual void GetPositionConstraint(int *x
, int *y
) const ;
433 wxObject
*GetChild(int number
) const ;
435 inline void SetShowing(bool show
);
436 inline bool IsUserEnabled() const;
437 inline bool GetTransparentBackground() const ;
439 // Responds to colour changes: passes event on to children.
440 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
442 // Transfers data to any child controls
443 void OnInitDialog(wxInitDialogEvent
& event
);
445 // Sends an OnInitDialog event, which in turns transfers data to
446 // to the window via validators.
447 virtual void InitDialog();
449 ////////////////////////////////////////////////////////////////////////
453 long m_windowStyle
; // Store the window's style
454 wxEvtHandler
* m_windowEventHandler
; // Usually is 'this'
455 wxLayoutConstraints
* m_constraints
; // Constraints for this window
456 wxList
* m_constraintsInvolvedIn
; // List of constraints we're involved in
457 wxSizer
* m_windowSizer
; // Window's top-level sizer (if any)
458 wxWindow
* m_sizerParent
; // Window's parent sizer (if any)
459 bool m_autoLayout
; // Whether to call Layout() in OnSize
460 wxWindow
* m_windowParent
; // Each window always knows its parent
461 wxValidator
* m_windowValidator
;
472 wxFont m_windowFont
; // Window's font
473 wxCursor m_windowCursor
; // Window's cursor
474 wxString m_windowName
; // Window name
476 wxButton
* m_defaultItem
;
478 wxColour m_backgroundColour
;
479 wxColour m_foregroundColour
;
480 wxColour m_defaultBackgroundColour
;
481 wxColour m_defaultForegroundColour
;
483 #if wxUSE_DRAG_AND_DROP
484 wxDropTarget
*m_pDropTarget
; // the current drop target or NULL
485 #endif //USE_DRAG_AND_DROP
488 wxRegion m_updateRegion
;
489 wxList
* m_children
; // Window's children
492 DECLARE_EVENT_TABLE()
495 ////////////////////////////////////////////////////////////////////////
498 inline void *wxWindow::GetHandle() const { return (void *)NULL
; }
499 inline int wxWindow::GetId() const { return m_windowId
; }
500 inline void wxWindow::SetId(int id
) { m_windowId
= id
; }
501 inline wxWindow
*wxWindow::GetParent() const { return m_windowParent
; }
502 inline void wxWindow::SetParent(wxWindow
*p
) { m_windowParent
= p
; }
503 inline wxWindow
*wxWindow::GetGrandParent() const { return (m_windowParent
? m_windowParent
->m_windowParent
: NULL
); }
504 inline wxList
*wxWindow::GetChildren() const { return m_children
; }
505 inline wxFont
*wxWindow::GetFont() const { return (wxFont
*) & m_windowFont
; }
506 inline wxString
wxWindow::GetName() const { return m_windowName
; }
507 inline void wxWindow::SetName(const wxString
& name
) { m_windowName
= name
; }
508 inline long wxWindow::GetWindowStyleFlag() const { return m_windowStyle
; }
509 inline void wxWindow::SetWindowStyleFlag(long flag
) { m_windowStyle
= flag
; }
510 inline void wxWindow::SetDoubleClick(bool flag
) { m_doubleClickAllowed
= flag
; }
511 inline bool wxWindow::GetDoubleClick() const { return m_doubleClickAllowed
; }
512 inline void wxWindow::SetEventHandler(wxEvtHandler
*handler
) { m_windowEventHandler
= handler
; }
513 inline wxEvtHandler
*wxWindow::GetEventHandler() const { return m_windowEventHandler
; }
514 inline void wxWindow::SetAutoLayout(bool a
) { m_autoLayout
= a
; }
515 inline bool wxWindow::GetAutoLayout() const { return m_autoLayout
; }
516 inline wxLayoutConstraints
*wxWindow::GetConstraints() const { return m_constraints
; }
517 inline void wxWindow::SetBackgroundColour(const wxColour
& col
) { m_backgroundColour
= col
; };
518 inline wxColour
wxWindow::GetBackgroundColour() const { return m_backgroundColour
; };
519 inline void wxWindow::SetForegroundColour(const wxColour
& col
) { m_foregroundColour
= col
; };
520 inline wxColour
wxWindow::GetForegroundColour() const { return m_foregroundColour
; };
521 inline void wxWindow::SetDefaultForegroundColour(const wxColour
& col
) { m_defaultForegroundColour
= col
; };
522 inline wxColour
wxWindow::GetDefaultForegroundColour(void) const { return m_defaultForegroundColour
; };
523 inline void wxWindow::SetDefaultBackgroundColour(const wxColour
& col
) { m_defaultBackgroundColour
= col
; };
524 inline wxColour
wxWindow::GetDefaultBackgroundColour(void) const { return m_defaultBackgroundColour
; };
526 inline wxButton
*wxWindow::GetDefaultItem() const { return m_defaultItem
; }
527 inline void wxWindow::SetDefaultItem(wxButton
*but
) { m_defaultItem
= but
; }
528 inline bool wxWindow::IsRetained() const { return ((m_windowStyle
& wxRETAINED
) == wxRETAINED
); }
530 inline void wxWindow::SetShowing(bool show
) { m_isShown
= show
; }
531 inline wxList
*wxWindow::GetConstraintsInvolvedIn() const { return m_constraintsInvolvedIn
; }
532 inline wxSizer
*wxWindow::GetSizer() const { return m_windowSizer
; }
533 inline wxWindow
*wxWindow::GetSizerParent() const { return m_sizerParent
; }
534 inline void wxWindow::SetSizerParent(wxWindow
*win
) { m_sizerParent
= win
; }
535 inline wxValidator
*wxWindow::GetValidator() const { return m_windowValidator
; }
536 inline bool wxWindow::IsUserEnabled() const { return m_winEnabled
; }
537 inline bool wxWindow::GetTransparentBackground() const { return m_backgroundTransparent
; }
538 inline void wxWindow::SetReturnCode(int retCode
) { m_returnCode
= retCode
; }
539 inline int wxWindow::GetReturnCode() { return m_returnCode
; }
541 // Get the active window.
542 wxWindow
* WXDLLEXPORT
wxGetActiveWindow();
544 WXDLLEXPORT_DATA(extern wxList
) wxTopLevelWindows
;