1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxCaretBase class - the interface of wxCaret
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_CARET_H_BASE_
13 #define _WX_CARET_H_BASE_
19 // ---------------------------------------------------------------------------
20 // forward declarations
21 // ---------------------------------------------------------------------------
23 class WXDLLIMPEXP_FWD_CORE wxWindow
;
24 class WXDLLIMPEXP_FWD_CORE wxWindowBase
;
26 // ----------------------------------------------------------------------------
27 // headers we have to include
28 // ----------------------------------------------------------------------------
30 #include "wx/gdicmn.h" // for wxPoint, wxSize
32 // ----------------------------------------------------------------------------
33 // A caret is a blinking cursor showing the position where the typed text will
34 // appear. It can be either a solid block or a custom bitmap (TODO)
35 // ----------------------------------------------------------------------------
37 class WXDLLIMPEXP_CORE wxCaretBase
42 // default - use Create
43 wxCaretBase() { Init(); }
44 // create the caret of given (in pixels) width and height and associate
45 // with the given window
46 wxCaretBase(wxWindowBase
*window
, int width
, int height
)
50 (void)Create(window
, width
, height
);
53 wxCaretBase(wxWindowBase
*window
, const wxSize
& size
)
57 (void)Create(window
, size
);
60 // a virtual dtor has been provided since this class has virtual members
61 virtual ~wxCaretBase() { }
63 // Create() functions - same as ctor but returns the success code
64 // --------------------------------------------------------------
67 bool Create(wxWindowBase
*window
, int width
, int height
)
68 { return DoCreate(window
, width
, height
); }
70 bool Create(wxWindowBase
*window
, const wxSize
& size
)
71 { return DoCreate(window
, size
.x
, size
.y
); }
76 // is the caret valid?
77 bool IsOk() const { return m_width
!= 0 && m_height
!= 0; }
79 // is the caret currently shown?
80 bool IsVisible() const { return m_countVisible
> 0; }
82 // get the caret position
83 void GetPosition(int *x
, int *y
) const
88 wxPoint
GetPosition() const { return wxPoint(m_x
, m_y
); }
91 void GetSize(int *width
, int *height
) const
93 if ( width
) *width
= m_width
;
94 if ( height
) *height
= m_height
;
96 wxSize
GetSize() const { return wxSize(m_width
, m_height
); }
98 // get the window we're associated with
99 wxWindow
*GetWindow() const { return (wxWindow
*)m_window
; }
101 // change the size of the caret
102 void SetSize(int width
, int height
) {
107 void SetSize(const wxSize
& size
) { SetSize(size
.x
, size
.y
); }
113 // move the caret to given position (in logical coords)
114 void Move(int x
, int y
) { m_x
= x
; m_y
= y
; DoMove(); }
115 void Move(const wxPoint
& pt
) { m_x
= pt
.x
; m_y
= pt
.y
; DoMove(); }
117 // show/hide the caret (should be called by wxWindow when needed):
118 // Show() must be called as many times as Hide() + 1 to make the caret
120 virtual void Show(bool show
= true)
124 if ( m_countVisible
++ == 0 )
129 if ( --m_countVisible
== 0 )
133 virtual void Hide() { Show(false); }
135 // blink time is measured in milliseconds and is the time elapsed
136 // between 2 inversions of the caret (blink time of the caret is common
137 // to all carets in the Universe, so these functions are static)
138 static int GetBlinkTime();
139 static void SetBlinkTime(int milliseconds
);
141 // implementation from now on
142 // --------------------------
144 // these functions should be called by wxWindow when the window gets/loses
145 // the focus - we create/show and hide/destroy the caret here
146 virtual void OnSetFocus() { }
147 virtual void OnKillFocus() { }
150 // these functions may be overriden in the derived classes, but they
151 // should call the base class version first
152 virtual bool DoCreate(wxWindowBase
*window
, int width
, int height
)
161 // pure virtuals to implement in the derived class
162 virtual void DoShow() = 0;
163 virtual void DoHide() = 0;
164 virtual void DoMove() = 0;
165 virtual void DoSize() { }
167 // the common initialization
170 m_window
= (wxWindowBase
*)NULL
;
172 m_width
= m_height
= 0;
176 // the size of the caret
177 int m_width
, m_height
;
179 // the position of the caret
182 // the window we're associated with
183 wxWindowBase
*m_window
;
185 // visibility count: the caret is visible only if it's positive
189 DECLARE_NO_COPY_CLASS(wxCaretBase
)
192 // ---------------------------------------------------------------------------
193 // now include the real thing
194 // ---------------------------------------------------------------------------
196 #if defined(__WXMSW__)
197 #include "wx/msw/caret.h"
199 #include "wx/generic/caret.h"
202 // ----------------------------------------------------------------------------
203 // wxCaretSuspend: a simple class which hides the caret in its ctor and
204 // restores it in the dtor, this should be used when drawing on the screen to
205 // avoid overdrawing the caret
206 // ----------------------------------------------------------------------------
208 #ifdef wxHAS_CARET_USING_OVERLAYS
210 // we don't need to hide the caret if it's rendered using overlays
211 class WXDLLIMPEXP_CORE wxCaretSuspend
214 wxCaretSuspend(wxWindow
*WXUNUSED(win
)) {}
216 DECLARE_NO_COPY_CLASS(wxCaretSuspend
)
219 #else // !wxHAS_CARET_USING_OVERLAYS
221 class WXDLLIMPEXP_CORE wxCaretSuspend
224 wxCaretSuspend(wxWindow
*win
)
226 m_caret
= win
->GetCaret();
228 if ( m_caret
&& m_caret
->IsVisible() )
237 if ( m_caret
&& m_show
)
245 DECLARE_NO_COPY_CLASS(wxCaretSuspend
)
248 #endif // wxHAS_CARET_USING_OVERLAYS/!wxHAS_CARET_USING_OVERLAYS
250 #endif // wxUSE_CARET
252 #endif // _WX_CARET_H_BASE_