]> git.saurik.com Git - wxWidgets.git/blob - src/generic/caret.cpp
Disable wxUSE_ENH_METAFILE for wxGTK builds.
[wxWidgets.git] / src / generic / caret.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/caret.cpp
3 // Purpose: generic wxCaret class implementation
4 // Author: Vadim Zeitlin (original code by Robert Roebling)
5 // Modified by:
6 // Created: 25.05.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CARET
28
29 #ifndef WX_PRECOMP
30 #include "wx/window.h"
31 #include "wx/dcclient.h"
32 #include "wx/dcmemory.h"
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)
42 static int gs_blinkTime = 500; // in milliseconds
43
44 // ============================================================================
45 // implementation
46 // ============================================================================
47
48 // ----------------------------------------------------------------------------
49 // timer stuff
50 // ----------------------------------------------------------------------------
51
52 wxCaretTimer::wxCaretTimer(wxCaret *caret)
53 {
54 m_caret = caret;
55 }
56
57 void wxCaretTimer::Notify()
58 {
59 m_caret->OnTimer();
60 }
61
62 void wxCaret::OnTimer()
63 {
64 // don't blink the caret when we don't have the focus
65 if ( m_hasFocus )
66 Blink();
67 }
68
69 // ----------------------------------------------------------------------------
70 // wxCaret static functions and data
71 // ----------------------------------------------------------------------------
72
73 int wxCaretBase::GetBlinkTime()
74 {
75 return gs_blinkTime;
76 }
77
78 void wxCaretBase::SetBlinkTime(int milliseconds)
79 {
80 gs_blinkTime = milliseconds;
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
94 }
95
96 // ----------------------------------------------------------------------------
97 // initialization and destruction
98 // ----------------------------------------------------------------------------
99
100 void wxCaret::InitGeneric()
101 {
102 m_hasFocus = true;
103 m_blinkedOut = true;
104 #ifndef wxHAS_CARET_USING_OVERLAYS
105 m_xOld =
106 m_yOld = -1;
107 m_bmpUnderCaret.Create(m_width, m_height);
108 #endif
109 }
110
111 wxCaret::~wxCaret()
112 {
113 if ( IsVisible() )
114 {
115 // stop blinking
116 if ( m_timer.IsRunning() )
117 m_timer.Stop();
118 }
119 }
120
121 // ----------------------------------------------------------------------------
122 // showing/hiding/moving the caret (base class interface)
123 // ----------------------------------------------------------------------------
124
125 void wxCaret::DoShow()
126 {
127 int blinkTime = GetBlinkTime();
128 if ( blinkTime )
129 m_timer.Start(blinkTime);
130
131 if ( m_blinkedOut )
132 Blink();
133 }
134
135 void wxCaret::DoHide()
136 {
137 m_timer.Stop();
138
139 if ( !m_blinkedOut )
140 {
141 Blink();
142 }
143 }
144
145 void wxCaret::DoMove()
146 {
147 #ifdef wxHAS_CARET_USING_OVERLAYS
148 m_overlay.Reset();
149 #endif
150 if ( IsVisible() )
151 {
152 if ( !m_blinkedOut )
153 {
154 // hide it right now and it will be shown the next time it blinks
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() )
160 Blink();
161 }
162 }
163 //else: will be shown at the correct location when it is shown
164 }
165
166 void wxCaret::DoSize()
167 {
168 int countVisible = m_countVisible;
169 if (countVisible > 0)
170 {
171 m_countVisible = 0;
172 DoHide();
173 }
174 #ifdef wxHAS_CARET_USING_OVERLAYS
175 m_overlay.Reset();
176 #else
177 // Change bitmap size
178 m_bmpUnderCaret = wxBitmap(m_width, m_height);
179 #endif
180 if (countVisible > 0)
181 {
182 m_countVisible = countVisible;
183 DoShow();
184 }
185 }
186
187 // ----------------------------------------------------------------------------
188 // handling the focus
189 // ----------------------------------------------------------------------------
190
191 void wxCaret::OnSetFocus()
192 {
193 m_hasFocus = true;
194
195 if ( IsVisible() )
196 Refresh();
197 }
198
199 void wxCaret::OnKillFocus()
200 {
201 m_hasFocus = false;
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
208
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 }
216 }
217
218 // ----------------------------------------------------------------------------
219 // drawing the caret
220 // ----------------------------------------------------------------------------
221
222 void wxCaret::Blink()
223 {
224 m_blinkedOut = !m_blinkedOut;
225
226 Refresh();
227 }
228
229 void wxCaret::Refresh()
230 {
231 wxClientDC dcWin(GetWindow());
232 // this is the new code, switch to 0 if this gives problems
233 #ifdef wxHAS_CARET_USING_OVERLAYS
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 {
241 DoDraw( &dcWin, GetWindow() );
242 }
243 #else
244 wxMemoryDC dcMem;
245 dcMem.SelectObject(m_bmpUnderCaret);
246 if ( m_blinkedOut )
247 {
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;
253 }
254 else
255 {
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,
260 &dcWin, m_x, m_y);
261
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
267
268 // and draw the caret there
269 DoDraw(&dcWin, GetWindow());
270 }
271 #endif
272 }
273
274 void wxCaret::DoDraw(wxDC *dc, wxWindow* win)
275 {
276 wxPen pen(*wxBLACK_PEN);
277 wxBrush brush(*wxBLACK_BRUSH);
278 if (win)
279 {
280 wxColour backgroundColour(win->GetBackgroundColour());
281 if (backgroundColour.Red() < 100 &&
282 backgroundColour.Green() < 100 &&
283 backgroundColour.Blue() < 100)
284 {
285 pen = *wxWHITE_PEN;
286 brush = *wxWHITE_BRUSH;
287 }
288 }
289 dc->SetPen( pen );
290 dc->SetBrush(m_hasFocus ? brush : *wxTRANSPARENT_BRUSH);
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);
297 }
298
299 #endif // wxUSE_CARET