ReactOS  0.4.14-dev-50-g13bb5e2
cardbutton.cpp
Go to the documentation of this file.
1 //
2 // CardLib - CardButton class
3 //
4 // Freeware
5 // Copyright J Brown 2001
6 //
7 
8 #include "cardlib.h"
9 
10 HPALETTE UseNicePalette(HDC, HPALETTE);
11 void RestorePalette(HDC, HPALETTE);
12 
13 void PaintRect(HDC hdc, RECT *rect, COLORREF colour);
14 
16  int x, int y, int width, int height)
17 
18  : parentWnd(parent), id(Id), uStyle(Style), fVisible(visible), ButtonCallback(0)
19 {
20  crText = RGB(255,255,255);
21  crBack = RGB(0, 128, 0);
22 
23  xadjust = 0;
24  yadjust = 0;
25  xjustify = 0;
26  yjustify = 0;
27 
28  fMouseDown = false;
29  fButtonDown = false;
30 
31  hIcon = 0;
32 
33  SetText(szText);
34  Move(x, y, width, height);
35 
36  mxlock = CreateMutex(0, FALSE, 0);
37 
38  hFont = 0;
39 }
40 
42 {
44 }
45 
46 void CardButton::DrawRect(HDC hdc, RECT *rect, bool fNormal)
47 {
48  RECT fill;
49 
50  HANDLE hOld;
51 
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);
55 
56  int x = rect->left;
57  int y = rect->top;
58  int width = rect->right-rect->left - 1;
59  int height = rect->bottom-rect->top - 1;
60 
61  SetRect(&fill, x+1, y+1, x+width-1, y+height-1);
62 
63  int one = 1;
64 
65  if(!fNormal)
66  {
67  x += width;
68  y += height;
69  width = -width;
70  height = -height;
71  one = -1;
72  OffsetRect(&fill, 1, 1);
73  }
74 
75  if(fNormal)
76  hOld = SelectObject(hdc, hhi);
77  else
78  hOld = SelectObject(hdc, hsh);
79 
80  MoveToEx(hdc, x, y+height, 0);
81  LineTo(hdc, x, y);
82  LineTo(hdc, x+width, y);
83  SelectObject(hdc, hOld);
84 
85  hOld = SelectObject(hdc, hbl);
86  LineTo(hdc, x+width, y+height);
87  LineTo(hdc, x-one, y+height);
88  SelectObject(hdc, hOld);
89 
90  hOld = SelectObject(hdc, hsh);
91  MoveToEx(hdc, x+one, y+height-one, 0);
93  LineTo(hdc, x+width-one, y);
94  SelectObject(hdc, hOld);
95 
97 
98  DeleteObject(hhi);
99  DeleteObject(hsh);
100 }
101 
103 {
104  if(fVisible == false) return;
105 
107 }
108 
109 void CardButton::Draw(HDC hdc, bool fNormal)
110 {
111  SIZE textsize;
112  int x, y; //text x, y
113  int ix, iy; //icon x, y
114  int iconwidth = 0;
115 
116  RECT cliprect;
117 
118  if(fVisible == 0) return;
119 
120  if(hFont == 0)
122  else
124 
126 
127  if(hIcon)
128  {
129  x = rect.left + 32 + 8;
130  }
131  else
132  {
133  if(uStyle & CB_ALIGN_LEFT)
134  {
135  x = rect.left + iconwidth;
136  }
137  else if(uStyle & CB_ALIGN_RIGHT)
138  {
139  x = rect.left + (rect.right-rect.left-iconwidth-textsize.cx);
140  }
141  else //centered
142  {
143  x = rect.right - rect.left - iconwidth;
144  x = (x - textsize.cx) / 2;
145  x += rect.left + iconwidth;
146  }
147  }
148 
149  y = rect.bottom - rect.top;
150  y = (y - textsize.cy) / 2;
151  y += rect.top;
152 
153  //calc icon position..
154  ix = rect.left + 4;
155  iy = rect.top + (rect.bottom-rect.top-32) / 2;
156 
157  //if button is pressed, then shift text
158  if(fNormal == false && (uStyle & CB_PUSHBUTTON))
159  {
160  x += 1;
161  y += 1;
162  ix += 1;
163  iy += 1;
164  }
165 
166  SetRect(&cliprect, x, y, x+textsize.cx, y+textsize.cy);
167  ExcludeClipRect(hdc, x, y, x+textsize.cx, y+textsize.cy);
168 
169  //
170  // Calc icon pos
171  //
172 
173  if(hIcon)
174  {
175  ExcludeClipRect(hdc, ix, iy, ix + 32, iy + 32);
176  }
177 
178  if(uStyle & CB_PUSHBUTTON)
179  {
180  DrawRect(hdc, &rect, fNormal);
181 
183  SetTextColor(hdc, crText);//MAKE_PALETTERGB(crText));
184 
185  SelectClipRgn(hdc, 0);
186 
187  ExtTextOut(hdc, x, y, ETO_OPAQUE, &cliprect, szText, lstrlen(szText), 0);
188  }
189  else
190  {
192  SetTextColor(hdc, crText);//MAKE_PALETTERGB(crText));
193 
194  SelectClipRgn(hdc, 0);
195 
197  }
198 
199  if(hIcon)
200  {
201  HBRUSH hbr = CreateSolidBrush(MAKE_PALETTERGB(crBack));
202  DrawIconEx(hdc, ix, iy, hIcon, 32, 32, 0, hbr, 0);
203  DeleteObject(hbr);
204  }
205 
206 }
207 
208 void CardButton::AdjustPosition(int winwidth, int winheight)
209 {
210  int width = rect.right-rect.left;
211  int height = rect.bottom-rect.top;
212 
213  width = width & ~0x1;
214 
215  switch(xjustify)
216  {
217  case CS_XJUST_NONE:
218  break;
219 
220  case CS_XJUST_CENTER: //centered
221  rect.left = (winwidth - (width)) / 2;
222  rect.left += xadjust;
224  break;
225 
226  case CS_XJUST_RIGHT: //right-aligned
227  rect.left = winwidth - width;
228  rect.left += xadjust;
230  break;
231  }
232 
233  switch(yjustify)
234  {
235  case CS_YJUST_NONE:
236  break;
237 
238  case CS_YJUST_CENTER: //centered
239  rect.top = (winheight - (height)) / 2;
240  rect.top += yadjust;
242  break;
243 
244  case CS_YJUST_BOTTOM: //right-aligned
245  rect.top = winheight - height;
246  rect.top += yadjust;
248  break;
249  }
250 
251 }
252 
254 {
255  if((uStyle & CB_PUSHBUTTON) == 0)
256  return 0;
257 
258  //make sure that the user is allowed to do something
260  {
261  return 0;
262  }
263  else
264  {
266  }
267 
268  fMouseDown = true;
269  fButtonDown = true;
270 
271  Redraw();
272 
273  SetCapture(hwnd);
274 
275  return 1;
276 }
277 
279 {
280  if(fMouseDown)
281  {
282  bool fOldButtonDown = fButtonDown;
283 
284  POINT pt;
285 
286  pt.x = x;
287  pt.y = y;
288 
289  if(PtInRect(&rect, pt))
290  fButtonDown = true;
291  else
292  fButtonDown = false;
293 
294  if(fButtonDown != fOldButtonDown)
295  Redraw();
296  }
297 
298  return 0;
299 }
300 
302 {
303  if(fMouseDown)
304  {
305  fMouseDown = false;
306  fButtonDown = false;
307 
308  if(uStyle & CB_PUSHBUTTON)
309  {
310  Redraw();
311  ReleaseCapture();
312  }
313 
314  //if have clicked the button
315  if(parentWnd.CardButtonFromPoint(x, y) == this)
316  {
317  if(ButtonCallback)
318  {
319  ButtonCallback(*this);
320  }
321  else
322  {
323  HWND hwnd = (HWND)parentWnd;
325  }
326  }
327  }
328 
329  return 0;
330 }
331 
332 //#define _countof(array) (sizeof(array)/sizeof(array[0]))
333 
334 CardButton *CardWindow::CreateButton(int id, TCHAR *szText, UINT uStyle, bool fVisible, int x, int y, int width, int height)
335 {
336  CardButton *cb;
337 
338  if(nNumButtons == MAXBUTTONS)
339  return 0;
340 
341  cb = new CardButton(*this, id, szText, uStyle, fVisible, x, y, width, height);
342  Buttons[nNumButtons++] = cb;
343 
344  if(uStyle & CB_PUSHBUTTON)
345  {
346  cb->SetBackColor(CardButton::GetFace(crBackgnd));
347  //cb->SetBackColor(ScaleLumRGB(crBackgnd, 0.1));
348  cb->SetForeColor(RGB(255,255,255));
349  }
350  else
351  {
352  cb->SetBackColor(crBackgnd);
353  cb->SetForeColor(RGB(255,255,255));
354  }
355 
356  return cb;
357 }
358 
359 void CardButton::SetText(TCHAR *lpszFormat, ...)
360 {
361  int count;
362 
363  va_list args;
364  va_start(args, lpszFormat);
365 
366  count = wvsprintf(szText, lpszFormat, args);
367  va_end(args);
368 }
369 
371 {
372  return id;
373 }
374 
375 void CardButton::Show(bool fShow)
376 {
377  fVisible = fShow;
378 }
379 
380 void CardButton::Move(int x, int y, int width, int height)
381 {
382  SetRect(&rect, x, y, x+width, y+height);
383 }
384 
386 {
387  HDC hdc = GetDC((HWND)parentWnd);
388 
389  HPALETTE hOldPal = UseNicePalette(hdc, __hPalette);
390 
391  Draw(hdc, !fButtonDown);
392 
393  RestorePalette(hdc, hOldPal);
394 
396 }
397 
399 {
400  crText = cr;
401 }
402 
404 {
405  crBack = cr;
406 
408  crShadow = GetShadow(cr);
409 
410  //crHighlight = ScaleLumRGB(cr, +0.25);
411  //crShadow = ScaleLumRGB(cr, -0.25);
412 }
413 
414 // Static member
416 {
417  return ColorScaleRGB(crBase, RGB(255,255,255), 0.25);
418 }
419 
420 // Static member
422 {
423  return ColorScaleRGB(crBase, RGB(0, 0, 0), 0.25);
424 }
425 
427 {
428  return ColorScaleRGB(crBase, RGB(255,255,255), 0.1);
429 }
430 
431 void CardButton::SetPlacement(UINT xJustify, UINT yJustify, int xAdjust, int yAdjust)
432 {
433  xadjust = xAdjust;
434  yadjust = yAdjust;
435  xjustify = xJustify;
436  yjustify = yJustify;
437 }
438 
439 void CardButton::SetIcon(HICON hicon, bool fRedraw)
440 {
441  hIcon = hicon;
442 
443  if(fRedraw)
444  Redraw();
445 }
446 
448 {
449  //don't delete the existing font..
450  hFont = font;
451 }
452 
454 {
456 }
457 
459 {
461 
462  if(dw == WAIT_OBJECT_0)
463  return true;
464  else
465  return false;
466 }
467 
469 {
470  if(ReleaseMutex(mxlock))
471  return true;
472  else
473  return false;
474 }
475 
477 {
478  uStyle = style;
479 }
480 
482 {
483  return uStyle;
484 }
HGDIOBJ WINAPI GetStockObject(_In_ int)
CardButton * CardButtonFromPoint(int x, int y)
Definition: cardwindow.cpp:229
void SetForeColor(COLORREF cr)
Definition: cardbutton.cpp:398
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
bool Lock()
Definition: cardbutton.cpp:458
GLint GLint GLsizei width
Definition: gl.h:1546
static HICON
Definition: imagelist.c:84
#define MAKE_PALETTERGB(colref)
Definition: cardcolor.h:17
#define MAKEWPARAM(l, h)
Definition: winuser.h:3948
HPEN WINAPI CreatePen(_In_ int, _In_ int, _In_ COLORREF)
#define CloseHandle
Definition: compat.h:398
void Move(int x, int y, int width, int height)
Definition: cardbutton.cpp:380
int WINAPI SelectClipRgn(_In_ HDC, _In_opt_ HRGN)
COLORREF ColorScaleRGB(const COLORREF Col1, const COLORREF Col2, const double Ratio)
Definition: cardcolor.cpp:154
HFONT hFont
Definition: cardbutton.h:81
BOOL WINAPI OffsetRect(_Inout_ LPRECT, _In_ int, _In_ int)
RECT rect
Definition: cardbutton.h:70
void RestorePalette(HDC, HPALETTE)
HDC WINAPI GetDC(_In_opt_ HWND)
void SetPlacement(UINT xJustify, UINT yJustify, int xAdjust, int yAdjust)
Definition: cardbutton.cpp:431
int OnLButtonUp(HWND hwnd, int x, int y)
Definition: cardbutton.cpp:301
Definition: mk_font.cpp:20
static COLORREF GetFace(COLORREF crBase)
Definition: cardbutton.cpp:426
static COLORREF GetHighlight(COLORREF crBase)
Definition: cardbutton.cpp:415
#define pt(x, y)
Definition: drawing.c:79
HPALETTE UseNicePalette(HDC, HPALETTE)
Definition: cardwindow.cpp:14
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define CS_XJUST_RIGHT
Definition: cardlib.h:38
void AdjustPosition(int winwidth, int winheight)
Definition: cardbutton.cpp:208
static HDC
Definition: imagelist.c:92
bool fVisible
Definition: cardbutton.h:73
LONG top
Definition: windef.h:292
HANDLE HWND
Definition: compat.h:13
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
BOOL WINAPI LineTo(_In_ HDC, _In_ int, _In_ int)
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
int OnMouseMove(HWND hwnd, int x, int y)
Definition: cardbutton.cpp:278
UINT uStyle
Definition: cardbutton.h:72
static HANDLE proc()
Definition: pdb.c:32
COLORREF crText
Definition: cardbutton.h:86
LONG left
Definition: windef.h:291
HICON hIcon
Definition: cardbutton.h:80
void SetStyle(UINT uStyle)
Definition: cardbutton.cpp:476
int iy
Definition: tritemp.h:491
LONG right
Definition: windef.h:293
Definition: match.c:390
& rect
Definition: startmenu.cpp:1413
void Redraw()
Definition: cardbutton.cpp:385
#define wvsprintf
Definition: winuser.h:5767
DWORD Id
static double one
Definition: j0_y0.c:80
bool fMouseDown
Definition: cardbutton.h:91
COLORREF WINAPI SetBkColor(_In_ HDC, _In_ COLORREF)
Definition: dc.c:975
BOOL WINAPI MoveToEx(_In_ HDC, _In_ int, _In_ int, _Out_opt_ LPPOINT)
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
COLORREF crBackgnd
Definition: cardwindow.h:109
#define va_end(ap)
Definition: acmsvcex.h:90
#define ETO_OPAQUE
Definition: wingdi.h:646
static const WCHAR szText[]
Definition: dialog.c:139
#define CS_XJUST_NONE
Definition: cardlib.h:37
int yadjust
Definition: cardbutton.h:77
int OnLButtonDown(HWND hwnd, int x, int y)
Definition: cardbutton.cpp:253
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
pButtonProc ButtonCallback
Definition: cardbutton.h:96
void DrawRect(HDC hdc, RECT *rect, bool fNormal)
Definition: cardbutton.cpp:46
#define CB_PUSHBUTTON
Definition: cardlib.h:46
#define MAXBUTTONS
Definition: cardwindow.h:4
void Clip(HDC hdc)
Definition: cardbutton.cpp:102
LONG cx
Definition: windef.h:319
char * va_list
Definition: acmsvcex.h:78
CardButton * CreateButton(int id, TCHAR *szText, UINT uStyle, bool fVisible, int x, int y, int width, int height)
Definition: cardbutton.cpp:334
#define CS_XJUST_CENTER
Definition: cardlib.h:39
static COLORREF GetShadow(COLORREF crBase)
Definition: cardbutton.cpp:421
COLORREF crHighlight
Definition: cardbutton.h:87
int xjustify
Definition: cardbutton.h:76
char TCHAR
Definition: xmlstorage.h:189
#define WAIT_OBJECT_0
Definition: winbase.h:387
const DWORD Style
Definition: appswitch.c:71
#define CreateMutex
Definition: winbase.h:3573
r parent
Definition: btrfs.c:2897
void SetFont(HFONT font)
Definition: cardbutton.cpp:447
#define CB_ALIGN_LEFT
Definition: cardlib.h:48
BOOL WINAPI DrawIconEx(_In_ HDC, _In_ int, _In_ int, _In_ HICON, _In_ int, _In_ int, _In_ UINT, _In_opt_ HBRUSH, _In_ UINT)
Definition: cursoricon.c:1997
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
TCHAR szText[MAXBUTTONTEXT]
Definition: cardbutton.h:83
DWORD COLORREF
Definition: windef.h:285
bool fButtonDown
Definition: cardbutton.h:92
HPALETTE __hPalette
Definition: cardlib.cpp:28
#define RGB(r, g, b)
Definition: wingdi.h:2935
#define BN_CLICKED
Definition: winuser.h:1907
unsigned long DWORD
Definition: ntddk_ex.h:95
CardButton * Buttons[MAXBUTTONS]
Definition: cardwindow.h:100
REFIID LPVOID DWORD dw
Definition: atlbase.h:40
static DWORD cb
Definition: integrity.c:41
#define DEFAULT_GUI_FONT
Definition: wingdi.h:908
#define CB_ALIGN_RIGHT
Definition: cardlib.h:49
#define CS_YJUST_NONE
Definition: cardlib.h:41
#define ExtTextOut
Definition: wingdi.h:4449
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex(IN HANDLE hMutex)
Definition: synch.c:618
void SetBackColor(COLORREF cr)
Definition: cardbutton.cpp:403
HDC hdc
Definition: main.c:9
void SetButtonProc(pButtonProc proc)
Definition: cardbutton.cpp:453
#define WM_COMMAND
Definition: winuser.h:1722
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
COLORREF crShadow
Definition: cardbutton.h:88
COLORREF crBack
Definition: cardbutton.h:85
COLORREF WINAPI SetTextColor(_In_ HDC, _In_ COLORREF)
Definition: text.c:888
#define CS_YJUST_BOTTOM
Definition: cardlib.h:42
HWND WINAPI GetParent(_In_ HWND)
BOOL WINAPI SetRect(_Out_ LPRECT, _In_ int, _In_ int, _In_ int, _In_ int)
HWND WINAPI SetCapture(_In_ HWND hWnd)
#define lstrlen
Definition: winbase.h:3693
void Draw(HDC hdc, bool fNormal)
Definition: cardbutton.cpp:109
bool UnLock()
Definition: cardbutton.cpp:468
int xadjust
Definition: cardbutton.h:75
int WINAPI ExcludeClipRect(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
#define BLACK_PEN
Definition: wingdi.h:902
CardButton(CardWindow &parent, int id, TCHAR *szText, UINT style, bool visible, int x, int y, int width, int height)
Definition: cardbutton.cpp:15
void PaintRect(HDC hdc, RECT *rect, COLORREF colour)
Definition: cardlib.cpp:116
int yjustify
Definition: cardbutton.h:78
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
#define CS_YJUST_CENTER
Definition: cardlib.h:43
#define va_start(ap, A)
Definition: acmsvcex.h:91
unsigned int UINT
Definition: ndis.h:50
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
friend class CardButton
Definition: cardwindow.h:16
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
BOOL WINAPI PtInRect(_In_ LPCRECT, _In_ POINT)
UINT GetStyle()
Definition: cardbutton.cpp:481
BOOL WINAPI ReleaseCapture(void)
Definition: message.c:2888
CardWindow & parentWnd
Definition: cardbutton.h:68
#define GetTextExtentPoint32
Definition: wingdi.h:4467
#define SendMessage
Definition: winuser.h:5744
void Show(bool fShow)
Definition: cardbutton.cpp:375
GLenum GLuint id
Definition: glext.h:5579
LONG bottom
Definition: windef.h:294
int nNumButtons
Definition: cardwindow.h:101
void SetText(TCHAR *fmt,...)
Definition: cardbutton.cpp:359
Arabic default style
Definition: afstyles.h:93
void(CARDLIBPROC * pButtonProc)(CardButton &pButton)
Definition: cardlib.h:94
#define args
Definition: format.c:66
LONG cy
Definition: windef.h:320
void SetIcon(HICON hicon, bool fRedraw)
Definition: cardbutton.cpp:439
HANDLE mxlock
Definition: cardbutton.h:94