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"
36 * Base class for frame, panel, canvas, panel items, dialog box.
41 * Event handler: windows have themselves as their event handlers
42 * by default, but their event handlers could be set to another
43 * object entirely. This separation can reduce the amount of
44 * derivation required, and allow alteration of a window's functionality
45 * (e.g. by a resource editor that temporarily switches event handlers).
48 class WXDLLEXPORT wxWindow
;
49 class WXDLLEXPORT wxEvent
;
50 class WXDLLEXPORT wxCommandEvent
;
51 class WXDLLEXPORT wxKeyEvent
;
52 class WXDLLEXPORT wxControl
;
53 class WXDLLEXPORT wxCursor
;
54 class WXDLLEXPORT wxColourMap
;
55 class WXDLLEXPORT wxFont
;
56 class WXDLLEXPORT wxMenu
;
57 class WXDLLEXPORT wxRect
;
58 class WXDLLEXPORT wxBitmap
;
59 class WXDLLEXPORT wxSizer
;
60 class WXDLLEXPORT wxList
;
61 class WXDLLEXPORT wxLayoutConstraints
;
62 class WXDLLEXPORT wxMouseEvent
;
63 class WXDLLEXPORT wxButton
;
64 class WXDLLEXPORT wxColour
;
65 class WXDLLEXPORT wxBrush
;
66 class WXDLLEXPORT wxPen
;
67 class WXDLLEXPORT wxIcon
;
68 class WXDLLEXPORT wxDC
;
69 class WXDLLEXPORT wxValidator
;
71 #if wxUSE_DRAG_AND_DROP
72 class WXDLLEXPORT wxDropTarget
;
75 #if wxUSE_WX_RESOURCES
76 class WXDLLEXPORT wxResourceTable
;
77 class WXDLLEXPORT wxItemResource
;
80 WXDLLEXPORT_DATA(extern const char*) wxPanelNameStr
;
82 WXDLLEXPORT_DATA(extern const wxSize
) wxDefaultSize
;
83 WXDLLEXPORT_DATA(extern const wxPoint
) wxDefaultPosition
;
85 //-----------------------------------------------------------------------------
87 //-----------------------------------------------------------------------------
93 virtual ~wxClientData() { }
96 //-----------------------------------------------------------------------------
98 //-----------------------------------------------------------------------------
100 class wxStringClientData
: public wxClientData
103 wxStringClientData() { }
104 wxStringClientData( wxString
&data
) { m_data
= data
; }
105 void SetData( wxString
&data
) { m_data
= data
; }
106 wxString
GetData() const { return m_data
; }
112 class WXDLLEXPORT wxWindow
: public wxEvtHandler
114 DECLARE_ABSTRACT_CLASS(wxWindow
)
117 friend class wxPaintDC
;
121 inline wxWindow(wxWindow
*parent
, wxWindowID id
,
122 const wxPoint
& pos
= wxDefaultPosition
,
123 const wxSize
& size
= wxDefaultSize
,
125 const wxString
& name
= wxPanelNameStr
)
127 m_children
= new wxList
;
128 Create(parent
, id
, pos
, size
, style
, name
);
133 bool Create(wxWindow
*parent
, wxWindowID id
,
134 const wxPoint
& pos
= wxDefaultPosition
,
135 const wxSize
& size
= wxDefaultSize
,
137 const wxString
& name
= wxPanelNameStr
);
139 // Fit the window around the items
142 // Show or hide the window
143 virtual bool Show(bool show
);
145 // Is the window shown?
146 virtual bool IsShown() const;
148 // Raise the window to the top of the Z order
149 virtual void Raise();
151 // Lower the window to the bottom of the Z order
152 virtual void Lower();
154 // Is the window enabled?
155 virtual bool IsEnabled() const;
158 inline bool Enabled() const { return IsEnabled(); }
160 // Dialog support: override these and call
161 // base class members to add functionality
162 // that can't be done using validators.
164 // Transfer values to controls. If returns FALSE,
165 // it's an application error (pops up a dialog)
166 virtual bool TransferDataToWindow();
168 // Transfer values from controls. If returns FALSE,
169 // transfer failed: don't quit
170 virtual bool TransferDataFromWindow();
172 // Validate controls. If returns FALSE,
173 // validation failed: don't quit
174 virtual bool Validate();
176 // Return code for dialogs
177 inline void SetReturnCode(int retCode
);
178 inline int GetReturnCode();
181 virtual void SetCursor(const wxCursor
& cursor
);
182 inline virtual wxCursor
*GetCursor() const { return (wxCursor
*)& m_windowCursor
; };
184 // Get the window with the focus
185 static wxWindow
*FindFocus();
187 // Get character size
188 virtual int GetCharHeight() const;
189 virtual int GetCharWidth() const;
191 // Get overall window size
192 virtual void GetSize(int *width
, int *height
) const;
194 // Get window position, relative to parent (or screen if no parent)
195 virtual void GetPosition(int *x
, int *y
) const;
197 // Get client (application-useable) size
198 virtual void GetClientSize(int *width
, int *height
) const;
200 // Set overall size and position
201 virtual void SetSize(int x
, int y
, int width
, int height
, int sizeFlags
= wxSIZE_AUTO
);
202 inline virtual void SetSize(int width
, int height
) { SetSize(-1, -1, width
, height
, wxSIZE_USE_EXISTING
); }
203 inline virtual void Move(int x
, int y
) { SetSize(x
, y
, -1, -1, wxSIZE_USE_EXISTING
); }
206 virtual void SetClientSize(int width
, int size
);
208 // Convert client to screen coordinates
209 virtual void ClientToScreen(int *x
, int *y
) const;
211 // Convert screen to client coordinates
212 virtual void ScreenToClient(int *x
, int *y
) const;
214 // Set the focus to this window
215 virtual void SetFocus();
217 // Capture/release mouse
218 virtual void CaptureMouse();
219 virtual void ReleaseMouse();
221 // Enable or disable the window
222 virtual void Enable(bool enable
);
224 #if wxUSE_DRAG_AND_DROP
225 // Associate a drop target with this window (if the window already had a drop
226 // target, it's deleted!) and return the current drop target (may be NULL).
227 void SetDropTarget(wxDropTarget
*pDropTarget
);
228 wxDropTarget
*GetDropTarget() const { return m_pDropTarget
; }
231 // Accept files for dragging
232 virtual void DragAcceptFiles(bool accept
);
235 // create a tooltip with this text
236 void SetToolTip(const wxString
& tip
);
240 // pointer may be NULL to remove the tooltip
241 void SetToolTip(wxToolTip
*tooltip
);
242 // get the current tooltip (may return NULL if none)
243 wxToolTip
* GetToolTip() const { return m_tooltip
; }
246 // Update region access
247 virtual wxRegion
GetUpdateRegion() const;
248 virtual bool IsExposed(int x
, int y
, int w
, int h
) const;
249 virtual bool IsExposed(const wxPoint
& pt
) const;
250 virtual bool IsExposed(const wxRect
& rect
) const;
252 // Set/get the window title
253 virtual inline void SetTitle(const wxString
& WXUNUSED(title
)) {};
254 inline virtual wxString
GetTitle() const { return wxString(""); };
255 // Most windows have the concept of a label; for frames, this is the
256 // title; for items, this is the label or button text.
257 inline virtual wxString
GetLabel() const { return GetTitle(); }
259 // Set/get the window name (used for resource setting in X)
260 inline virtual wxString
GetName() const;
261 inline virtual void SetName(const wxString
& name
);
264 virtual void Centre(int direction
) ;
265 inline void Center(int direction
= wxHORIZONTAL
) { Centre(direction
); }
268 virtual bool PopupMenu(wxMenu
*menu
, int x
, int y
);
270 // Send the window a refresh event
271 virtual void Refresh(bool eraseBack
= TRUE
, const wxRect
*rect
= NULL
);
273 // New functions that will replace the above.
274 virtual void SetScrollbar(int orient
, int pos
, int thumbVisible
,
275 int range
, bool refresh
= TRUE
);
277 virtual void SetScrollPos(int orient
, int pos
, bool refresh
= TRUE
);
278 virtual int GetScrollPos(int orient
) const;
279 virtual int GetScrollRange(int orient
) const;
280 virtual int GetScrollThumb(int orient
) const;
282 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
284 // Caret manipulation
285 virtual void CreateCaret(int w
, int h
);
286 virtual void CreateCaret(const wxBitmap
*bitmap
);
287 virtual void DestroyCaret();
288 virtual void ShowCaret(bool show
);
289 virtual void SetCaretPos(int x
, int y
);
290 virtual void GetCaretPos(int *x
, int *y
) const;
292 // Tell window how much it can be sized
293 virtual void SetSizeHints(int minW
= -1, int minH
= -1, int maxW
= -1, int maxH
= -1, int incW
= -1, int incH
= -1);
295 // Set/get the window's identifier
296 inline int GetId() const;
297 inline void SetId(int id
);
299 virtual void SetAcceleratorTable(const wxAcceleratorTable
& accel
);
300 inline virtual wxAcceleratorTable
& GetAcceleratorTable() const { return (wxAcceleratorTable
&) m_acceleratorTable
; }
302 // Make the window modal (all other windows unresponsive)
303 virtual void MakeModal(bool modal
);
305 // Get the private handle (platform-dependent)
306 inline void *GetHandle() const;
308 // Set/get the window's relatives
309 inline wxWindow
*GetParent() const;
310 inline void SetParent(wxWindow
*p
) ;
311 inline wxWindow
*GetGrandParent() const;
312 inline wxList
& GetChildren() const;
314 // Set/get the window's font
315 virtual void SetFont(const wxFont
& f
);
316 inline virtual wxFont
& GetFont() const;
318 // Set/get the window's validator
319 void SetValidator(const wxValidator
& validator
);
320 inline wxValidator
*GetValidator() const;
322 // Set/get the window's style
323 inline void SetWindowStyleFlag(long flag
);
324 inline long GetWindowStyleFlag() const;
326 // Handle a control command
327 virtual void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
329 // Set/get event handler
330 inline void SetEventHandler(wxEvtHandler
*handler
);
331 inline wxEvtHandler
*GetEventHandler() const;
333 // Push/pop event handler (i.e. allow a chain of event handlers
335 void PushEventHandler(wxEvtHandler
*handler
) ;
336 wxEvtHandler
*PopEventHandler(bool deleteHandler
= FALSE
) ;
338 // Close the window by calling OnClose, posting a deletion
339 virtual bool Close(bool force
= FALSE
);
341 // Destroy the window (delayed, if a managed window)
342 virtual bool Destroy() ;
344 // Mode for telling default OnSize members to
345 // call Layout(), if not using Sizers, just top-down constraints
346 inline void SetAutoLayout(bool a
);
347 inline bool GetAutoLayout() const;
349 // Set/get constraints
350 inline wxLayoutConstraints
*GetConstraints() const;
351 void SetConstraints(wxLayoutConstraints
*c
);
353 // Set/get window background colour
354 inline virtual void SetBackgroundColour(const wxColour
& col
);
355 inline virtual wxColour
GetBackgroundColour() const;
357 // Set/get window foreground colour
358 inline virtual void SetForegroundColour(const wxColour
& col
);
359 inline virtual wxColour
GetForegroundColour() const;
361 // Get the default button, if there is one
362 inline virtual wxButton
*GetDefaultItem() const;
363 inline virtual void SetDefaultItem(wxButton
*but
);
365 // Override to define new behaviour for default action (e.g. double clicking
367 virtual void OnDefaultAction(wxControl
*initiatingItem
);
370 #if wxUSE_WX_RESOURCES
371 virtual bool LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
= NULL
);
372 virtual wxControl
*CreateItem(const wxItemResource
* childResource
, const wxItemResource
* parentResource
,
373 const wxResourceTable
*table
= (const wxResourceTable
*) NULL
);
376 virtual void GetTextExtent(const wxString
& string
, int *x
, int *y
,
378 int *externalLeading
= NULL
,
379 const wxFont
*theFont
= NULL
, bool use16
= FALSE
) const;
381 // Is the window retained?
382 inline bool IsRetained() const;
384 // Warp the pointer the given position
385 virtual void WarpPointer(int x_pos
, int y_pos
) ;
388 virtual void Clear();
390 // Find a window by id or name
391 virtual wxWindow
*FindWindow(long id
);
392 virtual wxWindow
*FindWindow(const wxString
& name
);
394 // Constraint operations
396 void SetSizer(wxSizer
*sizer
); // Adds sizer child to this window
397 inline wxSizer
*GetSizer() const ;
398 inline wxWindow
*GetSizerParent() const ;
399 inline void SetSizerParent(wxWindow
*win
);
401 // Do Update UI processing for controls
402 void UpdateWindowUI();
404 void OnEraseBackground(wxEraseEvent
& event
);
405 void OnChar(wxKeyEvent
& event
);
406 void OnKeyDown(wxKeyEvent
& event
);
407 void OnKeyUp(wxKeyEvent
& event
);
408 void OnPaint(wxPaintEvent
& event
);
409 void OnIdle(wxIdleEvent
& event
);
411 // Does this window want to accept keyboard focus?
412 virtual bool AcceptsFocus() const;
414 virtual void PrepareDC( wxDC
&dc
) {};
418 ////////////////////////////////////////////////////////////////////////
421 // For implementation purposes - sometimes decorations make the client area
423 virtual wxPoint
GetClientAreaOrigin() const;
425 // Makes an adjustment to the window position (for example, a frame that has
426 // a toolbar that it manages itself).
427 virtual void AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
);
429 // Executes the default message
430 virtual long Default();
432 /* TODO: you may need something like this
433 // Determine whether 3D effects are wanted
434 virtual WXDWORD Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D);
437 virtual void AddChild(wxWindow
*child
); // Adds reference to the child object
438 virtual void RemoveChild(wxWindow
*child
); // Removes reference to child
439 // (but doesn't delete the child object)
440 virtual void DestroyChildren(); // Removes and destroys all children
442 inline bool IsBeingDeleted() const { return FALSE
; } // TODO: Should probably eliminate this
444 // Constraint implementation
445 void UnsetConstraints(wxLayoutConstraints
*c
);
446 inline wxList
*GetConstraintsInvolvedIn() const ;
447 // Back-pointer to other windows we're involved with, so if we delete
448 // this window, we must delete any constraints we're involved with.
449 void AddConstraintReference(wxWindow
*otherWin
);
450 void RemoveConstraintReference(wxWindow
*otherWin
);
451 void DeleteRelatedConstraints();
453 virtual void ResetConstraints();
454 virtual void SetConstraintSizes(bool recurse
= TRUE
);
455 virtual bool LayoutPhase1(int *noChanges
);
456 virtual bool LayoutPhase2(int *noChanges
);
457 virtual bool DoPhase(int);
458 // Transforms from sizer coordinate space to actual
459 // parent coordinate space
460 virtual void TransformSizerToActual(int *x
, int *y
) const ;
462 // Set size with transformation to actual coordinates if nec.
463 virtual void SizerSetSize(int x
, int y
, int w
, int h
);
464 virtual void SizerMove(int x
, int y
);
466 // Only set/get the size/position of the constraint (if any)
467 virtual void SetSizeConstraint(int x
, int y
, int w
, int h
);
468 virtual void MoveConstraint(int x
, int y
);
469 virtual void GetSizeConstraint(int *w
, int *h
) const ;
470 virtual void GetClientSizeConstraint(int *w
, int *h
) const ;
471 virtual void GetPositionConstraint(int *x
, int *y
) const ;
473 // Dialog units translations. Implemented in wincmn.cpp.
474 wxPoint
ConvertPixelsToDialog(const wxPoint
& pt
) ;
475 wxPoint
ConvertDialogToPixels(const wxPoint
& pt
) ;
476 inline wxSize
ConvertPixelsToDialog(const wxSize
& sz
)
477 { wxPoint
pt(ConvertPixelsToDialog(wxPoint(sz
.x
, sz
.y
))); return wxSize(pt
.x
, pt
.y
); }
478 inline wxSize
ConvertDialogToPixels(const wxSize
& sz
)
479 { wxPoint
pt(ConvertDialogToPixels(wxPoint(sz
.x
, sz
.y
))); return wxSize(pt
.x
, pt
.y
); }
481 wxObject
*GetChild(int number
) const ;
483 // Generates a new id for controls
484 static int NewControlId();
486 // Responds to colour changes: passes event on to children.
487 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
489 // Transfers data to any child controls
490 void OnInitDialog(wxInitDialogEvent
& event
);
492 // Sends an OnInitDialog event, which in turns transfers data to
493 // to the window via validators.
494 virtual void InitDialog();
496 ////////////////////////////////////////////////////////////////////////
500 long m_windowStyle
; // Store the window's style
501 wxEvtHandler
* m_windowEventHandler
; // Usually is 'this'
502 wxLayoutConstraints
* m_constraints
; // Constraints for this window
503 wxList
* m_constraintsInvolvedIn
; // List of constraints we're involved in
504 wxSizer
* m_windowSizer
; // Window's top-level sizer (if any)
505 wxWindow
* m_sizerParent
; // Window's parent sizer (if any)
506 bool m_autoLayout
; // Whether to call Layout() in OnSize
507 wxWindow
* m_windowParent
; // Each window always knows its parent
508 wxValidator
* m_windowValidator
;
519 wxFont m_windowFont
; // Window's font
520 wxCursor m_windowCursor
; // Window's cursor
521 wxString m_windowName
; // Window name
523 wxButton
* m_defaultItem
;
525 wxColour m_backgroundColour
;
526 wxColour m_foregroundColour
;
527 wxAcceleratorTable m_acceleratorTable
;
529 #if wxUSE_DRAG_AND_DROP
530 wxDropTarget
*m_pDropTarget
; // the current drop target or NULL
531 #endif //USE_DRAG_AND_DROP
534 wxRegion m_updateRegion
;
535 wxList
* m_children
; // Window's children
538 DECLARE_EVENT_TABLE()
541 ////////////////////////////////////////////////////////////////////////
544 inline void *wxWindow::GetHandle() const { return (void *)NULL
; }
545 inline int wxWindow::GetId() const { return m_windowId
; }
546 inline void wxWindow::SetId(int id
) { m_windowId
= id
; }
547 inline wxWindow
*wxWindow::GetParent() const { return m_windowParent
; }
548 inline void wxWindow::SetParent(wxWindow
*p
) { m_windowParent
= p
; }
549 inline wxWindow
*wxWindow::GetGrandParent() const { return (m_windowParent
? m_windowParent
->m_windowParent
: (wxWindow
*) NULL
); }
550 inline wxList
& wxWindow::GetChildren() const { return (wxList
&) * m_children
; }
551 inline wxFont
& wxWindow::GetFont() const { return (wxFont
&) m_windowFont
; }
552 inline wxString
wxWindow::GetName() const { return m_windowName
; }
553 inline void wxWindow::SetName(const wxString
& name
) { m_windowName
= name
; }
554 inline long wxWindow::GetWindowStyleFlag() const { return m_windowStyle
; }
555 inline void wxWindow::SetWindowStyleFlag(long flag
) { m_windowStyle
= flag
; }
556 inline void wxWindow::SetEventHandler(wxEvtHandler
*handler
) { m_windowEventHandler
= handler
; }
557 inline wxEvtHandler
*wxWindow::GetEventHandler() const { return m_windowEventHandler
; }
558 inline void wxWindow::SetAutoLayout(bool a
) { m_autoLayout
= a
; }
559 inline bool wxWindow::GetAutoLayout() const { return m_autoLayout
; }
560 inline wxLayoutConstraints
*wxWindow::GetConstraints() const { return m_constraints
; }
561 inline void wxWindow::SetBackgroundColour(const wxColour
& col
) { m_backgroundColour
= col
; };
562 inline wxColour
wxWindow::GetBackgroundColour() const { return m_backgroundColour
; };
563 inline void wxWindow::SetForegroundColour(const wxColour
& col
) { m_foregroundColour
= col
; };
564 inline wxColour
wxWindow::GetForegroundColour() const { return m_foregroundColour
; };
566 inline wxButton
*wxWindow::GetDefaultItem() const { return m_defaultItem
; }
567 inline void wxWindow::SetDefaultItem(wxButton
*but
) { m_defaultItem
= but
; }
568 inline bool wxWindow::IsRetained() const { return ((m_windowStyle
& wxRETAINED
) == wxRETAINED
); }
570 inline wxList
*wxWindow::GetConstraintsInvolvedIn() const { return m_constraintsInvolvedIn
; }
571 inline wxSizer
*wxWindow::GetSizer() const { return m_windowSizer
; }
572 inline wxWindow
*wxWindow::GetSizerParent() const { return m_sizerParent
; }
573 inline void wxWindow::SetSizerParent(wxWindow
*win
) { m_sizerParent
= win
; }
574 inline wxValidator
*wxWindow::GetValidator() const { return m_windowValidator
; }
575 inline void wxWindow::SetReturnCode(int retCode
) { m_returnCode
= retCode
; }
576 inline int wxWindow::GetReturnCode() { return m_returnCode
; }
578 // Get the active window.
579 wxWindow
* WXDLLEXPORT
wxGetActiveWindow();
581 WXDLLEXPORT_DATA(extern wxList
) wxTopLevelWindows
;