]> git.saurik.com Git - wxWidgets.git/blame - include/wx/caret.h
wxUSE_SPLINES markup and other cleanings.
[wxWidgets.git] / include / wx / caret.h
CommitLineData
789295bf
VZ
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$
77ffb593 8// Copyright: (c) wxWidgets team
65571936 9// Licence: wxWindows licence
789295bf
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_CARET_H_BASE_
13#define _WX_CARET_H_BASE_
14
1e6feb95
VZ
15#include "wx/defs.h"
16
17#if wxUSE_CARET
18
12028905 19#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e4628635
RR
20#pragma interface "caret.h"
21#endif
22
789295bf
VZ
23// ---------------------------------------------------------------------------
24// forward declarations
25// ---------------------------------------------------------------------------
26
27class WXDLLEXPORT wxWindow;
28class WXDLLEXPORT wxWindowBase;
29
0290598f
VZ
30// ----------------------------------------------------------------------------
31// headers we have to include
32// ----------------------------------------------------------------------------
33
34#include "wx/gdicmn.h" // for wxPoint, wxSize
35
36// ----------------------------------------------------------------------------
789295bf
VZ
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)
0290598f 39// ----------------------------------------------------------------------------
789295bf
VZ
40
41class WXDLLEXPORT wxCaretBase
42{
43public:
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
ed62f740
VZ
64 // a virtual dtor has been provided since this class has virtual members
65 virtual ~wxCaretBase() { }
66
789295bf
VZ
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
0290598f
VZ
83 // is the caret currently shown?
84 bool IsVisible() const { return m_countVisible > 0; }
85
789295bf
VZ
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
87fbe614 105 // change the size of the caret
cfb76a19
RD
106 void SetSize(int width, int height) {
107 m_width = width;
108 m_height = height;
109 DoSize();
110 }
87fbe614
RD
111 void SetSize(const wxSize& size) { SetSize(size.x, size.y); }
112
113
789295bf
VZ
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
68379eaf 124 virtual void Show(bool show = true)
789295bf
VZ
125 {
126 if ( show )
127 {
764a3a49 128 if ( m_countVisible++ == 0 )
789295bf
VZ
129 DoShow();
130 }
131 else
132 {
764a3a49 133 if ( --m_countVisible == 0 )
789295bf
VZ
134 DoHide();
135 }
136 }
68379eaf 137 virtual void Hide() { Show(false); }
789295bf
VZ
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
153protected:
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
68379eaf 162 return true;
789295bf
VZ
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;
cfb76a19 169 virtual void DoSize() { }
789295bf
VZ
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
192private:
1a0d517e 193 DECLARE_NO_COPY_CLASS(wxCaretBase)
789295bf
VZ
194};
195
196// ---------------------------------------------------------------------------
197// now include the real thing
198// ---------------------------------------------------------------------------
199
0290598f 200#if defined(__WXMSW__)
789295bf
VZ
201 #include "wx/msw/caret.h"
202#else
0290598f 203 #include "wx/generic/caret.h"
789295bf
VZ
204#endif // platform
205
f4b8bf2f
VZ
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
212class WXDLLEXPORT wxCaretSuspend
213{
214public:
215 wxCaretSuspend(wxWindow *win)
216 {
217 m_caret = win->GetCaret();
68379eaf 218 m_show = false;
e28c2d15
JS
219 if ( m_caret && m_caret->IsVisible() )
220 {
f4b8bf2f 221 m_caret->Hide();
68379eaf 222 m_show = true;
e28c2d15 223 }
f4b8bf2f
VZ
224 }
225
226 ~wxCaretSuspend()
227 {
e28c2d15 228 if ( m_caret && m_show )
f4b8bf2f
VZ
229 m_caret->Show();
230 }
231
232private:
233 wxCaret *m_caret;
e28c2d15 234 bool m_show;
22f3361e
VZ
235
236 DECLARE_NO_COPY_CLASS(wxCaretSuspend)
f4b8bf2f
VZ
237};
238
1e6feb95
VZ
239#endif // wxUSE_CARET
240
789295bf
VZ
241#endif // _WX_CARET_H_BASE_
242