]> git.saurik.com Git - wxWidgets.git/blame - src/generic/caret.cpp
Forward port of r60190 (wxMSW Cairo support) to trunk.
[wxWidgets.git] / src / generic / caret.cpp
CommitLineData
0290598f 1///////////////////////////////////////////////////////////////////////////////
f6bcfd97
BP
2// Name: generic/caret.cpp
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;
81}
82
83// ----------------------------------------------------------------------------
84// initialization and destruction
85// ----------------------------------------------------------------------------
86
87void wxCaret::InitGeneric()
88{
ca65c044
WS
89 m_hasFocus = true;
90 m_blinkedOut = true;
69a5bc23 91#ifndef wxHAS_CARET_USING_OVERLAYS
f4b8bf2f
VZ
92 m_xOld =
93 m_yOld = -1;
94 m_bmpUnderCaret.Create(m_width, m_height);
af76254d 95#endif
0290598f
VZ
96}
97
98wxCaret::~wxCaret()
99{
100 if ( IsVisible() )
101 {
102 // stop blinking
697e0cdd
VZ
103 if ( m_timer.IsRunning() )
104 m_timer.Stop();
0290598f
VZ
105 }
106}
107
108// ----------------------------------------------------------------------------
109// showing/hiding/moving the caret (base class interface)
110// ----------------------------------------------------------------------------
111
112void wxCaret::DoShow()
113{
697e0cdd
VZ
114 int blinkTime = GetBlinkTime();
115 if ( blinkTime )
116 m_timer.Start(blinkTime);
0290598f 117
ca5b5b49
VS
118 if ( m_blinkedOut )
119 Blink();
0290598f
VZ
120}
121
122void wxCaret::DoHide()
123{
124 m_timer.Stop();
125
126 if ( !m_blinkedOut )
127 {
128 Blink();
129 }
130}
131
132void wxCaret::DoMove()
133{
69a5bc23 134#ifdef wxHAS_CARET_USING_OVERLAYS
5cf94cc7
SC
135 m_overlay.Reset();
136#endif
697e0cdd 137 if ( IsVisible() )
0290598f 138 {
697e0cdd
VZ
139 if ( !m_blinkedOut )
140 {
141 // hide it right now and it will be shown the next time it blinks
f4b8bf2f
VZ
142 Blink();
143
144 // but if the caret is not blinking, we should blink it back into
145 // visibility manually
146 if ( !m_timer.IsRunning() )
697e0cdd 147 Blink();
697e0cdd 148 }
0290598f 149 }
697e0cdd 150 //else: will be shown at the correct location when it is shown
0290598f
VZ
151}
152
c328d0ad
VZ
153void wxCaret::DoSize()
154{
155 int countVisible = m_countVisible;
156 if (countVisible > 0)
157 {
158 m_countVisible = 0;
159 DoHide();
160 }
69a5bc23 161#ifdef wxHAS_CARET_USING_OVERLAYS
5cf94cc7
SC
162 m_overlay.Reset();
163#else
c328d0ad
VZ
164 // Change bitmap size
165 m_bmpUnderCaret = wxBitmap(m_width, m_height);
af76254d 166#endif
c328d0ad
VZ
167 if (countVisible > 0)
168 {
169 m_countVisible = countVisible;
170 DoShow();
171 }
172}
173
f6bcfd97
BP
174// ----------------------------------------------------------------------------
175// handling the focus
176// ----------------------------------------------------------------------------
177
178void wxCaret::OnSetFocus()
179{
ca65c044 180 m_hasFocus = true;
f6bcfd97 181
b6e8d5a3
VS
182 if ( IsVisible() )
183 Refresh();
f6bcfd97
BP
184}
185
186void wxCaret::OnKillFocus()
187{
ca65c044 188 m_hasFocus = false;
f6bcfd97
BP
189
190 if ( IsVisible() )
191 {
192 // the caret must be shown - otherwise, if it is hidden now, it will
193 // stay so until the focus doesn't return because it won't blink any
194 // more
f6bcfd97 195
f4b8bf2f
VZ
196 // hide it first if it isn't hidden ...
197 if ( !m_blinkedOut )
198 Blink();
199
200 // .. and show it in the new style
201 Blink();
202 }
f6bcfd97
BP
203}
204
0290598f
VZ
205// ----------------------------------------------------------------------------
206// drawing the caret
207// ----------------------------------------------------------------------------
208
209void wxCaret::Blink()
210{
211 m_blinkedOut = !m_blinkedOut;
212
f6bcfd97
BP
213 Refresh();
214}
215
216void wxCaret::Refresh()
217{
f4b8bf2f 218 wxClientDC dcWin(GetWindow());
af76254d 219// this is the new code, switch to 0 if this gives problems
69a5bc23 220#ifdef wxHAS_CARET_USING_OVERLAYS
af76254d
SC
221 wxDCOverlay dcOverlay( m_overlay, &dcWin, m_x, m_y, m_width , m_height );
222 if ( m_blinkedOut )
223 {
224 dcOverlay.Clear();
225 }
226 else
227 {
44bcee11 228 DoDraw( &dcWin, GetWindow() );
af76254d
SC
229 }
230#else
f4b8bf2f
VZ
231 wxMemoryDC dcMem;
232 dcMem.SelectObject(m_bmpUnderCaret);
233 if ( m_blinkedOut )
0290598f 234 {
f4b8bf2f
VZ
235 // restore the old image
236 dcWin.Blit(m_xOld, m_yOld, m_width, m_height,
237 &dcMem, 0, 0);
238 m_xOld =
239 m_yOld = -1;
0290598f
VZ
240 }
241 else
242 {
f4b8bf2f
VZ
243 if ( m_xOld == -1 && m_yOld == -1 )
244 {
245 // save the part we're going to overdraw
246 dcMem.Blit(0, 0, m_width, m_height,
a907139a 247 &dcWin, m_x, m_y);
1e6feb95 248
f4b8bf2f
VZ
249 m_xOld = m_x;
250 m_yOld = m_y;
251 }
252 //else: we already saved the image below the caret, don't do it any
253 // more
f6bcfd97 254
f4b8bf2f 255 // and draw the caret there
44bcee11 256 DoDraw(&dcWin, GetWindow());
0290598f 257 }
af76254d 258#endif
0290598f
VZ
259}
260
44bcee11 261void wxCaret::DoDraw(wxDC *dc, wxWindow* win)
0290598f 262{
44bcee11
JS
263 wxPen pen(*wxBLACK_PEN);
264 wxBrush brush(*wxBLACK_BRUSH);
265 if (win)
b331ca89 266 {
44bcee11
JS
267 wxColour backgroundColour(win->GetBackgroundColour());
268 if (backgroundColour.Red() < 100 &&
269 backgroundColour.Green() < 100 &&
270 backgroundColour.Blue() < 100)
b331ca89 271 {
44bcee11
JS
272 pen = *wxWHITE_PEN;
273 brush = *wxWHITE_BRUSH;
b331ca89 274 }
b331ca89 275 }
44bcee11
JS
276 dc->SetPen( pen );
277 dc->SetBrush(m_hasFocus ? brush : *wxTRANSPARENT_BRUSH);
f4b8bf2f
VZ
278
279 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
280 // done under wxGTK - no idea why
281 //dc->SetLogicalFunction(wxINVERT);
282
283 dc->DrawRectangle(m_x, m_y, m_width, m_height);
0290598f 284}
697e0cdd
VZ
285
286#endif // wxUSE_CARET