source: trunk/src/user32/button.cpp@ 1433

Last change on this file since 1433 was 1433, checked in by sandervl, 26 years ago

class changes

File size: 29.5 KB
Line 
1/* $Id: button.cpp,v 1.15 1999-10-24 22:56:00 sandervl Exp $ */
2/* File: button.cpp -- Button type widgets
3 *
4 * Copyright (C) 1993 Johannes Ruscheinski
5 * Copyright (C) 1993 David Metcalfe
6 * Copyright (C) 1994 Alexandre Julliard
7 * Copyright (c) 1999 Christoph Bratschi
8 *
9 * WINE version: 990923
10 */
11
12#include <string.h>
13#include <stdlib.h>
14
15#include <os2win.h>
16#include "controls.h"
17#include "button.h"
18#include <misc.h>
19#include "initterm.h"
20
21//Prototypes
22
23static void DrawDisabledText(HDC hdc,char* text,RECT* rtext,UINT format);
24
25static void PB_Paint(HWND hwnd,HDC hDC,WORD action);
26static void CB_Paint(HWND hwnd,HDC hDC,WORD action);
27static void GB_Paint(HWND hwnd,HDC hDC,WORD action);
28static void UB_Paint(HWND hwnd,HDC hDC,WORD action);
29static void OB_Paint(HWND hwnd,HDC hDC,WORD action);
30static void BUTTON_CheckAutoRadioButton(HWND hwnd);
31static void BUTTON_DrawPushButton(HWND hwnd,HDC hDC,WORD action,BOOL pushedState);
32static LRESULT BUTTON_LButtonDown(HWND hwnd,WPARAM wParam,LPARAM lParam);
33
34#define MAX_BTN_TYPE 12
35
36static const WORD maxCheckState[MAX_BTN_TYPE] =
37{
38 BUTTON_UNCHECKED, /* BS_PUSHBUTTON */
39 BUTTON_UNCHECKED, /* BS_DEFPUSHBUTTON */
40 BUTTON_CHECKED, /* BS_CHECKBOX */
41 BUTTON_CHECKED, /* BS_AUTOCHECKBOX */
42 BUTTON_CHECKED, /* BS_RADIOBUTTON */
43 BUTTON_3STATE, /* BS_3STATE */
44 BUTTON_3STATE, /* BS_AUTO3STATE */
45 BUTTON_UNCHECKED, /* BS_GROUPBOX */
46 BUTTON_UNCHECKED, /* BS_USERBUTTON */
47 BUTTON_CHECKED, /* BS_AUTORADIOBUTTON */
48 BUTTON_UNCHECKED, /* Not defined */
49 BUTTON_UNCHECKED /* BS_OWNERDRAW */
50};
51
52typedef void (*pfPaint)(HWND hwnd,HDC hdc,WORD action);
53
54static const pfPaint btnPaintFunc[MAX_BTN_TYPE] =
55{
56 PB_Paint, /* BS_PUSHBUTTON */
57 PB_Paint, /* BS_DEFPUSHBUTTON */
58 CB_Paint, /* BS_CHECKBOX */
59 CB_Paint, /* BS_AUTOCHECKBOX */
60 CB_Paint, /* BS_RADIOBUTTON */
61 CB_Paint, /* BS_3STATE */
62 CB_Paint, /* BS_AUTO3STATE */
63 GB_Paint, /* BS_GROUPBOX */
64 UB_Paint, /* BS_USERBUTTON */
65 CB_Paint, /* BS_AUTORADIOBUTTON */
66 NULL, /* Not defined */
67 OB_Paint /* BS_OWNERDRAW */
68};
69
70#define PAINT_BUTTON(hwnd,style,action) \
71 if (btnPaintFunc[style]) { \
72 HDC hdc = GetDC(hwnd); \
73 (btnPaintFunc[style])(hwnd,hdc,action); \
74 ReleaseDC(hwnd,hdc); }
75
76#define BUTTON_SEND_CTLCOLOR(hwnd,hdc) \
77 SendMessageA( GetParent(hwnd), WM_CTLCOLORBTN, \
78 (hdc),hwnd)
79
80static HBITMAP hbitmapCheckBoxes = 0;
81static WORD checkBoxWidth = 0, checkBoxHeight = 0;
82
83
84static LRESULT BUTTON_GetDlgCode(HWND hwnd,WPARAM wParam,LPARAM lParam)
85{
86 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
87
88 switch (dwStyle & 0x0f)
89 {
90 case BS_AUTOCHECKBOX:
91 case BS_CHECKBOX:
92 return DLGC_WANTCHARS | DLGC_BUTTON;
93
94 case BS_PUSHBUTTON:
95 return DLGC_UNDEFPUSHBUTTON;
96
97 case BS_DEFPUSHBUTTON:
98 return DLGC_DEFPUSHBUTTON;
99
100 case BS_AUTORADIOBUTTON:
101 case BS_RADIOBUTTON:
102 return DLGC_RADIOBUTTON;
103
104 case BS_GROUPBOX:;
105 return DLGC_STATIC;
106
107 default:
108 return DLGC_BUTTON;
109 }
110}
111
112static LRESULT BUTTON_Enable(HWND hwnd,WPARAM wParam,LPARAM lParam)
113{
114 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
115
116 //PAINT_BUTTON(hwnd,dwStyle & 0x0f,ODA_DRAWENTIRE);
117 //SvL: 09/10/99 Force it to redraw properly
118 InvalidateRect( hwnd, NULL, FALSE );
119
120 return 0;
121}
122
123static LRESULT BUTTON_Create(HWND hwnd,WPARAM wParam,LPARAM lParam)
124{
125 BUTTONINFO* infoPtr;
126 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
127
128 if (!hbitmapCheckBoxes)
129 {
130 BITMAP bmp;
131
132 hbitmapCheckBoxes = LoadBitmapA(hInstanceUser32, MAKEINTRESOURCEA(OBM_CHECKBOXES));
133 GetObjectA( hbitmapCheckBoxes, sizeof(bmp), &bmp );
134 if (GetObjectA(hbitmapCheckBoxes,sizeof(bmp),&bmp))
135 {
136 checkBoxWidth = bmp.bmWidth / 4;
137 checkBoxHeight = bmp.bmHeight / 3;
138 } else checkBoxWidth = checkBoxHeight = 0;
139 }
140 if (style < 0L || style >= MAX_BTN_TYPE) return -1; /* abort */
141
142 infoPtr = (BUTTONINFO*)malloc(sizeof(BUTTONINFO));
143 infoPtr->state = BUTTON_UNCHECKED;
144 infoPtr->hFont = 0;
145 infoPtr->hImage = NULL;
146 SetInfoPtr(hwnd,(DWORD)infoPtr);
147
148 return 0;
149}
150
151static LRESULT BUTTON_Destroy(HWND hwnd,WPARAM wParam,LPARAM lParam)
152{
153 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
154
155 free(infoPtr);
156
157 return 0;
158}
159
160static LRESULT BUTTON_EraseBkgnd(HWND hwnd,WPARAM wParam,LPARAM lParam)
161{
162 //SvL: Erase background for groupboxes as the paint function only draws
163 // a box
164 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
165// if(style == BS_GROUPBOX) {
166 return DefWindowProcA(hwnd, WM_ERASEBKGND, wParam, lParam);
167// }
168// return 1;
169}
170
171static LRESULT BUTTON_Paint(HWND hwnd,WPARAM wParam,LPARAM lParam)
172{
173 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
174
175 if (btnPaintFunc[style])
176 {
177 PAINTSTRUCT ps;
178
179 HDC hdc = wParam ? (HDC)wParam : BeginPaint(hwnd,&ps);
180 SetBkMode(hdc,OPAQUE);
181 (btnPaintFunc[style])(hwnd,hdc,ODA_DRAWENTIRE);
182 if(!wParam) EndPaint(hwnd,&ps);
183 }
184
185 return 0;
186}
187
188static LRESULT BUTTON_LButtonDblClk(HWND hwnd,WPARAM wParam,LPARAM lParam)
189{
190 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
191 DWORD style = dwStyle & 0x0f;
192
193 if(dwStyle & BS_NOTIFY || style == BS_RADIOBUTTON ||
194 style == BS_USERBUTTON || style == BS_OWNERDRAW)
195 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),BN_DOUBLECLICKED),hwnd);
196 else BUTTON_LButtonDown(hwnd,wParam,lParam);
197
198 return 0;
199}
200
201static LRESULT BUTTON_LButtonDown(HWND hwnd,WPARAM wParam,LPARAM lParam)
202{
203 SetCapture(hwnd);
204 SetFocus(hwnd);
205 SendMessageA(hwnd,BM_SETSTATE,TRUE,0);
206
207 return 0;
208}
209
210static LRESULT BUTTON_LButtonUp(HWND hwnd,WPARAM wParam,LPARAM lParam)
211{
212 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
213 RECT rect;
214 POINT pt;
215
216 pt.x = LOWORD(lParam);
217 pt.y = HIWORD(lParam);
218
219 ReleaseCapture();
220 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) return 0;
221 SendMessageA(hwnd,BM_SETSTATE,FALSE,0);
222 GetClientRect(hwnd,&rect);
223 if (PtInRect(&rect,pt))
224 {
225 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
226 DWORD id = GetWindowLongA(hwnd,GWL_ID);
227
228 switch(dwStyle & 0x0f)
229 {
230 case BS_AUTOCHECKBOX:
231 SendMessageA(hwnd,BM_SETCHECK,!(infoPtr->state & BUTTON_CHECKED),0);
232 break;
233 case BS_AUTORADIOBUTTON:
234 SendMessageA(hwnd,BM_SETCHECK,TRUE,0);
235 break;
236 case BS_AUTO3STATE:
237 SendMessageA(hwnd,BM_SETCHECK,
238 (infoPtr->state & BUTTON_3STATE) ? 0 :
239 ((infoPtr->state & 3)+1),0);
240 break;
241 }
242 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(id,BN_CLICKED),hwnd);
243 }
244
245 return 0;
246}
247
248static LRESULT BUTTON_MouseMove(HWND hwnd,WPARAM wParam,LPARAM lParam)
249{
250 if (GetCapture() == hwnd)
251 {
252 RECT rect;
253 POINT pt;
254
255 pt.x = LOWORD(lParam);
256 pt.y = HIWORD(lParam);
257
258 GetClientRect(hwnd,&rect);
259 SendMessageA(hwnd,BM_SETSTATE,PtInRect(&rect,pt),0);
260 }
261
262 return 1; //SvL: Bugfix -> PMWINDOW expects non-zero return value if
263 // we want to restore the default mouse cursor
264}
265
266static LRESULT BUTTON_NCHitTest(HWND hwnd,WPARAM wParam,LPARAM lParam)
267{
268 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
269
270 if (style == BS_GROUPBOX) return HTTRANSPARENT;
271
272 return DefWindowProcA(hwnd,WM_NCHITTEST,wParam,lParam);
273}
274
275static LRESULT BUTTON_SetText(HWND hwnd,WPARAM wParam,LPARAM lParam)
276{
277 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
278
279 DefWindowProcA(hwnd,WM_SETTEXT,wParam,lParam);
280 if (dwStyle & WS_VISIBLE) PAINT_BUTTON(hwnd,dwStyle & 0x0f,ODA_DRAWENTIRE);
281
282 return 0;
283}
284
285static LRESULT BUTTON_SetFont(HWND hwnd,WPARAM wParam,LPARAM lParam)
286{
287 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
288 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
289
290 infoPtr->hFont = (HFONT)wParam;
291 if (lParam && (dwStyle & WS_VISIBLE)) PAINT_BUTTON(hwnd,dwStyle & 0x0f,ODA_DRAWENTIRE);
292
293 return 0;
294}
295
296static LRESULT BUTTON_GetFont(HWND hwnd,WPARAM wParam,LPARAM lParam)
297{
298 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
299
300 return infoPtr->hFont;
301}
302
303static LRESULT BUTTON_KeyDown(HWND hwnd,WPARAM wParam,LPARAM lParam)
304{
305 if (wParam == VK_SPACE)
306 {
307 SendMessageA(hwnd,BM_SETSTATE,TRUE,0);
308 SetFocus(hwnd);
309 SetCapture(hwnd);
310 }
311
312 return 0;
313}
314
315static LRESULT BUTTON_KeyUp(HWND hwnd,WPARAM wParam,LPARAM lParam)
316{
317 if (wParam == VK_SPACE)
318 {
319 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
320 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
321 DWORD id = GetWindowLongA(hwnd,GWL_ID);
322
323 ReleaseCapture();
324 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) return 0;
325 SendMessageA(hwnd,BM_SETSTATE,FALSE,0);
326
327 switch(dwStyle & 0x0f)
328 {
329 case BS_AUTOCHECKBOX:
330 SendMessageA(hwnd,BM_SETCHECK,!(infoPtr->state & BUTTON_CHECKED),0);
331 break;
332 case BS_AUTORADIOBUTTON:
333 SendMessageA(hwnd,BM_SETCHECK,TRUE,0);
334 break;
335 case BS_AUTO3STATE:
336 SendMessageA(hwnd,BM_SETCHECK,
337 (infoPtr->state & BUTTON_3STATE) ? 0 :
338 ((infoPtr->state & 3)+1),0);
339 break;
340 }
341 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(id,BN_CLICKED),hwnd);
342 } else if (wParam != VK_TAB) ReleaseCapture();
343
344 return 0;
345}
346
347static LRESULT BUTTON_SysKeyUp(HWND hwnd,WPARAM wParam,LPARAM lParam)
348{
349 if (wParam != VK_TAB) ReleaseCapture();
350
351 return 0;
352}
353
354static LRESULT BUTTON_SetFocus(HWND hwnd,WPARAM wParam,LPARAM lParam)
355{
356 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
357 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
358
359 if ((style == BS_AUTORADIOBUTTON || style == BS_RADIOBUTTON) &&
360 (GetCapture() != hwnd) && !(SendMessageA(hwnd,BM_GETCHECK,0,0) & BST_CHECKED))
361 {
362 /* The notification is sent when the button (BS_AUTORADIOBUTTON)
363 is unckecked and the focus was not given by a mouse click. */
364 if (style == BS_AUTORADIOBUTTON) SendMessageA(hwnd,BM_SETCHECK,TRUE,0);
365 SendMessageA(GetParent(hwnd),WM_COMMAND,MAKEWPARAM(GetWindowLongA(hwnd,GWL_ID),BN_CLICKED),hwnd);
366 }
367
368 infoPtr->state |= BUTTON_HASFOCUS;
369 PAINT_BUTTON(hwnd,style,ODA_FOCUS);
370
371 return 0;
372}
373
374static LRESULT BUTTON_KillFocus(HWND hwnd,WPARAM wParam,LPARAM lParam)
375{
376 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
377 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
378
379 if (infoPtr->state & BUTTON_HASFOCUS)
380 {
381 infoPtr->state &= ~BUTTON_HASFOCUS;
382 PAINT_BUTTON(hwnd,style,ODA_FOCUS);
383 InvalidateRect(hwnd,NULL,TRUE);
384 }
385
386 return 0;
387}
388
389static LRESULT BUTTON_SysColorChange(HWND hwnd,WPARAM wParam,LPARAM lParam)
390{
391 InvalidateRect(hwnd,NULL,FALSE);
392
393 return 0;
394}
395
396static LRESULT BUTTON_Click(HWND hwnd,WPARAM wParam,LPARAM lParam)
397{
398 RECT rect;
399 LPARAM point;
400
401 GetClientRect(hwnd,&rect);
402 point = MAKELPARAM(rect.right/2,rect.bottom/2);
403 SendMessageA(hwnd,WM_LBUTTONDOWN,MK_LBUTTON,point);
404 Sleep(100);
405 SendMessageA(hwnd,WM_LBUTTONUP,0,point);
406
407 return 0;
408}
409
410static LRESULT BUTTON_SetStyle(HWND hwnd,WPARAM wParam,LPARAM lParam)
411{
412 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
413
414 if ((wParam & 0x0f) >= MAX_BTN_TYPE) return 0;
415 dwStyle = (dwStyle & 0xfffffff0) | (wParam & 0x0000000f);
416 SetWindowLongA(hwnd,GWL_STYLE,dwStyle);
417 PAINT_BUTTON(hwnd,dwStyle & 0x0f,ODA_DRAWENTIRE);
418
419 return 0;
420}
421
422static LRESULT BUTTON_SetImage(HWND hwnd,WPARAM wParam,LPARAM lParam)
423{
424 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
425 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
426 HANDLE oldHbitmap = infoPtr->hImage;
427
428 if (dwStyle & BS_BITMAP || dwStyle & BS_ICON) infoPtr->hImage = (HANDLE)lParam;
429
430 return oldHbitmap;
431}
432
433static LRESULT BUTTON_GetImage(HWND hwnd,WPARAM wParam,LPARAM lParam)
434{
435 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
436
437 switch(wParam)
438 {
439 case IMAGE_BITMAP:
440 return (HBITMAP)infoPtr->hImage;
441 case IMAGE_ICON:
442 return (HICON)infoPtr->hImage;
443 default:
444 return NULL;
445 }
446}
447
448static LRESULT BUTTON_GetCheck(HWND hwnd,WPARAM wParam,LPARAM lParam)
449{
450 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
451
452 return infoPtr->state & 3;
453}
454
455static LRESULT BUTTON_SetCheck(HWND hwnd,WPARAM wParam,LPARAM lParam)
456{
457 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
458 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
459 DWORD style = dwStyle & 0x0f;
460
461 if (wParam > maxCheckState[style]) wParam = maxCheckState[style];
462 if ((infoPtr->state & 3) != wParam)
463 {
464 if ((style == BS_RADIOBUTTON) || (style == BS_AUTORADIOBUTTON))
465 {
466 DWORD oldStyle = dwStyle;
467
468 if (wParam)
469 dwStyle |= WS_TABSTOP;
470 else
471 dwStyle &= ~WS_TABSTOP;
472
473 //if (oldStyle != dwStyle) SetWindowLongA(hwnd,GWL_STYLE,dwStyle);
474 }
475 infoPtr->state = (infoPtr->state & ~3) | wParam;
476 PAINT_BUTTON(hwnd,style,ODA_SELECT);
477 }
478 if ((style == BS_AUTORADIOBUTTON) && (wParam == BUTTON_CHECKED))
479 BUTTON_CheckAutoRadioButton(hwnd);
480
481 return 0;
482}
483
484static LRESULT BUTTON_GetState(HWND hwnd,WPARAM wParam,LPARAM lParam)
485{
486 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
487
488 return infoPtr->state;
489}
490
491static LRESULT BUTTON_SetState(HWND hwnd,WPARAM wParam,LPARAM lParam)
492{
493 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
494 DWORD style = GetWindowLongA(hwnd,GWL_STYLE) & 0x0f;
495
496 if (wParam)
497 {
498 if (infoPtr->state & BUTTON_HIGHLIGHTED) return 0;
499 infoPtr->state |= BUTTON_HIGHLIGHTED;
500 } else
501 {
502 if (!(infoPtr->state & BUTTON_HIGHLIGHTED)) return 0;
503 infoPtr->state &= ~BUTTON_HIGHLIGHTED;
504 }
505 PAINT_BUTTON(hwnd,style,ODA_SELECT);
506
507 return 0;
508}
509
510/***********************************************************************
511 * ButtonWndProc
512 */
513static
514LRESULT WINAPI ButtonWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
515{
516 switch (uMsg)
517 {
518 case WM_GETDLGCODE:
519 return BUTTON_GetDlgCode(hwnd,wParam,lParam);
520
521 case WM_ENABLE:
522 return BUTTON_Enable(hwnd,wParam,lParam);
523
524 case WM_CREATE:
525 return BUTTON_Create(hwnd,wParam,lParam);
526
527 case WM_DESTROY:
528 return BUTTON_Destroy(hwnd,wParam,lParam);
529
530 case WM_ERASEBKGND:
531 return BUTTON_EraseBkgnd(hwnd,wParam,lParam);
532
533 case WM_PAINT:
534 return BUTTON_Paint(hwnd,wParam,lParam);
535
536 case WM_LBUTTONDBLCLK:
537 return BUTTON_LButtonDblClk(hwnd,wParam,lParam);
538
539 case WM_LBUTTONDOWN:
540 return BUTTON_LButtonDown(hwnd,wParam,lParam);
541
542 case WM_LBUTTONUP:
543 return BUTTON_LButtonUp(hwnd,wParam,lParam);
544
545 case WM_MOUSEMOVE:
546 return BUTTON_MouseMove(hwnd,wParam,lParam);
547
548 case WM_NCHITTEST:
549 return BUTTON_NCHitTest(hwnd,wParam,lParam);
550
551 case WM_SETTEXT:
552 return BUTTON_SetText(hwnd,wParam,lParam);
553
554 case WM_SETFONT:
555 return BUTTON_SetFont(hwnd,wParam,lParam);
556
557 case WM_GETFONT:
558 return BUTTON_GetFont(hwnd,wParam,lParam);
559
560 case WM_KEYDOWN:
561 return BUTTON_KeyDown(hwnd,wParam,lParam);
562
563 case WM_KEYUP:
564 return BUTTON_KeyUp(hwnd,wParam,lParam);
565
566 case WM_SYSKEYUP:
567 return BUTTON_SysKeyUp(hwnd,wParam,lParam);
568
569 case WM_SETFOCUS:
570 return BUTTON_SetFocus(hwnd,wParam,lParam);
571
572 case WM_KILLFOCUS:
573 return BUTTON_KillFocus(hwnd,wParam,lParam);
574
575 case WM_SYSCOLORCHANGE:
576 return BUTTON_SysColorChange(hwnd,wParam,lParam);
577
578 case BM_CLICK:
579 return BUTTON_Click(hwnd,wParam,lParam);
580
581 case BM_SETSTYLE:
582 return BUTTON_SetStyle(hwnd,wParam,lParam);
583
584 case BM_SETIMAGE:
585 return BUTTON_SetImage(hwnd,wParam,lParam);
586
587 case BM_GETIMAGE:
588 return BUTTON_GetImage(hwnd,wParam,lParam);
589
590 case BM_GETCHECK:
591 return BUTTON_GetCheck(hwnd,wParam,lParam);
592
593 case BM_SETCHECK:
594 return BUTTON_SetCheck(hwnd,wParam,lParam);
595
596 case BM_GETSTATE:
597 return BUTTON_GetState(hwnd,wParam,lParam);
598
599 case BM_SETSTATE:
600 return BUTTON_SetState(hwnd,wParam,lParam);
601
602 default:
603 return DefWindowProcA(hwnd,uMsg,wParam,lParam);
604 }
605
606 return 0;
607}
608
609
610/**********************************************************************
611 * Push Button Functions
612 */
613static void PB_Paint( HWND hwnd, HDC hDC, WORD action )
614{
615 BUTTONINFO *infoPtr = (BUTTONINFO *)GetInfoPtr(hwnd);
616 BOOL bHighLighted = (infoPtr->state & BUTTON_HIGHLIGHTED);
617
618 /*
619 * Delegate this to the more generic pushbutton painting
620 * method.
621 */
622 BUTTON_DrawPushButton(hwnd,
623 hDC,
624 action,
625 bHighLighted);
626}
627
628/**********************************************************************
629 * This method will actually do the drawing of the pushbutton
630 * depending on it's state and the pushedState parameter.
631 */
632static void BUTTON_DrawPushButton(
633 HWND hwnd,
634 HDC hDC,
635 WORD action,
636 BOOL pushedState )
637{
638 RECT rc, focus_rect;
639 HPEN hOldPen;
640 HBRUSH hOldBrush;
641 BUTTONINFO *infoPtr = (BUTTONINFO *)GetInfoPtr(hwnd);
642 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
643 int xBorderOffset, yBorderOffset;
644 xBorderOffset = yBorderOffset = 0;
645 INT textLen;
646 char* text;
647
648 GetClientRect( hwnd, &rc );
649
650 /* Send WM_CTLCOLOR to allow changing the font (the colors are fixed) */
651 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
652 BUTTON_SEND_CTLCOLOR( hwnd, hDC );
653 hOldPen = (HPEN)SelectObject(hDC, GetSysColorPen(COLOR_WINDOWFRAME));
654 hOldBrush =(HBRUSH)SelectObject(hDC,GetSysColorBrush(COLOR_BTNFACE));
655 SetBkMode(hDC, TRANSPARENT);
656
657 if ((dwStyle & 0x000f) == BS_DEFPUSHBUTTON)
658 {
659 Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
660 InflateRect( &rc, -1, -1 );
661 }
662
663 UINT uState = DFCS_BUTTONPUSH;
664
665 if (pushedState)
666 {
667 if ( (dwStyle & 0x000f) == BS_DEFPUSHBUTTON )
668 uState |= DFCS_FLAT;
669 else
670 uState |= DFCS_PUSHED;
671 }
672
673 DrawFrameControl( hDC, &rc, DFC_BUTTON, uState );
674 InflateRect( &rc, -2, -2 );
675
676 focus_rect = rc;
677
678 if (pushedState)
679 {
680 rc.left += 2; /* To position the text down and right */
681 rc.top += 2;
682 }
683
684
685 /* draw button label, if any:
686 *
687 * In win9x we don't show text if there is a bitmap or icon.
688 * I don't know about win31 so I leave it as it was for win31.
689 * Dennis Björklund 12 Jul, 99
690 */
691 textLen = GetWindowTextLengthA(hwnd);
692 if (textLen > 0 && (!(dwStyle & (BS_ICON|BS_BITMAP))))
693 {
694 textLen++;
695 text = (char*)malloc(textLen);
696 GetWindowTextA(hwnd,text,textLen);
697
698 if (dwStyle & WS_DISABLED) DrawDisabledText(hDC,text,&rc,DT_SINGLELINE | DT_CENTER | DT_VCENTER); else
699 {
700 SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
701 DrawTextA( hDC, text, -1, &rc,
702 DT_SINGLELINE | DT_CENTER | DT_VCENTER );
703 /* do we have the focus?
704 * Win9x draws focus last with a size prop. to the button
705 */
706 }
707 free(text);
708 }
709 if ( ((dwStyle & BS_ICON) || (dwStyle & BS_BITMAP) ) &&
710 (infoPtr->hImage != NULL) )
711 {
712 int yOffset, xOffset;
713 int imageWidth, imageHeight;
714
715 /*
716 * We extract the size of the image from the handle.
717 */
718 if (dwStyle & BS_ICON)
719 {
720 ICONINFO iconInfo;
721 BITMAP bm;
722
723 GetIconInfo((HICON)infoPtr->hImage, &iconInfo);
724 GetObjectA (iconInfo.hbmColor, sizeof(BITMAP), &bm);
725
726 imageWidth = bm.bmWidth;
727 imageHeight = bm.bmHeight;
728
729 DeleteObject(iconInfo.hbmColor);
730 DeleteObject(iconInfo.hbmMask);
731
732 }
733 else
734 {
735 BITMAP bm;
736
737 GetObjectA (infoPtr->hImage, sizeof(BITMAP), &bm);
738
739 imageWidth = bm.bmWidth;
740 imageHeight = bm.bmHeight;
741 }
742
743 /* Center the bitmap */
744 xOffset = (((rc.right - rc.left) - 2*xBorderOffset) - imageWidth ) / 2;
745 yOffset = (((rc.bottom - rc.top) - 2*yBorderOffset) - imageHeight) / 2;
746
747 /* If the image is too big for the button then create a region*/
748 if(xOffset < 0 || yOffset < 0)
749 {
750 HRGN hBitmapRgn = NULL;
751 hBitmapRgn = CreateRectRgn(
752 rc.left + xBorderOffset, rc.top +yBorderOffset,
753 rc.right - xBorderOffset, rc.bottom - yBorderOffset);
754 SelectClipRgn(hDC, hBitmapRgn);
755 DeleteObject(hBitmapRgn);
756 }
757
758 /* Let minimum 1 space from border */
759 xOffset++, yOffset++;
760
761 /*
762 * Draw the image now.
763 */
764 if (dwStyle & BS_ICON)
765 {
766 DrawIcon(hDC,
767 rc.left + xOffset, rc.top + yOffset,
768 (HICON)infoPtr->hImage);
769 }
770 else
771 {
772 HDC hdcMem;
773
774 hdcMem = CreateCompatibleDC (hDC);
775 SelectObject (hdcMem, (HBITMAP)infoPtr->hImage);
776 BitBlt(hDC,
777 rc.left + xOffset,
778 rc.top + yOffset,
779 imageWidth, imageHeight,
780 hdcMem, 0, 0, SRCCOPY);
781
782 DeleteDC (hdcMem);
783 }
784
785 if(xOffset < 0 || yOffset < 0)
786 {
787 SelectClipRgn(hDC, NULL);
788 }
789 }
790
791 SelectObject( hDC, hOldPen );
792 SelectObject( hDC, hOldBrush );
793
794 if (infoPtr->state & BUTTON_HASFOCUS)
795 {
796 InflateRect( &focus_rect, -1, -1 );
797 DrawFocusRect( hDC, &focus_rect );
798 }
799}
800
801
802static void DrawDisabledText(HDC hdc,char* text,RECT* rtext,UINT format)
803{
804 COLORREF textColor = (GetSysColor(COLOR_GRAYTEXT) == GetBkColor(hdc)) ? COLOR_BTNTEXT:COLOR_GRAYTEXT;
805 RECT rect = *rtext;
806 COLORREF oldMode;
807
808 //CB: bug in Open32 DrawText: underscore is always black! -> two black lines!
809 SetTextColor(hdc,GetSysColor(COLOR_3DHILIGHT));
810 DrawTextA(hdc,text,-1,&rect,format);
811 SetTextColor(hdc,GetSysColor(COLOR_GRAYTEXT));
812 oldMode = SetBkMode(hdc,TRANSPARENT);
813 OffsetRect(&rect,-1,-1);
814 DrawTextA(hdc,text,-1,&rect,format);
815 SetBkMode(hdc,oldMode);
816}
817
818/**********************************************************************
819 * Check Box & Radio Button Functions
820 */
821
822static void CB_Paint(HWND hwnd,HDC hDC,WORD action)
823{
824 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
825 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
826 RECT rbox, rtext, client;
827 HBRUSH hBrush;
828 int textLen, delta;
829 char* text = NULL;
830
831 /*
832 * if the button has a bitmap/icon, draw a normal pushbutton
833 * instead of a radion button.
834 */
835 if (infoPtr->hImage!=NULL)
836 {
837 BOOL bHighLighted = ((infoPtr->state & BUTTON_HIGHLIGHTED) ||
838 (infoPtr->state & BUTTON_CHECKED));
839
840 BUTTON_DrawPushButton(hwnd,
841 hDC,
842 action,
843 bHighLighted);
844 return;
845 }
846
847 textLen = 0;
848 GetClientRect(hwnd, &client);
849 rbox = rtext = client;
850
851 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
852
853 /* Something is still not right, checkboxes (and edit controls)
854 * in wsping32 have white backgrounds instead of dark grey.
855 * BUTTON_SEND_CTLCOLOR() is even worse since it returns 0 in this
856 * particular case and the background is not painted at all.
857 */
858 //SvL: 20/09/99: This works well for us. Now the background of
859 // the dialog button strings in Solitaire is gray
860 SendMessageA(GetParent(hwnd),WM_CTLCOLORBTN,hDC,hwnd);
861
862 hBrush = GetSysColorBrush(COLOR_BTNFACE);
863
864 if (dwStyle & BS_LEFTTEXT)
865 {
866 /* magic +4 is what CTL3D expects */
867
868 rtext.right -= checkBoxWidth + 4;
869 rbox.left = rbox.right - checkBoxWidth;
870 }
871 else
872 {
873 rtext.left += checkBoxWidth + 4;
874 rbox.right = checkBoxWidth;
875 }
876
877 /* Draw the check-box bitmap */
878
879 textLen = GetWindowTextLengthA(hwnd);
880 if (textLen > 0)
881 {
882 textLen++;
883 text = (char*)malloc(textLen);
884 GetWindowTextA(hwnd,text,textLen);
885 }
886 if (action == ODA_DRAWENTIRE || action == ODA_SELECT)
887 {
888 UINT state;
889
890 if (((dwStyle & 0x0f) == BS_RADIOBUTTON) ||
891 ((dwStyle & 0x0f) == BS_AUTORADIOBUTTON)) state = DFCS_BUTTONRADIO;
892 else if (infoPtr->state & BUTTON_3STATE) state = DFCS_BUTTON3STATE;
893 else state = DFCS_BUTTONCHECK;
894
895 if (infoPtr->state & (BUTTON_CHECKED | BUTTON_3STATE)) state |= DFCS_CHECKED;
896
897 if (infoPtr->state & BUTTON_HIGHLIGHTED) state |= DFCS_PUSHED;
898
899 if (dwStyle & WS_DISABLED) state |= DFCS_INACTIVE;
900
901 DrawFrameControl( hDC, &rbox, DFC_BUTTON, state );
902
903 if( text && action != ODA_SELECT )
904 {
905
906 if (dwStyle & WS_DISABLED) DrawDisabledText(hDC,text,&rtext,DT_SINGLELINE | DT_VCENTER);
907 else DrawTextA(hDC,text,-1,&rtext,DT_SINGLELINE | DT_VCENTER);
908 }
909 }
910
911 if ((action == ODA_FOCUS) ||
912 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
913 {
914 /* again, this is what CTL3D expects */
915
916 SetRectEmpty(&rbox);
917 if(textLen > 0)
918 DrawTextA( hDC, text, -1, &rbox,
919 DT_SINGLELINE | DT_CALCRECT );
920 textLen = rbox.bottom - rbox.top;
921 delta = ((rtext.bottom - rtext.top) - textLen)/2;
922 rbox.bottom = (rbox.top = rtext.top + delta - 1) + textLen + 2;
923 textLen = rbox.right - rbox.left;
924 rbox.right = (rbox.left += --rtext.left) + textLen + 2;
925 IntersectRect(&rbox, &rbox, &rtext);
926 DrawFocusRect( hDC, &rbox );
927 }
928 if (text) free(text);
929}
930
931
932/**********************************************************************
933 * BUTTON_CheckAutoRadioButton
934 *
935 * wndPtr is checked, uncheck every other auto radio button in group
936 */
937static void BUTTON_CheckAutoRadioButton(HWND hwnd)
938{
939 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
940 HWND parent, sibling, start;
941
942 if (!(dwStyle & WS_CHILD)) return;
943 parent = GetParent(hwnd);
944 /* assure that starting control is not disabled or invisible */
945// start = sibling = GetNextDlgGroupItem( parent, hwnd, TRUE );
946 //@YD: bugfix
947 start = sibling = GetNextDlgGroupItem( parent, hwnd, FALSE );
948 do
949 {
950 if (!sibling) break;
951 if ((hwnd != sibling) &&
952 ((GetWindowLongA(sibling,GWL_STYLE) & 0x0f) == BS_AUTORADIOBUTTON))
953 SendMessageA( sibling, BM_SETCHECK, BUTTON_UNCHECKED, 0 );
954 sibling = GetNextDlgGroupItem( parent, sibling, FALSE );
955 } while (sibling != start);
956}
957
958
959/**********************************************************************
960 * Group Box Functions
961 */
962
963static void GB_Paint(HWND hwnd,HDC hDC,WORD action)
964{
965 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
966 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
967 RECT rc, rcFrame;
968 TEXTMETRICA tm;
969 INT textLen;
970 char* text;
971
972 if (action != ODA_DRAWENTIRE) return;
973
974 SendMessageA(GetParent(hwnd),WM_CTLCOLORBTN,hDC,hwnd);
975
976 GetClientRect(hwnd,&rc);
977
978 rcFrame = rc;
979
980 if (infoPtr->hFont)
981 SelectObject (hDC, infoPtr->hFont);
982 GetTextMetricsA (hDC, &tm);
983 rcFrame.top += (tm.tmHeight / 2) - 1;
984 DrawEdge (hDC, &rcFrame, EDGE_ETCHED, BF_RECT);
985
986 textLen = GetWindowTextLengthA(hwnd);
987 if (textLen > 0)
988 {
989 textLen++;
990 text = (char*)malloc(textLen);
991 GetWindowTextA(hwnd,text,textLen);
992 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
993 rc.left += 10;
994
995 if (dwStyle & WS_DISABLED) DrawDisabledText(hDC,text,&rc,DT_SINGLELINE | DT_NOCLIP); else
996 {
997 SetTextColor( hDC, GetSysColor(COLOR_BTNTEXT) );
998 DrawTextA( hDC, text, -1, &rc,
999 DT_SINGLELINE | DT_NOCLIP );
1000 }
1001
1002 free(text);
1003 }
1004}
1005
1006
1007/**********************************************************************
1008 * User Button Functions
1009 */
1010
1011static void UB_Paint(HWND hwnd,HDC hDC,WORD action)
1012{
1013 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
1014 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
1015 RECT rc;
1016 HBRUSH hBrush;
1017 if (action == ODA_SELECT) return;
1018
1019 GetClientRect(hwnd,&rc);
1020
1021 if (infoPtr->hFont) SelectObject( hDC, infoPtr->hFont );
1022 hBrush = GetSysColorBrush(COLOR_BTNFACE);
1023
1024 FillRect( hDC, &rc, hBrush );
1025 if ((action == ODA_FOCUS) ||
1026 ((action == ODA_DRAWENTIRE) && (infoPtr->state & BUTTON_HASFOCUS)))
1027 DrawFocusRect( hDC, &rc );
1028}
1029
1030
1031/**********************************************************************
1032 * Ownerdrawn Button Functions
1033 */
1034
1035static void OB_Paint(HWND hwnd,HDC hDC,WORD action)
1036{
1037 BUTTONINFO* infoPtr = (BUTTONINFO*)GetInfoPtr(hwnd);
1038 DWORD dwStyle = GetWindowLongA(hwnd,GWL_STYLE);
1039 DRAWITEMSTRUCT dis;
1040
1041 dis.CtlType = ODT_BUTTON;
1042 dis.CtlID = GetWindowLongA(hwnd,GWL_ID);
1043 dis.itemID = 0;
1044 dis.itemAction = action;
1045 dis.itemState = ((infoPtr->state & BUTTON_HASFOCUS) ? ODS_FOCUS : 0) |
1046 ((infoPtr->state & BUTTON_HIGHLIGHTED) ? ODS_SELECTED : 0) |
1047 ((dwStyle & WS_DISABLED) ? ODS_DISABLED : 0);
1048 dis.hwndItem = hwnd;
1049 dis.hDC = hDC;
1050 dis.itemData = 0;
1051 GetClientRect( hwnd, &dis.rcItem );
1052
1053 SetBkColor( hDC, GetSysColor( COLOR_BTNFACE ) );
1054 FillRect( hDC, &dis.rcItem, GetSysColorBrush( COLOR_BTNFACE ) );
1055
1056 dprintf(("OWNERDRAW button %x, enabled %d", hwnd, !(dwStyle & WS_DISABLED)));
1057 SendMessageA( GetParent(hwnd), WM_DRAWITEM,
1058 GetWindowLongA(hwnd,GWL_ID), (LPARAM)&dis );
1059}
1060
1061BOOL BUTTON_Register()
1062{
1063 WNDCLASSA wndClass;
1064
1065//SvL: Don't check this now
1066// if (GlobalFindAtomA(BUTTONCLASSNAME)) return FALSE;
1067
1068 ZeroMemory(&wndClass,sizeof(WNDCLASSA));
1069 wndClass.style = CS_GLOBALCLASS | CS_HREDRAW | CS_VREDRAW | CS_PARENTDC | CS_DBLCLKS;
1070 wndClass.lpfnWndProc = (WNDPROC)ButtonWndProc;
1071 wndClass.cbClsExtra = 0;
1072 wndClass.cbWndExtra = sizeof(BUTTONINFO);
1073 wndClass.hCursor = LoadCursorA(0,IDC_ARROWA);
1074// wndClass.hbrBackground = (HBRUSH)0;
1075 wndClass.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
1076 wndClass.lpszClassName = BUTTONCLASSNAME;
1077
1078 return RegisterClassA(&wndClass);
1079}
1080
1081BOOL BUTTON_Unregister()
1082{
1083 if (GlobalFindAtomA(BUTTONCLASSNAME))
1084 return UnregisterClassA(BUTTONCLASSNAME,(HINSTANCE)NULL);
1085 else return FALSE;
1086}
Note: See TracBrowser for help on using the repository browser.