caret.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/caret.h
  3. // Purpose: wxCaretBase class - the interface of wxCaret
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 23.05.99
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CARET_H_BASE_
  11. #define _WX_CARET_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_CARET
  14. // ---------------------------------------------------------------------------
  15. // forward declarations
  16. // ---------------------------------------------------------------------------
  17. class WXDLLIMPEXP_FWD_CORE wxWindow;
  18. class WXDLLIMPEXP_FWD_CORE wxWindowBase;
  19. // ----------------------------------------------------------------------------
  20. // headers we have to include
  21. // ----------------------------------------------------------------------------
  22. #include "wx/gdicmn.h" // for wxPoint, wxSize
  23. // ----------------------------------------------------------------------------
  24. // A caret is a blinking cursor showing the position where the typed text will
  25. // appear. It can be either a solid block or a custom bitmap (TODO)
  26. // ----------------------------------------------------------------------------
  27. class WXDLLIMPEXP_CORE wxCaretBase
  28. {
  29. public:
  30. // ctors
  31. // -----
  32. // default - use Create
  33. wxCaretBase() { Init(); }
  34. // create the caret of given (in pixels) width and height and associate
  35. // with the given window
  36. wxCaretBase(wxWindowBase *window, int width, int height)
  37. {
  38. Init();
  39. (void)Create(window, width, height);
  40. }
  41. // same as above
  42. wxCaretBase(wxWindowBase *window, const wxSize& size)
  43. {
  44. Init();
  45. (void)Create(window, size);
  46. }
  47. // a virtual dtor has been provided since this class has virtual members
  48. virtual ~wxCaretBase() { }
  49. // Create() functions - same as ctor but returns the success code
  50. // --------------------------------------------------------------
  51. // same as ctor
  52. bool Create(wxWindowBase *window, int width, int height)
  53. { return DoCreate(window, width, height); }
  54. // same as ctor
  55. bool Create(wxWindowBase *window, const wxSize& size)
  56. { return DoCreate(window, size.x, size.y); }
  57. // accessors
  58. // ---------
  59. // is the caret valid?
  60. bool IsOk() const { return m_width != 0 && m_height != 0; }
  61. // is the caret currently shown?
  62. bool IsVisible() const { return m_countVisible > 0; }
  63. // get the caret position
  64. void GetPosition(int *x, int *y) const
  65. {
  66. if ( x ) *x = m_x;
  67. if ( y ) *y = m_y;
  68. }
  69. wxPoint GetPosition() const { return wxPoint(m_x, m_y); }
  70. // get the caret size
  71. void GetSize(int *width, int *height) const
  72. {
  73. if ( width ) *width = m_width;
  74. if ( height ) *height = m_height;
  75. }
  76. wxSize GetSize() const { return wxSize(m_width, m_height); }
  77. // get the window we're associated with
  78. wxWindow *GetWindow() const { return (wxWindow *)m_window; }
  79. // change the size of the caret
  80. void SetSize(int width, int height) {
  81. m_width = width;
  82. m_height = height;
  83. DoSize();
  84. }
  85. void SetSize(const wxSize& size) { SetSize(size.x, size.y); }
  86. // operations
  87. // ----------
  88. // move the caret to given position (in logical coords)
  89. void Move(int x, int y) { m_x = x; m_y = y; DoMove(); }
  90. void Move(const wxPoint& pt) { m_x = pt.x; m_y = pt.y; DoMove(); }
  91. // show/hide the caret (should be called by wxWindow when needed):
  92. // Show() must be called as many times as Hide() + 1 to make the caret
  93. // visible
  94. virtual void Show(bool show = true)
  95. {
  96. if ( show )
  97. {
  98. if ( m_countVisible++ == 0 )
  99. DoShow();
  100. }
  101. else
  102. {
  103. if ( --m_countVisible == 0 )
  104. DoHide();
  105. }
  106. }
  107. virtual void Hide() { Show(false); }
  108. // blink time is measured in milliseconds and is the time elapsed
  109. // between 2 inversions of the caret (blink time of the caret is common
  110. // to all carets in the Universe, so these functions are static)
  111. static int GetBlinkTime();
  112. static void SetBlinkTime(int milliseconds);
  113. // implementation from now on
  114. // --------------------------
  115. // these functions should be called by wxWindow when the window gets/loses
  116. // the focus - we create/show and hide/destroy the caret here
  117. virtual void OnSetFocus() { }
  118. virtual void OnKillFocus() { }
  119. protected:
  120. // these functions may be overridden in the derived classes, but they
  121. // should call the base class version first
  122. virtual bool DoCreate(wxWindowBase *window, int width, int height)
  123. {
  124. m_window = window;
  125. m_width = width;
  126. m_height = height;
  127. return true;
  128. }
  129. // pure virtuals to implement in the derived class
  130. virtual void DoShow() = 0;
  131. virtual void DoHide() = 0;
  132. virtual void DoMove() = 0;
  133. virtual void DoSize() { }
  134. // the common initialization
  135. void Init()
  136. {
  137. m_window = NULL;
  138. m_x = m_y = 0;
  139. m_width = m_height = 0;
  140. m_countVisible = 0;
  141. }
  142. // the size of the caret
  143. int m_width, m_height;
  144. // the position of the caret
  145. int m_x, m_y;
  146. // the window we're associated with
  147. wxWindowBase *m_window;
  148. // visibility count: the caret is visible only if it's positive
  149. int m_countVisible;
  150. private:
  151. wxDECLARE_NO_COPY_CLASS(wxCaretBase);
  152. };
  153. // ---------------------------------------------------------------------------
  154. // now include the real thing
  155. // ---------------------------------------------------------------------------
  156. #if defined(__WXMSW__)
  157. #include "wx/msw/caret.h"
  158. #else
  159. #include "wx/generic/caret.h"
  160. #endif // platform
  161. // ----------------------------------------------------------------------------
  162. // wxCaretSuspend: a simple class which hides the caret in its ctor and
  163. // restores it in the dtor, this should be used when drawing on the screen to
  164. // avoid overdrawing the caret
  165. // ----------------------------------------------------------------------------
  166. #ifdef wxHAS_CARET_USING_OVERLAYS
  167. // we don't need to hide the caret if it's rendered using overlays
  168. class WXDLLIMPEXP_CORE wxCaretSuspend
  169. {
  170. public:
  171. wxCaretSuspend(wxWindow *WXUNUSED(win)) {}
  172. wxDECLARE_NO_COPY_CLASS(wxCaretSuspend);
  173. };
  174. #else // !wxHAS_CARET_USING_OVERLAYS
  175. class WXDLLIMPEXP_CORE wxCaretSuspend
  176. {
  177. public:
  178. wxCaretSuspend(wxWindow *win)
  179. {
  180. m_caret = win->GetCaret();
  181. m_show = false;
  182. if ( m_caret && m_caret->IsVisible() )
  183. {
  184. m_caret->Hide();
  185. m_show = true;
  186. }
  187. }
  188. ~wxCaretSuspend()
  189. {
  190. if ( m_caret && m_show )
  191. m_caret->Show();
  192. }
  193. private:
  194. wxCaret *m_caret;
  195. bool m_show;
  196. wxDECLARE_NO_COPY_CLASS(wxCaretSuspend);
  197. };
  198. #endif // wxHAS_CARET_USING_OVERLAYS/!wxHAS_CARET_USING_OVERLAYS
  199. #endif // wxUSE_CARET
  200. #endif // _WX_CARET_H_BASE_