Prevent calling null functions.
[wine/wine64.git] / windows / caret.c
blobcdefeeafc3784ec5d049d262d962ecf614fcbd1b
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 HBRUSH 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 HBRUSH 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 * CreateCaret (USER32.@)
134 BOOL WINAPI CreateCaret( HWND hwnd, HBITMAP bitmap,
135 INT width, INT height )
137 TRACE("hwnd=%04x\n", hwnd);
139 if (!hwnd) return FALSE;
141 /* if cursor already exists, destroy it */
142 if (Caret.hwnd) DestroyCaret();
144 if (bitmap && (bitmap != 1))
146 BITMAP bmp;
147 if (!GetObjectA( bitmap, sizeof(bmp), &bmp )) return FALSE;
148 Caret.width = bmp.bmWidth;
149 Caret.height = bmp.bmHeight;
150 /* FIXME: we should make a copy of the bitmap instead of a brush */
151 Caret.hBrush = CreatePatternBrush( bitmap );
153 else
155 Caret.width = width ? width : GetSystemMetrics(SM_CXBORDER);
156 Caret.height = height ? height : GetSystemMetrics(SM_CYBORDER);
157 Caret.hBrush = CreateSolidBrush(bitmap ?
158 GetSysColor(COLOR_GRAYTEXT) :
159 GetSysColor(COLOR_WINDOW) );
162 Caret.hwnd = WIN_GetFullHandle( hwnd );
163 Caret.hidden = 1;
164 Caret.on = FALSE;
165 Caret.x = 0;
166 Caret.y = 0;
168 Caret.timeout = GetProfileIntA( "windows", "CursorBlinkRate", 500 );
169 return TRUE;
173 /*****************************************************************
174 * DestroyCaret (USER.164)
176 void WINAPI DestroyCaret16(void)
178 DestroyCaret();
182 /*****************************************************************
183 * DestroyCaret (USER32.@)
185 BOOL WINAPI DestroyCaret(void)
187 if (!Caret.hwnd) return FALSE;
189 TRACE("hwnd=%04x, timerid=%d\n",
190 Caret.hwnd, Caret.timerid);
192 CARET_KillTimer();
193 CARET_DisplayCaret(CARET_OFF);
194 DeleteObject( Caret.hBrush );
195 Caret.hwnd = 0;
196 return TRUE;
200 /*****************************************************************
201 * SetCaretPos (USER.165)
203 void WINAPI SetCaretPos16( INT16 x, INT16 y )
205 SetCaretPos( x, y );
209 /*****************************************************************
210 * SetCaretPos (USER32.@)
212 BOOL WINAPI SetCaretPos( INT x, INT y)
214 if (!Caret.hwnd) return FALSE;
215 if ((x == Caret.x) && (y == Caret.y)) return TRUE;
217 TRACE("x=%d, y=%d\n", x, y);
219 CARET_KillTimer();
220 CARET_DisplayCaret(CARET_OFF);
221 Caret.x = x;
222 Caret.y = y;
223 if (!Caret.hidden)
225 CARET_DisplayCaret(CARET_ON);
226 CARET_SetTimer();
228 return TRUE;
232 /*****************************************************************
233 * HideCaret (USER32.@)
235 BOOL WINAPI HideCaret( HWND hwnd )
237 if (!Caret.hwnd) return FALSE;
238 if (hwnd && (Caret.hwnd != WIN_GetFullHandle(hwnd))) return FALSE;
240 TRACE("hwnd=%04x, hidden=%d\n",
241 hwnd, Caret.hidden);
243 CARET_KillTimer();
244 CARET_DisplayCaret(CARET_OFF);
245 Caret.hidden++;
246 return TRUE;
250 /*****************************************************************
251 * ShowCaret (USER32.@)
253 BOOL WINAPI ShowCaret( HWND hwnd )
255 if (!Caret.hwnd) return FALSE;
256 if (hwnd && (Caret.hwnd != WIN_GetFullHandle(hwnd))) return FALSE;
258 TRACE("hwnd=%04x, hidden=%d\n",
259 hwnd, Caret.hidden);
261 if (Caret.hidden)
263 Caret.hidden--;
264 if (!Caret.hidden)
266 CARET_DisplayCaret(CARET_ON);
267 CARET_SetTimer();
270 return TRUE;
274 /*****************************************************************
275 * SetCaretBlinkTime (USER.168)
277 void WINAPI SetCaretBlinkTime16( UINT16 msecs )
279 SetCaretBlinkTime( msecs );
282 /*****************************************************************
283 * SetCaretBlinkTime (USER32.@)
285 BOOL WINAPI SetCaretBlinkTime( UINT msecs )
287 if (!Caret.hwnd) return FALSE;
289 TRACE("hwnd=%04x, msecs=%d\n",
290 Caret.hwnd, msecs);
292 Caret.timeout = msecs;
293 CARET_ResetTimer();
294 return TRUE;
298 /*****************************************************************
299 * GetCaretBlinkTime (USER.169)
301 UINT16 WINAPI GetCaretBlinkTime16(void)
303 return (UINT16)GetCaretBlinkTime();
307 /*****************************************************************
308 * GetCaretBlinkTime (USER32.@)
310 UINT WINAPI GetCaretBlinkTime(void)
312 return Caret.timeout;
316 /*****************************************************************
317 * GetCaretPos (USER.183)
319 VOID WINAPI GetCaretPos16( LPPOINT16 pt )
321 if (!Caret.hwnd || !pt) return;
323 TRACE("hwnd=%04x, pt=%p, x=%d, y=%d\n",
324 Caret.hwnd, pt, Caret.x, Caret.y);
325 pt->x = (INT16)Caret.x;
326 pt->y = (INT16)Caret.y;
330 /*****************************************************************
331 * GetCaretPos (USER32.@)
333 BOOL WINAPI GetCaretPos( LPPOINT pt )
335 if (!Caret.hwnd || !pt) return FALSE;
336 pt->x = Caret.x;
337 pt->y = Caret.y;
338 return TRUE;