]> git.saurik.com Git - wxWidgets.git/blame - src/msw/caret.cpp
Copied gsockmsw.c revision 1.20
[wxWidgets.git] / src / msw / caret.cpp
CommitLineData
789295bf
VZ
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
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
789295bf
VZ
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"
0c589ad0 33 #include "wx/log.h"
789295bf
VZ
34#endif // WX_PRECOMP
35
36#include "wx/caret.h"
37
adf9e099
VZ
38#if wxUSE_CARET
39
789295bf
VZ
40#include "wx/msw/private.h"
41
e8225073
VZ
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
f6bcfd97
BP
51 #define CALL_CARET_API(api, args) \
52 if ( !api args ) \
53 wxLogLastError(_T(#api))
e8225073
VZ
54#endif // Win16/32
55
789295bf
VZ
56// ===========================================================================
57// implementation
58// ===========================================================================
59
60// ---------------------------------------------------------------------------
61// blink time
62// ---------------------------------------------------------------------------
63
64//static
65int wxCaretBase::GetBlinkTime()
66{
67 int blinkTime = ::GetCaretBlinkTime();
68 if ( !blinkTime )
69 {
f6bcfd97 70 wxLogLastError(wxT("GetCaretBlinkTime"));
789295bf
VZ
71 }
72
73 return blinkTime;
74}
75
76//static
77void wxCaretBase::SetBlinkTime(int milliseconds)
78{
e8225073 79 CALL_CARET_API(SetCaretBlinkTime, (milliseconds));
789295bf
VZ
80}
81
82// ---------------------------------------------------------------------------
83// creating/destroying the caret
84// ---------------------------------------------------------------------------
85
86bool wxCaret::MSWCreateCaret()
87{
223d09f6
KB
88 wxASSERT_MSG( GetWindow(), wxT("caret without window cannot be created") );
89 wxASSERT_MSG( IsOk(), wxT("caret of zero size cannot be created") );
789295bf
VZ
90
91 if ( !m_hasCaret )
92 {
e8225073
VZ
93 CALL_CARET_API(CreateCaret, (GetWinHwnd(GetWindow()), 0,
94 m_width, m_height));
95
25889d3c 96 m_hasCaret = TRUE;
789295bf
VZ
97 }
98
99 return m_hasCaret;
100}
101
102void 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
118void wxCaret::OnKillFocus()
119{
120 if ( m_hasCaret )
121 {
122 m_hasCaret = FALSE;
123
e8225073 124 CALL_CARET_API(DestroyCaret, ());
789295bf
VZ
125 }
126}
127
128// ---------------------------------------------------------------------------
129// showing/hiding the caret
130// ---------------------------------------------------------------------------
131
132void wxCaret::DoShow()
133{
223d09f6
KB
134 wxASSERT_MSG( GetWindow(), wxT("caret without window cannot be shown") );
135 wxASSERT_MSG( IsOk(), wxT("caret of zero size cannot be shown") );
789295bf 136
1e6feb95
VZ
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
e8225073 148 if ( m_hasCaret )
789295bf 149 {
e8225073 150 CALL_CARET_API(ShowCaret, (GetWinHwnd(GetWindow())));
789295bf 151 }
e8225073 152 //else: will be shown when we get the focus
789295bf
VZ
153}
154
155void wxCaret::DoHide()
156{
c6eba8f8 157 if ( m_hasCaret )
789295bf 158 {
e8225073 159 CALL_CARET_API(HideCaret, (GetWinHwnd(GetWindow())));
789295bf
VZ
160 }
161}
162
163// ---------------------------------------------------------------------------
164// moving the caret
165// ---------------------------------------------------------------------------
166
167void wxCaret::DoMove()
168{
a8f25787 169 if ( m_hasCaret )
789295bf 170 {
a17e237f
VZ
171 wxASSERT_MSG( wxWindow::FindFocus() == GetWindow(),
172 wxT("how did we lose focus?") );
e8225073 173
1e6feb95
VZ
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));
789295bf 178 }
a8f25787 179 //else: we don't have caret right now, nothing to do (this does happen)
789295bf 180}
cfb76a19
RD
181
182
183// ---------------------------------------------------------------------------
184// resizing the caret
185// ---------------------------------------------------------------------------
186
187void wxCaret::DoSize()
188{
189 if ( m_hasCaret )
190 {
191 m_hasCaret = FALSE;
192 CALL_CARET_API(DestroyCaret, ());
193 MSWCreateCaret();
fa06b433 194 OnSetFocus();
cfb76a19
RD
195 }
196}
adf9e099
VZ
197
198#endif