2 // CardLib - CardButton class
5 // Copyright J Brown 2001
10 HPALETTE
UseNicePalette(HDC
, HPALETTE
);
11 void RestorePalette(HDC
, HPALETTE
);
13 void PaintRect(HDC hdc
, RECT
*rect
, COLORREF colour
);
15 CardButton::CardButton(CardWindow
&parent
, int Id
, TCHAR
*szText
, UINT Style
, bool visible
,
16 int x
, int y
, int width
, int height
)
18 : parentWnd(parent
), id(Id
), uStyle(Style
), fVisible(visible
), ButtonCallback(0)
20 crText
= RGB(255,255,255);
21 crBack
= RGB(0, 128, 0);
34 Move(x
, y
, width
, height
);
36 mxlock
= CreateMutex(0, FALSE
, 0);
41 CardButton::~CardButton()
46 void CardButton::DrawRect(HDC hdc
, RECT
*rect
, bool fNormal
)
52 HPEN hhi
= CreatePen(0, 0, MAKE_PALETTERGB(crHighlight
));
53 HPEN hsh
= CreatePen(0, 0, MAKE_PALETTERGB(crShadow
));
54 HPEN hbl
= (HPEN
)GetStockObject(BLACK_PEN
);
58 int width
= rect
->right
-rect
->left
- 1;
59 int height
= rect
->bottom
-rect
->top
- 1;
61 SetRect(&fill
, x
+1, y
+1, x
+width
-1, y
+height
-1);
72 OffsetRect(&fill
, 1, 1);
76 hOld
= SelectObject(hdc
, hhi
);
78 hOld
= SelectObject(hdc
, hsh
);
80 MoveToEx(hdc
, x
, y
+height
, 0);
82 LineTo(hdc
, x
+width
, y
);
83 SelectObject(hdc
, hOld
);
85 hOld
= SelectObject(hdc
, hbl
);
86 LineTo(hdc
, x
+width
, y
+height
);
87 LineTo(hdc
, x
-one
, y
+height
);
88 SelectObject(hdc
, hOld
);
90 hOld
= SelectObject(hdc
, hsh
);
91 MoveToEx(hdc
, x
+one
, y
+height
-one
, 0);
92 LineTo(hdc
, x
+width
-one
, y
+height
-one
);
93 LineTo(hdc
, x
+width
-one
, y
);
94 SelectObject(hdc
, hOld
);
96 PaintRect(hdc
, &fill
, MAKE_PALETTERGB(crBack
));
102 void CardButton::Clip(HDC hdc
)
104 if(fVisible
== false) return;
106 ExcludeClipRect(hdc
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
109 void CardButton::Draw(HDC hdc
, bool fNormal
)
112 int x
, y
; //text x, y
113 int ix
, iy
; //icon x, y
118 if(fVisible
== 0) return;
121 SelectObject(hdc
, GetStockObject(DEFAULT_GUI_FONT
));
123 SelectObject(hdc
, hFont
);
125 GetTextExtentPoint32(hdc
, szText
, lstrlen(szText
), &textsize
);
129 x
= rect
.left
+ 32 + 8;
133 if(uStyle
& CB_ALIGN_LEFT
)
135 x
= rect
.left
+ iconwidth
;
137 else if(uStyle
& CB_ALIGN_RIGHT
)
139 x
= rect
.left
+ (rect
.right
-rect
.left
-iconwidth
-textsize
.cx
);
143 x
= rect
.right
- rect
.left
- iconwidth
;
144 x
= (x
- textsize
.cx
) / 2;
145 x
+= rect
.left
+ iconwidth
;
149 y
= rect
.bottom
- rect
.top
;
150 y
= (y
- textsize
.cy
) / 2;
153 //calc icon position..
155 iy
= rect
.top
+ (rect
.bottom
-rect
.top
-32) / 2;
157 //if button is pressed, then shift text
158 if(fNormal
== false && (uStyle
& CB_PUSHBUTTON
))
166 SetRect(&cliprect
, x
, y
, x
+textsize
.cx
, y
+textsize
.cy
);
167 ExcludeClipRect(hdc
, x
, y
, x
+textsize
.cx
, y
+textsize
.cy
);
175 ExcludeClipRect(hdc
, ix
, iy
, ix
+ 32, iy
+ 32);
178 if(uStyle
& CB_PUSHBUTTON
)
180 DrawRect(hdc
, &rect
, fNormal
);
182 SetBkColor(hdc
, MAKE_PALETTERGB(crBack
));
183 SetTextColor(hdc
, crText
);//MAKE_PALETTERGB(crText));
185 SelectClipRgn(hdc
, 0);
187 ExtTextOut(hdc
, x
, y
, ETO_OPAQUE
, &cliprect
, szText
, lstrlen(szText
), 0);
191 SetBkColor(hdc
, MAKE_PALETTERGB(crBack
));
192 SetTextColor(hdc
, crText
);//MAKE_PALETTERGB(crText));
194 SelectClipRgn(hdc
, 0);
196 ExtTextOut(hdc
, x
, y
, ETO_OPAQUE
, &rect
, szText
, lstrlen(szText
), 0);
201 HBRUSH hbr
= CreateSolidBrush(MAKE_PALETTERGB(crBack
));
202 DrawIconEx(hdc
, ix
, iy
, hIcon
, 32, 32, 0, hbr
, 0);
208 void CardButton::AdjustPosition(int winwidth
, int winheight
)
210 int width
= rect
.right
-rect
.left
;
211 int height
= rect
.bottom
-rect
.top
;
213 width
= width
& ~0x1;
220 case CS_XJUST_CENTER
: //centered
221 rect
.left
= (winwidth
- (width
)) / 2;
222 rect
.left
+= xadjust
;
223 rect
.right
= rect
.left
+width
;
226 case CS_XJUST_RIGHT
: //right-aligned
227 rect
.left
= winwidth
- width
;
228 rect
.left
+= xadjust
;
229 rect
.right
= rect
.left
+width
;
238 case CS_YJUST_CENTER
: //centered
239 rect
.top
= (winheight
- (height
)) / 2;
241 rect
.bottom
= rect
.top
+height
;
244 case CS_YJUST_BOTTOM
: //right-aligned
245 rect
.top
= winheight
- height
;
247 rect
.bottom
= rect
.top
+height
;
253 int CardButton::OnLButtonDown(HWND hwnd
, int x
, int y
)
255 if((uStyle
& CB_PUSHBUTTON
) == 0)
258 //make sure that the user is allowed to do something
259 if(WaitForSingleObject(mxlock
, 0) != WAIT_OBJECT_0
)
265 ReleaseMutex(mxlock
);
278 int CardButton::OnMouseMove(HWND hwnd
, int x
, int y
)
282 bool fOldButtonDown
= fButtonDown
;
289 if(PtInRect(&rect
, pt
))
294 if(fButtonDown
!= fOldButtonDown
)
301 int CardButton::OnLButtonUp(HWND hwnd
, int x
, int y
)
308 if(uStyle
& CB_PUSHBUTTON
)
314 //if have clicked the button
315 if(parentWnd
.CardButtonFromPoint(x
, y
) == this)
319 ButtonCallback(*this);
323 HWND hwnd
= (HWND
)parentWnd
;
324 SendMessage(GetParent(hwnd
), WM_COMMAND
, MAKEWPARAM(id
, BN_CLICKED
), (LONG_PTR
)hwnd
);
332 //#define _countof(array) (sizeof(array)/sizeof(array[0]))
334 CardButton
*CardWindow::CreateButton(int id
, TCHAR
*szText
, UINT uStyle
, bool fVisible
, int x
, int y
, int width
, int height
)
338 if(nNumButtons
== MAXBUTTONS
)
341 cb
= new CardButton(*this, id
, szText
, uStyle
, fVisible
, x
, y
, width
, height
);
342 Buttons
[nNumButtons
++] = cb
;
344 if(uStyle
& CB_PUSHBUTTON
)
346 cb
->SetBackColor(CardButton::GetFace(crBackgnd
));
347 //cb->SetBackColor(ScaleLumRGB(crBackgnd, 0.1));
348 cb
->SetForeColor(RGB(255,255,255));
352 cb
->SetBackColor(crBackgnd
);
353 cb
->SetForeColor(RGB(255,255,255));
359 void CardButton::SetText(TCHAR
*lpszFormat
, ...)
364 va_start(args
, lpszFormat
);
366 count
= wvsprintf(szText
, lpszFormat
, args
);
375 void CardButton::Show(bool fShow
)
380 void CardButton::Move(int x
, int y
, int width
, int height
)
382 SetRect(&rect
, x
, y
, x
+width
, y
+height
);
385 void CardButton::Redraw()
387 HDC hdc
= GetDC((HWND
)parentWnd
);
389 HPALETTE hOldPal
= UseNicePalette(hdc
, __hPalette
);
391 Draw(hdc
, !fButtonDown
);
393 RestorePalette(hdc
, hOldPal
);
395 ReleaseDC((HWND
)parentWnd
, hdc
);
398 void CardButton::SetForeColor(COLORREF cr
)
403 void CardButton::SetBackColor(COLORREF cr
)
407 crHighlight
= GetHighlight(cr
);
408 crShadow
= GetShadow(cr
);
410 //crHighlight = ScaleLumRGB(cr, +0.25);
411 //crShadow = ScaleLumRGB(cr, -0.25);
415 COLORREF
CardButton::GetHighlight(COLORREF crBase
)
417 return ColorScaleRGB(crBase
, RGB(255,255,255), 0.25);
421 COLORREF
CardButton::GetShadow(COLORREF crBase
)
423 return ColorScaleRGB(crBase
, RGB(0, 0, 0), 0.25);
426 COLORREF
CardButton::GetFace(COLORREF crBase
)
428 return ColorScaleRGB(crBase
, RGB(255,255,255), 0.1);
431 void CardButton::SetPlacement(UINT xJustify
, UINT yJustify
, int xAdjust
, int yAdjust
)
439 void CardButton::SetIcon(HICON hicon
, bool fRedraw
)
447 void CardButton::SetFont(HFONT font
)
449 //don't delete the existing font..
453 void CardButton::SetButtonProc(pButtonProc proc
)
455 ButtonCallback
= proc
;
458 bool CardButton::Lock()
460 DWORD dw
= WaitForSingleObject(mxlock
, 0);
462 if(dw
== WAIT_OBJECT_0
)
468 bool CardButton::UnLock()
470 if(ReleaseMutex(mxlock
))
476 void CardButton::SetStyle(UINT style
)
481 UINT
CardButton::GetStyle()