reshuffling of dlls
[reactos.git] / reactos / dll / user32 / controls / button.c
1 /* File: button.c -- Button type widgets
2 *
3 * Copyright (C) 1993 Johannes Ruscheinski
4 * Copyright (C) 1993 David Metcalfe
5 * Copyright (C) 1994 Alexandre Julliard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include <user32.h>
23
24 /* GetWindowLong offsets for window extra information */
25 #define STATE_GWL_OFFSET 0
26 #define HFONT_GWL_OFFSET (sizeof(LONG))
27 #define HIMAGE_GWL_OFFSET (HFONT_GWL_OFFSET*sizeof(LONG))
28 #define NB_EXTRA_BYTES (HIMAGE_GWL_OFFSET*sizeof(LONG))
29
30 /* Button state values */
31 #define BUTTON_UNCHECKED 0x00
32 #define BUTTON_CHECKED 0x01
33 #define BUTTON_3STATE 0x02
34 #define BUTTON_HIGHLIGHTED 0x04
35 #define BUTTON_HASFOCUS 0x08
36 #define BUTTON_NSTATES 0x0F
37 /* undocumented flags */
38 #define BUTTON_BTNPRESSED 0x40
39 #define BUTTON_UNKNOWN2 0x20
40 #define BUTTON_UNKNOWN3 0x10
41
42 #define BUTTON_NOTIFY_PARENT(hWnd, code) \
43 do { /* Notify parent which has created this button control */ \
44 TRACE("notification " #code " sent to hwnd=%p\n", GetParent(hWnd)); \
45 SendMessageW(GetParent(hWnd), WM_COMMAND, \
46 MAKEWPARAM(GetWindowLongPtrW((hWnd),GWLP_ID), (code)), \
47 (LPARAM)(hWnd)); \
48 } while(0)
49
50 static UINT BUTTON_CalcLabelRect( HWND hwnd, HDC hdc, RECT *rc );
51 static void PB_Paint( HWND hwnd, HDC hDC, UINT action );
52 static void CB_Paint( HWND hwnd, HDC hDC, UINT action );
53 static void GB_Paint( HWND hwnd, HDC hDC, UINT action );
54 static void UB_Paint( HWND hwnd, HDC hDC, UINT action );
55 static void OB_Paint( HWND hwnd, HDC hDC, UINT action );
56 static void BUTTON_CheckAutoRadioButton( HWND hwnd );
57 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
58 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
59
60 #define MAX_BTN_TYPE 12
61
62 static const WORD maxCheckState[MAX_BTN_TYPE] =
63 {
64 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
65 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
66 BUTTON_CHECKED, /* BS_CHECKBOX */
67 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
68 BUTTON_CHECKED, /* BS_RADIOBUTTON */
69 BUTTON_3STATE, /* BS_3STATE */
70 BUTTON_3STATE, /* BS_AUTO3STATE */
71 BUTTON_UNCHECKED, /* BS_GROUPBOX */
72 BUTTON_UNCHECKED, /* BS_USERBUTTON */
73 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
74 BUTTON_UNCHECKED, /* Not defined */
75 BUTTON_UNCHECKED /* BS_OWNERDRAW */
76 };
77
78 typedef void (*pfPaint)( HWND hwnd, HDC hdc, UINT action );
79
80 static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
81 {
82 PB_Paint, /* BS_PUSHBUTTON */
83 PB_Paint, /* BS_DEFPUSHBUTTON */
84 CB_Paint, /* BS_CHECKBOX */
85 CB_Paint, /* BS_AUTOCHECKBOX */
86 CB_Paint, /* BS_RADIOBUTTON */
87 CB_Paint, /* BS_3STATE */
88 CB_Paint, /* BS_AUTO3STATE */
89 GB_Paint, /* BS_GROUPBOX */
90 UB_Paint, /* BS_USERBUTTON */
91 CB_Paint, /* BS_AUTORADIOBUTTON */
92 NULL, /* Not defined */
93 OB_Paint /* BS_OWNERDRAW */
94 };
95
96 static HBITMAP hbitmapCheckBoxes = 0;
97 static WORD checkBoxWidth = 0, checkBoxHeight = 0;
98
99
100 /*********************************************************************
101 * button class descriptor
102 */
103 const struct builtin_class_descr BUTTON_builtin_class =
104 {
105 #ifdef __REACTOS__
106 L"Button", /* name */
107 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
108 ButtonWndProcW, /* procW */
109 ButtonWndProcA, /* procA */
110 NB_EXTRA_BYTES, /* extra */
111 (LPWSTR)IDC_ARROW, /* cursor */
112 0 /* brush */
113 #else
114 "Button", /* name */
115 CS_DBLCLKS | CS_VREDRAW | CS_HREDRAW | CS_PARENTDC, /* style */
116 ButtonWndProcA, /* procA */
117 ButtonWndProcW, /* procW */
118 NB_EXTRA_BYTES, /* extra */
119 IDC_ARROW, /* cursor */
120 0 /* brush */
121 #endif
122 };
123
124
125 __inline static LONG get_button_state( HWND hwnd )
126 {
127 return GetWindowLongW( hwnd, STATE_GWL_OFFSET );
128 }
129
130 __inline static void set_button_state( HWND hwnd, LONG state )
131 {
132 SetWindowLongW( hwnd, STATE_GWL_OFFSET, state );
133 }
134
135 __inline static HFONT get_button_font( HWND hwnd )
136 {
137 return (HFONT)GetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET );
138 }
139
140 __inline static void set_button_font( HWND hwnd, HFONT font )
141 {
142 SetWindowLongPtrW( hwnd, HFONT_GWL_OFFSET, (LONG)font );
143 }
144
145 __inline static UINT get_button_type( LONG window_style )
146 {
147 return (window_style & 0x0f);
148 }
149
150 /* paint a button of any type */
151 __inline static void paint_button( HWND hwnd, LONG style, UINT action )
152 {
153 if (btnPaintFunc[style] && IsWindowVisible(hwnd))
154 {
155 HDC hdc = GetDC( hwnd );
156 btnPaintFunc[style]( hwnd, hdc, action );
157 ReleaseDC( hwnd, hdc );
158 }
159 }
160
161 /* retrieve the button text; returned buffer must be freed by caller */
162 __inline static WCHAR *get_button_text( HWND hwnd )
163 {
164 INT len = 512;
165 WCHAR *buffer = HeapAlloc( GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR) );
166 if (buffer) InternalGetWindowText( hwnd, buffer, len + 1 );
167 return buffer;
168 }
169
170 /***********************************************************************
171 * ButtonWndProc_common
172 */
173 static LRESULT WINAPI ButtonWndProc_common(HWND hWnd, UINT uMsg,
174 WPARAM wParam, LPARAM lParam, BOOL unicode )
175 {
176 RECT rect;
177 POINT pt;
178 LONG style = GetWindowLongW( hWnd, GWL_STYLE );
179 UINT btn_type = get_button_type( style );
180 LONG state;
181 HANDLE oldHbitmap;
182
183 pt.x = LOWORD(lParam);
184 pt.y = HIWORD(lParam);
185
186 switch (uMsg)
187 {
188 case WM_GETDLGCODE:
189 switch(btn_type)
190 {
191 case BS_USERBUTTON:
192 case BS_PUSHBUTTON: return DLGC_BUTTON | DLGC_UNDEFPUSHBUTTON;
193 case BS_DEFPUSHBUTTON: return DLGC_BUTTON | DLGC_DEFPUSHBUTTON;
194 case BS_RADIOBUTTON:
195 case BS_AUTORADIOBUTTON: return DLGC_BUTTON | DLGC_RADIOBUTTON;
196 case BS_GROUPBOX: return DLGC_STATIC;
197 default: return DLGC_BUTTON;
198 }
199
200 case WM_ENABLE:
201 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
202 break;
203
204 case WM_CREATE:
205 if (!hbitmapCheckBoxes)
206 {
207 BITMAP bmp;
208 hbitmapCheckBoxes = LoadBitmapW(0, MAKEINTRESOURCEW(OBM_CHECKBOXES));
209 GetObjectW( hbitmapCheckBoxes, sizeof(bmp), &bmp );
210 checkBoxWidth = bmp.bmWidth / 4;
211 checkBoxHeight = bmp.bmHeight / 3;
212 }
213 if (btn_type >= MAX_BTN_TYPE)
214 return -1; /* abort */
215 set_button_state( hWnd, BUTTON_UNCHECKED );
216 return 0;
217
218 case WM_ERASEBKGND:
219 if (btn_type == BS_OWNERDRAW)
220 {
221 HDC hdc = (HDC)wParam;
222 RECT rc;
223 HBRUSH hBrush;
224 HWND parent = GetParent(hWnd);
225 if (!parent) parent = hWnd;
226 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hdc, (LPARAM)hWnd);
227 if (!hBrush) /* did the app forget to call defwindowproc ? */
228 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
229 (WPARAM)hdc, (LPARAM)hWnd);
230 GetClientRect(hWnd, &rc);
231 FillRect(hdc, &rc, hBrush);
232 }
233 return 1;
234
235 case WM_PRINTCLIENT:
236 case WM_PAINT:
237 if (btnPaintFunc[btn_type])
238 {
239 PAINTSTRUCT ps;
240 HDC hdc = wParam ? (HDC)wParam : BeginPaint( hWnd, &ps );
241 int nOldMode = SetBkMode( hdc, OPAQUE );
242 (btnPaintFunc[btn_type])( hWnd, hdc, ODA_DRAWENTIRE );
243 SetBkMode(hdc, nOldMode); /* reset painting mode */
244 if( !wParam ) EndPaint( hWnd, &ps );
245 }
246 break;
247
248 case WM_KEYDOWN:
249 if (wParam == VK_SPACE)
250 {
251 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
252 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
253 }
254 break;
255
256 case WM_LBUTTONDBLCLK:
257 if(style & BS_NOTIFY ||
258 btn_type == BS_RADIOBUTTON ||
259 btn_type == BS_USERBUTTON ||
260 btn_type == BS_OWNERDRAW)
261 {
262 BUTTON_NOTIFY_PARENT(hWnd, BN_DOUBLECLICKED);
263 break;
264 }
265 /* fall through */
266 case WM_LBUTTONDOWN:
267 SetCapture( hWnd );
268 SetFocus( hWnd );
269 set_button_state( hWnd, get_button_state( hWnd ) | BUTTON_BTNPRESSED );
270 SendMessageW( hWnd, BM_SETSTATE, TRUE, 0 );
271 break;
272
273 case WM_KEYUP:
274 if (wParam != VK_SPACE)
275 break;
276 /* fall through */
277 case WM_LBUTTONUP:
278 state = get_button_state( hWnd );
279 if (!(state & BUTTON_BTNPRESSED)) break;
280 state &= BUTTON_NSTATES;
281 set_button_state( hWnd, state );
282 if (!(state & BUTTON_HIGHLIGHTED))
283 {
284 ReleaseCapture();
285 break;
286 }
287 SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
288 ReleaseCapture();
289 GetClientRect( hWnd, &rect );
290 if (uMsg == WM_KEYUP || PtInRect( &rect, pt ))
291 {
292 state = get_button_state( hWnd );
293 switch(btn_type)
294 {
295 case BS_AUTOCHECKBOX:
296 SendMessageW( hWnd, BM_SETCHECK, !(state & BUTTON_CHECKED), 0 );
297 break;
298 case BS_AUTORADIOBUTTON:
299 SendMessageW( hWnd, BM_SETCHECK, TRUE, 0 );
300 break;
301 case BS_AUTO3STATE:
302 SendMessageW( hWnd, BM_SETCHECK,
303 (state & BUTTON_3STATE) ? 0 : ((state & 3) + 1), 0 );
304 break;
305 }
306 BUTTON_NOTIFY_PARENT(hWnd, BN_CLICKED);
307 }
308 break;
309
310 case WM_CAPTURECHANGED:
311 state = get_button_state( hWnd );
312 if (state & BUTTON_BTNPRESSED)
313 {
314 state &= BUTTON_NSTATES;
315 set_button_state( hWnd, state );
316 if (state & BUTTON_HIGHLIGHTED) SendMessageW( hWnd, BM_SETSTATE, FALSE, 0 );
317 }
318 break;
319
320 case WM_MOUSEMOVE:
321 if ((wParam & MK_LBUTTON) && GetCapture() == hWnd)
322 {
323 GetClientRect( hWnd, &rect );
324 SendMessageW( hWnd, BM_SETSTATE, PtInRect(&rect, pt), 0 );
325 }
326 break;
327
328 case WM_SETTEXT:
329 {
330 /* Clear an old text here as Windows does */
331 HDC hdc = GetDC(hWnd);
332 HBRUSH hbrush;
333 RECT client, rc;
334 HWND parent = GetParent(hWnd);
335
336 if (!parent) parent = hWnd;
337 hbrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
338 (WPARAM)hdc, (LPARAM)hWnd);
339 if (!hbrush) /* did the app forget to call DefWindowProc ? */
340 hbrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
341 (WPARAM)hdc, (LPARAM)hWnd);
342
343 GetClientRect(hWnd, &client);
344 rc = client;
345 BUTTON_CalcLabelRect(hWnd, hdc, &rc);
346 /* Clip by client rect bounds */
347 if (rc.right > client.right) rc.right = client.right;
348 if (rc.bottom > client.bottom) rc.bottom = client.bottom;
349 FillRect(hdc, &rc, hbrush);
350 ReleaseDC(hWnd, hdc);
351
352 if (unicode) DefWindowProcW( hWnd, WM_SETTEXT, wParam, lParam );
353 else DefWindowProcA( hWnd, WM_SETTEXT, wParam, lParam );
354 if (btn_type == BS_GROUPBOX) /* Yes, only for BS_GROUPBOX */
355 InvalidateRect( hWnd, NULL, TRUE );
356 else
357 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
358 return 1; /* success. FIXME: check text length */
359 }
360
361 case WM_SETFONT:
362 set_button_font( hWnd, (HFONT)wParam );
363 if (lParam) paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
364 break;
365
366 case WM_GETFONT:
367 return (LRESULT)get_button_font( hWnd );
368
369 case WM_SETFOCUS:
370 set_button_state( hWnd, get_button_state(hWnd) | BUTTON_HASFOCUS );
371 paint_button( hWnd, btn_type, ODA_FOCUS );
372 if (style & BS_NOTIFY)
373 BUTTON_NOTIFY_PARENT(hWnd, BN_SETFOCUS);
374 break;
375
376 case WM_KILLFOCUS:
377 state = get_button_state( hWnd );
378 set_button_state( hWnd, state & ~BUTTON_HASFOCUS );
379 paint_button( hWnd, btn_type, ODA_FOCUS );
380
381 if ((state & BUTTON_BTNPRESSED) && GetCapture() == hWnd)
382 ReleaseCapture();
383 if (style & BS_NOTIFY)
384 BUTTON_NOTIFY_PARENT(hWnd, BN_KILLFOCUS);
385
386 break;
387
388 case WM_SYSCOLORCHANGE:
389 InvalidateRect( hWnd, NULL, FALSE );
390 break;
391
392 #ifndef __REACTOS__
393 case BM_SETSTYLE16:
394 #endif
395 case BM_SETSTYLE:
396 if ((wParam & 0x0f) >= MAX_BTN_TYPE) break;
397 btn_type = wParam & 0x0f;
398 style = (style & ~0x0f) | btn_type;
399 SetWindowLongW( hWnd, GWL_STYLE, style );
400
401 /* Only redraw if lParam flag is set.*/
402 if (lParam)
403 paint_button( hWnd, btn_type, ODA_DRAWENTIRE );
404
405 break;
406
407 case BM_CLICK:
408 SendMessageW( hWnd, WM_LBUTTONDOWN, 0, 0 );
409 SendMessageW( hWnd, WM_LBUTTONUP, 0, 0 );
410 break;
411
412 case BM_SETIMAGE:
413 /* Check that image format matches button style */
414 switch (style & (BS_BITMAP|BS_ICON))
415 {
416 case BS_BITMAP:
417 if (wParam != IMAGE_BITMAP) return 0;
418 break;
419 case BS_ICON:
420 if (wParam != IMAGE_ICON) return 0;
421 break;
422 default:
423 return 0;
424 }
425 oldHbitmap = (HBITMAP)SetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET, lParam );
426 InvalidateRect( hWnd, NULL, FALSE );
427 return (LRESULT)oldHbitmap;
428
429 case BM_GETIMAGE:
430 return GetWindowLongPtrW( hWnd, HIMAGE_GWL_OFFSET );
431
432 #ifndef __REACTOS__
433 case BM_GETCHECK16:
434 #endif
435 case BM_GETCHECK:
436 return get_button_state( hWnd ) & 3;
437
438 #ifndef __REACTOS__
439 case BM_SETCHECK16:
440 #endif
441 case BM_SETCHECK:
442 if (wParam > maxCheckState[btn_type]) wParam = maxCheckState[btn_type];
443 state = get_button_state( hWnd );
444 if ((btn_type == BS_RADIOBUTTON) || (btn_type == BS_AUTORADIOBUTTON))
445 {
446 if (wParam) style |= WS_TABSTOP;
447 else style &= ~WS_TABSTOP;
448 SetWindowLongW( hWnd, GWL_STYLE, style );
449 }
450 if ((state & 3) != wParam)
451 {
452 set_button_state( hWnd, (state & ~3) | wParam );
453 paint_button( hWnd, btn_type, ODA_SELECT );
454 }
455 if ((btn_type == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED) && (style & WS_CHILD))
456 BUTTON_CheckAutoRadioButton( hWnd );
457 break;
458
459 #ifndef __REACTOS__
460 case BM_GETSTATE16:
461 #endif
462 case BM_GETSTATE:
463 return get_button_state( hWnd );
464
465 #ifndef __REACTOS__
466 case BM_SETSTATE16:
467 #endif
468 case BM_SETSTATE:
469 state = get_button_state( hWnd );
470 if (wParam)
471 {
472 if (state & BUTTON_HIGHLIGHTED) break;
473 set_button_state( hWnd, state | BUTTON_HIGHLIGHTED );
474 }
475 else
476 {
477 if (!(state & BUTTON_HIGHLIGHTED)) break;
478 set_button_state( hWnd, state & ~BUTTON_HIGHLIGHTED );
479 }
480 paint_button( hWnd, btn_type, ODA_SELECT );
481 break;
482
483 case WM_NCHITTEST:
484 if(btn_type == BS_GROUPBOX) return HTTRANSPARENT;
485 /* fall through */
486 default:
487 return unicode ? DefWindowProcW(hWnd, uMsg, wParam, lParam) :
488 DefWindowProcA(hWnd, uMsg, wParam, lParam);
489 }
490 return 0;
491 }
492
493 /***********************************************************************
494 * ButtonWndProcW
495 * The button window procedure. This is just a wrapper which locks
496 * the passed HWND and calls the real window procedure (with a WND*
497 * pointer pointing to the locked windowstructure).
498 */
499 static LRESULT WINAPI ButtonWndProcW( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
500 {
501 if (!IsWindow( hWnd )) return 0;
502 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, TRUE );
503 }
504
505
506 /***********************************************************************
507 * ButtonWndProcA
508 */
509 static LRESULT WINAPI ButtonWndProcA( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
510 {
511 if (!IsWindow( hWnd )) return 0;
512 return ButtonWndProc_common( hWnd, uMsg, wParam, lParam, FALSE );
513 }
514
515
516 /**********************************************************************
517 * Convert button styles to flags used by DrawText.
518 * TODO: handle WS_EX_RIGHT extended style.
519 */
520 static UINT BUTTON_BStoDT(DWORD style)
521 {
522 UINT dtStyle = DT_NOCLIP; /* We use SelectClipRgn to limit output */
523
524 /* "Convert" pushlike buttons to pushbuttons */
525 if (style & BS_PUSHLIKE)
526 style &= ~0x0F;
527
528 if (!(style & BS_MULTILINE))
529 dtStyle |= DT_SINGLELINE;
530 else
531 dtStyle |= DT_WORDBREAK;
532
533 switch (style & BS_CENTER)
534 {
535 case BS_LEFT: /* DT_LEFT is 0 */ break;
536 case BS_RIGHT: dtStyle |= DT_RIGHT; break;
537 case BS_CENTER: dtStyle |= DT_CENTER; break;
538 default:
539 /* Pushbutton's text is centered by default */
540 if (get_button_type(style) <= BS_DEFPUSHBUTTON) dtStyle |= DT_CENTER;
541 /* all other flavours have left aligned text */
542 }
543
544 /* DrawText ignores vertical alignment for multiline text,
545 * but we use these flags to align label manualy.
546 */
547 if (get_button_type(style) != BS_GROUPBOX)
548 {
549 switch (style & BS_VCENTER)
550 {
551 case BS_TOP: /* DT_TOP is 0 */ break;
552 case BS_BOTTOM: dtStyle |= DT_BOTTOM; break;
553 case BS_VCENTER: /* fall through */
554 default: dtStyle |= DT_VCENTER; break;
555 }
556 }
557 else
558 /* GroupBox's text is always single line and is top aligned. */
559 dtStyle |= DT_SINGLELINE;
560
561 return dtStyle;
562 }
563
564 /**********************************************************************
565 * BUTTON_CalcLabelRect
566 *
567 * Calculates label's rectangle depending on button style.
568 *
569 * Returns flags to be passed to DrawText.
570 * Calculated rectangle doesn't take into account button state
571 * (pushed, etc.). If there is nothing to draw (no text/image) output
572 * rectangle is empty, and return value is (UINT)-1.
573 */
574 static UINT BUTTON_CalcLabelRect(HWND hwnd, HDC hdc, RECT *rc)
575 {
576 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
577 WCHAR *text;
578 ICONINFO iconInfo;
579 BITMAP bm;
580 UINT dtStyle = BUTTON_BStoDT(style);
581 RECT r = *rc;
582 INT n;
583
584 /* Calculate label rectangle according to label type */
585 switch (style & (BS_ICON|BS_BITMAP))
586 {
587 case BS_TEXT:
588 if (!(text = get_button_text( hwnd ))) goto empty_rect;
589 if (!text[0])
590 {
591 HeapFree( GetProcessHeap(), 0, text );
592 goto empty_rect;
593 }
594 DrawTextW(hdc, text, -1, &r, dtStyle | DT_CALCRECT);
595 HeapFree( GetProcessHeap(), 0, text );
596 break;
597
598 case BS_ICON:
599 if (!GetIconInfo((HICON)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), &iconInfo))
600 goto empty_rect;
601
602 GetObjectW (iconInfo.hbmColor, sizeof(BITMAP), &bm);
603
604 r.right = r.left + bm.bmWidth;
605 r.bottom = r.top + bm.bmHeight;
606
607 DeleteObject(iconInfo.hbmColor);
608 DeleteObject(iconInfo.hbmMask);
609 break;
610
611 case BS_BITMAP:
612 if (!GetObjectW( (HANDLE)GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET ), sizeof(BITMAP), &bm))
613 goto empty_rect;
614
615 r.right = r.left + bm.bmWidth;
616 r.bottom = r.top + bm.bmHeight;
617 break;
618
619 default:
620 empty_rect:
621 rc->right = r.left;
622 rc->bottom = r.top;
623 return (UINT)(LONG)-1;
624 }
625
626 /* Position label inside bounding rectangle according to
627 * alignment flags. (calculated rect is always left-top aligned).
628 * If label is aligned to any side - shift label in opposite
629 * direction to leave extra space for focus rectangle.
630 */
631 switch (dtStyle & (DT_CENTER|DT_RIGHT))
632 {
633 case DT_LEFT: r.left++; r.right++; break;
634 case DT_CENTER: n = r.right - r.left;
635 r.left = rc->left + ((rc->right - rc->left) - n) / 2;
636 r.right = r.left + n; break;
637 case DT_RIGHT: n = r.right - r.left;
638 r.right = rc->right - 1;
639 r.left = r.right - n;
640 break;
641 }
642
643 switch (dtStyle & (DT_VCENTER|DT_BOTTOM))
644 {
645 case DT_TOP: r.top++; r.bottom++; break;
646 case DT_VCENTER: n = r.bottom - r.top;
647 r.top = rc->top + ((rc->bottom - rc->top) - n) / 2;
648 r.bottom = r.top + n; break;
649 case DT_BOTTOM: n = r.bottom - r.top;
650 r.bottom = rc->bottom - 1;
651 r.top = r.bottom - n;
652 break;
653 }
654
655 *rc = r;
656 return dtStyle;
657 }
658
659
660 /**********************************************************************
661 * BUTTON_DrawTextCallback
662 *
663 * Callback function used by DrawStateW function.
664 */
665 static BOOL CALLBACK BUTTON_DrawTextCallback(HDC hdc, LPARAM lp, WPARAM wp, int cx, int cy)
666 {
667 RECT rc;
668 rc.left = 0;
669 rc.top = 0;
670 rc.right = cx;
671 rc.bottom = cy;
672
673 DrawTextW(hdc, (LPCWSTR)lp, -1, &rc, (UINT)wp);
674 return TRUE;
675 }
676
677
678 /**********************************************************************
679 * BUTTON_DrawLabel
680 *
681 * Common function for drawing button label.
682 */
683 static void BUTTON_DrawLabel(HWND hwnd, HDC hdc, UINT dtFlags, RECT *rc)
684 {
685 DRAWSTATEPROC lpOutputProc = NULL;
686 LPARAM lp;
687 WPARAM wp = 0;
688 HBRUSH hbr = 0;
689 UINT flags = IsWindowEnabled(hwnd) ? DSS_NORMAL : DSS_DISABLED;
690 LONG state = get_button_state( hwnd );
691 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
692 WCHAR *text = NULL;
693
694 /* FIXME: To draw disabled label in Win31 look-and-feel, we probably
695 * must use DSS_MONO flag and COLOR_GRAYTEXT brush (or maybe DSS_UNION).
696 * I don't have Win31 on hand to verify that, so I leave it as is.
697 */
698
699 if ((style & BS_PUSHLIKE) && (state & BUTTON_3STATE))
700 {
701 hbr = GetSysColorBrush(COLOR_GRAYTEXT);
702 flags |= DSS_MONO;
703 }
704
705 switch (style & (BS_ICON|BS_BITMAP))
706 {
707 case BS_TEXT:
708 /* DST_COMPLEX -- is 0 */
709 lpOutputProc = BUTTON_DrawTextCallback;
710 if (!(text = get_button_text( hwnd ))) return;
711 lp = (LPARAM)text;
712 wp = (WPARAM)dtFlags;
713 break;
714
715 case BS_ICON:
716 flags |= DST_ICON;
717 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
718 break;
719
720 case BS_BITMAP:
721 flags |= DST_BITMAP;
722 lp = GetWindowLongPtrW( hwnd, HIMAGE_GWL_OFFSET );
723 break;
724
725 default:
726 return;
727 }
728
729 DrawStateW(hdc, hbr, lpOutputProc, lp, wp, rc->left, rc->top,
730 rc->right - rc->left, rc->bottom - rc->top, flags);
731 HeapFree( GetProcessHeap(), 0, text );
732 }
733
734 /**********************************************************************
735 * Push Button Functions
736 */
737 static void PB_Paint( HWND hwnd, HDC hDC, UINT action )
738 {
739 RECT rc, focus_rect, r;
740 UINT dtFlags, uState;
741 HPEN hOldPen;
742 HBRUSH hOldBrush;
743 INT oldBkMode;
744 COLORREF oldTxtColor;
745 HFONT hFont;
746 LONG state = get_button_state( hwnd );
747 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
748 BOOL pushedState = (state & BUTTON_HIGHLIGHTED);
749 HWND parent;
750
751 GetClientRect( hwnd, &rc );
752
753 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
754 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
755 parent = GetParent(hwnd);
756 if (!parent) parent = hwnd;
757 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
758 #ifdef __REACTOS__
759 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
760 #else
761 hOldPen = (HPEN)SelectObject(hDC, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
762 #endif
763 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
764 oldBkMode = SetBkMode(hDC, TRANSPARENT);
765
766 if (get_button_type(style) == BS_DEFPUSHBUTTON)
767 {
768 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
769 InflateRect( &rc, -1, -1 );
770 }
771
772 uState = DFCS_BUTTONPUSH | DFCS_ADJUSTRECT;
773
774 if (style & BS_FLAT)
775 uState |= DFCS_MONO;
776 else if (pushedState)
777 {
778 if (get_button_type(style) == BS_DEFPUSHBUTTON )
779 uState |= DFCS_FLAT;
780 else
781 uState |= DFCS_PUSHED;
782 }
783
784 if (state & (BUTTON_CHECKED | BUTTON_3STATE))
785 uState |= DFCS_CHECKED;
786
787 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
788
789 focus_rect = rc;
790
791 /* draw button label */
792 r = rc;
793 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &r);
794
795 if (dtFlags == (UINT)-1L)
796 goto cleanup;
797
798 if (pushedState)
799 OffsetRect(&r, 1, 1);
800
801 IntersectClipRect(hDC, rc.left, rc.top, rc.right, rc.bottom);
802
803 oldTxtColor = SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
804
805 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &r);
806
807 SetTextColor( hDC, oldTxtColor );
808
809 if (state & BUTTON_HASFOCUS)
810 {
811 InflateRect( &focus_rect, -1, -1 );
812 IntersectRect(&focus_rect, &focus_rect, &rc);
813 DrawFocusRect( hDC, &focus_rect );
814 }
815
816 cleanup:
817 SelectObject( hDC, hOldPen );
818 SelectObject( hDC, hOldBrush );
819 SetBkMode(hDC, oldBkMode);
820 }
821
822
823 /**********************************************************************
824 * Check Box & Radio Button Functions
825 */
826
827 static void CB_Paint( HWND hwnd, HDC hDC, UINT action )
828 {
829 RECT rbox, rtext, client;
830 HBRUSH hBrush;
831 int delta;
832 UINT dtFlags;
833 HFONT hFont;
834 LONG state = get_button_state( hwnd );
835 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
836 HWND parent;
837
838 if (style & BS_PUSHLIKE)
839 {
840 PB_Paint( hwnd, hDC, action );
841 return;
842 }
843
844 GetClientRect(hwnd, &client);
845 rbox = rtext = client;
846
847 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
848
849 parent = GetParent(hwnd);
850 if (!parent) parent = hwnd;
851 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC,
852 (WPARAM)hDC, (LPARAM)hwnd);
853 if (!hBrush) /* did the app forget to call defwindowproc ? */
854 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
855 (WPARAM)hDC, (LPARAM)hwnd );
856
857 if (style & BS_LEFTTEXT)
858 {
859 /* magic +4 is what CTL3D expects */
860
861 rtext.right -= checkBoxWidth + 4;
862 rbox.left = rbox.right - checkBoxWidth;
863 }
864 else
865 {
866 rtext.left += checkBoxWidth + 4;
867 rbox.right = checkBoxWidth;
868 }
869
870 /* Since WM_ERASEBKGND does nothing, first prepare background */
871 if (action == ODA_SELECT) FillRect( hDC, &rbox, hBrush );
872 if (action == ODA_DRAWENTIRE) FillRect( hDC, &client, hBrush );
873
874 /* Draw label */
875 client = rtext;
876 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rtext);
877
878 /* Only adjust rbox when rtext is valid */
879 if (dtFlags != (UINT)-1L)
880 {
881 rbox.top = rtext.top;
882 rbox.bottom = rtext.bottom;
883 }
884
885 /* Draw the check-box bitmap */
886 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
887 {
888 UINT flags;
889
890 if ((get_button_type(style) == BS_RADIOBUTTON) ||
891 (get_button_type(style) == BS_AUTORADIOBUTTON)) flags = DFCS_BUTTONRADIO;
892 else if (state & BUTTON_3STATE) flags = DFCS_BUTTON3STATE;
893 else flags = DFCS_BUTTONCHECK;
894
895 if (state & (BUTTON_CHECKED | BUTTON_3STATE)) flags |= DFCS_CHECKED;
896 if (state & BUTTON_HIGHLIGHTED) flags |= DFCS_PUSHED;
897
898 if (style & WS_DISABLED) flags |= DFCS_INACTIVE;
899
900 /* rbox must have the correct height */
901 delta = rbox.bottom - rbox.top - checkBoxHeight;
902
903 if (style & BS_TOP) {
904 if (delta > 0) {
905 rbox.bottom = rbox.top + checkBoxHeight;
906 } else {
907 rbox.top -= -delta/2 + 1;
908 rbox.bottom = rbox.top + checkBoxHeight;
909 }
910 } else if (style & BS_BOTTOM) {
911 if (delta > 0) {
912 rbox.top = rbox.bottom - checkBoxHeight;
913 } else {
914 rbox.bottom += -delta/2 + 1;
915 rbox.top = rbox.bottom - checkBoxHeight;
916 }
917 } else { /* Default */
918 if (delta > 0) {
919 int ofs = (delta / 2);
920 rbox.bottom -= ofs + 1;
921 rbox.top = rbox.bottom - checkBoxHeight;
922 } else if (delta < 0) {
923 int ofs = (-delta / 2);
924 rbox.top -= ofs + 1;
925 rbox.bottom = rbox.top + checkBoxHeight;
926 }
927 }
928
929 DrawFrameControl( hDC, &rbox, DFC_BUTTON, flags );
930 }
931
932 if (dtFlags == (UINT)-1L) /* Noting to draw */
933 return;
934
935 IntersectClipRect(hDC, client.left, client.top, client.right, client.bottom);
936
937 if (action == ODA_DRAWENTIRE)
938 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rtext);
939
940 /* ... and focus */
941 if ((action == ODA_FOCUS) ||
942 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
943 {
944 rtext.left--;
945 rtext.right++;
946 IntersectRect(&rtext, &rtext, &client);
947 DrawFocusRect( hDC, &rtext );
948 }
949 }
950
951
952 /**********************************************************************
953 * BUTTON_CheckAutoRadioButton
954 *
955 * hwnd is checked, uncheck every other auto radio button in group
956 */
957 static void BUTTON_CheckAutoRadioButton( HWND hwnd )
958 {
959 HWND parent, sibling, start;
960
961 parent = GetParent(hwnd);
962 /* make sure that starting control is not disabled or invisible */
963 start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
964 do
965 {
966 if (!sibling) break;
967 if ((hwnd != sibling) &&
968 ((GetWindowLongW( sibling, GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
969 SendMessageW( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
970 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
971 } while (sibling != start);
972 }
973
974
975 /**********************************************************************
976 * Group Box Functions
977 */
978
979 static void GB_Paint( HWND hwnd, HDC hDC, UINT action )
980 {
981 RECT rc, rcFrame;
982 HBRUSH hbr;
983 HFONT hFont;
984 UINT dtFlags;
985 TEXTMETRICW tm;
986 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
987 HWND parent;
988
989 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
990 /* GroupBox acts like static control, so it sends CTLCOLORSTATIC */
991 parent = GetParent(hwnd);
992 if (!parent) parent = hwnd;
993 hbr = (HBRUSH)SendMessageW(parent, WM_CTLCOLORSTATIC, (WPARAM)hDC, (LPARAM)hwnd);
994 if (!hbr) /* did the app forget to call defwindowproc ? */
995 hbr = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORSTATIC,
996 (WPARAM)hDC, (LPARAM)hwnd);
997
998 GetClientRect( hwnd, &rc);
999 rcFrame = rc;
1000
1001 GetTextMetricsW (hDC, &tm);
1002 rcFrame.top += (tm.tmHeight / 2) - 1;
1003 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT | ((style & BS_FLAT) ? BF_FLAT : 0));
1004
1005 InflateRect(&rc, -7, 1);
1006 dtFlags = BUTTON_CalcLabelRect(hwnd, hDC, &rc);
1007
1008 if (dtFlags == (UINT)-1L)
1009 return;
1010
1011 /* Because buttons have CS_PARENTDC class style, there is a chance
1012 * that label will be drawn out of client rect.
1013 * But Windows doesn't clip label's rect, so do I.
1014 */
1015
1016 /* There is 1-pixel marging at the left, right, and bottom */
1017 rc.left--; rc.right++; rc.bottom++;
1018 FillRect(hDC, &rc, hbr);
1019 rc.left++; rc.right--; rc.bottom--;
1020
1021 BUTTON_DrawLabel(hwnd, hDC, dtFlags, &rc);
1022 }
1023
1024
1025 /**********************************************************************
1026 * User Button Functions
1027 */
1028
1029 static void UB_Paint( HWND hwnd, HDC hDC, UINT action )
1030 {
1031 RECT rc;
1032 HBRUSH hBrush;
1033 HFONT hFont;
1034 LONG state = get_button_state( hwnd );
1035 HWND parent;
1036
1037 if (action == ODA_SELECT) return;
1038
1039 GetClientRect( hwnd, &rc);
1040
1041 if ((hFont = get_button_font( hwnd ))) SelectObject( hDC, hFont );
1042
1043 parent = GetParent(hwnd);
1044 if (!parent) parent = hwnd;
1045 hBrush = (HBRUSH)SendMessageW(parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd);
1046 if (!hBrush) /* did the app forget to call defwindowproc ? */
1047 hBrush = (HBRUSH)DefWindowProcW(parent, WM_CTLCOLORBTN,
1048 (WPARAM)hDC, (LPARAM)hwnd);
1049
1050 FillRect( hDC, &rc, hBrush );
1051 if ((action == ODA_FOCUS) ||
1052 ((action == ODA_DRAWENTIRE) && (state & BUTTON_HASFOCUS)))
1053 DrawFocusRect( hDC, &rc );
1054 }
1055
1056 /**********************************************************************
1057 * Ownerdrawn Button Functions
1058 */
1059
1060 static void OB_Paint( HWND hwnd, HDC hDC, UINT action )
1061 {
1062 LONG state = get_button_state( hwnd );
1063 DRAWITEMSTRUCT dis;
1064 HRGN clipRegion;
1065 RECT clipRect;
1066 LONG_PTR id = GetWindowLongPtrW( hwnd, GWLP_ID );
1067 HWND parent;
1068 HFONT hFont, hPrevFont = 0;
1069
1070 dis.CtlType = ODT_BUTTON;
1071 dis.CtlID = id;
1072 dis.itemID = 0;
1073 dis.itemAction = action;
1074 dis.itemState = ((state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1075 ((state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1076 (IsWindowEnabled(hwnd) ? 0: ODS_DISABLED);
1077 dis.hwndItem = hwnd;
1078 dis.hDC = hDC;
1079 dis.itemData = 0;
1080 GetClientRect( hwnd, &dis.rcItem );
1081
1082 clipRegion = CreateRectRgnIndirect(&dis.rcItem);
1083 if (GetClipRgn(hDC, clipRegion) != 1)
1084 {
1085 DeleteObject(clipRegion);
1086 clipRegion=NULL;
1087 }
1088 clipRect = dis.rcItem;
1089 DPtoLP(hDC, (LPPOINT) &clipRect, 2);
1090 IntersectClipRect(hDC, clipRect.left, clipRect.top, clipRect.right, clipRect.bottom);
1091
1092 if ((hFont = get_button_font( hwnd ))) hPrevFont = SelectObject( hDC, hFont );
1093 parent = GetParent(hwnd);
1094 if (!parent) parent = hwnd;
1095 SendMessageW( parent, WM_CTLCOLORBTN, (WPARAM)hDC, (LPARAM)hwnd );
1096 SendMessageW( GetParent(hwnd), WM_DRAWITEM, id, (LPARAM)&dis );
1097 if (hPrevFont) SelectObject(hDC, hPrevFont);
1098 SelectClipRgn(hDC, clipRegion);
1099 }