]> git.saurik.com Git - wxWidgets.git/blame - include/wx/caret.h
fix wxBitmapComboBox Gtk-CRITICAL assertion `GTK_IS_ENTRY (entry)' failed
[wxWidgets.git] / include / wx / caret.h
CommitLineData
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
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
789295bf
VZ
19// ---------------------------------------------------------------------------
20// forward declarations
21// ---------------------------------------------------------------------------
22
b5dbe15d
VS
23class WXDLLIMPEXP_FWD_CORE wxWindow;
24class WXDLLIMPEXP_FWD_CORE wxWindowBase;
789295bf 25
0290598f
VZ
26// ----------------------------------------------------------------------------
27// headers we have to include
28// ----------------------------------------------------------------------------
29
30#include "wx/gdicmn.h" // for wxPoint, wxSize
31
32// ----------------------------------------------------------------------------
789295bf
VZ
33// A caret is a blinking cursor showing the position where the typed text will
34// appear. It can be either a solid block or a custom bitmap (TODO)
0290598f 35// ----------------------------------------------------------------------------
789295bf 36
53a2db12 37class WXDLLIMPEXP_CORE wxCaretBase
789295bf
VZ
38{
39public:
40 // ctors
41 // -----
42 // default - use Create
43 wxCaretBase() { Init(); }
44 // create the caret of given (in pixels) width and height and associate
45 // with the given window
46 wxCaretBase(wxWindowBase *window, int width, int height)
47 {
48 Init();
49
50 (void)Create(window, width, height);
51 }
52 // same as above
53 wxCaretBase(wxWindowBase *window, const wxSize& size)
54 {
55 Init();
56
57 (void)Create(window, size);
58 }
59
ed62f740
VZ
60 // a virtual dtor has been provided since this class has virtual members
61 virtual ~wxCaretBase() { }
62
789295bf
VZ
63 // Create() functions - same as ctor but returns the success code
64 // --------------------------------------------------------------
65
66 // same as ctor
67 bool Create(wxWindowBase *window, int width, int height)
68 { return DoCreate(window, width, height); }
69 // same as ctor
70 bool Create(wxWindowBase *window, const wxSize& size)
71 { return DoCreate(window, size.x, size.y); }
72
73 // accessors
74 // ---------
75
76 // is the caret valid?
77 bool IsOk() const { return m_width != 0 && m_height != 0; }
78
0290598f
VZ
79 // is the caret currently shown?
80 bool IsVisible() const { return m_countVisible > 0; }
81
789295bf
VZ
82 // get the caret position
83 void GetPosition(int *x, int *y) const
84 {
85 if ( x ) *x = m_x;
86 if ( y ) *y = m_y;
87 }
88 wxPoint GetPosition() const { return wxPoint(m_x, m_y); }
89
90 // get the caret size
91 void GetSize(int *width, int *height) const
92 {
93 if ( width ) *width = m_width;
94 if ( height ) *height = m_height;
95 }
96 wxSize GetSize() const { return wxSize(m_width, m_height); }
97
98 // get the window we're associated with
99 wxWindow *GetWindow() const { return (wxWindow *)m_window; }
100
87fbe614 101 // change the size of the caret
cfb76a19
RD
102 void SetSize(int width, int height) {
103 m_width = width;
104 m_height = height;
105 DoSize();
106 }
87fbe614
RD
107 void SetSize(const wxSize& size) { SetSize(size.x, size.y); }
108
109
789295bf
VZ
110 // operations
111 // ----------
112
113 // move the caret to given position (in logical coords)
114 void Move(int x, int y) { m_x = x; m_y = y; DoMove(); }
115 void Move(const wxPoint& pt) { m_x = pt.x; m_y = pt.y; DoMove(); }
116
117 // show/hide the caret (should be called by wxWindow when needed):
118 // Show() must be called as many times as Hide() + 1 to make the caret
119 // visible
68379eaf 120 virtual void Show(bool show = true)
789295bf
VZ
121 {
122 if ( show )
123 {
764a3a49 124 if ( m_countVisible++ == 0 )
789295bf
VZ
125 DoShow();
126 }
127 else
128 {
764a3a49 129 if ( --m_countVisible == 0 )
789295bf
VZ
130 DoHide();
131 }
132 }
68379eaf 133 virtual void Hide() { Show(false); }
789295bf
VZ
134
135 // blink time is measured in milliseconds and is the time elapsed
136 // between 2 inversions of the caret (blink time of the caret is common
137 // to all carets in the Universe, so these functions are static)
138 static int GetBlinkTime();
139 static void SetBlinkTime(int milliseconds);
140
141 // implementation from now on
142 // --------------------------
143
144 // these functions should be called by wxWindow when the window gets/loses
145 // the focus - we create/show and hide/destroy the caret here
146 virtual void OnSetFocus() { }
147 virtual void OnKillFocus() { }
148
149protected:
4c51a665 150 // these functions may be overridden in the derived classes, but they
789295bf
VZ
151 // should call the base class version first
152 virtual bool DoCreate(wxWindowBase *window, int width, int height)
153 {
154 m_window = window;
155 m_width = width;
156 m_height = height;
157
68379eaf 158 return true;
789295bf
VZ
159 }
160
161 // pure virtuals to implement in the derived class
162 virtual void DoShow() = 0;
163 virtual void DoHide() = 0;
164 virtual void DoMove() = 0;
cfb76a19 165 virtual void DoSize() { }
789295bf
VZ
166
167 // the common initialization
168 void Init()
169 {
d3b9f782 170 m_window = NULL;
789295bf
VZ
171 m_x = m_y = 0;
172 m_width = m_height = 0;
173 m_countVisible = 0;
174 }
175
176 // the size of the caret
177 int m_width, m_height;
178
179 // the position of the caret
180 int m_x, m_y;
181
182 // the window we're associated with
183 wxWindowBase *m_window;
184
185 // visibility count: the caret is visible only if it's positive
186 int m_countVisible;
187
188private:
c0c133e1 189 wxDECLARE_NO_COPY_CLASS(wxCaretBase);
789295bf
VZ
190};
191
192// ---------------------------------------------------------------------------
193// now include the real thing
194// ---------------------------------------------------------------------------
195
0290598f 196#if defined(__WXMSW__)
789295bf
VZ
197 #include "wx/msw/caret.h"
198#else
0290598f 199 #include "wx/generic/caret.h"
789295bf
VZ
200#endif // platform
201
f4b8bf2f
VZ
202// ----------------------------------------------------------------------------
203// wxCaretSuspend: a simple class which hides the caret in its ctor and
204// restores it in the dtor, this should be used when drawing on the screen to
205// avoid overdrawing the caret
206// ----------------------------------------------------------------------------
207
69a5bc23 208#ifdef wxHAS_CARET_USING_OVERLAYS
30c841c8
VS
209
210// we don't need to hide the caret if it's rendered using overlays
53a2db12 211class WXDLLIMPEXP_CORE wxCaretSuspend
30c841c8
VS
212{
213public:
214 wxCaretSuspend(wxWindow *WXUNUSED(win)) {}
215
c0c133e1 216 wxDECLARE_NO_COPY_CLASS(wxCaretSuspend);
30c841c8
VS
217};
218
219#else // !wxHAS_CARET_USING_OVERLAYS
220
53a2db12 221class WXDLLIMPEXP_CORE wxCaretSuspend
f4b8bf2f
VZ
222{
223public:
224 wxCaretSuspend(wxWindow *win)
225 {
226 m_caret = win->GetCaret();
68379eaf 227 m_show = false;
e28c2d15
JS
228 if ( m_caret && m_caret->IsVisible() )
229 {
f4b8bf2f 230 m_caret->Hide();
68379eaf 231 m_show = true;
e28c2d15 232 }
f4b8bf2f
VZ
233 }
234
235 ~wxCaretSuspend()
236 {
e28c2d15 237 if ( m_caret && m_show )
f4b8bf2f
VZ
238 m_caret->Show();
239 }
240
241private:
242 wxCaret *m_caret;
e28c2d15 243 bool m_show;
22f3361e 244
c0c133e1 245 wxDECLARE_NO_COPY_CLASS(wxCaretSuspend);
f4b8bf2f
VZ
246};
247
30c841c8
VS
248#endif // wxHAS_CARET_USING_OVERLAYS/!wxHAS_CARET_USING_OVERLAYS
249
1e6feb95
VZ
250#endif // wxUSE_CARET
251
789295bf 252#endif // _WX_CARET_H_BASE_