921d59cdebd9da2aa8c831a1398833cee4a43c48
2 // CardLib - CardButton class
5 // Copyright J Brown 2001
10 #define WIN32_NO_STATUS
14 //#include "cardwindow.h"
15 //#include "cardbutton.h"
16 #include "cardcolor.h"
18 HPALETTE
UseNicePalette(HDC
, HPALETTE
);
19 void RestorePalette(HDC
, HPALETTE
);
21 void PaintRect(HDC hdc
, RECT
*rect
, COLORREF colour
);
23 CardButton::CardButton(CardWindow
&parent
, int Id
, TCHAR
*szText
, UINT Style
, bool visible
,
24 int x
, int y
, int width
, int height
)
26 : parentWnd(parent
), id(Id
), uStyle(Style
), fVisible(visible
), ButtonCallback(0)
28 crText
= RGB(255,255,255);
29 crBack
= RGB(0, 128, 0);
42 Move(x
, y
, width
, height
);
44 mxlock
= CreateMutex(0, FALSE
, 0);
49 CardButton::~CardButton()
54 void CardButton::DrawRect(HDC hdc
, RECT
*rect
, bool fNormal
)
60 HPEN hhi
= CreatePen(0, 0, MAKE_PALETTERGB(crHighlight
));
61 HPEN hsh
= CreatePen(0, 0, MAKE_PALETTERGB(crShadow
));
62 HPEN hbl
= (HPEN
)GetStockObject(BLACK_PEN
);
66 int width
= rect
->right
-rect
->left
- 1;
67 int height
= rect
->bottom
-rect
->top
- 1;
69 SetRect(&fill
, x
+1, y
+1, x
+width
-1, y
+height
-1);
80 OffsetRect(&fill
, 1, 1);
84 hOld
= SelectObject(hdc
, hhi
);
86 hOld
= SelectObject(hdc
, hhi
);
88 MoveToEx(hdc
, x
, y
+height
, 0);
90 LineTo(hdc
, x
+width
, y
);
91 SelectObject(hdc
, hOld
);
93 hOld
= SelectObject(hdc
, hbl
);
94 LineTo(hdc
, x
+width
, y
+height
);
95 LineTo(hdc
, x
-one
, y
+height
);
96 SelectObject(hdc
, hOld
);
98 hOld
= SelectObject(hdc
, hsh
);
99 MoveToEx(hdc
, x
+one
, y
+height
-one
, 0);
100 LineTo(hdc
, x
+width
-one
, y
+height
-one
);
101 LineTo(hdc
, x
+width
-one
, y
);
102 SelectObject(hdc
, hOld
);
104 PaintRect(hdc
, &fill
, MAKE_PALETTERGB(crBack
));
110 void CardButton::Clip(HDC hdc
)
112 if(fVisible
== false) return;
114 ExcludeClipRect(hdc
, rect
.left
, rect
.top
, rect
.right
, rect
.bottom
);
117 void CardButton::Draw(HDC hdc
, bool fNormal
)
120 int x
, y
; //text x, y
121 int ix
, iy
; //icon x, y
126 if(fVisible
== 0) return;
129 SelectObject(hdc
, GetStockObject(DEFAULT_GUI_FONT
));
131 SelectObject(hdc
, hFont
);
133 GetTextExtentPoint32(hdc
, szText
, lstrlen(szText
), &textsize
);
137 x
= rect
.left
+ 32 + 8;
141 if(uStyle
& CB_ALIGN_LEFT
)
143 x
= rect
.left
+ iconwidth
;
145 else if(uStyle
& CB_ALIGN_RIGHT
)
147 x
= rect
.left
+ (rect
.right
-rect
.left
-iconwidth
-textsize
.cx
);
151 x
= rect
.right
- rect
.left
- iconwidth
;
152 x
= (x
- textsize
.cx
) / 2;
153 x
+= rect
.left
+ iconwidth
;
157 y
= rect
.bottom
- rect
.top
;
158 y
= (y
- textsize
.cy
) / 2;
161 //calc icon position..
163 iy
= rect
.top
+ (rect
.bottom
-rect
.top
-32) / 2;
165 //if button is pressed, then shift text
166 if(fNormal
== false && (uStyle
& CB_PUSHBUTTON
))
174 SetRect(&cliprect
, x
, y
, x
+textsize
.cx
, y
+textsize
.cy
);
175 ExcludeClipRect(hdc
, x
, y
, x
+textsize
.cx
, y
+textsize
.cy
);
183 ExcludeClipRect(hdc
, ix
, iy
, ix
+ 32, iy
+ 32);
186 if(uStyle
& CB_PUSHBUTTON
)
188 DrawRect(hdc
, &rect
, fNormal
);
190 SetBkColor(hdc
, MAKE_PALETTERGB(crBack
));
191 SetTextColor(hdc
, crText
);//MAKE_PALETTERGB(crText));
193 SelectClipRgn(hdc
, 0);
195 ExtTextOut(hdc
, x
, y
, ETO_OPAQUE
, &cliprect
, szText
, lstrlen(szText
), 0);
199 SetBkColor(hdc
, MAKE_PALETTERGB(crBack
));
200 SetTextColor(hdc
, crText
);//MAKE_PALETTERGB(crText));
202 SelectClipRgn(hdc
, 0);
204 ExtTextOut(hdc
, x
, y
, ETO_OPAQUE
, &rect
, szText
, lstrlen(szText
), 0);
209 HBRUSH hbr
= CreateSolidBrush(MAKE_PALETTERGB(crBack
));
210 DrawIconEx(hdc
, ix
, iy
, hIcon
, 32, 32, 0, hbr
, 0);
216 void CardButton::AdjustPosition(int winwidth
, int winheight
)
218 int width
= rect
.right
-rect
.left
;
219 int height
= rect
.bottom
-rect
.top
;
221 width
= width
& ~0x1;
228 case CS_XJUST_CENTER
: //centered
229 rect
.left
= (winwidth
- (width
)) / 2;
230 rect
.left
+= xadjust
;
231 rect
.right
= rect
.left
+width
;
234 case CS_XJUST_RIGHT
: //right-aligned
235 rect
.left
= winwidth
- width
;
236 rect
.left
+= xadjust
;
237 rect
.right
= rect
.left
+width
;
246 case CS_YJUST_CENTER
: //centered
247 rect
.top
= (winheight
- (height
)) / 2;
249 rect
.bottom
= rect
.top
+height
;
252 case CS_YJUST_BOTTOM
: //right-aligned
253 rect
.top
= winheight
- height
;
255 rect
.bottom
= rect
.top
+height
;
261 int CardButton::OnLButtonDown(HWND hwnd
, int x
, int y
)
263 if((uStyle
& CB_PUSHBUTTON
) == 0)
266 //make sure that the user is allowed to do something
267 if(WaitForSingleObject(mxlock
, 0) != WAIT_OBJECT_0
)
273 ReleaseMutex(mxlock
);
286 int CardButton::OnMouseMove(HWND hwnd
, int x
, int y
)
290 bool fOldButtonDown
= fButtonDown
;
297 if(PtInRect(&rect
, pt
))
302 if(fButtonDown
!= fOldButtonDown
)
309 int CardButton::OnLButtonUp(HWND hwnd
, int x
, int y
)
316 if(uStyle
& CB_PUSHBUTTON
)
322 //if have clicked the button
323 if(parentWnd
.CardButtonFromPoint(x
, y
) == this)
327 ButtonCallback(*this);
331 HWND hwnd
= (HWND
)parentWnd
;
332 SendMessage(GetParent(hwnd
), WM_COMMAND
, MAKEWPARAM(id
, BN_CLICKED
), (LONG_PTR
)hwnd
);
340 //#define _countof(array) (sizeof(array)/sizeof(array[0]))
342 CardButton
*CardWindow::CreateButton(int id
, TCHAR
*szText
, UINT uStyle
, bool fVisible
, int x
, int y
, int width
, int height
)
346 if(nNumButtons
== MAXBUTTONS
)
349 cb
= new CardButton(*this, id
, szText
, uStyle
, fVisible
, x
, y
, width
, height
);
350 Buttons
[nNumButtons
++] = cb
;
352 if(uStyle
& CB_PUSHBUTTON
)
354 cb
->SetBackColor(CardButton::GetFace(crBackgnd
));
355 //cb->SetBackColor(ScaleLumRGB(crBackgnd, 0.1));
356 cb
->SetForeColor(RGB(255,255,255));
360 cb
->SetBackColor(crBackgnd
);
361 cb
->SetForeColor(RGB(255,255,255));
367 void CardButton::SetText(TCHAR
*lpszFormat
, ...)
372 va_start(args
, lpszFormat
);
374 count
= wvsprintf(szText
, lpszFormat
, args
);
383 void CardButton::Show(bool fShow
)
388 void CardButton::Move(int x
, int y
, int width
, int height
)
390 SetRect(&rect
, x
, y
, x
+width
, y
+height
);
393 void CardButton::Redraw()
395 HDC hdc
= GetDC((HWND
)parentWnd
);
397 HPALETTE hOldPal
= UseNicePalette(hdc
, __hPalette
);
399 Draw(hdc
, !fButtonDown
);
401 RestorePalette(hdc
, hOldPal
);
403 ReleaseDC((HWND
)parentWnd
, hdc
);
406 void CardButton::SetForeColor(COLORREF cr
)
411 void CardButton::SetBackColor(COLORREF cr
)
415 crHighlight
= GetHighlight(cr
);
416 crShadow
= GetShadow(cr
);
418 //crHighlight = ScaleLumRGB(cr, +0.25);
419 //crShadow = ScaleLumRGB(cr, -0.25);
423 COLORREF
CardButton::GetHighlight(COLORREF crBase
)
425 return ColorScaleRGB(crBase
, RGB(255,255,255), 0.25);
429 COLORREF
CardButton::GetShadow(COLORREF crBase
)
431 return ColorScaleRGB(crBase
, RGB(0, 0, 0), 0.25);
434 COLORREF
CardButton::GetFace(COLORREF crBase
)
436 return ColorScaleRGB(crBase
, RGB(255,255,255), 0.1);
439 void CardButton::SetPlacement(UINT xJustify
, UINT yJustify
, int xAdjust
, int yAdjust
)
447 void CardButton::SetIcon(HICON hicon
, bool fRedraw
)
455 void CardButton::SetFont(HFONT font
)
457 //don't delete the existing font..
461 void CardButton::SetButtonProc(pButtonProc proc
)
463 ButtonCallback
= proc
;
466 bool CardButton::Lock()
468 DWORD dw
= WaitForSingleObject(mxlock
, 0);
470 if(dw
== WAIT_OBJECT_0
)
476 bool CardButton::UnLock()
478 if(ReleaseMutex(mxlock
))
484 void CardButton::SetStyle(UINT style
)
489 UINT
CardButton::GetStyle()