home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / wxos2240.zip / wxWindows-2.4.0 / src / generic / caret.cpp < prev    next >
C/C++ Source or Header  |  2002-01-27  |  7KB  |  250 lines

  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: caret.cpp,v 1.9 2002/01/27 23:37:58 VS Exp $
  8. // Copyright:   (c) wxWindows team
  9. // Licence:     wxWindows licence
  10. ///////////////////////////////////////////////////////////////////////////////
  11.  
  12. // ============================================================================
  13. // declarations
  14. // ============================================================================
  15.  
  16. // ----------------------------------------------------------------------------
  17. // headers
  18. // ----------------------------------------------------------------------------
  19.  
  20. #ifdef __GNUG__
  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.     m_caret = caret; 
  58. }
  59.  
  60. void wxCaretTimer::Notify() 
  61.     m_caret->OnTimer(); 
  62. }
  63.  
  64. void wxCaret::OnTimer()
  65. {
  66.     // don't blink the caret when we don't have the focus
  67.     if ( m_hasFocus )
  68.         Blink();
  69. }
  70.  
  71. // ----------------------------------------------------------------------------
  72. // wxCaret static functions and data
  73. // ----------------------------------------------------------------------------
  74.  
  75. int wxCaretBase::GetBlinkTime()
  76. {
  77.     return gs_blinkTime;
  78. }
  79.  
  80. void wxCaretBase::SetBlinkTime(int milliseconds)
  81. {
  82.     gs_blinkTime = milliseconds;
  83. }
  84.  
  85. // ----------------------------------------------------------------------------
  86. // initialization and destruction
  87. // ----------------------------------------------------------------------------
  88.  
  89. void wxCaret::InitGeneric()
  90. {
  91.     m_hasFocus = TRUE;
  92.     m_blinkedOut = TRUE;
  93.  
  94.     m_xOld =
  95.     m_yOld = -1;
  96.     m_bmpUnderCaret.Create(m_width, m_height);
  97. }
  98.  
  99. wxCaret::~wxCaret()
  100. {
  101.     if ( IsVisible() )
  102.     {
  103.         // stop blinking
  104.         if ( m_timer.IsRunning() )
  105.             m_timer.Stop();
  106.     }
  107. }
  108.  
  109. // ----------------------------------------------------------------------------
  110. // showing/hiding/moving the caret (base class interface)
  111. // ----------------------------------------------------------------------------
  112.  
  113. void wxCaret::DoShow()
  114. {
  115.     int blinkTime = GetBlinkTime();
  116.     if ( blinkTime )
  117.         m_timer.Start(blinkTime);
  118.  
  119.     if ( m_blinkedOut )
  120.         Blink();
  121. }
  122.  
  123. void wxCaret::DoHide()
  124. {
  125.     m_timer.Stop();
  126.  
  127.     if ( !m_blinkedOut )
  128.     {
  129.         Blink();
  130.     }
  131. }
  132.  
  133. void wxCaret::DoMove()
  134. {
  135.     if ( IsVisible() )
  136.     {
  137.         if ( !m_blinkedOut )
  138.         {
  139.             // hide it right now and it will be shown the next time it blinks
  140.             Blink();
  141.  
  142.             // but if the caret is not blinking, we should blink it back into
  143.             // visibility manually
  144.             if ( !m_timer.IsRunning() )
  145.                 Blink();
  146.         }
  147.     }
  148.     //else: will be shown at the correct location when it is shown
  149. }
  150.  
  151. // ----------------------------------------------------------------------------
  152. // handling the focus
  153. // ----------------------------------------------------------------------------
  154.  
  155. void wxCaret::OnSetFocus()
  156. {
  157.     m_hasFocus = TRUE;
  158.  
  159.     if ( IsVisible() )
  160.         Refresh();
  161. }
  162.  
  163. void wxCaret::OnKillFocus()
  164. {
  165.     m_hasFocus = FALSE;
  166.  
  167.     if ( IsVisible() )
  168.     {
  169.         // the caret must be shown - otherwise, if it is hidden now, it will
  170.         // stay so until the focus doesn't return because it won't blink any
  171.         // more
  172.  
  173.         // hide it first if it isn't hidden ...
  174.         if ( !m_blinkedOut )
  175.             Blink();
  176.  
  177.         // .. and show it in the new style
  178.         Blink();
  179.     }
  180. }
  181.  
  182. // ----------------------------------------------------------------------------
  183. // drawing the caret
  184. // ----------------------------------------------------------------------------
  185.  
  186. void wxCaret::Blink()
  187. {
  188.     m_blinkedOut = !m_blinkedOut;
  189.  
  190.     Refresh();
  191. }
  192.  
  193. void wxCaret::Refresh()
  194. {
  195.     wxClientDC dcWin(GetWindow());
  196.     wxMemoryDC dcMem;
  197.     dcMem.SelectObject(m_bmpUnderCaret);
  198.     if ( m_blinkedOut )
  199.     {
  200.         // restore the old image
  201.         dcWin.Blit(m_xOld, m_yOld, m_width, m_height,
  202.                    &dcMem, 0, 0);
  203.         m_xOld =
  204.         m_yOld = -1;
  205.     }
  206.     else
  207.     {
  208.         if ( m_xOld == -1 && m_yOld == -1 )
  209.         {
  210.             // save the part we're going to overdraw
  211.  
  212.             int x = m_x,
  213.                 y = m_y;
  214. #if defined(__WXGTK__) && !defined(__WX_DC_BLIT_FIXED__)
  215.             wxPoint pt = dcWin.GetDeviceOrigin();
  216.             x += pt.x;
  217.             y += pt.y;
  218. #endif // broken wxGTK wxDC::Blit
  219.             dcMem.Blit(0, 0, m_width, m_height,
  220.                        &dcWin, x, y);
  221.  
  222.             m_xOld = m_x;
  223.             m_yOld = m_y;
  224.         }
  225.         //else: we already saved the image below the caret, don't do it any
  226.         //      more
  227.  
  228.         // and draw the caret there
  229.         DoDraw(&dcWin);
  230.     }
  231. }
  232.  
  233. void wxCaret::DoDraw(wxDC *dc)
  234. {
  235.     dc->SetPen( *wxBLACK_PEN );
  236.  
  237.     dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
  238.     dc->SetPen(*wxBLACK_PEN);
  239.  
  240.     // VZ: unfortunately, the rectangle comes out a pixel smaller when this is
  241.     //     done under wxGTK - no idea why
  242.     //dc->SetLogicalFunction(wxINVERT);
  243.  
  244.     dc->DrawRectangle(m_x, m_y, m_width, m_height);
  245. }
  246.  
  247. #endif // wxUSE_CARET
  248.