]> git.saurik.com Git - wxWidgets.git/blob - src/generic/caret.cpp
More common code moved from generic dialogs to wxDialogBase::CreateButtonSizer()...
[wxWidgets.git] / src / generic / caret.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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
83 // ----------------------------------------------------------------------------
84 // initialization and destruction
85 // ----------------------------------------------------------------------------
86
87 void wxCaret::InitGeneric()
88 {
89 m_hasFocus = true;
90 m_blinkedOut = true;
91
92 m_xOld =
93 m_yOld = -1;
94 m_bmpUnderCaret.Create(m_width, m_height);
95 }
96
97 wxCaret::~wxCaret()
98 {
99 if ( IsVisible() )
100 {
101 // stop blinking
102 if ( m_timer.IsRunning() )
103 m_timer.Stop();
104 }
105 }
106
107 // ----------------------------------------------------------------------------
108 // showing/hiding/moving the caret (base class interface)
109 // ----------------------------------------------------------------------------
110
111 void wxCaret::DoShow()
112 {
113 int blinkTime = GetBlinkTime();
114 if ( blinkTime )
115 m_timer.Start(blinkTime);
116
117 if ( m_blinkedOut )
118 Blink();
119 }
120
121 void wxCaret::DoHide()
122 {
123 m_timer.Stop();
124
125 if ( !m_blinkedOut )
126 {
127 Blink();
128 }
129 }
130
131 void wxCaret::DoMove()
132 {
133 if ( IsVisible() )
134 {
135 if ( !m_blinkedOut )
136 {
137 // hide it right now and it will be shown the next time it blinks
138 Blink();
139
140 // but if the caret is not blinking, we should blink it back into
141 // visibility manually
142 if ( !m_timer.IsRunning() )
143 Blink();
144 }
145 }
146 //else: will be shown at the correct location when it is shown
147 }
148
149 void wxCaret::DoSize()
150 {
151 int countVisible = m_countVisible;
152 if (countVisible > 0)
153 {
154 m_countVisible = 0;
155 DoHide();
156 }
157 // Change bitmap size
158 m_bmpUnderCaret = wxBitmap(m_width, m_height);
159 if (countVisible > 0)
160 {
161 m_countVisible = countVisible;
162 DoShow();
163 }
164 }
165
166 // ----------------------------------------------------------------------------
167 // handling the focus
168 // ----------------------------------------------------------------------------
169
170 void wxCaret::OnSetFocus()
171 {
172 m_hasFocus = true;
173
174 if ( IsVisible() )
175 Refresh();
176 }
177
178 void wxCaret::OnKillFocus()
179 {
180 m_hasFocus = false;
181
182 if ( IsVisible() )
183 {
184 // the caret must be shown - otherwise, if it is hidden now, it will
185 // stay so until the focus doesn't return because it won't blink any
186 // more
187
188 // hide it first if it isn't hidden ...
189 if ( !m_blinkedOut )
190 Blink();
191
192 // .. and show it in the new style
193 Blink();
194 }
195 }
196
197 // ----------------------------------------------------------------------------
198 // drawing the caret
199 // ----------------------------------------------------------------------------
200
201 void wxCaret::Blink()
202 {
203 m_blinkedOut = !m_blinkedOut;
204
205 Refresh();
206 }
207
208 void wxCaret::Refresh()
209 {
210 wxClientDC dcWin(GetWindow());
211 wxMemoryDC dcMem;
212 dcMem.SelectObject(m_bmpUnderCaret);
213 if ( m_blinkedOut )
214 {
215 // restore the old image
216 dcWin.Blit(m_xOld, m_yOld, m_width, m_height,
217 &dcMem, 0, 0);
218 m_xOld =
219 m_yOld = -1;
220 }
221 else
222 {
223 if ( m_xOld == -1 && m_yOld == -1 )
224 {
225 // save the part we're going to overdraw
226
227 int x = m_x,
228 y = m_y;
229 #if defined(__WXGTK__) && !defined(__WX_DC_BLIT_FIXED__)
230 wxPoint pt = dcWin.GetDeviceOrigin();
231 x += pt.x;
232 y += pt.y;
233 #endif // broken wxGTK wxDC::Blit
234 dcMem.Blit(0, 0, m_width, m_height,
235 &dcWin, x, y);
236
237 m_xOld = m_x;
238 m_yOld = m_y;
239 }
240 //else: we already saved the image below the caret, don't do it any
241 // more
242
243 // and draw the caret there
244 DoDraw(&dcWin);
245 }
246 }
247
248 void wxCaret::DoDraw(wxDC *dc)
249 {
250 dc->SetPen( *wxBLACK_PEN );
251
252 dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
253 dc->SetPen(*wxBLACK_PEN);
254
255 // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
256 // done under wxGTK - no idea why
257 //dc->SetLogicalFunction(wxINVERT);
258
259 dc->DrawRectangle(m_x, m_y, m_width, m_height);
260 }
261
262 #endif // wxUSE_CARET