1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
//===============================================================
// vbtncmd.cpp - button Cmd - Windows
//
// Copyright (C) 1995-1998 Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================
#include <v/vwin32.h> // for Win 32 stuff
#include <v/vbtncmd.h> // our definitions
#include <v/vcmdprnt.h> // a command parent
#include <v/vutil.h>
#include <v/vicon.h>
#include <v/vapp.h>
//====================>>> vButtonCmd::vButtonCmd <<<=======================
vButtonCmd::vButtonCmd(vCmdParent* dp, CommandObject* dc) :
vCmd(dp, dc)
{
initialize();
}
//====================>>> vButtonCmd::~vButtonCmd <<<=======================
vButtonCmd::~vButtonCmd()
{
SysDebug(Destructor,"vButtonCmd::~vButtonCmd() destructor\n")
}
//=====================>>> vButtonCmd::initialize <<<=======================
void vButtonCmd::initialize()
{
// build a button command for use in a parent window
SysDebug(Constructor,"vButtonCmd::vButtonCmd() constructor\n")
long style;
if (_parentWin->_dialogType != aCmdBar)
style = WS_TABSTOP | WS_GROUP; // default for a button
else
style = WS_GROUP;
CopyToLocal(); // Make local copies of CmdObject
if (!(dlgCmd->attrs & CA_Hidden)) // Check for Hidden
style |= WS_VISIBLE;
if ((dlgCmd->cmdType == C_IconButton || dlgCmd->cmdType == C_ToggleIconButton)
&& _itemList != 0) // icon
{
initIconButton(style); // Special init if icon
return;
}
if (dlgCmd->cmdType == C_ColorButton) // need owner draw if color
style |= BS_OWNERDRAW;
else if (dlgCmd->cmdType == C_ToggleButton) // need owner draw if toggle
style |= BS_OWNERDRAW;
else if ((dlgCmd->attrs & CA_DefaultButton)) // default button?
style |= BS_DEFPUSHBUTTON; // Only regular buttons can be default
else
style |= BS_PUSHBUTTON; // Plain old button
_w = vLblLen(_title)*4+10; // set my width
_h = 14; // default height
if ((dlgCmd->cmdType == C_Button || dlgCmd->cmdType == C_ToggleButton)
&& dlgCmd->size > 0) // size - width
{
int w = vLblLen(_title)*4+10;
if (w < dlgCmd->size)
{
_w = dlgCmd->size;
}
}
else if (dlgCmd->size > 0) // size - square (icons, color buttons)
{
_w = _h = dlgCmd->size;
}
_parentWin->SetPosition(_x, _y, _w, _h, dlgCmd->cFrame, dlgCmd->cRightOf,
dlgCmd->cBelow);
_CtrlOffset = _parentWin->AddDlgControl(_x, _y, _w, _h, _cmdId,
style, "BUTTON", _title, sizeof(vButtonCmd*), (LPBYTE)this);
}
//=====================>>> vButtonCmd::initIconButton <<<====================
void vButtonCmd::initIconButton(long style)
{
_ip = (vIcon *) _itemList; // to access bitmap
style |= BS_OWNERDRAW;
#ifdef OLD
_h = (_ip->height / 2) + 6;
#ifndef VWIN16
_w = (_ip->width / 2) + 8;
#else
_w = (_ip->width / 2) + 4;
#endif
#else
_h = (_ip->height / 2) + 5;
_w = (_ip->width / 2) + 5;
#endif
_parentWin->SetPosition(_x, _y, _w, _h, dlgCmd->cFrame,
dlgCmd->cRightOf, dlgCmd->cBelow);
_CtrlOffset = _parentWin->AddDlgControl(_x, _y , _w, _h, _cmdId,
style, "BUTTON", "", sizeof(vButtonCmd*), (LPBYTE)this);
}
//==================>>> vButtonCmd::GetCmdValue <<<=========================
int vButtonCmd::GetCmdValue(ItemVal id) VCONST
{
if (id != _cmdId)
return -1;
return _retVal;
}
//================>>> vButtonCmd::ResetItemValue <<<======================
void vButtonCmd::ResetItemValue(void)
{
// We have to toggle things
if (dlgCmd->cmdType != C_ToggleIconButton &&
dlgCmd->cmdType != C_ToggleButton)
return;
if (_retVal == _origVal) // No op if no change
return;
HWND myHwnd = GetMyHwnd(_cmdId);
_retVal = _origVal; // restore
::ShowWindow(myHwnd,SW_HIDE); // trick into
::ShowWindow(myHwnd,SW_SHOW); // repaint the window
// let parent window handle now
_parentWin->ProcessCmd(_cmdId, _retVal, dlgCmd->cmdType);
}
//================>>> vButtonCmd::SetCmdVal <<<============================
void vButtonCmd::SetCmdVal(ItemVal val, ItemSetType st)
{
SysDebug1(Misc,"vButtonCmd::SetCmdVal(val:%d)\n",val)
HWND myHwnd = GetMyHwnd(_cmdId);
if (st == Sensitive)
{
_Sensitive = val; // set
::EnableWindow(myHwnd, val);
}
else if (st == Hidden) // hide or unhide
{
if (val)
{
::ShowWindow(myHwnd,SW_HIDE);
}
else
{
::ShowWindow(myHwnd,SW_SHOW);
}
}
else if (st == Value || st == Checked)
{
_retVal = val; // set
::ShowWindow(myHwnd,SW_HIDE); // this tricks it into always
::ShowWindow(myHwnd,SW_SHOW); // repainting.
}
}
//================>>> vButtonCmd::SetCmdStr <<<============================
void vButtonCmd::SetCmdStr(VCONST char* str)
{
// Set the string value of button
SysDebug1(Misc,"vButtonCmd::SetCmdStr(str:%s)\n",str)
::SetDlgItemText(_parentWin->getParent(),_cmdId, str);
_title = str;
}
//================>>> vButtonCmd::MEASUREITEM <<<============================
int vButtonCmd::MEASUREITEM(int id, MEASUREITEMSTRUCT* mis)
{
mis->CtlType = ODT_BUTTON;
mis->CtlID = id;
mis->itemWidth = _w;
mis->itemHeight = _h;
return 1;
}
//================>>> vButtonCmd::DRAWITEM <<<============================
int vButtonCmd::DRAWITEM(int /* id */, DRAWITEMSTRUCT* dis)
{
HBITMAP hbm;
switch (dis->itemAction)
{
case ODA_DRAWENTIRE: // redraw entire control
{
DRAW_NORMAL:
if (dlgCmd->cmdType == C_IconButton ||
dlgCmd->cmdType == C_ToggleIconButton) //Draw the icon
{
POINT ptSize, ptOrg;
// Draw the button rectangle in LTGRAY with a BLACK border
HPEN oldpen = (HPEN) ::SelectObject(dis->hDC, ::GetStockObject(BLACK_PEN));
HBRUSH oldbrush = static_cast<HBRUSH>(::SelectObject(dis->hDC, theApp->_BarBrush));
(void)::Rectangle(dis->hDC,dis->rcItem.left, dis->rcItem.top,
dis->rcItem.right, dis->rcItem.bottom);
// Draw the shadows - WHITE left, top, GRAY right, bottom
if (dlgCmd->cmdType == C_IconButton)
::SelectObject(dis->hDC, theApp->_LightPen);
else if (_retVal == 0)
::SelectObject(dis->hDC, theApp->_LightPen);
else
::SelectObject(dis->hDC,theApp->_ShadowPen);
::MoveTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom);
::LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
::LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
::MoveTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom+1);
::LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.top+1);
if (dlgCmd->cmdType == C_IconButton || _retVal == 0)
::SelectObject(dis->hDC,theApp->_ShadowPen);
else
::SelectObject(dis->hDC, theApp->_LightPen);
::MoveTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.bottom-1);
::LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-1);
::MoveTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.bottom-2);
::LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom-2);
// Get color to draw icon - GRAY or BLACK
HBRUSH dbrush = 0;
if (dis->itemState & ODS_DISABLED)
{
// This stuff needed to be sure it shows
if (::GetSysColor(COLOR_BTNSHADOW) == ::GetSysColor(COLOR_BTNFACE))
{
::SelectObject(dis->hDC, ::GetStockObject(GRAY_BRUSH));
}
else
{
dbrush = ::CreateSolidBrush(::GetSysColor(COLOR_BTNSHADOW));
::SelectObject(dis->hDC, dbrush);
}
}
else
{
if (_ip->iType == Transparent)
{
// Prepare for BitBlt with raster operation of PSDPxax, i.e.
// paint with the brush where the color of the pixel on the
// destination bitmap is zero
dbrush = ::CreateSolidBrush(::GetSysColor(COLOR_BTNFACE));
::SelectObject(dis->hDC, dbrush);
}
else
{
::SelectObject(dis->hDC, ::GetStockObject(BLACK_BRUSH));
}
}
// Now setup coordinate to draw icon
HDC hdcMem = ::CreateCompatibleDC(dis->hDC);
::SetMapMode(hdcMem, GetMapMode(dis->hDC));
hbm = _ip->GetIconHBM(dis->hDC); // get icon HBITMAP
HBITMAP oldbm = static_cast<HBITMAP>(::SelectObject(hdcMem,hbm));
ptSize.x = _ip->width;
ptSize.y = _ip->height;
::DPtoLP(dis->hDC, &ptSize, 1);
ptOrg.x = 0;
ptOrg.y = 0;
::DPtoLP(hdcMem, &ptOrg, 1);
int l = (dis->rcItem.right - _ip->width) / 2;
int t = (dis->rcItem.bottom - _ip->height) / 2;
// Now BitBlt the icon BMP into the drawing canvas, leaving
// background untouched, and the pen color to draw 1 bits
// (that's what the E20746L does) for monochrome
if (_ip->depth == 1)
{
::SetBkColor(dis->hDC, RGB(255,255,255)); // white bg for icon
::BitBlt(dis->hDC, l, t, ptSize.x,
ptSize.y, hdcMem, ptOrg.x, ptOrg.y, 0xE20746L);
}
else
{
::BitBlt(dis->hDC, l, t, ptSize.x,
ptSize.y, hdcMem, ptOrg.x, ptOrg.y, SRCCOPY);
if (_ip->iType == Transparent)
{
// Now restore area from the button where the mask is zero
HDC hdcMemMask = ::CreateCompatibleDC( dis->hDC );
HBITMAP hmsk = static_cast<HBITMAP>(_ip->GetIconMask( dis->hDC)); // get icon mask HBITMAP
HBITMAP oldBM = static_cast<HBITMAP>(::SelectObject( hdcMemMask, hmsk ));
::BitBlt( dis->hDC, l, t, ptSize.x,
ptSize.y, hdcMemMask, ptOrg.x, ptOrg.y, 0xB8074A); //PSDPxax
::SelectObject( hdcMemMask, oldBM );
::DeleteDC( hdcMemMask );
}
}
::SelectObject(hdcMem,oldbm);
::DeleteDC(hdcMem);
::SelectObject(dis->hDC, oldpen); // restore hdc
::SelectObject(dis->hDC, oldbrush);
if (dbrush) ::DeleteObject(dbrush);
}
else if (dlgCmd->cmdType == C_ToggleButton)
{
// Draw a toggle button
HPEN pen;
if (dis->itemState & ODS_DISABLED)
pen = (HPEN) ::GetStockObject(WHITE_PEN);
else
pen = (HPEN) ::GetStockObject(BLACK_PEN);
// Set to the new brush color, remember the old one
HPEN oldpen = (HPEN)::SelectObject(dis->hDC, pen);
// Draw the button with a black border
(void)::Rectangle(dis->hDC,dis->rcItem.left, dis->rcItem.top,
dis->rcItem.right, dis->rcItem.bottom);
// Draw text for the Toggle button if it has it
if (*_title )
{
int tl = strlen(_title); // This little kludge
int tblanks = 0; // prevents drawing
for (VCONST char* tp = _title ; *tp ; ++tp) // all-blank titles
{ // which can leave
if (*tp == ' ') // an ugly "pure color"
++tblanks; // block in the middle
} // of the button.
if (tblanks != tl)
{
RECT rc = dis->rcItem;
if (_retVal)
{
rc.left += 2; rc.top += 2; // shift text
}
::DrawText(dis->hDC, _title, strlen(_title),
&rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
// Now draw the shadow around the button
if (_retVal == 0)
::SelectObject(dis->hDC, theApp->_LightPen);
else
::SelectObject(dis->hDC,theApp->_ShadowPen);
::MoveTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom);
::LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
::LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top);
::MoveTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom+1);
::LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.top+1);
if (_retVal == 0)
::SelectObject(dis->hDC,theApp->_ShadowPen);
else
::SelectObject(dis->hDC, theApp->_LightPen);
::MoveTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.bottom-1);
::LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom-1);
::MoveTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-2, dis->rcItem.bottom-2);
::LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom-2);
::SelectObject(dis->hDC, oldpen); // restore hdc
}
return 1;
}
case ODA_SELECT: // indicated selected
{
if (dlgCmd->cmdType == C_IconButton ||
dlgCmd->cmdType == C_ToggleIconButton)
{
if (dis->itemState & ODS_SELECTED) //Draw the icon
{
POINT ptSize, ptOrg;
// Draw the button rectangle in LTGRAY with a BLACK border
HBRUSH fbrush = ::CreateSolidBrush(
::GetSysColor(COLOR_BTNFACE));
HPEN oldpen = (HPEN) ::SelectObject(dis->hDC,
::GetStockObject(BLACK_PEN));
HBRUSH oldbrush = static_cast<HBRUSH>(::SelectObject(dis->hDC, fbrush));
(void)::Rectangle(dis->hDC,dis->rcItem.left,
dis->rcItem.top, dis->rcItem.right, dis->rcItem.bottom);
// Draw the shadows - GRAY left, top, No right, bottom
::SelectObject(dis->hDC, theApp->_ShadowPen);
::MoveTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom);
::LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
::LineTo(dis->hDC, dis->rcItem.right, dis->rcItem.top);
::MoveTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom-1);
::LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top+1);
// Now setup coordinate to draw icon
HDC hdcMem = ::CreateCompatibleDC(dis->hDC);
::SetMapMode(hdcMem, GetMapMode(dis->hDC));
hbm = _ip->GetIconHBM(hdcMem); // get icon HBITMAP
HBITMAP oldbm = static_cast<HBITMAP>(::SelectObject(hdcMem,hbm));
ptSize.x = _ip->width;
ptSize.y = _ip->height;
DPtoLP(dis->hDC, &ptSize, 1);
ptOrg.x = 0;
ptOrg.y = 0;
DPtoLP(hdcMem, &ptOrg, 1);
::SetBkColor(dis->hDC, RGB(255,255,255)); // white bg for icon
::SelectObject(dis->hDC, ::GetStockObject(BLACK_BRUSH));
// Now BitBlt the icon BMP into the drawing canvas, leaving
// background untouched, and the pen color to draw 1 bits
// (that's what the E20746L does) (shifted 1 from normal icon)
int l = (dis->rcItem.right - _ip->width) / 2;
int t = (dis->rcItem.bottom - _ip->height) / 2;
::BitBlt(dis->hDC, l+1, t+1, ptSize.x,
ptSize.y, hdcMem, ptOrg.x, ptOrg.y, 0xE20746L);
::SelectObject(hdcMem,oldbm);
::DeleteDC(hdcMem);
::SelectObject(dis->hDC, oldpen); // restore hdc
::SelectObject(dis->hDC, oldbrush);
::DeleteObject(fbrush);
return 1;
}
else
goto DRAW_NORMAL;
}
else if (dlgCmd->cmdType == C_ToggleButton)
{
// nearly the same as regular, but don't worry about
// insensitive and draw inverted shadow
if (dis->itemState & ODS_SELECTED) //Draw the icon
{
// Draw the button rectangle in LTGRAY with a BLACK border
HBRUSH fbrush = ::CreateSolidBrush(
::GetSysColor(COLOR_BTNFACE));
HPEN oldpen = (HPEN) ::SelectObject(dis->hDC,
::GetStockObject(BLACK_PEN));
HBRUSH oldbrush = static_cast<HBRUSH>(::SelectObject(dis->hDC, fbrush));
(void)::Rectangle(dis->hDC,dis->rcItem.left,
dis->rcItem.top, dis->rcItem.right, dis->rcItem.bottom);
if (*_title)
{
RECT rc = dis->rcItem;
rc.left += 2; rc.top += 2; // shift text
::DrawText(dis->hDC, _title, strlen(_title),
&rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
::SelectObject(dis->hDC, theApp->_ShadowPen);
::MoveTo(dis->hDC, dis->rcItem.left, dis->rcItem.bottom);
::LineTo(dis->hDC, dis->rcItem.left, dis->rcItem.top);
::LineTo(dis->hDC, dis->rcItem.right, dis->rcItem.top);
::MoveTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.bottom+1);
::LineTo(dis->hDC, dis->rcItem.left+1, dis->rcItem.top+1);
::LineTo(dis->hDC, dis->rcItem.right-1, dis->rcItem.top+1);
::SelectObject(dis->hDC, oldpen); // restore hdc
::SelectObject(dis->hDC, oldbrush);
::DeleteObject(fbrush);
return 1;
}
else
goto DRAW_NORMAL;
}
return 1; // no op for icon label
}
case ODA_FOCUS: // gets focus
{
return 1; // no op for icon label
}
default:
break;
}
return 0;
}
//====================>>> vButtondCmd::CmdCallback <<<=======================
void vButtonCmd::CmdCallback(int /* id */, WORD /* codeNotify */)
{
if (dlgCmd->cmdType == C_ToggleIconButton || // depends on state
dlgCmd->cmdType == C_ToggleButton)
{
SetCmdVal(!_retVal, Checked);
}
// let parent window handle now
_parentWin->ProcessCmd(_cmdId, _retVal, dlgCmd->cmdType);
}
|