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