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