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