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
;
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 USE_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 // Get the default button, if there is one
315 inline virtual wxButton
*GetDefaultItem() const;
316 inline virtual void SetDefaultItem(wxButton
*but
);
318 // Override to define new behaviour for default action (e.g. double clicking
320 virtual void OnDefaultAction(wxControl
*initiatingItem
);
324 virtual bool LoadFromResource(wxWindow
*parent
, const wxString
& resourceName
, const wxResourceTable
*table
= NULL
);
325 virtual wxControl
*CreateItem(const wxItemResource
*childResource
, const wxResourceTable
*table
= NULL
);
328 // Native resource loading
329 virtual bool LoadNativeDialog(wxWindow
* parent
, wxWindowID
& id
);
330 virtual bool LoadNativeDialog(wxWindow
* parent
, const wxString
& name
);
331 virtual wxWindow
* GetWindowChild1(wxWindowID
& id
);
332 virtual wxWindow
* GetWindowChild(wxWindowID
& id
);
334 virtual void GetTextExtent(const wxString
& string
, int *x
, int *y
,
336 int *externalLeading
= NULL
,
337 const wxFont
*theFont
= NULL
, bool use16
= FALSE
) const;
339 // Is the window retained?
340 inline bool IsRetained() const;
342 // Warp the pointer the given position
343 virtual void WarpPointer(int x_pos
, int y_pos
) ;
346 virtual void Clear();
348 // Find a window by id or name
349 virtual wxWindow
*FindWindow(long id
);
350 virtual wxWindow
*FindWindow(const wxString
& name
);
352 // Constraint operations
354 void SetSizer(wxSizer
*sizer
); // Adds sizer child to this window
355 inline wxSizer
*GetSizer() const ;
356 inline wxWindow
*GetSizerParent() const ;
357 inline void SetSizerParent(wxWindow
*win
);
359 // Do Update UI processing for controls
360 void UpdateWindowUI();
362 void OnEraseBackground(wxEraseEvent
& event
);
363 void OnChar(wxKeyEvent
& event
);
364 void OnPaint(wxPaintEvent
& event
);
365 void OnIdle(wxIdleEvent
& event
);
367 // Does this window want to accept keyboard focus?
368 virtual bool AcceptsFocus() const;
371 ////////////////////////////////////////////////////////////////////////
374 // For implementation purposes - sometimes decorations make the client area
376 virtual wxPoint
GetClientAreaOrigin() const;
378 // Makes an adjustment to the window position (for example, a frame that has
379 // a toolbar that it manages itself).
380 virtual void AdjustForParentClientOrigin(int& x
, int& y
, int sizeFlags
);
382 // Executes the default message
383 virtual long Default();
385 /* TODO: implement your own data access
386 virtual WXHWND GetHWND() const ;
387 virtual void SetHWND(WXHWND hWnd);
390 /* TODO: you may need something like this
391 // Determine whether 3D effects are wanted
392 virtual WXDWORD Determine3DEffects(WXDWORD defaultBorderStyle, bool *want3D);
395 virtual void AddChild(wxWindow
*child
); // Adds reference to the child object
396 virtual void RemoveChild(wxWindow
*child
); // Removes reference to child
397 // (but doesn't delete the child object)
398 virtual void DestroyChildren(); // Removes and destroys all children
400 // Constraint implementation
401 void UnsetConstraints(wxLayoutConstraints
*c
);
402 inline wxList
*GetConstraintsInvolvedIn() const ;
403 // Back-pointer to other windows we're involved with, so if we delete
404 // this window, we must delete any constraints we're involved with.
405 void AddConstraintReference(wxWindow
*otherWin
);
406 void RemoveConstraintReference(wxWindow
*otherWin
);
407 void DeleteRelatedConstraints();
409 virtual void ResetConstraints();
410 virtual void SetConstraintSizes(bool recurse
= TRUE
);
411 virtual bool LayoutPhase1(int *noChanges
);
412 virtual bool LayoutPhase2(int *noChanges
);
413 virtual bool DoPhase(int);
414 // Transforms from sizer coordinate space to actual
415 // parent coordinate space
416 virtual void TransformSizerToActual(int *x
, int *y
) const ;
418 // Set size with transformation to actual coordinates if nec.
419 virtual void SizerSetSize(int x
, int y
, int w
, int h
);
420 virtual void SizerMove(int x
, int y
);
422 // Only set/get the size/position of the constraint (if any)
423 virtual void SetSizeConstraint(int x
, int y
, int w
, int h
);
424 virtual void MoveConstraint(int x
, int y
);
425 virtual void GetSizeConstraint(int *w
, int *h
) const ;
426 virtual void GetClientSizeConstraint(int *w
, int *h
) const ;
427 virtual void GetPositionConstraint(int *x
, int *y
) const ;
429 wxObject
*GetChild(int number
) const ;
431 inline void SetShowing(bool show
);
432 inline bool IsUserEnabled() const;
433 inline bool GetTransparentBackground() const ;
435 // Responds to colour changes: passes event on to children.
436 void OnSysColourChanged(wxSysColourChangedEvent
& event
);
438 // Transfers data to any child controls
439 void OnInitDialog(wxInitDialogEvent
& event
);
441 // Sends an OnInitDialog event, which in turns transfers data to
442 // to the window via validators.
443 virtual void InitDialog();
445 ////////////////////////////////////////////////////////////////////////
449 long m_windowStyle
; // Store the window's style
450 wxEvtHandler
* m_windowEventHandler
; // Usually is 'this'
451 wxLayoutConstraints
* m_constraints
; // Constraints for this window
452 wxList
* m_constraintsInvolvedIn
; // List of constraints we're involved in
453 wxSizer
* m_windowSizer
; // Window's top-level sizer (if any)
454 wxWindow
* m_sizerParent
; // Window's parent sizer (if any)
455 bool m_autoLayout
; // Whether to call Layout() in OnSize
456 wxWindow
* m_windowParent
; // Each window always knows its parent
457 wxValidator
* m_windowValidator
;
458 bool m_inOnSize
; // Protection against OnSize reentry
470 wxFont m_windowFont
; // Window's font
472 wxCursor m_windowCursor
; // Window's cursor
473 wxString m_windowName
; // Window name
475 wxButton
* m_defaultItem
;
477 wxColour m_backgroundColour
;
478 wxColour m_foregroundColour
;
479 bool m_backgroundTransparent
;
488 bool m_mouseInWindow
;
490 #if USE_DRAG_AND_DROP
491 wxDropTarget
*m_pDropTarget
; // the current drop target or NULL
492 #endif //USE_DRAG_AND_DROP
495 /* TODO: implementation of window handle, note of last message, etc.
496 WXHWND m_hWnd; // MS Windows window handle
498 WXWPARAM m_lastWParam;
499 WXLPARAM m_lastLParam;
500 WXHMENU m_hMenu; // Menu, if any
504 wxRegion m_updateRegion
;
505 wxList
* m_children
; // Window's children
508 DECLARE_EVENT_TABLE()
511 ////////////////////////////////////////////////////////////////////////
514 inline void *wxWindow::GetHandle() const { return (void *)NULL
; }
515 inline int wxWindow::GetId() const { return m_windowId
; }
516 inline void wxWindow::SetId(int id
) { m_windowId
= id
; }
517 inline wxWindow
*wxWindow::GetParent() const { return m_windowParent
; }
518 inline void wxWindow::SetParent(wxWindow
*p
) { m_windowParent
= p
; }
519 inline wxWindow
*wxWindow::GetGrandParent() const { return (m_windowParent
? m_windowParent
->m_windowParent
: NULL
); }
520 inline wxList
*wxWindow::GetChildren() const { return m_children
; }
521 inline wxFont
*wxWindow::GetFont() const { return (wxFont
*) & m_windowFont
; }
522 inline wxString
wxWindow::GetName() const { return m_windowName
; }
523 inline void wxWindow::SetName(const wxString
& name
) { m_windowName
= name
; }
524 inline long wxWindow::GetWindowStyleFlag() const { return m_windowStyle
; }
525 inline void wxWindow::SetWindowStyleFlag(long flag
) { m_windowStyle
= flag
; }
526 inline void wxWindow::SetDoubleClick(bool flag
) { m_doubleClickAllowed
= flag
; }
527 inline bool wxWindow::GetDoubleClick() const { return m_doubleClickAllowed
; }
528 inline void wxWindow::SetEventHandler(wxEvtHandler
*handler
) { m_windowEventHandler
= handler
; }
529 inline wxEvtHandler
*wxWindow::GetEventHandler() const { return m_windowEventHandler
; }
530 inline void wxWindow::SetAutoLayout(bool a
) { m_autoLayout
= a
; }
531 inline bool wxWindow::GetAutoLayout() const { return m_autoLayout
; }
532 inline wxLayoutConstraints
*wxWindow::GetConstraints() const { return m_constraints
; }
533 inline void wxWindow::SetBackgroundColour(const wxColour
& col
) { m_backgroundColour
= col
; };
534 inline wxColour
wxWindow::GetBackgroundColour() const { return m_backgroundColour
; };
535 inline void wxWindow::SetForegroundColour(const wxColour
& col
) { m_foregroundColour
= col
; };
536 inline wxColour
wxWindow::GetForegroundColour() const { return m_foregroundColour
; };
538 inline wxButton
*wxWindow::GetDefaultItem() const { return m_defaultItem
; }
539 inline void wxWindow::SetDefaultItem(wxButton
*but
) { m_defaultItem
= but
; }
540 inline bool wxWindow::IsRetained() const { return ((m_windowStyle
& wxRETAINED
) == wxRETAINED
); }
542 inline void wxWindow::SetShowing(bool show
) { m_isShown
= show
; }
543 inline wxList
*wxWindow::GetConstraintsInvolvedIn() const { return m_constraintsInvolvedIn
; }
544 inline wxSizer
*wxWindow::GetSizer() const { return m_windowSizer
; }
545 inline wxWindow
*wxWindow::GetSizerParent() const { return m_sizerParent
; }
546 inline void wxWindow::SetSizerParent(wxWindow
*win
) { m_sizerParent
= win
; }
547 inline wxValidator
*wxWindow::GetValidator() const { return m_windowValidator
; }
548 inline bool wxWindow::IsUserEnabled() const { return m_winEnabled
; }
549 inline bool wxWindow::GetTransparentBackground() const { return m_backgroundTransparent
; }
550 inline void wxWindow::SetReturnCode(int retCode
) { m_returnCode
= retCode
; }
551 inline int wxWindow::GetReturnCode() { return m_returnCode
; }
553 // Get the active window.
554 wxWindow
* WXDLLEXPORT
wxGetActiveWindow();
556 WXDLLEXPORT_DATA(extern wxList
) wxTopLevelWindows
;