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