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"
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 wxRect
;
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
;
70 #if wxUSE_DRAG_AND_DROP
71 class WXDLLEXPORT wxDropTarget
;
74 #if wxUSE_WX_RESOURCES
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 //-----------------------------------------------------------------------------
86 //-----------------------------------------------------------------------------
92 virtual ~wxClientData() { }
95 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
99 class wxStringClientData
: public wxClientData
102 wxStringClientData() { }
103 wxStringClientData( wxString
&data
) { m_data
= data
; }
104 void SetData( wxString
&data
) { m_data
= data
; }
105 wxString
GetData() const { return m_data
; }
111 class WXDLLEXPORT wxWindow
: public wxEvtHandler
113 DECLARE_ABSTRACT_CLASS(wxWindow
)
116 friend class wxPaintDC
;
120 inline wxWindow(wxWindow
*parent
, wxWindowID id
,
121 const wxPoint
& pos
= wxDefaultPosition
,
122 const wxSize
& size
= wxDefaultSize
,
124 const wxString
& name
= wxPanelNameStr
)
126 m_children
= new wxList
;
127 Create(parent
, id
, pos
, size
, style
, name
);
132 bool Create(wxWindow
*parent
, wxWindowID id
,
133 const wxPoint
& pos
= wxDefaultPosition
,
134 const wxSize
& size
= wxDefaultSize
,
136 const wxString
& name
= wxPanelNameStr
);
138 // Fit the window around the items
141 // Show or hide the window
142 virtual bool Show(bool show
);
144 // Is the window shown?
145 virtual bool IsShown() const;
147 // Raise the window to the top of the Z order
148 virtual void Raise();
150 // Lower the window to the bottom of the Z order
151 virtual void Lower();
153 // Is the window enabled?
154 virtual bool IsEnabled() const;
157 inline bool Enabled() const { return IsEnabled(); }
159 // Dialog support: override these and call
160 // base class members to add functionality
161 // that can't be done using validators.
163 // Transfer values to controls. If returns FALSE,
164 // it's an application error (pops up a dialog)
165 virtual bool TransferDataToWindow();
167 // Transfer values from controls. If returns FALSE,
168 // transfer failed: don't quit
169 virtual bool TransferDataFromWindow();
171 // Validate controls. If returns FALSE,
172 // validation failed: don't quit
173 virtual bool Validate();
175 // Return code for dialogs
176 inline void SetReturnCode(int retCode
);
177 inline int GetReturnCode();
180 virtual void SetCursor(const wxCursor
& cursor
);
181 inline virtual wxCursor
*GetCursor() const { return (wxCursor
*)& m_windowCursor
; };
183 // Get the window with the focus
184 static wxWindow
*FindFocus();
186 // Get character size
187 virtual int GetCharHeight() const;
188 virtual int GetCharWidth() const;
190 // Get overall window size
191 virtual void GetSize(int *width
, int *height
) const;
193 // Get window position, relative to parent (or screen if no parent)
194 virtual void GetPosition(int *x
, int *y
) const;
196 // Get client (application-useable) size
197 virtual void GetClientSize(int *width
, int *height
) const;
199 // Set overall size and position
200 virtual void SetSize(int x
, int y
, int width
, int height
, int sizeFlags
= wxSIZE_AUTO
);
201 inline virtual void SetSize(int width
, int height
) { SetSize(-1, -1, width
, height
, wxSIZE_USE_EXISTING
); }
202 inline virtual void Move(int x
, int y
) { SetSize(x
, y
, -1, -1, wxSIZE_USE_EXISTING
); }
205 virtual void SetClientSize(int width
, int size
);
207 // Convert client to screen coordinates
208 virtual void ClientToScreen(int *x
, int *y
) const;
210 // Convert screen to client coordinates
211 virtual void ScreenToClient(int *x
, int *y
) const;
213 // Set the focus to this window
214 virtual void SetFocus();
216 // Capture/release mouse
217 virtual void CaptureMouse();
218 virtual void ReleaseMouse();
220 // Enable or disable the window
221 virtual void Enable(bool enable
);
223 #if wxUSE_DRAG_AND_DROP
224 // Associate a drop target with this window (if the window already had a drop
225 // target, it's deleted!) and return the current drop target (may be NULL).
226 void SetDropTarget(wxDropTarget
*pDropTarget
);
227 wxDropTarget
*GetDropTarget() const { return m_pDropTarget
; }
230 // Accept files for dragging
231 virtual void DragAcceptFiles(bool accept
);
234 // create a tooltip with this text
235 void SetToolTip(const wxString
& tip
);
239 // pointer may be NULL to remove the tooltip
240 void SetToolTip(wxToolTip
*tooltip
);
241 // get the current tooltip (may return NULL if none)
242 wxToolTip
* GetToolTip() const { return m_tooltip
; }
245 // Update region access
246 virtual wxRegion
GetUpdateRegion() const;
247 virtual bool IsExposed(int x
, int y
, int w
, int h
) const;
248 virtual bool IsExposed(const wxPoint
& pt
) const;
249 virtual bool IsExposed(const wxRect
& rect
) const;
251 // Set/get the window title
252 virtual inline void SetTitle(const wxString
& WXUNUSED(title
)) {};
253 inline virtual wxString
GetTitle() const { return wxString(""); };
254 // Most windows have the concept of a label; for frames, this is the
255 // title; for items, this is the label or button text.
256 inline virtual wxString
GetLabel() const { return GetTitle(); }
258 // Set/get the window name (used for resource setting in X)
259 inline virtual wxString
GetName() const;
260 inline virtual void SetName(const wxString
& name
);
263 virtual void Centre(int direction
) ;
264 inline void Center(int direction
= wxHORIZONTAL
) { Centre(direction
); }
267 virtual bool PopupMenu(wxMenu
*menu
, int x
, int y
);
269 // Send the window a refresh event
270 virtual void Refresh(bool eraseBack
= TRUE
, const wxRect
*rect
= NULL
);
272 // New functions that will replace the above.
273 virtual void SetScrollbar(int orient
, int pos
, int thumbVisible
,
274 int range
, bool refresh
= TRUE
);
276 virtual void SetScrollPos(int orient
, int pos
, bool refresh
= TRUE
);
277 virtual int GetScrollPos(int orient
) const;
278 virtual int GetScrollRange(int orient
) const;
279 virtual int GetScrollThumb(int orient
) const;
281 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
283 // Caret manipulation
284 virtual void CreateCaret(int w
, int h
);
285 virtual void CreateCaret(const wxBitmap
*bitmap
);
286 virtual void DestroyCaret();
287 virtual void ShowCaret(bool show
);
288 virtual void SetCaretPos(int x
, int y
);
289 virtual void GetCaretPos(int *x
, int *y
) const;
291 // Tell window how much it can be sized
292 virtual void SetSizeHints(int minW
= -1, int minH
= -1, int maxW
= -1, int maxH
= -1, int incW
= -1, int incH
= -1);
294 // Set/get the window's identifier
295 inline int GetId() const;
296 inline void SetId(int id
);
298 virtual void SetAcceleratorTable(const wxAcceleratorTable
& accel
);
299 inline virtual wxAcceleratorTable
& GetAcceleratorTable() const { return (wxAcceleratorTable
&) m_acceleratorTable
; }
301 // Make the window modal (all other windows unresponsive)
302 virtual void MakeModal(bool modal
);
304 // Get the private handle (platform-dependent)
305 inline void *GetHandle() const;
307 // Set/get the window's relatives
308 inline wxWindow
*GetParent() const;
309 inline void SetParent(wxWindow
*p
) ;
310 inline wxWindow
*GetGrandParent() const;
311 inline wxList
& GetChildren() const;
313 // Set/get the window's font
314 virtual void SetFont(const wxFont
& f
);
315 inline virtual wxFont
& GetFont() const;
317 // Set/get the window's validator
318 void SetValidator(const wxValidator
& validator
);
319 inline wxValidator
*GetValidator() const;
321 // Set/get the window's style
322 inline void SetWindowStyleFlag(long flag
);
323 inline long GetWindowStyleFlag() const;
325 // Handle a control command
326 virtual void OnCommand(wxWindow
& win
, wxCommandEvent
& event
);
328 // Set/get event handler
329 inline void SetEventHandler(wxEvtHandler
*handler
);
330 inline wxEvtHandler
*GetEventHandler() const;
332 // Push/pop event handler (i.e. allow a chain of event handlers
334 void PushEventHandler(wxEvtHandler
*handler
) ;
335 wxEvtHandler
*PopEventHandler(bool deleteHandler
= FALSE
) ;
337 // Close the window by calling OnClose, posting a deletion
338 virtual bool Close(bool force
= FALSE
);
340 // Destroy the window (delayed, if a managed window)
341 virtual bool Destroy() ;
343 // Mode for telling default OnSize members to
344 // call Layout(), if not using Sizers, just top-down constraints
345 inline void SetAutoLayout(bool a
);
346 inline bool GetAutoLayout() const;
348 // Set/get constraints
349 inline wxLayoutConstraints
*GetConstraints() const;
350 void SetConstraints(wxLayoutConstraints
*c
);
352 // Set/get window background colour
353 inline virtual void SetBackgroundColour(const wxColour
& col
);
354 inline virtual wxColour
GetBackgroundColour() const;
356 // Set/get window foreground colour
357 inline virtual void SetForegroundColour(const wxColour
& col
);
358 inline virtual wxColour
GetForegroundColour() const;
360 // Get the default button, if there is one
361 inline virtual wxButton
*GetDefaultItem() const;
362 inline virtual void SetDefaultItem(wxButton
*but
);
364 // Override to define new behaviour for default action (e.g. double clicking
366 virtual void OnDefaultAction(wxControl
*initiatingItem
);
369 #if wxUSE_WX_RESOURCES
370 virtual bool LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
= NULL
);
371 virtual wxControl
*CreateItem(const wxItemResource
* childResource
, const wxItemResource
* parentResource
,
372 const wxResourceTable
*table
= (const wxResourceTable
*) NULL
);
375 virtual void GetTextExtent(const wxString
& string
, int *x
, int *y
,
377 int *externalLeading
= NULL
,
378 const wxFont
*theFont
= NULL
, bool use16
= FALSE
) const;
380 // Is the window retained?
381 inline bool IsRetained() const;
383 // Warp the pointer the given position
384 virtual void WarpPointer(int x_pos
, int y_pos
) ;
387 virtual void Clear();
389 // Find a window by id or name
390 virtual wxWindow
*FindWindow(long id
);
391 virtual wxWindow
*FindWindow(const wxString
& name
);
393 // Constraint operations
395 void SetSizer(wxSizer
*sizer
); // Adds sizer child to this window
396 inline wxSizer
*GetSizer() const ;
397 inline wxWindow
*GetSizerParent() const ;
398 inline void SetSizerParent(wxWindow
*win
);
400 // Do Update UI processing for controls
401 void UpdateWindowUI();
403 void OnEraseBackground(wxEraseEvent
& event
);
404 void OnChar(wxKeyEvent
& event
);
405 void OnKeyDown(wxKeyEvent
& event
);
406 void OnKeyUp(wxKeyEvent
& event
);
407 void OnPaint(wxPaintEvent
& event
);
408 void OnIdle(wxIdleEvent
& event
);
410 // Does this window want to accept keyboard focus?
411 virtual bool AcceptsFocus() const;
413 virtual void PrepareDC( wxDC
&dc
) {};
417 ////////////////////////////////////////////////////////////////////////
420 // For implementation purposes - sometimes decorations make the client area
422 virtual wxPoint
GetClientAreaOrigin() const;
424 // Makes an adjustment to the window position (for example, a frame that has
425 // a toolbar that it manages itself).
426 virtual void AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
);
428 // Executes the default message
429 virtual long Default();
431 /* TODO: you may need something like this
432 // Determine whether 3D effects are wanted
433 virtual WXDWORD Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D);
436 virtual void AddChild(wxWindow
*child
); // Adds reference to the child object
437 virtual void RemoveChild(wxWindow
*child
); // Removes reference to child
438 // (but doesn't delete the child object)
439 virtual void DestroyChildren(); // Removes and destroys all children
441 inline bool IsBeingDeleted() const { return FALSE
; } // TODO: Should probably eliminate this
443 // Constraint implementation
444 void UnsetConstraints(wxLayoutConstraints
*c
);
445 inline wxList
*GetConstraintsInvolvedIn() const ;
446 // Back-pointer to other windows we're involved with, so if we delete
447 // this window, we must delete any constraints we're involved with.
448 void AddConstraintReference(wxWindow
*otherWin
);
449 void RemoveConstraintReference(wxWindow
*otherWin
);
450 void DeleteRelatedConstraints();
452 virtual void ResetConstraints();
453 virtual void SetConstraintSizes(bool recurse
= TRUE
);
454 virtual bool LayoutPhase1(int *noChanges
);
455 virtual bool LayoutPhase2(int *noChanges
);
456 virtual bool DoPhase(int);
457 // Transforms from sizer coordinate space to actual
458 // parent coordinate space
459 virtual void TransformSizerToActual(int *x
, int *y
) const ;
461 // Set size with transformation to actual coordinates if nec.
462 virtual void SizerSetSize(int x
, int y
, int w
, int h
);
463 virtual void SizerMove(int x
, int y
);
465 // Only set/get the size/position of the constraint (if any)
466 virtual void SetSizeConstraint(int x
, int y
, int w
, int h
);
467 virtual void MoveConstraint(int x
, int y
);
468 virtual void GetSizeConstraint(int *w
, int *h
) const ;
469 virtual void GetClientSizeConstraint(int *w
, int *h
) const ;
470 virtual void GetPositionConstraint(int *x
, int *y
) const ;
472 // Dialog units translations. Implemented in wincmn.cpp.
473 wxPoint
ConvertPixelsToDialog(const wxPoint
& pt
) ;
474 wxPoint
ConvertDialogToPixels(const wxPoint
& pt
) ;
475 inline wxSize
ConvertPixelsToDialog(const wxSize
& sz
)
476 { wxPoint
pt(ConvertPixelsToDialog(wxPoint(sz
.x
, sz
.y
))); return wxSize(pt
.x
, pt
.y
); }
477 inline wxSize
ConvertDialogToPixels(const wxSize
& sz
)
478 { wxPoint
pt(ConvertDialogToPixels(wxPoint(sz
.x
, sz
.y
))); return wxSize(pt
.x
, pt
.y
); }
480 wxObject
*GetChild(int number
) const ;
482 // Generates a new id for controls
483 static int NewControlId();
485 // Responds to colour changes: passes event on to children.
486 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
488 // Transfers data to any child controls
489 void OnInitDialog(wxInitDialogEvent
& event
);
491 // Sends an OnInitDialog event, which in turns transfers data to
492 // to the window via validators.
493 virtual void InitDialog();
495 ////////////////////////////////////////////////////////////////////////
499 long m_windowStyle
; // Store the window's style
500 wxEvtHandler
* m_windowEventHandler
; // Usually is 'this'
501 wxLayoutConstraints
* m_constraints
; // Constraints for this window
502 wxList
* m_constraintsInvolvedIn
; // List of constraints we're involved in
503 wxSizer
* m_windowSizer
; // Window's top-level sizer (if any)
504 wxWindow
* m_sizerParent
; // Window's parent sizer (if any)
505 bool m_autoLayout
; // Whether to call Layout() in OnSize
506 wxWindow
* m_windowParent
; // Each window always knows its parent
507 wxValidator
* m_windowValidator
;
518 wxFont m_windowFont
; // Window's font
519 wxCursor m_windowCursor
; // Window's cursor
520 wxString m_windowName
; // Window name
522 wxButton
* m_defaultItem
;
524 wxColour m_backgroundColour
;
525 wxColour m_foregroundColour
;
526 wxAcceleratorTable m_acceleratorTable
;
528 #if wxUSE_DRAG_AND_DROP
529 wxDropTarget
*m_pDropTarget
; // the current drop target or NULL
530 #endif //USE_DRAG_AND_DROP
533 wxRegion m_updateRegion
;
534 wxList
* m_children
; // Window's children
537 DECLARE_EVENT_TABLE()
540 ////////////////////////////////////////////////////////////////////////
543 inline void *wxWindow::GetHandle() const { return (void *)NULL
; }
544 inline int wxWindow::GetId() const { return m_windowId
; }
545 inline void wxWindow::SetId(int id
) { m_windowId
= id
; }
546 inline wxWindow
*wxWindow::GetParent() const { return m_windowParent
; }
547 inline void wxWindow::SetParent(wxWindow
*p
) { m_windowParent
= p
; }
548 inline wxWindow
*wxWindow::GetGrandParent() const { return (m_windowParent
? m_windowParent
->m_windowParent
: (wxWindow
*) NULL
); }
549 inline wxList
& wxWindow::GetChildren() const { return (wxList
&) * m_children
; }
550 inline wxFont
& wxWindow::GetFont() const { return (wxFont
&) m_windowFont
; }
551 inline wxString
wxWindow::GetName() const { return m_windowName
; }
552 inline void wxWindow::SetName(const wxString
& name
) { m_windowName
= name
; }
553 inline long wxWindow::GetWindowStyleFlag() const { return m_windowStyle
; }
554 inline void wxWindow::SetWindowStyleFlag(long flag
) { m_windowStyle
= flag
; }
555 inline void wxWindow::SetEventHandler(wxEvtHandler
*handler
) { m_windowEventHandler
= handler
; }
556 inline wxEvtHandler
*wxWindow::GetEventHandler() const { return m_windowEventHandler
; }
557 inline void wxWindow::SetAutoLayout(bool a
) { m_autoLayout
= a
; }
558 inline bool wxWindow::GetAutoLayout() const { return m_autoLayout
; }
559 inline wxLayoutConstraints
*wxWindow::GetConstraints() const { return m_constraints
; }
560 inline void wxWindow::SetBackgroundColour(const wxColour
& col
) { m_backgroundColour
= col
; };
561 inline wxColour
wxWindow::GetBackgroundColour() const { return m_backgroundColour
; };
562 inline void wxWindow::SetForegroundColour(const wxColour
& col
) { m_foregroundColour
= col
; };
563 inline wxColour
wxWindow::GetForegroundColour() const { return m_foregroundColour
; };
565 inline wxButton
*wxWindow::GetDefaultItem() const { return m_defaultItem
; }
566 inline void wxWindow::SetDefaultItem(wxButton
*but
) { m_defaultItem
= but
; }
567 inline bool wxWindow::IsRetained() const { return ((m_windowStyle
& wxRETAINED
) == wxRETAINED
); }
569 inline wxList
*wxWindow::GetConstraintsInvolvedIn() const { return m_constraintsInvolvedIn
; }
570 inline wxSizer
*wxWindow::GetSizer() const { return m_windowSizer
; }
571 inline wxWindow
*wxWindow::GetSizerParent() const { return m_sizerParent
; }
572 inline void wxWindow::SetSizerParent(wxWindow
*win
) { m_sizerParent
= win
; }
573 inline wxValidator
*wxWindow::GetValidator() const { return m_windowValidator
; }
574 inline void wxWindow::SetReturnCode(int retCode
) { m_returnCode
= retCode
; }
575 inline int wxWindow::GetReturnCode() { return m_returnCode
; }
577 // Get the active window.
578 wxWindow
* WXDLLEXPORT
wxGetActiveWindow();
580 WXDLLEXPORT_DATA(extern wxList
) wxTopLevelWindows
;