Fixed a couple of recent bugs, and added some more safeguards (could
[wine.git] / windows / caret.c
blob5dc43f6fdb72917be5e565c43149d9ea77ed5e59
1 /*
2 * Caret functions
4 * Copyright 1993 David Metcalfe
5 * Copyright 1996 Frans van Dorsselaer
6 */
8 #include "windef.h"
9 #include "winbase.h"
10 #include "wingdi.h"
11 #include "winuser.h"
12 #include "wine/wingdi16.h"
13 #include "wine/winuser16.h"
14 #include "win.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(caret);
19 typedef struct
21 HWND hwnd;
22 UINT hidden;
23 BOOL on;
24 INT x;
25 INT y;
26 INT width;
27 INT height;
28 HBRUSH16 hBrush;
29 UINT timeout;
30 UINT timerid;
31 } CARET;
33 typedef enum
35 CARET_OFF = 0,
36 CARET_ON,
37 CARET_TOGGLE
38 } DISPLAY_CARET;
40 static CARET Caret = { 0, 0, FALSE, 0, 0, 2, 12, 0, 500, 0 };
42 /*****************************************************************
43 * CARET_GetHwnd
45 HWND CARET_GetHwnd(void)
47 return Caret.hwnd;
50 /*****************************************************************
51 * CARET_GetRect
53 void CARET_GetRect(LPRECT lprc)
55 lprc->right = (lprc->left = Caret.x) + Caret.width - 1;
56 lprc->bottom = (lprc->top = Caret.y) + Caret.height - 1;
59 /*****************************************************************
60 * CARET_DisplayCaret
62 static void CARET_DisplayCaret( DISPLAY_CARET status )
64 HDC hdc;
65 HBRUSH16 hPrevBrush;
67 if (Caret.on && (status == CARET_ON)) return;
68 if (!Caret.on && (status == CARET_OFF)) return;
70 /* So now it's always a toggle */
72 Caret.on = !Caret.on;
73 /* do not use DCX_CACHE here, for x,y,width,height are in logical units */
74 if (!(hdc = GetDCEx( Caret.hwnd, 0, DCX_USESTYLE /*| DCX_CACHE*/ ))) return;
75 hPrevBrush = SelectObject( hdc, Caret.hBrush );
76 PatBlt( hdc, Caret.x, Caret.y, Caret.width, Caret.height, PATINVERT );
77 SelectObject( hdc, hPrevBrush );
78 ReleaseDC( Caret.hwnd, hdc );
82 /*****************************************************************
83 * CARET_Callback
85 static VOID CALLBACK CARET_Callback( HWND hwnd, UINT msg, UINT id, DWORD ctime)
87 TRACE("hwnd=%04x, timerid=%d, caret=%d\n",
88 hwnd, id, Caret.on);
89 CARET_DisplayCaret(CARET_TOGGLE);
93 /*****************************************************************
94 * CARET_SetTimer
96 static void CARET_SetTimer(void)
98 if (Caret.timerid) KillSystemTimer( (HWND)0, Caret.timerid );
99 Caret.timerid = SetSystemTimer( (HWND)0, 0, Caret.timeout,
100 CARET_Callback );
104 /*****************************************************************
105 * CARET_ResetTimer
107 static void CARET_ResetTimer(void)
109 if (Caret.timerid)
111 KillSystemTimer( (HWND)0, Caret.timerid );
112 Caret.timerid = SetSystemTimer( (HWND)0, 0, Caret.timeout,
113 CARET_Callback );
118 /*****************************************************************
119 * CARET_KillTimer
121 static void CARET_KillTimer(void)
123 if (Caret.timerid)
125 KillSystemTimer( (HWND)0, Caret.timerid );
126 Caret.timerid = 0;
131 /*****************************************************************
132 * CreateCaret16 (USER.163)
134 void WINAPI CreateCaret16( HWND16 hwnd, HBITMAP16 bitmap,
135 INT16 width, INT16 height )
137 CreateCaret( hwnd, bitmap, width, height );
140 /*****************************************************************
141 * CreateCaret (USER32.66)
143 BOOL WINAPI CreateCaret( HWND hwnd, HBITMAP bitmap,
144 INT width, INT height )
146 TRACE("hwnd=%04x\n", hwnd);
148 if (!hwnd) return FALSE;
150 /* if cursor already exists, destroy it */
151 if (Caret.hwnd) DestroyCaret();
153 if (bitmap && (bitmap != 1))
155 BITMAP bmp;
156 if (!GetObjectA( bitmap, sizeof(bmp), &bmp )) return FALSE;
157 Caret.width = bmp.bmWidth;
158 Caret.height = bmp.bmHeight;
159 /* FIXME: we should make a copy of the bitmap instead of a brush */
160 Caret.hBrush = CreatePatternBrush( bitmap );
162 else
164 Caret.width = width ? width : GetSystemMetrics(SM_CXBORDER);
165 Caret.height = height ? height : GetSystemMetrics(SM_CYBORDER);
166 Caret.hBrush = CreateSolidBrush(bitmap ?
167 GetSysColor(COLOR_GRAYTEXT) :
168 GetSysColor(COLOR_WINDOW) );
171 Caret.hwnd = hwnd;
172 Caret.hidden = 1;
173 Caret.on = FALSE;
174 Caret.x = 0;
175 Caret.y = 0;
177 Caret.timeout = GetProfileIntA( "windows", "CursorBlinkRate", 500 );
178 return TRUE;
182 /*****************************************************************
183 * DestroyCaret16 (USER.164)
185 void WINAPI DestroyCaret16(void)
187 DestroyCaret();
191 /*****************************************************************
192 * DestroyCaret (USER32.131)
194 BOOL WINAPI DestroyCaret(void)
196 if (!Caret.hwnd) return FALSE;
198 TRACE("hwnd=%04x, timerid=%d\n",
199 Caret.hwnd, Caret.timerid);
201 CARET_KillTimer();
202 CARET_DisplayCaret(CARET_OFF);
203 DeleteObject( Caret.hBrush );
204 Caret.hwnd = 0;
205 return TRUE;
209 /*****************************************************************
210 * SetCaretPos16 (USER.165)
212 void WINAPI SetCaretPos16( INT16 x, INT16 y )
214 SetCaretPos( x, y );
218 /*****************************************************************
219 * SetCaretPos (USER32.466)
221 BOOL WINAPI SetCaretPos( INT x, INT y)
223 if (!Caret.hwnd) return FALSE;
224 if ((x == Caret.x) && (y == Caret.y)) return TRUE;
226 TRACE("x=%d, y=%d\n", x, y);
228 CARET_KillTimer();
229 CARET_DisplayCaret(CARET_OFF);
230 Caret.x = x;
231 Caret.y = y;
232 if (!Caret.hidden)
234 CARET_DisplayCaret(CARET_ON);
235 CARET_SetTimer();
237 return TRUE;
241 /*****************************************************************
242 * HideCaret16 (USER.166)
244 void WINAPI HideCaret16( HWND16 hwnd )
246 HideCaret( hwnd );
250 /*****************************************************************
251 * HideCaret (USER32.317)
253 BOOL WINAPI HideCaret( HWND hwnd )
255 if (!Caret.hwnd) return FALSE;
256 if (hwnd && (Caret.hwnd != hwnd)) return FALSE;
258 TRACE("hwnd=%04x, hidden=%d\n",
259 hwnd, Caret.hidden);
261 CARET_KillTimer();
262 CARET_DisplayCaret(CARET_OFF);
263 Caret.hidden++;
264 return TRUE;
268 /*****************************************************************
269 * ShowCaret16 (USER.167)
271 void WINAPI ShowCaret16( HWND16 hwnd )
273 ShowCaret( hwnd );
277 /*****************************************************************
278 * ShowCaret (USER32.529)
280 BOOL WINAPI ShowCaret( HWND hwnd )
282 if (!Caret.hwnd) return FALSE;
283 if (hwnd && (Caret.hwnd != hwnd)) return FALSE;
285 TRACE("hwnd=%04x, hidden=%d\n",
286 hwnd, Caret.hidden);
288 if (Caret.hidden)
290 Caret.hidden--;
291 if (!Caret.hidden)
293 CARET_DisplayCaret(CARET_ON);
294 CARET_SetTimer();
297 return TRUE;
301 /*****************************************************************
302 * SetCaretBlinkTime16 (USER.168)
304 void WINAPI SetCaretBlinkTime16( UINT16 msecs )
306 SetCaretBlinkTime( msecs );
309 /*****************************************************************
310 * SetCaretBlinkTime (USER32.465)
312 BOOL WINAPI SetCaretBlinkTime( UINT msecs )
314 if (!Caret.hwnd) return FALSE;
316 TRACE("hwnd=%04x, msecs=%d\n",
317 Caret.hwnd, msecs);
319 Caret.timeout = msecs;
320 CARET_ResetTimer();
321 return TRUE;
325 /*****************************************************************
326 * GetCaretBlinkTime16 (USER.169)
328 UINT16 WINAPI GetCaretBlinkTime16(void)
330 return (UINT16)GetCaretBlinkTime();
334 /*****************************************************************
335 * GetCaretBlinkTime (USER32.209)
337 UINT WINAPI GetCaretBlinkTime(void)
339 return Caret.timeout;
343 /*****************************************************************
344 * GetCaretPos16 (USER.183)
346 VOID WINAPI GetCaretPos16( LPPOINT16 pt )
348 if (!Caret.hwnd || !pt) return;
350 TRACE("hwnd=%04x, pt=%p, x=%d, y=%d\n",
351 Caret.hwnd, pt, Caret.x, Caret.y);
352 pt->x = (INT16)Caret.x;
353 pt->y = (INT16)Caret.y;
357 /*****************************************************************
358 * GetCaretPos (USER32.210)
360 BOOL WINAPI GetCaretPos( LPPOINT pt )
362 if (!Caret.hwnd || !pt) return FALSE;
363 pt->x = Caret.x;
364 pt->y = Caret.y;
365 return TRUE;