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