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