2 // CardLib - CardButton class
5 // Copyright J Brown 2001
11 #include "cardwindow.h"
12 #include "cardbutton.h"
13 #include "cardcolor.h"
15 HPALETTE UseNicePalette(HDC, HPALETTE);
16 void RestorePalette(HDC, HPALETTE);
18 void PaintRect(HDC hdc, RECT *rect, COLORREF colour);
20 CardButton::CardButton(CardWindow &parent, int Id, TCHAR *szText, UINT Style, bool visible,
21 int x, int y, int width, int height)
23 : parentWnd(parent), id(Id), fVisible(visible), uStyle(Style), ButtonCallback(0)
25 crText = RGB(255,255,255);
26 crBack = RGB(0, 128, 0);
39 Move(x, y, width, height);
41 mxlock = CreateMutex(0, FALSE, 0);
46 CardButton::~CardButton()
51 void CardButton::DrawRect(HDC hdc, RECT *rect, bool fNormal)
57 HPEN hhi = CreatePen(0, 0, MAKE_PALETTERGB(crHighlight));
58 HPEN hsh = CreatePen(0, 0, MAKE_PALETTERGB(crShadow));
59 HPEN hbl = (HPEN)GetStockObject(BLACK_PEN);
63 int width = rect->right-rect->left - 1;
64 int height = rect->bottom-rect->top - 1;
66 SetRect(&fill, x+1, y+1, x+width-1, y+height-1);
77 OffsetRect(&fill, 1, 1);
81 hOld = SelectObject(hdc, hhi);
83 hOld = SelectObject(hdc, hhi);
85 MoveToEx(hdc, x, y+height, 0);
87 LineTo(hdc, x+width, y);
88 SelectObject(hdc, hOld);
90 hOld = SelectObject(hdc, hbl);
91 LineTo(hdc, x+width, y+height);
92 LineTo(hdc, x-one, y+height);
93 SelectObject(hdc, hOld);
95 hOld = SelectObject(hdc, hsh);
96 MoveToEx(hdc, x+one, y+height-one, 0);
97 LineTo(hdc, x+width-one, y+height-one);
98 LineTo(hdc, x+width-one, y);
99 SelectObject(hdc, hOld);
101 PaintRect(hdc, &fill, MAKE_PALETTERGB(crBack));
107 void CardButton::Clip(HDC hdc)
109 if(fVisible == false) return;
111 ExcludeClipRect(hdc, rect.left, rect.top, rect.right, rect.bottom);
114 void CardButton::Draw(HDC hdc, bool fNormal)
117 int x, y; //text x, y
118 int ix, iy; //icon x, y
123 if(fVisible == 0) return;
126 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
128 SelectObject(hdc, hFont);
130 GetTextExtentPoint32(hdc, szText, lstrlen(szText), &textsize);
134 x = rect.left + 32 + 8;
138 if(uStyle & CB_ALIGN_LEFT)
140 x = rect.left + iconwidth;
142 else if(uStyle & CB_ALIGN_RIGHT)
144 x = rect.left + (rect.right-rect.left-iconwidth-textsize.cx);
148 x = rect.right - rect.left - iconwidth;
149 x = (x - textsize.cx) / 2;
150 x += rect.left + iconwidth;
154 y = rect.bottom - rect.top;
155 y = (y - textsize.cy) / 2;
158 //calc icon position..
160 iy = rect.top + (rect.bottom-rect.top-32) / 2;
162 //if button is pressed, then shift text
163 if(fNormal == false && (uStyle & CB_PUSHBUTTON))
171 SetRect(&cliprect, x, y, x+textsize.cx, y+textsize.cy);
172 ExcludeClipRect(hdc, x, y, x+textsize.cx, y+textsize.cy);
180 ExcludeClipRect(hdc, ix, iy, ix + 32, iy + 32);
183 if(uStyle & CB_PUSHBUTTON)
185 DrawRect(hdc, &rect, fNormal);
187 SetBkColor(hdc, MAKE_PALETTERGB(crBack));
188 SetTextColor(hdc, crText);//MAKE_PALETTERGB(crText));
190 SelectClipRgn(hdc, 0);
192 ExtTextOut(hdc, x, y, ETO_OPAQUE, &cliprect, szText, lstrlen(szText), 0);
196 SetBkColor(hdc, MAKE_PALETTERGB(crBack));
197 SetTextColor(hdc, crText);//MAKE_PALETTERGB(crText));
199 SelectClipRgn(hdc, 0);
201 ExtTextOut(hdc, x, y, ETO_OPAQUE, &rect, szText, lstrlen(szText), 0);
206 HBRUSH hbr = CreateSolidBrush(MAKE_PALETTERGB(crBack));
207 DrawIconEx(hdc, ix, iy, hIcon, 32, 32, 0, hbr, 0);
213 void CardButton::AdjustPosition(int winwidth, int winheight)
215 int width = rect.right-rect.left;
216 int height = rect.bottom-rect.top;
218 width = width & ~0x1;
225 case CS_XJUST_CENTER: //centered
226 rect.left = (winwidth - (width)) / 2;
227 rect.left += xadjust;
228 rect.right = rect.left+width;
231 case CS_XJUST_RIGHT: //right-aligned
232 rect.left = winwidth - width;
233 rect.left += xadjust;
234 rect.right = rect.left+width;
243 case CS_YJUST_CENTER: //centered
244 rect.top = (winheight - (height)) / 2;
246 rect.bottom = rect.top+height;
249 case CS_YJUST_BOTTOM: //right-aligned
250 rect.top = winheight - height;
252 rect.bottom = rect.top+height;
258 int CardButton::OnLButtonDown(HWND hwnd, int x, int y)
260 if((uStyle & CB_PUSHBUTTON) == 0)
263 //make sure that the user is allowed to do something
264 if(WaitForSingleObject(mxlock, 0) != WAIT_OBJECT_0)
270 ReleaseMutex(mxlock);
283 int CardButton::OnMouseMove(HWND hwnd, int x, int y)
287 bool fOldButtonDown = fButtonDown;
294 if(PtInRect(&rect, pt))
299 if(fButtonDown != fOldButtonDown)
306 int CardButton::OnLButtonUp(HWND hwnd, int x, int y)
313 if(uStyle & CB_PUSHBUTTON)
319 //if have clicked the button
320 if(parentWnd.CardButtonFromPoint(x, y) == this)
324 ButtonCallback(*this);
328 HWND hwnd = (HWND)parentWnd;
329 SendMessage(GetParent(hwnd), WM_COMMAND, MAKEWPARAM(id, BN_CLICKED), (LONG)hwnd);
337 #define _countof(array) (sizeof(array)/sizeof(array[0]))
339 CardButton *CardWindow::CreateButton(int id, TCHAR *szText, UINT uStyle, bool fVisible, int x, int y, int width, int height)
343 if(nNumButtons == MAXBUTTONS)
346 cb = new CardButton(*this, id, szText, uStyle, fVisible, x, y, width, height);
347 Buttons[nNumButtons++] = cb;
349 if(uStyle & CB_PUSHBUTTON)
351 cb->SetBackColor(CardButton::GetFace(crBackgnd));
352 //cb->SetBackColor(ScaleLumRGB(crBackgnd, 0.1));
353 cb->SetForeColor(RGB(255,255,255));
357 cb->SetBackColor(crBackgnd);
358 cb->SetForeColor(RGB(255,255,255));
364 void CardButton::SetText(TCHAR *lpszFormat, ...)
369 va_start(args, lpszFormat);
371 count = wvsprintf(szText, lpszFormat, args);
380 void CardButton::Show(bool fShow)
385 void CardButton::Move(int x, int y, int width, int height)
387 SetRect(&rect, x, y, x+width, y+height);
390 void CardButton::Redraw()
392 HDC hdc = GetDC((HWND)parentWnd);
394 HPALETTE hOldPal = UseNicePalette(hdc, __hPalette);
396 Draw(hdc, !fButtonDown);
398 RestorePalette(hdc, hOldPal);
400 ReleaseDC((HWND)parentWnd, hdc);
403 void CardButton::SetForeColor(COLORREF cr)
408 void CardButton::SetBackColor(COLORREF cr)
412 crHighlight = GetHighlight(cr);
413 crShadow = GetShadow(cr);
415 //crHighlight = ScaleLumRGB(cr, +0.25);
416 //crShadow = ScaleLumRGB(cr, -0.25);
420 COLORREF CardButton::GetHighlight(COLORREF crBase)
422 return ColorScaleRGB(crBase, RGB(255,255,255), 0.25);
426 COLORREF CardButton::GetShadow(COLORREF crBase)
428 return ColorScaleRGB(crBase, RGB(0, 0, 0), 0.25);
431 COLORREF CardButton::GetFace(COLORREF crBase)
433 return ColorScaleRGB(crBase, RGB(255,255,255), 0.1);
436 void CardButton::SetPlacement(UINT xJustify, UINT yJustify, int xAdjust, int yAdjust)
444 void CardButton::SetIcon(HICON hicon, bool fRedraw)
452 void CardButton::SetFont(HFONT font)
454 //don't delete the existing font..
458 void CardButton::SetButtonProc(pButtonProc proc)
460 ButtonCallback = proc;
463 bool CardButton::Lock()
465 DWORD dw = WaitForSingleObject(mxlock, 0);
467 if(dw == WAIT_OBJECT_0)
473 bool CardButton::UnLock()
475 if(ReleaseMutex(mxlock))
481 void CardButton::SetStyle(UINT style)
486 UINT CardButton::GetStyle()