]> git.saurik.com Git - wxWidgets.git/blob - include/wx/caret.h
Removed previous broken fix for deferred positioning bug, and added
[wxWidgets.git] / include / wx / caret.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: caret.h
3 // Purpose: wxCaretBase class - the interface of wxCaret
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 23.05.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_CARET_H_BASE_
13 #define _WX_CARET_H_BASE_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_CARET
18
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "caret.h"
21 #endif
22
23 // ---------------------------------------------------------------------------
24 // forward declarations
25 // ---------------------------------------------------------------------------
26
27 class WXDLLEXPORT wxWindow;
28 class WXDLLEXPORT wxWindowBase;
29
30 // ----------------------------------------------------------------------------
31 // headers we have to include
32 // ----------------------------------------------------------------------------
33
34 #include "wx/gdicmn.h" // for wxPoint, wxSize
35
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 // ----------------------------------------------------------------------------
40
41 class WXDLLEXPORT wxCaretBase
42 {
43 public:
44 // ctors
45 // -----
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)
51 {
52 Init();
53
54 (void)Create(window, width, height);
55 }
56 // same as above
57 wxCaretBase(wxWindowBase *window, const wxSize& size)
58 {
59 Init();
60
61 (void)Create(window, size);
62 }
63
64 // a virtual dtor has been provided since this class has virtual members
65 virtual ~wxCaretBase() { }
66
67 // Create() functions - same as ctor but returns the success code
68 // --------------------------------------------------------------
69
70 // same as ctor
71 bool Create(wxWindowBase *window, int width, int height)
72 { return DoCreate(window, width, height); }
73 // same as ctor
74 bool Create(wxWindowBase *window, const wxSize& size)
75 { return DoCreate(window, size.x, size.y); }
76
77 // accessors
78 // ---------
79
80 // is the caret valid?
81 bool IsOk() const { return m_width != 0 && m_height != 0; }
82
83 // is the caret currently shown?
84 bool IsVisible() const { return m_countVisible > 0; }
85
86 // get the caret position
87 void GetPosition(int *x, int *y) const
88 {
89 if ( x ) *x = m_x;
90 if ( y ) *y = m_y;
91 }
92 wxPoint GetPosition() const { return wxPoint(m_x, m_y); }
93
94 // get the caret size
95 void GetSize(int *width, int *height) const
96 {
97 if ( width ) *width = m_width;
98 if ( height ) *height = m_height;
99 }
100 wxSize GetSize() const { return wxSize(m_width, m_height); }
101
102 // get the window we're associated with
103 wxWindow *GetWindow() const { return (wxWindow *)m_window; }
104
105 // change the size of the caret
106 void SetSize(int width, int height) {
107 m_width = width;
108 m_height = height;
109 DoSize();
110 }
111 void SetSize(const wxSize& size) { SetSize(size.x, size.y); }
112
113
114 // operations
115 // ----------
116
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(); }
120
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
123 // visible
124 virtual void Show(bool show = true)
125 {
126 if ( show )
127 {
128 if ( m_countVisible++ == 0 )
129 DoShow();
130 }
131 else
132 {
133 if ( --m_countVisible == 0 )
134 DoHide();
135 }
136 }
137 virtual void Hide() { Show(false); }
138
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);
144
145 // implementation from now on
146 // --------------------------
147
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() { }
152
153 protected:
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)
157 {
158 m_window = window;
159 m_width = width;
160 m_height = height;
161
162 return true;
163 }
164
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() { }
170
171 // the common initialization
172 void Init()
173 {
174 m_window = (wxWindowBase *)NULL;
175 m_x = m_y = 0;
176 m_width = m_height = 0;
177 m_countVisible = 0;
178 }
179
180 // the size of the caret
181 int m_width, m_height;
182
183 // the position of the caret
184 int m_x, m_y;
185
186 // the window we're associated with
187 wxWindowBase *m_window;
188
189 // visibility count: the caret is visible only if it's positive
190 int m_countVisible;
191
192 private:
193 DECLARE_NO_COPY_CLASS(wxCaretBase)
194 };
195
196 // ---------------------------------------------------------------------------
197 // now include the real thing
198 // ---------------------------------------------------------------------------
199
200 #if defined(__WXMSW__)
201 #include "wx/msw/caret.h"
202 #else
203 #include "wx/generic/caret.h"
204 #endif // platform
205
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 // ----------------------------------------------------------------------------
211
212 class WXDLLEXPORT wxCaretSuspend
213 {
214 public:
215 wxCaretSuspend(wxWindow *win)
216 {
217 m_caret = win->GetCaret();
218 m_show = false;
219 if ( m_caret && m_caret->IsVisible() )
220 {
221 m_caret->Hide();
222 m_show = true;
223 }
224 }
225
226 ~wxCaretSuspend()
227 {
228 if ( m_caret && m_show )
229 m_caret->Show();
230 }
231
232 private:
233 wxCaret *m_caret;
234 bool m_show;
235
236 DECLARE_NO_COPY_CLASS(wxCaretSuspend)
237 };
238
239 #endif // wxUSE_CARET
240
241 #endif // _WX_CARET_H_BASE_
242