Create a new DC in GetDCEx when none are available.
[wine] / windows / caret.c
1 /*
2  * Caret functions
3  *
4  * Copyright 1993 David Metcalfe
5  * Copyright 1996 Frans van Dorsselaer
6  */
7
8 #include "winuser.h"
9 #include "wine/winuser16.h"
10 #include "module.h"
11 #include "debug.h"
12
13 typedef struct
14 {
15     HWND     hwnd;
16     UINT     hidden;
17     BOOL     on;
18     INT      x;
19     INT      y;
20     INT      width;
21     INT      height;
22     HBRUSH16   hBrush;
23     UINT     timeout;
24     UINT     timerid;
25 } CARET;
26
27 typedef enum
28 {
29     CARET_OFF = 0,
30     CARET_ON,
31     CARET_TOGGLE,
32 } DISPLAY_CARET;
33
34 static CARET Caret = { 0, 0, FALSE, 0, 0, 2, 12, 0, 500, 0 };
35
36 /*****************************************************************
37  *              CARET_GetHwnd
38  */
39 HWND CARET_GetHwnd(void)
40 {
41     return Caret.hwnd;
42 }
43
44 /*****************************************************************
45  *              CARET_GetRect
46  */
47 void CARET_GetRect(LPRECT lprc)
48 {
49     lprc->right = (lprc->left = Caret.x) + Caret.width - 1;
50     lprc->bottom = (lprc->top = Caret.y) + Caret.height - 1;
51 }
52
53 /*****************************************************************
54  *               CARET_DisplayCaret
55  */
56 static void CARET_DisplayCaret( DISPLAY_CARET status )
57 {
58     HDC hdc;
59     HBRUSH16 hPrevBrush;
60
61     if (Caret.on && (status == CARET_ON)) return;
62     if (!Caret.on && (status == CARET_OFF)) return;
63
64     /* So now it's always a toggle */
65
66     Caret.on = !Caret.on;
67     /* do not use DCX_CACHE here, for x,y,width,height are in logical units */
68     if (!(hdc = GetDCEx( Caret.hwnd, 0, DCX_USESTYLE /*| DCX_CACHE*/ ))) return;
69     hPrevBrush = SelectObject( hdc, Caret.hBrush );
70     PatBlt( hdc, Caret.x, Caret.y, Caret.width, Caret.height, PATINVERT );
71     SelectObject( hdc, hPrevBrush );
72     ReleaseDC( Caret.hwnd, hdc );
73 }
74
75   
76 /*****************************************************************
77  *               CARET_Callback
78  */
79 static VOID CALLBACK CARET_Callback( HWND hwnd, UINT msg, UINT id, DWORD ctime)
80 {
81     TRACE(caret,"hwnd=%04x, timerid=%d, caret=%d\n",
82                   hwnd, id, Caret.on);
83     CARET_DisplayCaret(CARET_TOGGLE);
84 }
85
86
87 /*****************************************************************
88  *               CARET_SetTimer
89  */
90 static void CARET_SetTimer(void)
91 {
92     if (Caret.timerid) KillSystemTimer( (HWND)0, Caret.timerid );
93     Caret.timerid = SetSystemTimer( (HWND)0, 0, Caret.timeout,
94                                       CARET_Callback );
95 }
96
97
98 /*****************************************************************
99  *               CARET_ResetTimer
100  */
101 static void CARET_ResetTimer(void)
102 {
103     if (Caret.timerid) 
104     {
105         KillSystemTimer( (HWND)0, Caret.timerid );
106         Caret.timerid = SetSystemTimer( (HWND)0, 0, Caret.timeout,
107                                           CARET_Callback );
108     }
109 }
110
111
112 /*****************************************************************
113  *               CARET_KillTimer
114  */
115 static void CARET_KillTimer(void)
116 {
117     if (Caret.timerid) 
118     {
119         KillSystemTimer( (HWND)0, Caret.timerid );
120         Caret.timerid = 0;
121     }
122 }
123
124
125 /*****************************************************************
126  *           CreateCaret16   (USER.163)
127  */
128 void WINAPI CreateCaret16( HWND16 hwnd, HBITMAP16 bitmap,
129                            INT16 width, INT16 height )
130 {
131     CreateCaret( hwnd, bitmap, width, height );
132 }
133
134 /*****************************************************************
135  *           CreateCaret32   (USER32.66)
136  */
137 BOOL WINAPI CreateCaret( HWND hwnd, HBITMAP bitmap,
138                              INT width, INT height )
139 {
140     TRACE(caret,"hwnd=%04x\n", hwnd);
141
142     if (!hwnd) return FALSE;
143
144     /* if cursor already exists, destroy it */
145     if (Caret.hwnd) DestroyCaret();
146
147     if (bitmap && (bitmap != 1))
148     {
149         BITMAP16 bmp;
150         if (!GetObject16( bitmap, sizeof(bmp), &bmp )) return FALSE;
151         Caret.width = bmp.bmWidth;
152         Caret.height = bmp.bmHeight;
153         /* FIXME: we should make a copy of the bitmap instead of a brush */
154         Caret.hBrush = CreatePatternBrush( bitmap );
155     }
156     else
157     {
158         Caret.width = width ? width : GetSystemMetrics(SM_CXBORDER);
159         Caret.height = height ? height : GetSystemMetrics(SM_CYBORDER);
160         Caret.hBrush = CreateSolidBrush(bitmap ?
161                                           GetSysColor(COLOR_GRAYTEXT) :
162                                           GetSysColor(COLOR_WINDOW) );
163     }
164
165     Caret.hwnd = hwnd;
166     Caret.hidden = 1;
167     Caret.on = FALSE;
168     Caret.x = 0;
169     Caret.y = 0;
170
171     Caret.timeout = GetProfileIntA( "windows", "CursorBlinkRate", 500 );
172     return TRUE;
173 }
174    
175
176 /*****************************************************************
177  *           DestroyCaret16   (USER.164)
178  */
179 void WINAPI DestroyCaret16(void)
180 {
181     DestroyCaret();
182 }
183
184
185 /*****************************************************************
186  *           DestroyCaret32   (USER32.131)
187  */
188 BOOL WINAPI DestroyCaret(void)
189 {
190     if (!Caret.hwnd) return FALSE;
191
192     TRACE(caret,"hwnd=%04x, timerid=%d\n",
193                 Caret.hwnd, Caret.timerid);
194
195     CARET_KillTimer();
196     CARET_DisplayCaret(CARET_OFF);
197     DeleteObject( Caret.hBrush );
198     Caret.hwnd = 0;
199     return TRUE;
200 }
201
202
203 /*****************************************************************
204  *           SetCaretPos16   (USER.165)
205  */
206 void WINAPI SetCaretPos16( INT16 x, INT16 y )
207 {
208     SetCaretPos( x, y );
209 }
210
211
212 /*****************************************************************
213  *           SetCaretPos32   (USER32.466)
214  */
215 BOOL WINAPI SetCaretPos( INT x, INT y)
216 {
217     if (!Caret.hwnd) return FALSE;
218     if ((x == Caret.x) && (y == Caret.y)) return TRUE;
219
220     TRACE(caret,"x=%d, y=%d\n", x, y);
221
222     CARET_KillTimer();
223     CARET_DisplayCaret(CARET_OFF);
224     Caret.x = x;
225     Caret.y = y;
226     if (!Caret.hidden)
227     {
228         CARET_DisplayCaret(CARET_ON);
229         CARET_SetTimer();
230     }
231     return TRUE;
232 }
233
234
235 /*****************************************************************
236  *           HideCaret16   (USER.166)
237  */
238 void WINAPI HideCaret16( HWND16 hwnd )
239 {
240     HideCaret( hwnd );
241 }
242
243
244 /*****************************************************************
245  *           HideCaret32   (USER32.317)
246  */
247 BOOL WINAPI HideCaret( HWND hwnd )
248 {
249     if (!Caret.hwnd) return FALSE;
250     if (hwnd && (Caret.hwnd != hwnd)) return FALSE;
251
252     TRACE(caret,"hwnd=%04x, hidden=%d\n",
253                   hwnd, Caret.hidden);
254
255     CARET_KillTimer();
256     CARET_DisplayCaret(CARET_OFF);
257     Caret.hidden++;
258     return TRUE;
259 }
260
261
262 /*****************************************************************
263  *           ShowCaret16   (USER.167)
264  */
265 void WINAPI ShowCaret16( HWND16 hwnd )
266 {
267     ShowCaret( hwnd );
268 }
269
270
271 /*****************************************************************
272  *           ShowCaret32   (USER32.529)
273  */
274 BOOL WINAPI ShowCaret( HWND hwnd )
275 {
276     if (!Caret.hwnd) return FALSE;
277     if (hwnd && (Caret.hwnd != hwnd)) return FALSE;
278
279     TRACE(caret,"hwnd=%04x, hidden=%d\n",
280                 hwnd, Caret.hidden);
281
282     if (Caret.hidden)
283     {
284         Caret.hidden--;
285         if (!Caret.hidden)
286         {
287             CARET_DisplayCaret(CARET_ON);
288             CARET_SetTimer();
289         }
290     }
291     return TRUE;
292 }
293
294
295 /*****************************************************************
296  *           SetCaretBlinkTime16   (USER.168)
297  */
298 void WINAPI SetCaretBlinkTime16( UINT16 msecs )
299 {
300     SetCaretBlinkTime( msecs );
301 }
302
303 /*****************************************************************
304  *           SetCaretBlinkTime32   (USER32.465)
305  */
306 BOOL WINAPI SetCaretBlinkTime( UINT msecs )
307 {
308     if (!Caret.hwnd) return FALSE;
309
310     TRACE(caret,"hwnd=%04x, msecs=%d\n",
311                 Caret.hwnd, msecs);
312
313     Caret.timeout = msecs;
314     CARET_ResetTimer();
315     return TRUE;
316 }
317
318
319 /*****************************************************************
320  *           GetCaretBlinkTime16   (USER.169)
321  */
322 UINT16 WINAPI GetCaretBlinkTime16(void)
323 {
324     return (UINT16)GetCaretBlinkTime();
325 }
326
327
328 /*****************************************************************
329  *           GetCaretBlinkTime32   (USER32.209)
330  */
331 UINT WINAPI GetCaretBlinkTime(void)
332 {
333     return Caret.timeout;
334 }
335
336
337 /*****************************************************************
338  *           GetCaretPos16   (USER.183)
339  */
340 VOID WINAPI GetCaretPos16( LPPOINT16 pt )
341 {
342     if (!Caret.hwnd || !pt) return;
343
344     TRACE(caret,"hwnd=%04x, pt=%p, x=%d, y=%d\n",
345                   Caret.hwnd, pt, Caret.x, Caret.y);
346     pt->x = (INT16)Caret.x;
347     pt->y = (INT16)Caret.y;
348 }
349
350
351 /*****************************************************************
352  *           GetCaretPos32   (USER32.210)
353  */
354 BOOL WINAPI GetCaretPos( LPPOINT pt )
355 {
356     if (!Caret.hwnd || !pt) return FALSE;
357     pt->x = Caret.x;
358     pt->y = Caret.y;
359     return TRUE;
360 }