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