]>
Commit | Line | Data |
---|---|---|
789295bf | 1 | /////////////////////////////////////////////////////////////////////////////// |
ce7208d4 | 2 | // Name: wx/caret.h |
789295bf VZ |
3 | // Purpose: wxCaretBase class - the interface of wxCaret |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 23.05.99 | |
77ffb593 | 7 | // Copyright: (c) wxWidgets team |
65571936 | 8 | // Licence: wxWindows licence |
789295bf VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | #ifndef _WX_CARET_H_BASE_ | |
12 | #define _WX_CARET_H_BASE_ | |
13 | ||
1e6feb95 VZ |
14 | #include "wx/defs.h" |
15 | ||
16 | #if wxUSE_CARET | |
17 | ||
789295bf VZ |
18 | // --------------------------------------------------------------------------- |
19 | // forward declarations | |
20 | // --------------------------------------------------------------------------- | |
21 | ||
b5dbe15d VS |
22 | class WXDLLIMPEXP_FWD_CORE wxWindow; |
23 | class WXDLLIMPEXP_FWD_CORE wxWindowBase; | |
789295bf | 24 | |
0290598f VZ |
25 | // ---------------------------------------------------------------------------- |
26 | // headers we have to include | |
27 | // ---------------------------------------------------------------------------- | |
28 | ||
29 | #include "wx/gdicmn.h" // for wxPoint, wxSize | |
30 | ||
31 | // ---------------------------------------------------------------------------- | |
789295bf VZ |
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) | |
0290598f | 34 | // ---------------------------------------------------------------------------- |
789295bf | 35 | |
53a2db12 | 36 | class WXDLLIMPEXP_CORE wxCaretBase |
789295bf VZ |
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 | ||
ed62f740 VZ |
59 | // a virtual dtor has been provided since this class has virtual members |
60 | virtual ~wxCaretBase() { } | |
61 | ||
789295bf VZ |
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 | ||
0290598f VZ |
78 | // is the caret currently shown? |
79 | bool IsVisible() const { return m_countVisible > 0; } | |
80 | ||
789295bf VZ |
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 | ||
87fbe614 | 100 | // change the size of the caret |
cfb76a19 RD |
101 | void SetSize(int width, int height) { |
102 | m_width = width; | |
103 | m_height = height; | |
104 | DoSize(); | |
105 | } | |
87fbe614 RD |
106 | void SetSize(const wxSize& size) { SetSize(size.x, size.y); } |
107 | ||
108 | ||
789295bf VZ |
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 | |
68379eaf | 119 | virtual void Show(bool show = true) |
789295bf VZ |
120 | { |
121 | if ( show ) | |
122 | { | |
764a3a49 | 123 | if ( m_countVisible++ == 0 ) |
789295bf VZ |
124 | DoShow(); |
125 | } | |
126 | else | |
127 | { | |
764a3a49 | 128 | if ( --m_countVisible == 0 ) |
789295bf VZ |
129 | DoHide(); |
130 | } | |
131 | } | |
68379eaf | 132 | virtual void Hide() { Show(false); } |
789295bf VZ |
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: | |
4c51a665 | 149 | // these functions may be overridden in the derived classes, but they |
789295bf VZ |
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 | ||
68379eaf | 157 | return true; |
789295bf VZ |
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; | |
cfb76a19 | 164 | virtual void DoSize() { } |
789295bf VZ |
165 | |
166 | // the common initialization | |
167 | void Init() | |
168 | { | |
d3b9f782 | 169 | m_window = NULL; |
789295bf VZ |
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: | |
c0c133e1 | 188 | wxDECLARE_NO_COPY_CLASS(wxCaretBase); |
789295bf VZ |
189 | }; |
190 | ||
191 | // --------------------------------------------------------------------------- | |
192 | // now include the real thing | |
193 | // --------------------------------------------------------------------------- | |
194 | ||
0290598f | 195 | #if defined(__WXMSW__) |
789295bf VZ |
196 | #include "wx/msw/caret.h" |
197 | #else | |
0290598f | 198 | #include "wx/generic/caret.h" |
789295bf VZ |
199 | #endif // platform |
200 | ||
f4b8bf2f VZ |
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 | ||
69a5bc23 | 207 | #ifdef wxHAS_CARET_USING_OVERLAYS |
30c841c8 VS |
208 | |
209 | // we don't need to hide the caret if it's rendered using overlays | |
53a2db12 | 210 | class WXDLLIMPEXP_CORE wxCaretSuspend |
30c841c8 VS |
211 | { |
212 | public: | |
213 | wxCaretSuspend(wxWindow *WXUNUSED(win)) {} | |
214 | ||
c0c133e1 | 215 | wxDECLARE_NO_COPY_CLASS(wxCaretSuspend); |
30c841c8 VS |
216 | }; |
217 | ||
218 | #else // !wxHAS_CARET_USING_OVERLAYS | |
219 | ||
53a2db12 | 220 | class WXDLLIMPEXP_CORE wxCaretSuspend |
f4b8bf2f VZ |
221 | { |
222 | public: | |
223 | wxCaretSuspend(wxWindow *win) | |
224 | { | |
225 | m_caret = win->GetCaret(); | |
68379eaf | 226 | m_show = false; |
e28c2d15 JS |
227 | if ( m_caret && m_caret->IsVisible() ) |
228 | { | |
f4b8bf2f | 229 | m_caret->Hide(); |
68379eaf | 230 | m_show = true; |
e28c2d15 | 231 | } |
f4b8bf2f VZ |
232 | } |
233 | ||
234 | ~wxCaretSuspend() | |
235 | { | |
e28c2d15 | 236 | if ( m_caret && m_show ) |
f4b8bf2f VZ |
237 | m_caret->Show(); |
238 | } | |
239 | ||
240 | private: | |
241 | wxCaret *m_caret; | |
e28c2d15 | 242 | bool m_show; |
22f3361e | 243 | |
c0c133e1 | 244 | wxDECLARE_NO_COPY_CLASS(wxCaretSuspend); |
f4b8bf2f VZ |
245 | }; |
246 | ||
30c841c8 VS |
247 | #endif // wxHAS_CARET_USING_OVERLAYS/!wxHAS_CARET_USING_OVERLAYS |
248 | ||
1e6feb95 VZ |
249 | #endif // wxUSE_CARET |
250 | ||
789295bf | 251 | #endif // _WX_CARET_H_BASE_ |