]>
Commit | Line | Data |
---|---|---|
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 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
f6bcfd97 | 21 | #pragma implementation "caret.h" |
0290598f VZ |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
697e0cdd VZ |
31 | #if wxUSE_CARET |
32 | ||
0290598f | 33 | #ifndef WX_PRECOMP |
e179bd65 RR |
34 | #include "wx/window.h" |
35 | #include "wx/dcclient.h" | |
f4b8bf2f | 36 | #include "wx/dcmemory.h" |
0290598f VZ |
37 | #endif //WX_PRECOMP |
38 | ||
39 | #include "wx/caret.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // global variables for this module | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | // the blink time (common to all carets for MSW compatibility) | |
46 | static int gs_blinkTime = 500; // in milliseconds | |
47 | ||
48 | // ============================================================================ | |
49 | // implementation | |
50 | // ============================================================================ | |
51 | ||
f6bcfd97 BP |
52 | // ---------------------------------------------------------------------------- |
53 | // timer stuff | |
54 | // ---------------------------------------------------------------------------- | |
55 | ||
ca65c044 WS |
56 | wxCaretTimer::wxCaretTimer(wxCaret *caret) |
57 | { | |
58 | m_caret = caret; | |
e4628635 RR |
59 | } |
60 | ||
ca65c044 WS |
61 | void wxCaretTimer::Notify() |
62 | { | |
63 | m_caret->OnTimer(); | |
f6bcfd97 BP |
64 | } |
65 | ||
66 | void wxCaret::OnTimer() | |
67 | { | |
68 | // don't blink the caret when we don't have the focus | |
69 | if ( m_hasFocus ) | |
70 | Blink(); | |
e4628635 RR |
71 | } |
72 | ||
0290598f VZ |
73 | // ---------------------------------------------------------------------------- |
74 | // wxCaret static functions and data | |
75 | // ---------------------------------------------------------------------------- | |
76 | ||
0290598f VZ |
77 | int wxCaretBase::GetBlinkTime() |
78 | { | |
79 | return gs_blinkTime; | |
80 | } | |
81 | ||
82 | void wxCaretBase::SetBlinkTime(int milliseconds) | |
83 | { | |
84 | gs_blinkTime = milliseconds; | |
85 | } | |
86 | ||
87 | // ---------------------------------------------------------------------------- | |
88 | // initialization and destruction | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
91 | void wxCaret::InitGeneric() | |
92 | { | |
ca65c044 WS |
93 | m_hasFocus = true; |
94 | m_blinkedOut = true; | |
f4b8bf2f VZ |
95 | |
96 | m_xOld = | |
97 | m_yOld = -1; | |
98 | m_bmpUnderCaret.Create(m_width, m_height); | |
0290598f VZ |
99 | } |
100 | ||
101 | wxCaret::~wxCaret() | |
102 | { | |
103 | if ( IsVisible() ) | |
104 | { | |
105 | // stop blinking | |
697e0cdd VZ |
106 | if ( m_timer.IsRunning() ) |
107 | m_timer.Stop(); | |
0290598f VZ |
108 | } |
109 | } | |
110 | ||
111 | // ---------------------------------------------------------------------------- | |
112 | // showing/hiding/moving the caret (base class interface) | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | void wxCaret::DoShow() | |
116 | { | |
697e0cdd VZ |
117 | int blinkTime = GetBlinkTime(); |
118 | if ( blinkTime ) | |
119 | m_timer.Start(blinkTime); | |
0290598f | 120 | |
ca5b5b49 VS |
121 | if ( m_blinkedOut ) |
122 | Blink(); | |
0290598f VZ |
123 | } |
124 | ||
125 | void wxCaret::DoHide() | |
126 | { | |
127 | m_timer.Stop(); | |
128 | ||
129 | if ( !m_blinkedOut ) | |
130 | { | |
131 | Blink(); | |
132 | } | |
133 | } | |
134 | ||
135 | void wxCaret::DoMove() | |
136 | { | |
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 |
153 | void wxCaret::DoSize() |
154 | { | |
155 | int countVisible = m_countVisible; | |
156 | if (countVisible > 0) | |
157 | { | |
158 | m_countVisible = 0; | |
159 | DoHide(); | |
160 | } | |
161 | // Change bitmap size | |
162 | m_bmpUnderCaret = wxBitmap(m_width, m_height); | |
163 | if (countVisible > 0) | |
164 | { | |
165 | m_countVisible = countVisible; | |
166 | DoShow(); | |
167 | } | |
168 | } | |
169 | ||
f6bcfd97 BP |
170 | // ---------------------------------------------------------------------------- |
171 | // handling the focus | |
172 | // ---------------------------------------------------------------------------- | |
173 | ||
174 | void wxCaret::OnSetFocus() | |
175 | { | |
ca65c044 | 176 | m_hasFocus = true; |
f6bcfd97 | 177 | |
b6e8d5a3 VS |
178 | if ( IsVisible() ) |
179 | Refresh(); | |
f6bcfd97 BP |
180 | } |
181 | ||
182 | void wxCaret::OnKillFocus() | |
183 | { | |
ca65c044 | 184 | m_hasFocus = false; |
f6bcfd97 BP |
185 | |
186 | if ( IsVisible() ) | |
187 | { | |
188 | // the caret must be shown - otherwise, if it is hidden now, it will | |
189 | // stay so until the focus doesn't return because it won't blink any | |
190 | // more | |
f6bcfd97 | 191 | |
f4b8bf2f VZ |
192 | // hide it first if it isn't hidden ... |
193 | if ( !m_blinkedOut ) | |
194 | Blink(); | |
195 | ||
196 | // .. and show it in the new style | |
197 | Blink(); | |
198 | } | |
f6bcfd97 BP |
199 | } |
200 | ||
0290598f VZ |
201 | // ---------------------------------------------------------------------------- |
202 | // drawing the caret | |
203 | // ---------------------------------------------------------------------------- | |
204 | ||
205 | void wxCaret::Blink() | |
206 | { | |
207 | m_blinkedOut = !m_blinkedOut; | |
208 | ||
f6bcfd97 BP |
209 | Refresh(); |
210 | } | |
211 | ||
212 | void wxCaret::Refresh() | |
213 | { | |
f4b8bf2f VZ |
214 | wxClientDC dcWin(GetWindow()); |
215 | wxMemoryDC dcMem; | |
216 | dcMem.SelectObject(m_bmpUnderCaret); | |
217 | if ( m_blinkedOut ) | |
0290598f | 218 | { |
f4b8bf2f VZ |
219 | // restore the old image |
220 | dcWin.Blit(m_xOld, m_yOld, m_width, m_height, | |
221 | &dcMem, 0, 0); | |
222 | m_xOld = | |
223 | m_yOld = -1; | |
0290598f VZ |
224 | } |
225 | else | |
226 | { | |
f4b8bf2f VZ |
227 | if ( m_xOld == -1 && m_yOld == -1 ) |
228 | { | |
229 | // save the part we're going to overdraw | |
1e6feb95 VZ |
230 | |
231 | int x = m_x, | |
232 | y = m_y; | |
233 | #if defined(__WXGTK__) && !defined(__WX_DC_BLIT_FIXED__) | |
234 | wxPoint pt = dcWin.GetDeviceOrigin(); | |
235 | x += pt.x; | |
236 | y += pt.y; | |
237 | #endif // broken wxGTK wxDC::Blit | |
f4b8bf2f | 238 | dcMem.Blit(0, 0, m_width, m_height, |
1e6feb95 VZ |
239 | &dcWin, x, y); |
240 | ||
f4b8bf2f VZ |
241 | m_xOld = m_x; |
242 | m_yOld = m_y; | |
243 | } | |
244 | //else: we already saved the image below the caret, don't do it any | |
245 | // more | |
f6bcfd97 | 246 | |
f4b8bf2f VZ |
247 | // and draw the caret there |
248 | DoDraw(&dcWin); | |
0290598f VZ |
249 | } |
250 | } | |
251 | ||
252 | void wxCaret::DoDraw(wxDC *dc) | |
253 | { | |
254 | dc->SetPen( *wxBLACK_PEN ); | |
f6bcfd97 | 255 | |
f4b8bf2f VZ |
256 | dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH)); |
257 | dc->SetPen(*wxBLACK_PEN); | |
258 | ||
259 | // VZ: unfortunately, the rectangle comes out a pixel smaller when this is | |
260 | // done under wxGTK - no idea why | |
261 | //dc->SetLogicalFunction(wxINVERT); | |
262 | ||
263 | dc->DrawRectangle(m_x, m_y, m_width, m_height); | |
0290598f | 264 | } |
697e0cdd VZ |
265 | |
266 | #endif // wxUSE_CARET |