]> git.saurik.com Git - wxWidgets.git/blame - src/generic/caret.cpp
Don't crash in wxGridCellAutoWrapStringRenderer when the column is hidden.
[wxWidgets.git] / src / generic / caret.cpp
CommitLineData
0290598f 1///////////////////////////////////////////////////////////////////////////////
80fdcdb9 2// Name: src/generic/caret.cpp
f6bcfd97 3// Purpose: generic wxCaret class implementation
0290598f
VZ
4// Author: Vadim Zeitlin (original code by Robert Roebling)
5// Modified by:
6// Created: 25.05.99
77ffb593 7// Copyright: (c) wxWidgets team
65571936 8// Licence: wxWindows licence
0290598f
VZ
9///////////////////////////////////////////////////////////////////////////////
10
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
0290598f
VZ
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
697e0cdd
VZ
26#if wxUSE_CARET
27
0290598f 28#ifndef WX_PRECOMP
e179bd65
RR
29 #include "wx/window.h"
30 #include "wx/dcclient.h"
f4b8bf2f 31 #include "wx/dcmemory.h"
0290598f
VZ
32#endif //WX_PRECOMP
33
34#include "wx/caret.h"
35
36// ----------------------------------------------------------------------------
37// global variables for this module
38// ----------------------------------------------------------------------------
39
40// the blink time (common to all carets for MSW compatibility)
41static int gs_blinkTime = 500; // in milliseconds
42
43// ============================================================================
44// implementation
45// ============================================================================
46
f6bcfd97
BP
47// ----------------------------------------------------------------------------
48// timer stuff
49// ----------------------------------------------------------------------------
50
ca65c044
WS
51wxCaretTimer::wxCaretTimer(wxCaret *caret)
52{
53 m_caret = caret;
e4628635
RR
54}
55
ca65c044
WS
56void wxCaretTimer::Notify()
57{
58 m_caret->OnTimer();
f6bcfd97
BP
59}
60
61void wxCaret::OnTimer()
62{
63 // don't blink the caret when we don't have the focus
64 if ( m_hasFocus )
65 Blink();
e4628635
RR
66}
67
0290598f
VZ
68// ----------------------------------------------------------------------------
69// wxCaret static functions and data
70// ----------------------------------------------------------------------------
71
0290598f
VZ
72int wxCaretBase::GetBlinkTime()
73{
74 return gs_blinkTime;
75}
76
77void wxCaretBase::SetBlinkTime(int milliseconds)
78{
79 gs_blinkTime = milliseconds;
a352fbb0
JS
80
81#ifdef _WXGTK__
82 GtkSettings *settings = gtk_settings_get_default();
83 if (millseconds == 0)
84 {
85 gtk_settings_set_long_property(settings, "gtk-cursor-blink", gtk_false, NULL);
86 }
87 else
88 {
89 gtk_settings_set_long_property(settings, "gtk-cursor-blink", gtk_true, NULL);
90 gtk_settings_set_long_property(settings, "gtk-cursor-time", milliseconds, NULL);
91 }
92#endif
0290598f
VZ
93}
94
95// ----------------------------------------------------------------------------
96// initialization and destruction
97// ----------------------------------------------------------------------------
98
99void wxCaret::InitGeneric()
100{
ca65c044
WS
101 m_hasFocus = true;
102 m_blinkedOut = true;
69a5bc23 103#ifndef wxHAS_CARET_USING_OVERLAYS
f4b8bf2f
VZ
104 m_xOld =
105 m_yOld = -1;
106 m_bmpUnderCaret.Create(m_width, m_height);
af76254d 107#endif
0290598f
VZ
108}
109
110wxCaret::~wxCaret()
111{
112 if ( IsVisible() )
113 {
114 // stop blinking
697e0cdd
VZ
115 if ( m_timer.IsRunning() )
116 m_timer.Stop();
0290598f
VZ
117 }
118}
119
120// ----------------------------------------------------------------------------
121// showing/hiding/moving the caret (base class interface)
122// ----------------------------------------------------------------------------
123
124void wxCaret::DoShow()
125{
697e0cdd
VZ
126 int blinkTime = GetBlinkTime();
127 if ( blinkTime )
128 m_timer.Start(blinkTime);
0290598f 129
ca5b5b49
VS
130 if ( m_blinkedOut )
131 Blink();
0290598f
VZ
132}
133
134void wxCaret::DoHide()
135{
136 m_timer.Stop();
137
138 if ( !m_blinkedOut )
139 {
140 Blink();
141 }
142}
143
144void wxCaret::DoMove()
145{
69a5bc23 146#ifdef wxHAS_CARET_USING_OVERLAYS
5cf94cc7
SC
147 m_overlay.Reset();
148#endif
697e0cdd 149 if ( IsVisible() )
0290598f 150 {
697e0cdd
VZ
151 if ( !m_blinkedOut )
152 {
153 // hide it right now and it will be shown the next time it blinks
f4b8bf2f
VZ
154 Blink();
155
156 // but if the caret is not blinking, we should blink it back into
157 // visibility manually
158 if ( !m_timer.IsRunning() )
697e0cdd 159 Blink();
697e0cdd 160 }
0290598f 161 }
697e0cdd 162 //else: will be shown at the correct location when it is shown
0290598f
VZ
163}
164
c328d0ad
VZ
165void wxCaret::DoSize()
166{
167 int countVisible = m_countVisible;
168 if (countVisible > 0)
169 {
170 m_countVisible = 0;
171 DoHide();
172 }
69a5bc23 173#ifdef wxHAS_CARET_USING_OVERLAYS
5cf94cc7
SC
174 m_overlay.Reset();
175#else
c328d0ad
VZ
176 // Change bitmap size
177 m_bmpUnderCaret = wxBitmap(m_width, m_height);
af76254d 178#endif
c328d0ad
VZ
179 if (countVisible > 0)
180 {
181 m_countVisible = countVisible;
182 DoShow();
183 }
184}
185
f6bcfd97
BP
186// ----------------------------------------------------------------------------
187// handling the focus
188// ----------------------------------------------------------------------------
189
190void wxCaret::OnSetFocus()
191{
ca65c044 192 m_hasFocus = true;
f6bcfd97 193
b6e8d5a3
VS
194 if ( IsVisible() )
195 Refresh();
f6bcfd97
BP
196}
197
198void wxCaret::OnKillFocus()
199{
ca65c044 200 m_hasFocus = false;
f6bcfd97
BP
201
202 if ( IsVisible() )
203 {
204 // the caret must be shown - otherwise, if it is hidden now, it will
205 // stay so until the focus doesn't return because it won't blink any
206 // more
f6bcfd97 207
f4b8bf2f
VZ
208 // hide it first if it isn't hidden ...
209 if ( !m_blinkedOut )
210 Blink();
211
212 // .. and show it in the new style
213 Blink();
214 }
f6bcfd97
BP
215}
216
0290598f
VZ
217// ----------------------------------------------------------------------------
218// drawing the caret
219// ----------------------------------------------------------------------------
220
221void wxCaret::Blink()
222{
223 m_blinkedOut = !m_blinkedOut;
224
f6bcfd97
BP
225 Refresh();
226}
227
228void wxCaret::Refresh()
229{
f4b8bf2f 230 wxClientDC dcWin(GetWindow());
af76254d 231// this is the new code, switch to 0 if this gives problems
69a5bc23 232#ifdef wxHAS_CARET_USING_OVERLAYS
af76254d
SC
233 wxDCOverlay dcOverlay( m_overlay, &dcWin, m_x, m_y, m_width , m_height );
234 if ( m_blinkedOut )
235 {
236 dcOverlay.Clear();
237 }
238 else
239 {
44bcee11 240 DoDraw( &dcWin, GetWindow() );
af76254d
SC
241 }
242#else
f4b8bf2f
VZ
243 wxMemoryDC dcMem;
244 dcMem.SelectObject(m_bmpUnderCaret);
245 if ( m_blinkedOut )
0290598f 246 {
f4b8bf2f
VZ
247 // restore the old image
248 dcWin.Blit(m_xOld, m_yOld, m_width, m_height,
249 &dcMem, 0, 0);
250 m_xOld =
251 m_yOld = -1;
0290598f
VZ
252 }
253 else
254 {
f4b8bf2f
VZ
255 if ( m_xOld == -1 && m_yOld == -1 )
256 {
257 // save the part we're going to overdraw
258 dcMem.Blit(0, 0, m_width, m_height,
a907139a 259 &dcWin, m_x, m_y);
1e6feb95 260
f4b8bf2f
VZ
261 m_xOld = m_x;
262 m_yOld = m_y;
263 }
264 //else: we already saved the image below the caret, don't do it any
265 // more
f6bcfd97 266
f4b8bf2f 267 // and draw the caret there
44bcee11 268 DoDraw(&dcWin, GetWindow());
0290598f 269 }
af76254d 270#endif
0290598f
VZ
271}
272
44bcee11 273void wxCaret::DoDraw(wxDC *dc, wxWindow* win)
0290598f 274{
44bcee11
JS
275 wxPen pen(*wxBLACK_PEN);
276 wxBrush brush(*wxBLACK_BRUSH);
277 if (win)
b331ca89 278 {
44bcee11
JS
279 wxColour backgroundColour(win->GetBackgroundColour());
280 if (backgroundColour.Red() < 100 &&
281 backgroundColour.Green() < 100 &&
282 backgroundColour.Blue() < 100)
b331ca89 283 {
44bcee11
JS
284 pen = *wxWHITE_PEN;
285 brush = *wxWHITE_BRUSH;
b331ca89 286 }
b331ca89 287 }
44bcee11
JS
288 dc->SetPen( pen );
289 dc->SetBrush(m_hasFocus ? brush : *wxTRANSPARENT_BRUSH);
f4b8bf2f
VZ
290
291 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
292 // done under wxGTK - no idea why
293 //dc->SetLogicalFunction(wxINVERT);
294
295 dc->DrawRectangle(m_x, m_y, m_width, m_height);
0290598f 296}
697e0cdd
VZ
297
298#endif // wxUSE_CARET