]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/caret.h | |
3 | // Purpose: wxCaretBase class - the interface of wxCaret | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 23.05.99 | |
7 | // Copyright: (c) wxWidgets team | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_CARET_H_BASE_ | |
12 | #define _WX_CARET_H_BASE_ | |
13 | ||
14 | #include "wx/defs.h" | |
15 | ||
16 | #if wxUSE_CARET | |
17 | ||
18 | // --------------------------------------------------------------------------- | |
19 | // forward declarations | |
20 | // --------------------------------------------------------------------------- | |
21 | ||
22 | class WXDLLIMPEXP_FWD_CORE wxWindow; | |
23 | class WXDLLIMPEXP_FWD_CORE wxWindowBase; | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // headers we have to include | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | #include "wx/gdicmn.h" // for wxPoint, wxSize | |
30 | ||
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 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | class WXDLLIMPEXP_CORE wxCaretBase | |
37 | { | |
38 | public: | |
39 | // ctors | |
40 | // ----- | |
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) | |
46 | { | |
47 | Init(); | |
48 | ||
49 | (void)Create(window, width, height); | |
50 | } | |
51 | // same as above | |
52 | wxCaretBase(wxWindowBase *window, const wxSize& size) | |
53 | { | |
54 | Init(); | |
55 | ||
56 | (void)Create(window, size); | |
57 | } | |
58 | ||
59 | // a virtual dtor has been provided since this class has virtual members | |
60 | virtual ~wxCaretBase() { } | |
61 | ||
62 | // Create() functions - same as ctor but returns the success code | |
63 | // -------------------------------------------------------------- | |
64 | ||
65 | // same as ctor | |
66 | bool Create(wxWindowBase *window, int width, int height) | |
67 | { return DoCreate(window, width, height); } | |
68 | // same as ctor | |
69 | bool Create(wxWindowBase *window, const wxSize& size) | |
70 | { return DoCreate(window, size.x, size.y); } | |
71 | ||
72 | // accessors | |
73 | // --------- | |
74 | ||
75 | // is the caret valid? | |
76 | bool IsOk() const { return m_width != 0 && m_height != 0; } | |
77 | ||
78 | // is the caret currently shown? | |
79 | bool IsVisible() const { return m_countVisible > 0; } | |
80 | ||
81 | // get the caret position | |
82 | void GetPosition(int *x, int *y) const | |
83 | { | |
84 | if ( x ) *x = m_x; | |
85 | if ( y ) *y = m_y; | |
86 | } | |
87 | wxPoint GetPosition() const { return wxPoint(m_x, m_y); } | |
88 | ||
89 | // get the caret size | |
90 | void GetSize(int *width, int *height) const | |
91 | { | |
92 | if ( width ) *width = m_width; | |
93 | if ( height ) *height = m_height; | |
94 | } | |
95 | wxSize GetSize() const { return wxSize(m_width, m_height); } | |
96 | ||
97 | // get the window we're associated with | |
98 | wxWindow *GetWindow() const { return (wxWindow *)m_window; } | |
99 | ||
100 | // change the size of the caret | |
101 | void SetSize(int width, int height) { | |
102 | m_width = width; | |
103 | m_height = height; | |
104 | DoSize(); | |
105 | } | |
106 | void SetSize(const wxSize& size) { SetSize(size.x, size.y); } | |
107 | ||
108 | ||
109 | // operations | |
110 | // ---------- | |
111 | ||
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(); } | |
115 | ||
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 | |
118 | // visible | |
119 | virtual void Show(bool show = true) | |
120 | { | |
121 | if ( show ) | |
122 | { | |
123 | if ( m_countVisible++ == 0 ) | |
124 | DoShow(); | |
125 | } | |
126 | else | |
127 | { | |
128 | if ( --m_countVisible == 0 ) | |
129 | DoHide(); | |
130 | } | |
131 | } | |
132 | virtual void Hide() { Show(false); } | |
133 | ||
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); | |
139 | ||
140 | // implementation from now on | |
141 | // -------------------------- | |
142 | ||
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() { } | |
147 | ||
148 | protected: | |
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) | |
152 | { | |
153 | m_window = window; | |
154 | m_width = width; | |
155 | m_height = height; | |
156 | ||
157 | return true; | |
158 | } | |
159 | ||
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() { } | |
165 | ||
166 | // the common initialization | |
167 | void Init() | |
168 | { | |
169 | m_window = NULL; | |
170 | m_x = m_y = 0; | |
171 | m_width = m_height = 0; | |
172 | m_countVisible = 0; | |
173 | } | |
174 | ||
175 | // the size of the caret | |
176 | int m_width, m_height; | |
177 | ||
178 | // the position of the caret | |
179 | int m_x, m_y; | |
180 | ||
181 | // the window we're associated with | |
182 | wxWindowBase *m_window; | |
183 | ||
184 | // visibility count: the caret is visible only if it's positive | |
185 | int m_countVisible; | |
186 | ||
187 | private: | |
188 | wxDECLARE_NO_COPY_CLASS(wxCaretBase); | |
189 | }; | |
190 | ||
191 | // --------------------------------------------------------------------------- | |
192 | // now include the real thing | |
193 | // --------------------------------------------------------------------------- | |
194 | ||
195 | #if defined(__WXMSW__) | |
196 | #include "wx/msw/caret.h" | |
197 | #else | |
198 | #include "wx/generic/caret.h" | |
199 | #endif // platform | |
200 | ||
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 | // ---------------------------------------------------------------------------- | |
206 | ||
207 | #ifdef wxHAS_CARET_USING_OVERLAYS | |
208 | ||
209 | // we don't need to hide the caret if it's rendered using overlays | |
210 | class WXDLLIMPEXP_CORE wxCaretSuspend | |
211 | { | |
212 | public: | |
213 | wxCaretSuspend(wxWindow *WXUNUSED(win)) {} | |
214 | ||
215 | wxDECLARE_NO_COPY_CLASS(wxCaretSuspend); | |
216 | }; | |
217 | ||
218 | #else // !wxHAS_CARET_USING_OVERLAYS | |
219 | ||
220 | class WXDLLIMPEXP_CORE wxCaretSuspend | |
221 | { | |
222 | public: | |
223 | wxCaretSuspend(wxWindow *win) | |
224 | { | |
225 | m_caret = win->GetCaret(); | |
226 | m_show = false; | |
227 | if ( m_caret && m_caret->IsVisible() ) | |
228 | { | |
229 | m_caret->Hide(); | |
230 | m_show = true; | |
231 | } | |
232 | } | |
233 | ||
234 | ~wxCaretSuspend() | |
235 | { | |
236 | if ( m_caret && m_show ) | |
237 | m_caret->Show(); | |
238 | } | |
239 | ||
240 | private: | |
241 | wxCaret *m_caret; | |
242 | bool m_show; | |
243 | ||
244 | wxDECLARE_NO_COPY_CLASS(wxCaretSuspend); | |
245 | }; | |
246 | ||
247 | #endif // wxHAS_CARET_USING_OVERLAYS/!wxHAS_CARET_USING_OVERLAYS | |
248 | ||
249 | #endif // wxUSE_CARET | |
250 | ||
251 | #endif // _WX_CARET_H_BASE_ |