1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        src/msw/caret.cpp
3 // Purpose:     MSW implementation 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 
11 // ===========================================================================
12 // declarations
13 // ===========================================================================
14 
15 // ---------------------------------------------------------------------------
16 // headers
17 // ---------------------------------------------------------------------------
18 
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21 
22 #ifdef __BORLANDC__
23     #pragma hdrstop
24 #endif
25 
26 #ifndef WX_PRECOMP
27     #include "wx/window.h"
28     #include "wx/log.h"
29 #endif // WX_PRECOMP
30 
31 #include "wx/caret.h"
32 
33 #if wxUSE_CARET
34 
35 #include "wx/msw/private.h"
36 
37 // ---------------------------------------------------------------------------
38 // macros
39 // ---------------------------------------------------------------------------
40 
41 #define CALL_CARET_API(api, args) \
42         if ( !api args ) \
43         { \
44             wxLogLastError(wxT(#api)); \
45         }
46 
47 // ===========================================================================
48 // implementation
49 // ===========================================================================
50 
51 // ---------------------------------------------------------------------------
52 // blink time
53 // ---------------------------------------------------------------------------
54 
55 //static
GetBlinkTime()56 int wxCaretBase::GetBlinkTime()
57 {
58     int blinkTime = ::GetCaretBlinkTime();
59     if ( !blinkTime )
60     {
61         wxLogLastError(wxT("GetCaretBlinkTime"));
62     }
63 
64     return blinkTime;
65 }
66 
67 //static
SetBlinkTime(int milliseconds)68 void wxCaretBase::SetBlinkTime(int milliseconds)
69 {
70     CALL_CARET_API(SetCaretBlinkTime, (milliseconds));
71 }
72 
73 // ---------------------------------------------------------------------------
74 // creating/destroying the caret
75 // ---------------------------------------------------------------------------
76 
MSWCreateCaret()77 bool wxCaret::MSWCreateCaret()
78 {
79     wxASSERT_MSG( GetWindow(), wxT("caret without window cannot be created") );
80     wxASSERT_MSG( IsOk(),  wxT("caret of zero size cannot be created") );
81 
82     if ( !m_hasCaret )
83     {
84         CALL_CARET_API(CreateCaret, (GetWinHwnd(GetWindow()), 0,
85                                      m_width, m_height));
86 
87         m_hasCaret = true;
88     }
89 
90     return m_hasCaret;
91 }
92 
OnSetFocus()93 void wxCaret::OnSetFocus()
94 {
95     if ( m_countVisible > 0 )
96     {
97         if ( MSWCreateCaret() )
98         {
99             // the caret was recreated but it doesn't remember its position and
100             // it's not shown
101 
102             DoMove();
103             DoShow();
104         }
105     }
106     //else: caret is invisible, don't waste time creating it
107 }
108 
OnKillFocus()109 void wxCaret::OnKillFocus()
110 {
111     if ( m_hasCaret )
112     {
113         m_hasCaret = false;
114 
115         CALL_CARET_API(DestroyCaret, ());
116     }
117 }
118 
119 // ---------------------------------------------------------------------------
120 // showing/hiding the caret
121 // ---------------------------------------------------------------------------
122 
DoShow()123 void wxCaret::DoShow()
124 {
125     wxASSERT_MSG( GetWindow(), wxT("caret without window cannot be shown") );
126     wxASSERT_MSG( IsOk(), wxT("caret of zero size cannot be shown") );
127 
128     // we might not have created the caret yet if we had got the focus first
129     // and the caret was shown later - so do it now if we have the focus but
130     // not the caret
131     if ( !m_hasCaret && (wxWindow::FindFocus() == GetWindow()) )
132     {
133         if ( MSWCreateCaret() )
134         {
135             DoMove();
136         }
137     }
138 
139     if ( m_hasCaret )
140     {
141         CALL_CARET_API(ShowCaret, (GetWinHwnd(GetWindow())));
142     }
143     //else: will be shown when we get the focus
144 }
145 
DoHide()146 void wxCaret::DoHide()
147 {
148     if ( m_hasCaret )
149     {
150         CALL_CARET_API(HideCaret, (GetWinHwnd(GetWindow())));
151     }
152 }
153 
154 // ---------------------------------------------------------------------------
155 // moving the caret
156 // ---------------------------------------------------------------------------
157 
DoMove()158 void wxCaret::DoMove()
159 {
160     if ( m_hasCaret )
161     {
162         wxASSERT_MSG( wxWindow::FindFocus() == GetWindow(),
163                       wxT("how did we lose focus?") );
164 
165         // for compatibility with the generic version, the coordinates are
166         // client ones
167         wxPoint pt = GetWindow()->GetClientAreaOrigin();
168         CALL_CARET_API(SetCaretPos, (m_x + pt.x, m_y + pt.y));
169     }
170     //else: we don't have caret right now, nothing to do (this does happen)
171 }
172 
173 
174 // ---------------------------------------------------------------------------
175 // resizing the caret
176 // ---------------------------------------------------------------------------
177 
DoSize()178 void wxCaret::DoSize()
179 {
180     if ( m_hasCaret )
181     {
182         m_hasCaret = false;
183         CALL_CARET_API(DestroyCaret, ());
184         MSWCreateCaret();
185         OnSetFocus();
186     }
187 }
188 
189 #endif
190