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 // a virtual dtor has been provided since this class has virtual members
65 virtual ~wxCaretBase() { }
67 // Create() functions - same as ctor but returns the success code
68 // --------------------------------------------------------------
71 bool Create(wxWindowBase
*window
, int width
, int height
)
72 { return DoCreate(window
, width
, height
); }
74 bool Create(wxWindowBase
*window
, const wxSize
& size
)
75 { return DoCreate(window
, size
.x
, size
.y
); }
80 // is the caret valid?
81 bool IsOk() const { return m_width
!= 0 && m_height
!= 0; }
83 // is the caret currently shown?
84 bool IsVisible() const { return m_countVisible
> 0; }
86 // get the caret position
87 void GetPosition(int *x
, int *y
) const
92 wxPoint
GetPosition() const { return wxPoint(m_x
, m_y
); }
95 void GetSize(int *width
, int *height
) const
97 if ( width
) *width
= m_width
;
98 if ( height
) *height
= m_height
;
100 wxSize
GetSize() const { return wxSize(m_width
, m_height
); }
102 // get the window we're associated with
103 wxWindow
*GetWindow() const { return (wxWindow
*)m_window
; }
105 // change the size of the caret
106 void SetSize(int width
, int height
) {
111 void SetSize(const wxSize
& size
) { SetSize(size
.x
, size
.y
); }
117 // move the caret to given position (in logical coords)
118 void Move(int x
, int y
) { m_x
= x
; m_y
= y
; DoMove(); }
119 void Move(const wxPoint
& pt
) { m_x
= pt
.x
; m_y
= pt
.y
; DoMove(); }
121 // show/hide the caret (should be called by wxWindow when needed):
122 // Show() must be called as many times as Hide() + 1 to make the caret
124 virtual void Show(bool show
= true)
128 if ( m_countVisible
++ == 0 )
133 if ( --m_countVisible
== 0 )
137 virtual void Hide() { Show(false); }
139 // blink time is measured in milliseconds and is the time elapsed
140 // between 2 inversions of the caret (blink time of the caret is common
141 // to all carets in the Universe, so these functions are static)
142 static int GetBlinkTime();
143 static void SetBlinkTime(int milliseconds
);
145 // implementation from now on
146 // --------------------------
148 // these functions should be called by wxWindow when the window gets/loses
149 // the focus - we create/show and hide/destroy the caret here
150 virtual void OnSetFocus() { }
151 virtual void OnKillFocus() { }
154 // these functions may be overriden in the derived classes, but they
155 // should call the base class version first
156 virtual bool DoCreate(wxWindowBase
*window
, int width
, int height
)
165 // pure virtuals to implement in the derived class
166 virtual void DoShow() = 0;
167 virtual void DoHide() = 0;
168 virtual void DoMove() = 0;
169 virtual void DoSize() { }
171 // the common initialization
174 m_window
= (wxWindowBase
*)NULL
;
176 m_width
= m_height
= 0;
180 // the size of the caret
181 int m_width
, m_height
;
183 // the position of the caret
186 // the window we're associated with
187 wxWindowBase
*m_window
;
189 // visibility count: the caret is visible only if it's positive
193 DECLARE_NO_COPY_CLASS(wxCaretBase
)
196 // ---------------------------------------------------------------------------
197 // now include the real thing
198 // ---------------------------------------------------------------------------
200 #if defined(__WXMSW__)
201 #include "wx/msw/caret.h"
203 #include "wx/generic/caret.h"
206 // ----------------------------------------------------------------------------
207 // wxCaretSuspend: a simple class which hides the caret in its ctor and
208 // restores it in the dtor, this should be used when drawing on the screen to
209 // avoid overdrawing the caret
210 // ----------------------------------------------------------------------------
212 class WXDLLEXPORT wxCaretSuspend
215 wxCaretSuspend(wxWindow
*win
)
217 m_caret
= win
->GetCaret();
219 if ( m_caret
&& m_caret
->IsVisible() )
228 if ( m_caret
&& m_show
)
236 DECLARE_NO_COPY_CLASS(wxCaretSuspend
)
239 #endif // wxUSE_CARET
241 #endif // _WX_CARET_H_BASE_