Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygencardbutton.cppGo to the documentation of this file.00001 // 00002 // CardLib - CardButton class 00003 // 00004 // Freeware 00005 // Copyright J Brown 2001 00006 // 00007 #include <windows.h> 00008 #include <tchar.h> 00009 00010 #include "cardlib.h" 00011 #include "cardwindow.h" 00012 #include "cardbutton.h" 00013 #include "cardcolor.h" 00014 00015 HPALETTE UseNicePalette(HDC, HPALETTE); 00016 void RestorePalette(HDC, HPALETTE); 00017 00018 void PaintRect(HDC hdc, RECT *rect, COLORREF colour); 00019 00020 CardButton::CardButton(CardWindow &parent, int Id, TCHAR *szText, UINT Style, bool visible, 00021 int x, int y, int width, int height) 00022 00023 : parentWnd(parent), id(Id), uStyle(Style), fVisible(visible), ButtonCallback(0) 00024 { 00025 crText = RGB(255,255,255); 00026 crBack = RGB(0, 128, 0); 00027 00028 xadjust = 0; 00029 yadjust = 0; 00030 xjustify = 0; 00031 yjustify = 0; 00032 00033 fMouseDown = false; 00034 fButtonDown = false; 00035 00036 hIcon = 0; 00037 00038 SetText(szText); 00039 Move(x, y, width, height); 00040 00041 mxlock = CreateMutex(0, FALSE, 0); 00042 00043 hFont = 0; 00044 } 00045 00046 CardButton::~CardButton() 00047 { 00048 CloseHandle(mxlock); 00049 } 00050 00051 void CardButton::DrawRect(HDC hdc, RECT *rect, bool fNormal) 00052 { 00053 RECT fill; 00054 00055 HANDLE hOld; 00056 00057 HPEN hhi = CreatePen(0, 0, MAKE_PALETTERGB(crHighlight)); 00058 HPEN hsh = CreatePen(0, 0, MAKE_PALETTERGB(crShadow)); 00059 HPEN hbl = (HPEN)GetStockObject(BLACK_PEN); 00060 00061 int x = rect->left; 00062 int y = rect->top; 00063 int width = rect->right-rect->left - 1; 00064 int height = rect->bottom-rect->top - 1; 00065 00066 SetRect(&fill, x+1, y+1, x+width-1, y+height-1); 00067 00068 int one = 1; 00069 00070 if(!fNormal) 00071 { 00072 x += width; 00073 y += height; 00074 width = -width; 00075 height = -height; 00076 one = -1; 00077 OffsetRect(&fill, 1, 1); 00078 } 00079 00080 if(fNormal) 00081 hOld = SelectObject(hdc, hhi); 00082 else 00083 hOld = SelectObject(hdc, hhi); 00084 00085 MoveToEx(hdc, x, y+height, 0); 00086 LineTo(hdc, x, y); 00087 LineTo(hdc, x+width, y); 00088 SelectObject(hdc, hOld); 00089 00090 hOld = SelectObject(hdc, hbl); 00091 LineTo(hdc, x+width, y+height); 00092 LineTo(hdc, x-one, y+height); 00093 SelectObject(hdc, hOld); 00094 00095 hOld = SelectObject(hdc, hsh); 00096 MoveToEx(hdc, x+one, y+height-one, 0); 00097 LineTo(hdc, x+width-one, y+height-one); 00098 LineTo(hdc, x+width-one, y); 00099 SelectObject(hdc, hOld); 00100 00101 PaintRect(hdc, &fill, MAKE_PALETTERGB(crBack)); 00102 00103 DeleteObject(hhi); 00104 DeleteObject(hsh); 00105 } 00106 00107 void CardButton::Clip(HDC hdc) 00108 { 00109 if(fVisible == false) return; 00110 00111 ExcludeClipRect(hdc, rect.left, rect.top, rect.right, rect.bottom); 00112 } 00113 00114 void CardButton::Draw(HDC hdc, bool fNormal) 00115 { 00116 SIZE textsize; 00117 int x, y; //text x, y 00118 int ix, iy; //icon x, y 00119 int iconwidth = 0; 00120 00121 RECT cliprect; 00122 00123 if(fVisible == 0) return; 00124 00125 if(hFont == 0) 00126 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT)); 00127 else 00128 SelectObject(hdc, hFont); 00129 00130 GetTextExtentPoint32(hdc, szText, lstrlen(szText), &textsize); 00131 00132 if(hIcon) 00133 { 00134 x = rect.left + 32 + 8; 00135 } 00136 else 00137 { 00138 if(uStyle & CB_ALIGN_LEFT) 00139 { 00140 x = rect.left + iconwidth; 00141 } 00142 else if(uStyle & CB_ALIGN_RIGHT) 00143 { 00144 x = rect.left + (rect.right-rect.left-iconwidth-textsize.cx); 00145 } 00146 else //centered 00147 { 00148 x = rect.right - rect.left - iconwidth; 00149 x = (x - textsize.cx) / 2; 00150 x += rect.left + iconwidth; 00151 } 00152 } 00153 00154 y = rect.bottom - rect.top; 00155 y = (y - textsize.cy) / 2; 00156 y += rect.top; 00157 00158 //calc icon position.. 00159 ix = rect.left + 4; 00160 iy = rect.top + (rect.bottom-rect.top-32) / 2; 00161 00162 //if button is pressed, then shift text 00163 if(fNormal == false && (uStyle & CB_PUSHBUTTON)) 00164 { 00165 x += 1; 00166 y += 1; 00167 ix += 1; 00168 iy += 1; 00169 } 00170 00171 SetRect(&cliprect, x, y, x+textsize.cx, y+textsize.cy); 00172 ExcludeClipRect(hdc, x, y, x+textsize.cx, y+textsize.cy); 00173 00174 // 00175 // Calc icon pos 00176 // 00177 00178 if(hIcon) 00179 { 00180 ExcludeClipRect(hdc, ix, iy, ix + 32, iy + 32); 00181 } 00182 00183 if(uStyle & CB_PUSHBUTTON) 00184 { 00185 DrawRect(hdc, &rect, fNormal); 00186 00187 SetBkColor(hdc, MAKE_PALETTERGB(crBack)); 00188 SetTextColor(hdc, crText);//MAKE_PALETTERGB(crText)); 00189 00190 SelectClipRgn(hdc, 0); 00191 00192 ExtTextOut(hdc, x, y, ETO_OPAQUE, &cliprect, szText, lstrlen(szText), 0); 00193 } 00194 else 00195 { 00196 SetBkColor(hdc, MAKE_PALETTERGB(crBack)); 00197 SetTextColor(hdc, crText);//MAKE_PALETTERGB(crText)); 00198 00199 SelectClipRgn(hdc, 0); 00200 00201 ExtTextOut(hdc, x, y, ETO_OPAQUE, &rect, szText, lstrlen(szText), 0); 00202 } 00203 00204 if(hIcon) 00205 { 00206 HBRUSH hbr = CreateSolidBrush(MAKE_PALETTERGB(crBack)); 00207 DrawIconEx(hdc, ix, iy, hIcon, 32, 32, 0, hbr, 0); 00208 DeleteObject(hbr); 00209 } 00210 00211 } 00212 00213 void CardButton::AdjustPosition(int winwidth, int winheight) 00214 { 00215 int width = rect.right-rect.left; 00216 int height = rect.bottom-rect.top; 00217 00218 width = width & ~0x1; 00219 00220 switch(xjustify) 00221 { 00222 case CS_XJUST_NONE: 00223 break; 00224 00225 case CS_XJUST_CENTER: //centered 00226 rect.left = (winwidth - (width)) / 2; 00227 rect.left += xadjust; 00228 rect.right = rect.left+width; 00229 break; 00230 00231 case CS_XJUST_RIGHT: //right-aligned 00232 rect.left = winwidth - width; 00233 rect.left += xadjust; 00234 rect.right = rect.left+width; 00235 break; 00236 } 00237 00238 switch(yjustify) 00239 { 00240 case CS_YJUST_NONE: 00241 break; 00242 00243 case CS_YJUST_CENTER: //centered 00244 rect.top = (winheight - (height)) / 2; 00245 rect.top += yadjust; 00246 rect.bottom = rect.top+height; 00247 break; 00248 00249 case CS_YJUST_BOTTOM: //right-aligned 00250 rect.top = winheight - height; 00251 rect.top += yadjust; 00252 rect.bottom = rect.top+height; 00253 break; 00254 } 00255 00256 } 00257 00258 int CardButton::OnLButtonDown(HWND hwnd, int x, int y) 00259 { 00260 if((uStyle & CB_PUSHBUTTON) == 0) 00261 return 0; 00262 00263 //make sure that the user is allowed to do something 00264 if(WaitForSingleObject(mxlock, 0) != WAIT_OBJECT_0) 00265 { 00266 return 0; 00267 } 00268 else 00269 { 00270 ReleaseMutex(mxlock); 00271 } 00272 00273 fMouseDown = true; 00274 fButtonDown = true; 00275 00276 Redraw(); 00277 00278 SetCapture(hwnd); 00279 00280 return 1; 00281 } 00282 00283 int CardButton::OnMouseMove(HWND hwnd, int x, int y) 00284 { 00285 if(fMouseDown) 00286 { 00287 bool fOldButtonDown = fButtonDown; 00288 00289 POINT pt; 00290 00291 pt.x = x; 00292 pt.y = y; 00293 00294 if(PtInRect(&rect, pt)) 00295 fButtonDown = true; 00296 else 00297 fButtonDown = false; 00298 00299 if(fButtonDown != fOldButtonDown) 00300 Redraw(); 00301 } 00302 00303 return 0; 00304 } 00305 00306 int CardButton::OnLButtonUp(HWND hwnd, int x, int y) 00307 { 00308 if(fMouseDown) 00309 { 00310 fMouseDown = false; 00311 fButtonDown = false; 00312 00313 if(uStyle & CB_PUSHBUTTON) 00314 { 00315 Redraw(); 00316 ReleaseCapture(); 00317 } 00318 00319 //if have clicked the button 00320 if(parentWnd.CardButtonFromPoint(x, y) == this) 00321 { 00322 if(ButtonCallback) 00323 { 00324 ButtonCallback(*this); 00325 } 00326 else 00327 { 00328 HWND hwnd = (HWND)parentWnd; 00329 SendMessage(GetParent(hwnd), WM_COMMAND, MAKEWPARAM(id, BN_CLICKED), (LONG_PTR)hwnd); 00330 } 00331 } 00332 } 00333 00334 return 0; 00335 } 00336 00337 //#define _countof(array) (sizeof(array)/sizeof(array[0])) 00338 00339 CardButton *CardWindow::CreateButton(int id, TCHAR *szText, UINT uStyle, bool fVisible, int x, int y, int width, int height) 00340 { 00341 CardButton *cb; 00342 00343 if(nNumButtons == MAXBUTTONS) 00344 return 0; 00345 00346 cb = new CardButton(*this, id, szText, uStyle, fVisible, x, y, width, height); 00347 Buttons[nNumButtons++] = cb; 00348 00349 if(uStyle & CB_PUSHBUTTON) 00350 { 00351 cb->SetBackColor(CardButton::GetFace(crBackgnd)); 00352 //cb->SetBackColor(ScaleLumRGB(crBackgnd, 0.1)); 00353 cb->SetForeColor(RGB(255,255,255)); 00354 } 00355 else 00356 { 00357 cb->SetBackColor(crBackgnd); 00358 cb->SetForeColor(RGB(255,255,255)); 00359 } 00360 00361 return cb; 00362 } 00363 00364 void CardButton::SetText(TCHAR *lpszFormat, ...) 00365 { 00366 int count; 00367 00368 va_list args; 00369 va_start(args, lpszFormat); 00370 00371 count = wvsprintf(szText, lpszFormat, args); 00372 va_end(args); 00373 } 00374 00375 int CardButton::Id() 00376 { 00377 return id; 00378 } 00379 00380 void CardButton::Show(bool fShow) 00381 { 00382 fVisible = fShow; 00383 } 00384 00385 void CardButton::Move(int x, int y, int width, int height) 00386 { 00387 SetRect(&rect, x, y, x+width, y+height); 00388 } 00389 00390 void CardButton::Redraw() 00391 { 00392 HDC hdc = GetDC((HWND)parentWnd); 00393 00394 HPALETTE hOldPal = UseNicePalette(hdc, __hPalette); 00395 00396 Draw(hdc, !fButtonDown); 00397 00398 RestorePalette(hdc, hOldPal); 00399 00400 ReleaseDC((HWND)parentWnd, hdc); 00401 } 00402 00403 void CardButton::SetForeColor(COLORREF cr) 00404 { 00405 crText = cr; 00406 } 00407 00408 void CardButton::SetBackColor(COLORREF cr) 00409 { 00410 crBack = cr; 00411 00412 crHighlight = GetHighlight(cr); 00413 crShadow = GetShadow(cr); 00414 00415 //crHighlight = ScaleLumRGB(cr, +0.25); 00416 //crShadow = ScaleLumRGB(cr, -0.25); 00417 } 00418 00419 // Static member 00420 COLORREF CardButton::GetHighlight(COLORREF crBase) 00421 { 00422 return ColorScaleRGB(crBase, RGB(255,255,255), 0.25); 00423 } 00424 00425 // Static member 00426 COLORREF CardButton::GetShadow(COLORREF crBase) 00427 { 00428 return ColorScaleRGB(crBase, RGB(0, 0, 0), 0.25); 00429 } 00430 00431 COLORREF CardButton::GetFace(COLORREF crBase) 00432 { 00433 return ColorScaleRGB(crBase, RGB(255,255,255), 0.1); 00434 } 00435 00436 void CardButton::SetPlacement(UINT xJustify, UINT yJustify, int xAdjust, int yAdjust) 00437 { 00438 xadjust = xAdjust; 00439 yadjust = yAdjust; 00440 xjustify = xJustify; 00441 yjustify = yJustify; 00442 } 00443 00444 void CardButton::SetIcon(HICON hicon, bool fRedraw) 00445 { 00446 hIcon = hicon; 00447 00448 if(fRedraw) 00449 Redraw(); 00450 } 00451 00452 void CardButton::SetFont(HFONT font) 00453 { 00454 //don't delete the existing font.. 00455 hFont = font; 00456 } 00457 00458 void CardButton::SetButtonProc(pButtonProc proc) 00459 { 00460 ButtonCallback = proc; 00461 } 00462 00463 bool CardButton::Lock() 00464 { 00465 DWORD dw = WaitForSingleObject(mxlock, 0); 00466 00467 if(dw == WAIT_OBJECT_0) 00468 return true; 00469 else 00470 return false; 00471 } 00472 00473 bool CardButton::UnLock() 00474 { 00475 if(ReleaseMutex(mxlock)) 00476 return true; 00477 else 00478 return false; 00479 } 00480 00481 void CardButton::SetStyle(UINT style) 00482 { 00483 uStyle = style; 00484 } 00485 00486 UINT CardButton::GetStyle() 00487 { 00488 return uStyle; 00489 } Generated on Thu Feb 9 04:59:24 2012 for ReactOS by
1.6.3
|