Release 940301
[wine] / windows / caret.c
1 /*
2  * Caret functions
3  *
4  * Copyright 1993 David Metcalfe
5  */
6
7 static char Copyright[] = "Copyright  David Metcalfe, 1993";
8
9 #include "windows.h"
10
11 typedef struct
12 {
13     HWND          hwnd;
14     short         hidden;
15     BOOL          on;
16     short         x;
17     short         y;
18     short         width;
19     short         height;
20     COLORREF      color;
21     HBITMAP       bitmap;
22     WORD          timeout;
23     WORD          timerid;
24 } CARET;
25
26 static CARET Caret;
27 static BOOL LockCaret;
28
29 static WORD CARET_Callback(HWND hwnd, WORD msg, WORD timerid, LONG ctime);
30 static void CARET_HideCaret();
31
32
33 /*****************************************************************
34  *               CARET_Callback
35  */
36
37 static WORD CARET_Callback(HWND hwnd, WORD msg, WORD timerid, LONG ctime)
38 {
39     HDC hdc;
40     HBRUSH hBrush;
41     HRGN rgn;
42
43 #ifdef DEBUG_CARET
44     printf("CARET_Callback: id=%d: LockCaret=%d, hidden=%d, on=%d\n",
45            timerid, LockCaret, Caret.hidden, Caret.on);
46 #endif
47     if (!LockCaret && (!Caret.hidden || Caret.on))
48     {
49         Caret.on = (Caret.on ? FALSE : TRUE);
50         hdc = GetDC(Caret.hwnd);
51         if (Caret.bitmap == 0 || Caret.bitmap == 1)
52             hBrush = CreateSolidBrush(Caret.color);
53         else
54             hBrush = CreatePatternBrush(Caret.bitmap);
55         SelectObject(hdc, (HANDLE)hBrush);
56         SetROP2(hdc, R2_XORPEN);
57         rgn = CreateRectRgn(Caret.x, Caret.y, 
58                             Caret.x + Caret.width,
59                             Caret.y + Caret.height);
60         FillRgn(hdc, rgn, hBrush);
61         DeleteObject((HANDLE)rgn);
62         DeleteObject((HANDLE)hBrush);
63         ReleaseDC(Caret.hwnd, hdc);
64     }
65     return 0;
66 }
67
68
69 /*****************************************************************
70  *               CARET_HideCaret
71  */
72
73 static void CARET_HideCaret()
74 {
75     HDC hdc;
76     HBRUSH hBrush;
77     HRGN rgn;
78
79     Caret.on = FALSE;
80     hdc = GetDC(Caret.hwnd);
81     if (Caret.bitmap == 0 || Caret.bitmap == 1)
82         hBrush = CreateSolidBrush(Caret.color);
83     else
84         hBrush = CreatePatternBrush(Caret.bitmap);
85     SelectObject(hdc, (HANDLE)hBrush);
86     SetROP2(hdc, R2_XORPEN);
87     rgn = CreateRectRgn(Caret.x, Caret.y, 
88                         Caret.x + Caret.width,
89                         Caret.y + Caret.height);
90     FillRgn(hdc, rgn, hBrush);
91     DeleteObject((HANDLE)rgn);
92     DeleteObject((HANDLE)hBrush);
93     ReleaseDC(Caret.hwnd, hdc);
94 }
95
96
97 /*****************************************************************
98  *               CreateCaret          (USER.163)
99  */
100
101 void CreateCaret(HWND hwnd, HBITMAP bitmap, short width, short height)
102 {
103     if (!hwnd) return;
104
105
106     /* if cursor already exists, destroy it */
107 /*    if (Caret.hwnd)
108         DestroyCaret();
109 */
110     if (bitmap && bitmap != 1)
111         Caret.bitmap = bitmap;
112
113     if (width)
114         Caret.width = width;
115     else
116         Caret.width = GetSystemMetrics(SM_CXBORDER);
117
118     if (height)
119         Caret.height = height;
120     else
121         Caret.height = GetSystemMetrics(SM_CYBORDER);
122
123     Caret.hwnd = hwnd;
124     Caret.hidden = 1;
125     Caret.on = FALSE;
126     Caret.x = 0;
127     Caret.y = 0;
128     if (bitmap == 1)
129         Caret.color = GetSysColor(COLOR_GRAYTEXT);
130     else
131         Caret.color = GetSysColor(COLOR_WINDOWTEXT);
132     Caret.timeout = 750;
133     LockCaret = FALSE;
134
135     Caret.timerid = SetSystemTimer(NULL, 0, Caret.timeout, CARET_Callback);
136
137 #ifdef DEBUG_CARET
138     printf("CreateCaret: hwnd=%d, timerid=%d\n", hwnd, Caret.timerid);
139 #endif
140 }
141    
142
143 /*****************************************************************
144  *               DestroyCaret         (USER.164)
145  */
146
147 void DestroyCaret()
148 {
149 /*    if (!Caret.hwnd) return;
150 */
151 #ifdef DEBUG_CARET
152     printf("DestroyCaret: timerid\n", Caret.timerid);
153 #endif
154
155     KillSystemTimer(NULL, Caret.timerid);
156
157     if (Caret.on)
158         CARET_HideCaret();
159
160     Caret.hwnd = 0;          /* cursor marked as not existing */
161 }
162
163
164 /*****************************************************************
165  *               SetCaretPos          (USER.165)
166  */
167
168 void SetCaretPos(short x, short y)
169 {
170     HDC hdc;
171     HBRUSH hBrush;
172     HRGN rgn;
173
174     if (!Caret.hwnd) return;
175
176 #ifdef DEBUG_CARET
177     printf("SetCaretPos: x=%d, y=%d\n", x, y);
178 #endif
179
180     LockCaret = TRUE;
181     if (Caret.on)
182         CARET_HideCaret();
183
184     Caret.x = x;
185     Caret.y = y;
186     LockCaret = FALSE;
187 }
188
189 /*****************************************************************
190  *               HideCaret            (USER.166)
191  */
192
193 void HideCaret(HWND hwnd)
194 {
195     if (!Caret.hwnd) return;
196     if (hwnd && (Caret.hwnd != hwnd)) return;
197
198     LockCaret = TRUE;
199     if (Caret.on)
200         CARET_HideCaret();
201
202     ++Caret.hidden;
203     LockCaret = FALSE;
204 }
205
206
207 /*****************************************************************
208  *               ShowCaret            (USER.167)
209  */
210
211 void ShowCaret(HWND hwnd)
212 {
213     if (!Caret.hwnd) return;
214     if (hwnd && (Caret.hwnd != hwnd)) return;
215
216 #ifdef DEBUG_CARET
217     printf("ShowCaret: hidden=%d\n", Caret.hidden);
218 #endif
219     if (Caret.hidden)
220         --Caret.hidden;
221 }
222
223
224 /*****************************************************************
225  *               SetCaretBlinkTime    (USER.168)
226  */
227
228 void SetCaretBlinkTime(WORD msecs)
229 {
230     if (!Caret.hwnd) return;
231
232     KillSystemTimer(NULL, Caret.timerid);
233     Caret.timeout = msecs;
234     Caret.timerid = SetSystemTimer(NULL, 0, Caret.timeout, CARET_Callback);
235 }
236
237
238 /*****************************************************************
239  *               GetCaretBlinkTime    (USER.169)
240  */
241
242 WORD GetCaretBlinkTime()
243 {
244     if (!Caret.hwnd) return;
245
246     return Caret.timeout;
247 }
248
249
250 /*****************************************************************
251  *               GetCaretPos          (USER.183)
252  */
253
254 void GetCaretPos(LPPOINT pt)
255 {
256     if (!Caret.hwnd || !pt) return;
257
258     pt->x = Caret.x;
259     pt->y = Caret.y;
260 }