| 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) wxWindows team |
| 9 | // Licence: wxWindows licence |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
| 21 | #pragma implementation "caret.h" |
| 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 | |
| 31 | #if wxUSE_CARET |
| 32 | |
| 33 | #ifndef WX_PRECOMP |
| 34 | #include "wx/window.h" |
| 35 | #include "wx/dcclient.h" |
| 36 | #include "wx/dcmemory.h" |
| 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 | |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | // timer stuff |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | |
| 56 | wxCaretTimer::wxCaretTimer(wxCaret *caret) |
| 57 | { |
| 58 | m_caret = caret; |
| 59 | } |
| 60 | |
| 61 | void wxCaretTimer::Notify() |
| 62 | { |
| 63 | m_caret->OnTimer(); |
| 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(); |
| 71 | } |
| 72 | |
| 73 | // ---------------------------------------------------------------------------- |
| 74 | // wxCaret static functions and data |
| 75 | // ---------------------------------------------------------------------------- |
| 76 | |
| 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 | { |
| 93 | m_hasFocus = TRUE; |
| 94 | m_blinkedOut = TRUE; |
| 95 | |
| 96 | m_xOld = |
| 97 | m_yOld = -1; |
| 98 | m_bmpUnderCaret.Create(m_width, m_height); |
| 99 | } |
| 100 | |
| 101 | wxCaret::~wxCaret() |
| 102 | { |
| 103 | if ( IsVisible() ) |
| 104 | { |
| 105 | // stop blinking |
| 106 | if ( m_timer.IsRunning() ) |
| 107 | m_timer.Stop(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // ---------------------------------------------------------------------------- |
| 112 | // showing/hiding/moving the caret (base class interface) |
| 113 | // ---------------------------------------------------------------------------- |
| 114 | |
| 115 | void wxCaret::DoShow() |
| 116 | { |
| 117 | int blinkTime = GetBlinkTime(); |
| 118 | if ( blinkTime ) |
| 119 | m_timer.Start(blinkTime); |
| 120 | |
| 121 | if ( m_blinkedOut ) |
| 122 | Blink(); |
| 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 | { |
| 137 | if ( IsVisible() ) |
| 138 | { |
| 139 | if ( !m_blinkedOut ) |
| 140 | { |
| 141 | // hide it right now and it will be shown the next time it blinks |
| 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() ) |
| 147 | Blink(); |
| 148 | } |
| 149 | } |
| 150 | //else: will be shown at the correct location when it is shown |
| 151 | } |
| 152 | |
| 153 | // ---------------------------------------------------------------------------- |
| 154 | // handling the focus |
| 155 | // ---------------------------------------------------------------------------- |
| 156 | |
| 157 | void wxCaret::OnSetFocus() |
| 158 | { |
| 159 | m_hasFocus = TRUE; |
| 160 | |
| 161 | if ( IsVisible() ) |
| 162 | Refresh(); |
| 163 | } |
| 164 | |
| 165 | void wxCaret::OnKillFocus() |
| 166 | { |
| 167 | m_hasFocus = FALSE; |
| 168 | |
| 169 | if ( IsVisible() ) |
| 170 | { |
| 171 | // the caret must be shown - otherwise, if it is hidden now, it will |
| 172 | // stay so until the focus doesn't return because it won't blink any |
| 173 | // more |
| 174 | |
| 175 | // hide it first if it isn't hidden ... |
| 176 | if ( !m_blinkedOut ) |
| 177 | Blink(); |
| 178 | |
| 179 | // .. and show it in the new style |
| 180 | Blink(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // ---------------------------------------------------------------------------- |
| 185 | // drawing the caret |
| 186 | // ---------------------------------------------------------------------------- |
| 187 | |
| 188 | void wxCaret::Blink() |
| 189 | { |
| 190 | m_blinkedOut = !m_blinkedOut; |
| 191 | |
| 192 | Refresh(); |
| 193 | } |
| 194 | |
| 195 | void wxCaret::Refresh() |
| 196 | { |
| 197 | wxClientDC dcWin(GetWindow()); |
| 198 | wxMemoryDC dcMem; |
| 199 | dcMem.SelectObject(m_bmpUnderCaret); |
| 200 | if ( m_blinkedOut ) |
| 201 | { |
| 202 | // restore the old image |
| 203 | dcWin.Blit(m_xOld, m_yOld, m_width, m_height, |
| 204 | &dcMem, 0, 0); |
| 205 | m_xOld = |
| 206 | m_yOld = -1; |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | if ( m_xOld == -1 && m_yOld == -1 ) |
| 211 | { |
| 212 | // save the part we're going to overdraw |
| 213 | |
| 214 | int x = m_x, |
| 215 | y = m_y; |
| 216 | #if defined(__WXGTK__) && !defined(__WX_DC_BLIT_FIXED__) |
| 217 | wxPoint pt = dcWin.GetDeviceOrigin(); |
| 218 | x += pt.x; |
| 219 | y += pt.y; |
| 220 | #endif // broken wxGTK wxDC::Blit |
| 221 | dcMem.Blit(0, 0, m_width, m_height, |
| 222 | &dcWin, x, y); |
| 223 | |
| 224 | m_xOld = m_x; |
| 225 | m_yOld = m_y; |
| 226 | } |
| 227 | //else: we already saved the image below the caret, don't do it any |
| 228 | // more |
| 229 | |
| 230 | // and draw the caret there |
| 231 | DoDraw(&dcWin); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | void wxCaret::DoDraw(wxDC *dc) |
| 236 | { |
| 237 | dc->SetPen( *wxBLACK_PEN ); |
| 238 | |
| 239 | dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH)); |
| 240 | dc->SetPen(*wxBLACK_PEN); |
| 241 | |
| 242 | // VZ: unfortunately, the rectangle comes out a pixel smaller when this is |
| 243 | // done under wxGTK - no idea why |
| 244 | //dc->SetLogicalFunction(wxINVERT); |
| 245 | |
| 246 | dc->DrawRectangle(m_x, m_y, m_width, m_height); |
| 247 | } |
| 248 | |
| 249 | #endif // wxUSE_CARET |