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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "caret.h"
23 // ---------------------------------------------------------------------------
24 // forward declarations
25 // ---------------------------------------------------------------------------
27 class WXDLLEXPORT wxWindow
;
28 class WXDLLEXPORT wxWindowBase
;
30 // ----------------------------------------------------------------------------
31 // headers we have to include
32 // ----------------------------------------------------------------------------
34 #include "wx/gdicmn.h" // for wxPoint, wxSize
36 // ----------------------------------------------------------------------------
37 // A caret is a blinking cursor showing the position where the typed text will
38 // appear. It can be either a solid block or a custom bitmap (TODO)
39 // ----------------------------------------------------------------------------
41 class WXDLLEXPORT wxCaretBase
46 // default - use Create
47 wxCaretBase() { Init(); }
48 // create the caret of given (in pixels) width and height and associate
49 // with the given window
50 wxCaretBase(wxWindowBase
*window
, int width
, int height
)
54 (void)Create(window
, width
, height
);
57 wxCaretBase(wxWindowBase
*window
, const wxSize
& size
)
61 (void)Create(window
, size
);
64 // Create() functions - same as ctor but returns the success code
65 // --------------------------------------------------------------
68 bool Create(wxWindowBase
*window
, int width
, int height
)
69 { return DoCreate(window
, width
, height
); }
71 bool Create(wxWindowBase
*window
, const wxSize
& size
)
72 { return DoCreate(window
, size
.x
, size
.y
); }
77 // is the caret valid?
78 bool IsOk() const { return m_width
!= 0 && m_height
!= 0; }
80 // is the caret currently shown?
81 bool IsVisible() const { return m_countVisible
> 0; }
83 // get the caret position
84 void GetPosition(int *x
, int *y
) const
89 wxPoint
GetPosition() const { return wxPoint(m_x
, m_y
); }
92 void GetSize(int *width
, int *height
) const
94 if ( width
) *width
= m_width
;
95 if ( height
) *height
= m_height
;
97 wxSize
GetSize() const { return wxSize(m_width
, m_height
); }
99 // get the window we're associated with
100 wxWindow
*GetWindow() const { return (wxWindow
*)m_window
; }
102 // change the size of the caret
103 void SetSize(int width
, int height
) {
108 void SetSize(const wxSize
& size
) { SetSize(size
.x
, size
.y
); }
114 // move the caret to given position (in logical coords)
115 void Move(int x
, int y
) { m_x
= x
; m_y
= y
; DoMove(); }
116 void Move(const wxPoint
& pt
) { m_x
= pt
.x
; m_y
= pt
.y
; DoMove(); }
118 // show/hide the caret (should be called by wxWindow when needed):
119 // Show() must be called as many times as Hide() + 1 to make the caret
121 virtual void Show(bool show
= true)
125 if ( m_countVisible
++ == 0 )
130 if ( --m_countVisible
== 0 )
134 virtual void Hide() { Show(false); }
136 // blink time is measured in milliseconds and is the time elapsed
137 // between 2 inversions of the caret (blink time of the caret is common
138 // to all carets in the Universe, so these functions are static)
139 static int GetBlinkTime();
140 static void SetBlinkTime(int milliseconds
);
142 // implementation from now on
143 // --------------------------
145 // these functions should be called by wxWindow when the window gets/loses
146 // the focus - we create/show and hide/destroy the caret here
147 virtual void OnSetFocus() { }
148 virtual void OnKillFocus() { }
151 // these functions may be overriden in the derived classes, but they
152 // should call the base class version first
153 virtual bool DoCreate(wxWindowBase
*window
, int width
, int height
)
162 // pure virtuals to implement in the derived class
163 virtual void DoShow() = 0;
164 virtual void DoHide() = 0;
165 virtual void DoMove() = 0;
166 virtual void DoSize() { }
168 // the common initialization
171 m_window
= (wxWindowBase
*)NULL
;
173 m_width
= m_height
= 0;
177 // the size of the caret
178 int m_width
, m_height
;
180 // the position of the caret
183 // the window we're associated with
184 wxWindowBase
*m_window
;
186 // visibility count: the caret is visible only if it's positive
190 DECLARE_NO_COPY_CLASS(wxCaretBase
)
193 // ---------------------------------------------------------------------------
194 // now include the real thing
195 // ---------------------------------------------------------------------------
197 #if defined(__WXMSW__)
198 #include "wx/msw/caret.h"
200 #include "wx/generic/caret.h"
203 // ----------------------------------------------------------------------------
204 // wxCaretSuspend: a simple class which hides the caret in its ctor and
205 // restores it in the dtor, this should be used when drawing on the screen to
206 // avoid overdrawing the caret
207 // ----------------------------------------------------------------------------
209 class WXDLLEXPORT wxCaretSuspend
212 wxCaretSuspend(wxWindow
*win
)
214 m_caret
= win
->GetCaret();
216 if ( m_caret
&& m_caret
->IsVisible() )
225 if ( m_caret
&& m_show
)
233 DECLARE_NO_COPY_CLASS(wxCaretSuspend
)
236 #endif // wxUSE_CARET
238 #endif // _WX_CARET_H_BASE_