ReactOS 0.4.15-dev-7953-g1f49173
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
10HPALETTE UseNicePalette(HDC, HPALETTE);
11void RestorePalette(HDC, HPALETTE);
12
13void 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
34 Move(x, y, width, height);
35
36 mxlock = CreateMutex(0, FALSE, 0);
37
38 hFont = 0;
39}
40
42{
44}
45
46void 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);
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
109void 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 {
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
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 {
202 DrawIconEx(hdc, ix, iy, hIcon, 32, 32, 0, hbr, 0);
203 DeleteObject(hbr);
204 }
205
206}
207
208void 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
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
309 {
310 Redraw();
312 }
313
314 //if have clicked the button
315 if(parentWnd.CardButtonFromPoint(x, y) == this)
316 {
318 {
319 ButtonCallback(*this);
320 }
321 else
322 {
325 }
326 }
327 }
328
329 return 0;
330}
331
332//#define _countof(array) (sizeof(array)/sizeof(array[0]))
333
334CardButton *CardWindow::CreateButton(int id, TCHAR *szText, UINT uStyle, bool fVisible, int x, int y, int width, int height)
335{
336 CardButton *cb;
337
339 return 0;
340
341 cb = new CardButton(*this, id, szText, uStyle, fVisible, x, y, width, height);
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
359void CardButton::SetText(TCHAR *lpszFormat, ...)
360{
361 int count;
362
364 va_start(args, lpszFormat);
365
366 count = wvsprintf(szText, lpszFormat, args);
367 va_end(args);
368}
369
371{
372 return id;
373}
374
375void CardButton::Show(bool fShow)
376{
377 fVisible = fShow;
378}
379
380void CardButton::Move(int x, int y, int width, int height)
381{
382 SetRect(&rect, x, y, x+width, y+height);
383}
384
386{
388
389 HPALETTE hOldPal = UseNicePalette(hdc, __hPalette);
390
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
431void 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
439void 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{
471 return true;
472 else
473 return false;
474}
475
477{
478 uStyle = style;
479}
480
482{
483 return uStyle;
484}
DWORD Id
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
Arabic default style
Definition: afstyles.h:94
const DWORD Style
Definition: appswitch.c:71
void PaintRect(HDC hdc, RECT *rect, COLORREF col)
Definition: cardlib.cpp:116
void RestorePalette(HDC, HPALETTE)
HPALETTE UseNicePalette(HDC, HPALETTE)
Definition: cardwindow.cpp:14
void PaintRect(HDC hdc, RECT *rect, COLORREF colour)
Definition: cardlib.cpp:116
COLORREF ColorScaleRGB(const COLORREF Col1, const COLORREF Col2, const double Ratio)
Definition: cardcolor.cpp:154
#define MAKE_PALETTERGB(colref)
Definition: cardcolor.h:17
HPALETTE __hPalette
Definition: cardlib.cpp:28
#define CS_YJUST_BOTTOM
Definition: cardlib.h:42
void(CARDLIBPROC * pButtonProc)(CardButton &pButton)
Definition: cardlib.h:94
#define CB_ALIGN_LEFT
Definition: cardlib.h:48
#define CB_ALIGN_RIGHT
Definition: cardlib.h:49
#define CS_XJUST_CENTER
Definition: cardlib.h:39
#define CS_XJUST_RIGHT
Definition: cardlib.h:38
#define CB_PUSHBUTTON
Definition: cardlib.h:46
#define CS_YJUST_CENTER
Definition: cardlib.h:43
#define CS_YJUST_NONE
Definition: cardlib.h:41
#define CS_XJUST_NONE
Definition: cardlib.h:37
#define MAXBUTTONS
Definition: cardwindow.h:4
void SetPlacement(UINT xJustify, UINT yJustify, int xAdjust, int yAdjust)
Definition: cardbutton.cpp:431
void Show(bool fShow)
Definition: cardbutton.cpp:375
void SetForeColor(COLORREF cr)
Definition: cardbutton.cpp:398
COLORREF crText
Definition: cardbutton.h:86
void SetFont(HFONT font)
Definition: cardbutton.cpp:447
TCHAR szText[MAXBUTTONTEXT]
Definition: cardbutton.h:83
int OnMouseMove(HWND hwnd, int x, int y)
Definition: cardbutton.cpp:278
pButtonProc ButtonCallback
Definition: cardbutton.h:96
RECT rect
Definition: cardbutton.h:70
HICON hIcon
Definition: cardbutton.h:80
static COLORREF GetHighlight(COLORREF crBase)
Definition: cardbutton.cpp:415
CardWindow & parentWnd
Definition: cardbutton.h:68
void SetIcon(HICON hicon, bool fRedraw)
Definition: cardbutton.cpp:439
void SetText(TCHAR *fmt,...)
Definition: cardbutton.cpp:359
HANDLE mxlock
Definition: cardbutton.h:94
COLORREF crBack
Definition: cardbutton.h:85
int yadjust
Definition: cardbutton.h:77
int xjustify
Definition: cardbutton.h:76
UINT GetStyle()
Definition: cardbutton.cpp:481
int yjustify
Definition: cardbutton.h:78
static COLORREF GetShadow(COLORREF crBase)
Definition: cardbutton.cpp:421
void DrawRect(HDC hdc, RECT *rect, bool fNormal)
Definition: cardbutton.cpp:46
bool Lock()
Definition: cardbutton.cpp:458
UINT uStyle
Definition: cardbutton.h:72
int OnLButtonUp(HWND hwnd, int x, int y)
Definition: cardbutton.cpp:301
void SetBackColor(COLORREF cr)
Definition: cardbutton.cpp:403
void Redraw()
Definition: cardbutton.cpp:385
CardButton(CardWindow &parent, int id, TCHAR *szText, UINT style, bool visible, int x, int y, int width, int height)
Definition: cardbutton.cpp:15
void Draw(HDC hdc, bool fNormal)
Definition: cardbutton.cpp:109
void AdjustPosition(int winwidth, int winheight)
Definition: cardbutton.cpp:208
bool fMouseDown
Definition: cardbutton.h:91
static COLORREF GetFace(COLORREF crBase)
Definition: cardbutton.cpp:426
bool fButtonDown
Definition: cardbutton.h:92
void Clip(HDC hdc)
Definition: cardbutton.cpp:102
int OnLButtonDown(HWND hwnd, int x, int y)
Definition: cardbutton.cpp:253
void SetButtonProc(pButtonProc proc)
Definition: cardbutton.cpp:453
bool fVisible
Definition: cardbutton.h:73
COLORREF crShadow
Definition: cardbutton.h:88
void SetStyle(UINT uStyle)
Definition: cardbutton.cpp:476
COLORREF crHighlight
Definition: cardbutton.h:87
bool UnLock()
Definition: cardbutton.cpp:468
HFONT hFont
Definition: cardbutton.h:81
int xadjust
Definition: cardbutton.h:75
void Move(int x, int y, int width, int height)
Definition: cardbutton.cpp:380
friend class CardButton
Definition: cardwindow.h:16
CardButton * Buttons[MAXBUTTONS]
Definition: cardwindow.h:100
COLORREF crBackgnd
Definition: cardwindow.h:109
CardButton * CardButtonFromPoint(int x, int y)
Definition: cardwindow.cpp:229
int nNumButtons
Definition: cardwindow.h:101
CardButton * CreateButton(int id, TCHAR *szText, UINT uStyle, bool fVisible, int x, int y, int width, int height)
Definition: cardbutton.cpp:334
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
HANDLE HWND
Definition: compat.h:19
#define pt(x, y)
Definition: drawing.c:79
#define RGB(r, g, b)
Definition: precomp.h:71
r parent
Definition: btrfs.c:3010
unsigned long DWORD
Definition: ntddk_ex.h:95
pKey DeleteObject()
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLint GLint GLsizei GLsizei height
Definition: gl.h:1546
GLint GLint GLsizei width
Definition: gl.h:1546
GLuint id
Definition: glext.h:5910
REFIID LPVOID DWORD_PTR dw
Definition: atlbase.h:40
HDC hdc
Definition: main.c:9
static HDC
Definition: imagelist.c:92
static HICON
Definition: imagelist.c:84
static DWORD *static HFONT(WINAPI *pCreateFontIndirectExA)(const ENUMLOGFONTEXDVA *)
static HMODULE MODULEINFO DWORD cb
Definition: module.c:33
__int3264 LONG_PTR
Definition: mstsclib_h.h:276
Definition: mk_font.cpp:20
unsigned int UINT
Definition: ndis.h:50
static HANDLE proc()
Definition: pdb.c:34
#define args
Definition: format.c:66
int one
Definition: sehframes.cpp:28
& rect
Definition: startmenu.cpp:1413
LONG cx
Definition: kdterminal.h:27
LONG cy
Definition: kdterminal.h:28
Definition: match.c:390
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex(IN HANDLE hMutex)
Definition: synch.c:618
int iy
Definition: tritemp.h:491
#define CreateMutex
Definition: winbase.h:3756
#define WAIT_OBJECT_0
Definition: winbase.h:406
#define lstrlen
Definition: winbase.h:3876
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
DWORD COLORREF
Definition: windef.h:300
HGDIOBJ WINAPI GetStockObject(_In_ int)
COLORREF WINAPI SetBkColor(_In_ HDC, _In_ COLORREF)
Definition: dc.c:999
#define GetTextExtentPoint32
Definition: wingdi.h:4472
int WINAPI ExcludeClipRect(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int)
#define DEFAULT_GUI_FONT
Definition: wingdi.h:909
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1539
BOOL WINAPI MoveToEx(_In_ HDC, _In_ int, _In_ int, _Out_opt_ LPPOINT)
#define ExtTextOut
Definition: wingdi.h:4454
#define ETO_OPAQUE
Definition: wingdi.h:647
#define BLACK_PEN
Definition: wingdi.h:903
COLORREF WINAPI SetTextColor(_In_ HDC, _In_ COLORREF)
Definition: text.c:918
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
HPEN WINAPI CreatePen(_In_ int, _In_ int, _In_ COLORREF)
int WINAPI SelectClipRgn(_In_ HDC, _In_opt_ HRGN)
BOOL WINAPI LineTo(_In_ HDC, _In_ int, _In_ int)
HWND WINAPI SetCapture(_In_ HWND hWnd)
int WINAPI ReleaseDC(_In_opt_ HWND, _In_ HDC)
#define MAKEWPARAM(l, h)
Definition: winuser.h:4009
#define wvsprintf
Definition: winuser.h:5866
BOOL WINAPI ReleaseCapture(void)
Definition: message.c:2890
#define WM_COMMAND
Definition: winuser.h:1740
BOOL WINAPI PtInRect(_In_ LPCRECT, _In_ POINT)
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:2028
#define SendMessage
Definition: winuser.h:5843
HDC WINAPI GetDC(_In_opt_ HWND)
HWND WINAPI GetParent(_In_ HWND)
BOOL WINAPI OffsetRect(_Inout_ LPRECT, _In_ int, _In_ int)
#define BN_CLICKED
Definition: winuser.h:1925
BOOL WINAPI SetRect(_Out_ LPRECT, _In_ int, _In_ int, _In_ int, _In_ int)
char TCHAR
Definition: xmlstorage.h:189