1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCaretBase class - the interface of wxCaret
4 // Author: Vadim Zeitlin
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_CARET_H_BASE_
12 #define _WX_CARET_H_BASE_
18 // ---------------------------------------------------------------------------
19 // forward declarations
20 // ---------------------------------------------------------------------------
22 class WXDLLIMPEXP_FWD_CORE wxWindow
;
23 class WXDLLIMPEXP_FWD_CORE wxWindowBase
;
25 // ----------------------------------------------------------------------------
26 // headers we have to include
27 // ----------------------------------------------------------------------------
29 #include "wx/gdicmn.h" // for wxPoint, wxSize
31 // ----------------------------------------------------------------------------
32 // A caret is a blinking cursor showing the position where the typed text will
33 // appear. It can be either a solid block or a custom bitmap (TODO)
34 // ----------------------------------------------------------------------------
36 class WXDLLIMPEXP_CORE wxCaretBase
41 // default - use Create
42 wxCaretBase() { Init(); }
43 // create the caret of given (in pixels) width and height and associate
44 // with the given window
45 wxCaretBase(wxWindowBase
*window
, int width
, int height
)
49 (void)Create(window
, width
, height
);
52 wxCaretBase(wxWindowBase
*window
, const wxSize
& size
)
56 (void)Create(window
, size
);
59 // a virtual dtor has been provided since this class has virtual members
60 virtual ~wxCaretBase() { }
62 // Create() functions - same as ctor but returns the success code
63 // --------------------------------------------------------------
66 bool Create(wxWindowBase
*window
, int width
, int height
)
67 { return DoCreate(window
, width
, height
); }
69 bool Create(wxWindowBase
*window
, const wxSize
& size
)
70 { return DoCreate(window
, size
.x
, size
.y
); }
75 // is the caret valid?
76 bool IsOk() const { return m_width
!= 0 && m_height
!= 0; }
78 // is the caret currently shown?
79 bool IsVisible() const { return m_countVisible
> 0; }
81 // get the caret position
82 void GetPosition(int *x
, int *y
) const
87 wxPoint
GetPosition() const { return wxPoint(m_x
, m_y
); }
90 void GetSize(int *width
, int *height
) const
92 if ( width
) *width
= m_width
;
93 if ( height
) *height
= m_height
;
95 wxSize
GetSize() const { return wxSize(m_width
, m_height
); }
97 // get the window we're associated with
98 wxWindow
*GetWindow() const { return (wxWindow
*)m_window
; }
100 // change the size of the caret
101 void SetSize(int width
, int height
) {
106 void SetSize(const wxSize
& size
) { SetSize(size
.x
, size
.y
); }
112 // move the caret to given position (in logical coords)
113 void Move(int x
, int y
) { m_x
= x
; m_y
= y
; DoMove(); }
114 void Move(const wxPoint
& pt
) { m_x
= pt
.x
; m_y
= pt
.y
; DoMove(); }
116 // show/hide the caret (should be called by wxWindow when needed):
117 // Show() must be called as many times as Hide() + 1 to make the caret
119 virtual void Show(bool show
= true)
123 if ( m_countVisible
++ == 0 )
128 if ( --m_countVisible
== 0 )
132 virtual void Hide() { Show(false); }
134 // blink time is measured in milliseconds and is the time elapsed
135 // between 2 inversions of the caret (blink time of the caret is common
136 // to all carets in the Universe, so these functions are static)
137 static int GetBlinkTime();
138 static void SetBlinkTime(int milliseconds
);
140 // implementation from now on
141 // --------------------------
143 // these functions should be called by wxWindow when the window gets/loses
144 // the focus - we create/show and hide/destroy the caret here
145 virtual void OnSetFocus() { }
146 virtual void OnKillFocus() { }
149 // these functions may be overridden in the derived classes, but they
150 // should call the base class version first
151 virtual bool DoCreate(wxWindowBase
*window
, int width
, int height
)
160 // pure virtuals to implement in the derived class
161 virtual void DoShow() = 0;
162 virtual void DoHide() = 0;
163 virtual void DoMove() = 0;
164 virtual void DoSize() { }
166 // the common initialization
171 m_width
= m_height
= 0;
175 // the size of the caret
176 int m_width
, m_height
;
178 // the position of the caret
181 // the window we're associated with
182 wxWindowBase
*m_window
;
184 // visibility count: the caret is visible only if it's positive
188 wxDECLARE_NO_COPY_CLASS(wxCaretBase
);
191 // ---------------------------------------------------------------------------
192 // now include the real thing
193 // ---------------------------------------------------------------------------
195 #if defined(__WXMSW__)
196 #include "wx/msw/caret.h"
198 #include "wx/generic/caret.h"
201 // ----------------------------------------------------------------------------
202 // wxCaretSuspend: a simple class which hides the caret in its ctor and
203 // restores it in the dtor, this should be used when drawing on the screen to
204 // avoid overdrawing the caret
205 // ----------------------------------------------------------------------------
207 #ifdef wxHAS_CARET_USING_OVERLAYS
209 // we don't need to hide the caret if it's rendered using overlays
210 class WXDLLIMPEXP_CORE wxCaretSuspend
213 wxCaretSuspend(wxWindow
*WXUNUSED(win
)) {}
215 wxDECLARE_NO_COPY_CLASS(wxCaretSuspend
);
218 #else // !wxHAS_CARET_USING_OVERLAYS
220 class WXDLLIMPEXP_CORE wxCaretSuspend
223 wxCaretSuspend(wxWindow
*win
)
225 m_caret
= win
->GetCaret();
227 if ( m_caret
&& m_caret
->IsVisible() )
236 if ( m_caret
&& m_show
)
244 wxDECLARE_NO_COPY_CLASS(wxCaretSuspend
);
247 #endif // wxHAS_CARET_USING_OVERLAYS/!wxHAS_CARET_USING_OVERLAYS
249 #endif // wxUSE_CARET
251 #endif // _WX_CARET_H_BASE_