| 1 | /* |
| 2 | * Copyright 2006-2010, Haiku. |
| 3 | * Distributed under the terms of the MIT License. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Stephan Aßmus <superstippi@gmx.de> |
| 7 | */ |
| 8 | |
| 9 | // NOTE: this file is a duplicate of the version in Icon-O-Matic/generic |
| 10 | // it should be placed into a common folder for generic useful stuff |
| 11 | |
| 12 | #include "IconButton.h" |
| 13 | |
| 14 | #include <new> |
| 15 | #include <stdio.h> |
| 16 | |
| 17 | #include <Application.h> |
| 18 | #include <Bitmap.h> |
| 19 | #include <Control.h> |
| 20 | #include <ControlLook.h> |
| 21 | #include <Entry.h> |
| 22 | #include <Looper.h> |
| 23 | #include <Message.h> |
| 24 | #include <Mime.h> |
| 25 | #include <Path.h> |
| 26 | #include <Region.h> |
| 27 | #include <Resources.h> |
| 28 | #include <Roster.h> |
| 29 | #include <TranslationUtils.h> |
| 30 | #include <Window.h> |
| 31 | #include "IconUtils.h" |
| 32 | |
| 33 | using std::nothrow; |
| 34 | |
| 35 | // constructor |
| 36 | IconButton::IconButton(const char* name, uint32 id, uint32 size, |
| 37 | const char* label, BMessage* message, BHandler* target) |
| 38 | : |
| 39 | BView(name, B_WILL_DRAW), |
| 40 | BInvoker(message, target), |
| 41 | fButtonState(STATE_ENABLED), |
| 42 | fID(id), |
| 43 | fSize(size), |
| 44 | fNormalBitmap(NULL), |
| 45 | fDisabledBitmap(NULL), |
| 46 | fClickedBitmap(NULL), |
| 47 | fDisabledClickedBitmap(NULL), |
| 48 | fLabel(label), |
| 49 | fTargetCache(target) |
| 50 | { |
| 51 | SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); |
| 52 | } |
| 53 | |
| 54 | // destructor |
| 55 | IconButton::~IconButton() |
| 56 | { |
| 57 | _DeleteBitmaps(); |
| 58 | } |
| 59 | |
| 60 | // MessageReceived |
| 61 | void |
| 62 | IconButton::MessageReceived(BMessage* message) |
| 63 | { |
| 64 | switch (message->what) { |
| 65 | default: |
| 66 | BView::MessageReceived(message); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // AttachedToWindow |
| 72 | void |
| 73 | IconButton::AttachedToWindow() |
| 74 | { |
| 75 | rgb_color background = B_TRANSPARENT_COLOR; |
| 76 | if (BView* parent = Parent()) { |
| 77 | background = parent->ViewColor(); |
| 78 | if (background == B_TRANSPARENT_COLOR) |
| 79 | background = parent->LowColor(); |
| 80 | } |
| 81 | if (background == B_TRANSPARENT_COLOR) |
| 82 | background = ui_color(B_PANEL_BACKGROUND_COLOR); |
| 83 | SetLowColor(background); |
| 84 | |
| 85 | SetTarget(fTargetCache); |
| 86 | if (!Target()) |
| 87 | SetTarget(Window()); |
| 88 | } |
| 89 | |
| 90 | // Draw |
| 91 | void |
| 92 | IconButton::Draw(BRect area) |
| 93 | { |
| 94 | rgb_color background = LowColor(); |
| 95 | |
| 96 | BRect r(Bounds()); |
| 97 | |
| 98 | uint32 flags = 0; |
| 99 | BBitmap* bitmap = fNormalBitmap; |
| 100 | if (!IsEnabled()) { |
| 101 | flags |= BControlLook::B_DISABLED; |
| 102 | bitmap = fDisabledBitmap; |
| 103 | } |
| 104 | if (_HasFlags(STATE_PRESSED) || _HasFlags(STATE_FORCE_PRESSED)) |
| 105 | flags |= BControlLook::B_ACTIVATED; |
| 106 | |
| 107 | if (DrawBorder()) { |
| 108 | be_control_look->DrawButtonFrame(this, r, area, background, |
| 109 | background, flags); |
| 110 | be_control_look->DrawButtonBackground(this, r, area, background, |
| 111 | flags); |
| 112 | } |
| 113 | |
| 114 | if (bitmap && bitmap->IsValid()) { |
| 115 | float x = r.left + floorf((r.Width() |
| 116 | - bitmap->Bounds().Width()) / 2.0 + 0.5); |
| 117 | float y = r.top + floorf((r.Height() |
| 118 | - bitmap->Bounds().Height()) / 2.0 + 0.5); |
| 119 | BPoint point(x, y); |
| 120 | if (_HasFlags(STATE_PRESSED) || _HasFlags(STATE_FORCE_PRESSED)) |
| 121 | point += BPoint(1.0, 1.0); |
| 122 | if (bitmap->ColorSpace() == B_RGBA32 |
| 123 | || bitmap->ColorSpace() == B_RGBA32_BIG) { |
| 124 | SetDrawingMode(B_OP_ALPHA); |
| 125 | SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY); |
| 126 | } |
| 127 | DrawBitmap(bitmap, point); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // MouseDown |
| 132 | void |
| 133 | IconButton::MouseDown(BPoint where) |
| 134 | { |
| 135 | if (!IsValid()) |
| 136 | return; |
| 137 | |
| 138 | if (_HasFlags(STATE_ENABLED)/* && !_HasFlags(STATE_FORCE_PRESSED)*/) { |
| 139 | if (Bounds().Contains(where)) { |
| 140 | SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); |
| 141 | _AddFlags(STATE_PRESSED | STATE_TRACKING); |
| 142 | } else { |
| 143 | _ClearFlags(STATE_PRESSED | STATE_TRACKING); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // MouseUp |
| 149 | void |
| 150 | IconButton::MouseUp(BPoint where) |
| 151 | { |
| 152 | if (!IsValid()) |
| 153 | return; |
| 154 | |
| 155 | // if (!_HasFlags(STATE_FORCE_PRESSED)) { |
| 156 | if (_HasFlags(STATE_ENABLED) && _HasFlags(STATE_PRESSED) && Bounds().Contains(where)) |
| 157 | Invoke(); |
| 158 | else if (Bounds().Contains(where)) |
| 159 | _AddFlags(STATE_INSIDE); |
| 160 | _ClearFlags(STATE_PRESSED | STATE_TRACKING); |
| 161 | // } |
| 162 | } |
| 163 | |
| 164 | // MouseMoved |
| 165 | void |
| 166 | IconButton::MouseMoved(BPoint where, uint32 transit, const BMessage* message) |
| 167 | { |
| 168 | if (!IsValid()) |
| 169 | return; |
| 170 | |
| 171 | uint32 buttons = 0; |
| 172 | Window()->CurrentMessage()->FindInt32("buttons", (int32*)&buttons); |
| 173 | // catch a mouse up event that we might have missed |
| 174 | if (!buttons && _HasFlags(STATE_PRESSED)) { |
| 175 | MouseUp(where); |
| 176 | return; |
| 177 | } |
| 178 | if (buttons && !_HasFlags(STATE_TRACKING)) |
| 179 | return; |
| 180 | if ((transit == B_INSIDE_VIEW || transit == B_ENTERED_VIEW) |
| 181 | && _HasFlags(STATE_ENABLED)) |
| 182 | _AddFlags(STATE_INSIDE); |
| 183 | else |
| 184 | _ClearFlags(STATE_INSIDE); |
| 185 | if (_HasFlags(STATE_TRACKING)) { |
| 186 | if (Bounds().Contains(where)) |
| 187 | _AddFlags(STATE_PRESSED); |
| 188 | else |
| 189 | _ClearFlags(STATE_PRESSED); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #define MIN_SPACE 15.0 |
| 194 | |
| 195 | // GetPreferredSize |
| 196 | void |
| 197 | IconButton::GetPreferredSize(float* width, float* height) |
| 198 | { |
| 199 | float minWidth = 0.0; |
| 200 | float minHeight = 0.0; |
| 201 | if (IsValid()) { |
| 202 | minWidth += fNormalBitmap->Bounds().IntegerWidth() + 1.0; |
| 203 | minHeight += fNormalBitmap->Bounds().IntegerHeight() + 1.0; |
| 204 | } |
| 205 | if (minWidth < MIN_SPACE) |
| 206 | minWidth = MIN_SPACE; |
| 207 | if (minHeight < MIN_SPACE) |
| 208 | minHeight = MIN_SPACE; |
| 209 | |
| 210 | float hPadding = max_c(6.0, ceilf(minHeight / 4.0)); |
| 211 | float vPadding = max_c(6.0, ceilf(minWidth / 4.0)); |
| 212 | |
| 213 | if (fLabel.CountChars() > 0) { |
| 214 | font_height fh; |
| 215 | GetFontHeight(&fh); |
| 216 | minHeight += ceilf(fh.ascent + fh.descent) + vPadding; |
| 217 | minWidth += StringWidth(fLabel.String()) + vPadding; |
| 218 | } |
| 219 | |
| 220 | if (width) |
| 221 | *width = minWidth + hPadding; |
| 222 | if (height) |
| 223 | *height = minHeight + vPadding; |
| 224 | } |
| 225 | |
| 226 | // MinSize |
| 227 | BSize |
| 228 | IconButton::MinSize() |
| 229 | { |
| 230 | BSize size; |
| 231 | GetPreferredSize(&size.width, &size.height); |
| 232 | return size; |
| 233 | } |
| 234 | |
| 235 | // MaxSize |
| 236 | BSize |
| 237 | IconButton::MaxSize() |
| 238 | { |
| 239 | return MinSize(); |
| 240 | } |
| 241 | |
| 242 | // Invoke |
| 243 | status_t |
| 244 | IconButton::Invoke(BMessage* message) |
| 245 | { |
| 246 | if (!message) |
| 247 | message = Message(); |
| 248 | if (message) { |
| 249 | BMessage clone(*message); |
| 250 | clone.AddInt64("be:when", system_time()); |
| 251 | clone.AddPointer("be:source", (BView*)this); |
| 252 | clone.AddInt32("be:value", Value()); |
| 253 | clone.AddInt32("id", ID()); |
| 254 | return BInvoker::Invoke(&clone); |
| 255 | } |
| 256 | return BInvoker::Invoke(message); |
| 257 | } |
| 258 | |
| 259 | // SetPressed |
| 260 | void |
| 261 | IconButton::SetPressed(bool pressed) |
| 262 | { |
| 263 | if (pressed) |
| 264 | _AddFlags(STATE_FORCE_PRESSED); |
| 265 | else |
| 266 | _ClearFlags(STATE_FORCE_PRESSED); |
| 267 | } |
| 268 | |
| 269 | // IsPressed |
| 270 | bool |
| 271 | IconButton::IsPressed() const |
| 272 | { |
| 273 | return _HasFlags(STATE_FORCE_PRESSED); |
| 274 | } |
| 275 | |
| 276 | status_t |
| 277 | IconButton::SetIcon(int32 resourceID) |
| 278 | { |
| 279 | app_info info; |
| 280 | status_t status = be_app->GetAppInfo(&info); |
| 281 | if (status != B_OK) |
| 282 | return status; |
| 283 | |
| 284 | BResources resources(&info.ref); |
| 285 | status = resources.InitCheck(); |
| 286 | if (status != B_OK) |
| 287 | return status; |
| 288 | |
| 289 | size_t size; |
| 290 | const void* data = resources.LoadResource(B_VECTOR_ICON_TYPE, resourceID, |
| 291 | &size); |
| 292 | if (data != NULL) { |
| 293 | BBitmap bitmap(BRect(0, 0, fSize - 1, fSize - 1), B_BITMAP_NO_SERVER_LINK, B_RGBA32); |
| 294 | status = bitmap.InitCheck(); |
| 295 | if (status != B_OK) |
| 296 | return status; |
| 297 | status = BIconUtils::GetVectorIcon(reinterpret_cast<const uint8*>(data), |
| 298 | size, &bitmap); |
| 299 | if (status != B_OK) |
| 300 | return status; |
| 301 | return SetIcon(&bitmap); |
| 302 | } |
| 303 | // const void* data = resources.LoadResource(B_BITMAP_TYPE, resourceID, &size); |
| 304 | return B_ERROR; |
| 305 | } |
| 306 | |
| 307 | // SetIcon |
| 308 | status_t |
| 309 | IconButton::SetIcon(const char* pathToBitmap) |
| 310 | { |
| 311 | if (pathToBitmap == NULL) |
| 312 | return B_BAD_VALUE; |
| 313 | |
| 314 | status_t status = B_BAD_VALUE; |
| 315 | BBitmap* fileBitmap = NULL; |
| 316 | // try to load bitmap from either relative or absolute path |
| 317 | BEntry entry(pathToBitmap, true); |
| 318 | if (!entry.Exists()) { |
| 319 | app_info info; |
| 320 | status = be_app->GetAppInfo(&info); |
| 321 | if (status == B_OK) { |
| 322 | BEntry app_entry(&info.ref, true); |
| 323 | BPath path; |
| 324 | app_entry.GetPath(&path); |
| 325 | status = path.InitCheck(); |
| 326 | if (status == B_OK) { |
| 327 | status = path.GetParent(&path); |
| 328 | if (status == B_OK) { |
| 329 | status = path.Append(pathToBitmap, true); |
| 330 | if (status == B_OK) |
| 331 | fileBitmap = BTranslationUtils::GetBitmap(path.Path()); |
| 332 | else |
| 333 | printf("IconButton::SetIcon() - path.Append() failed: %s\n", strerror(status)); |
| 334 | } else |
| 335 | printf("IconButton::SetIcon() - path.GetParent() failed: %s\n", strerror(status)); |
| 336 | } else |
| 337 | printf("IconButton::SetIcon() - path.InitCheck() failed: %s\n", strerror(status)); |
| 338 | } else |
| 339 | printf("IconButton::SetIcon() - be_app->GetAppInfo() failed: %s\n", strerror(status)); |
| 340 | } else |
| 341 | fileBitmap = BTranslationUtils::GetBitmap(pathToBitmap); |
| 342 | if (fileBitmap) { |
| 343 | status = _MakeBitmaps(fileBitmap); |
| 344 | delete fileBitmap; |
| 345 | } else |
| 346 | status = B_ERROR; |
| 347 | return status; |
| 348 | } |
| 349 | |
| 350 | // SetIcon |
| 351 | status_t |
| 352 | IconButton::SetIcon(const BBitmap* bitmap) |
| 353 | { |
| 354 | if (bitmap && bitmap->ColorSpace() == B_CMAP8) { |
| 355 | status_t status = bitmap->InitCheck(); |
| 356 | if (status >= B_OK) { |
| 357 | if (BBitmap* rgb32Bitmap = _ConvertToRGB32(bitmap)) { |
| 358 | status = _MakeBitmaps(rgb32Bitmap); |
| 359 | delete rgb32Bitmap; |
| 360 | } else |
| 361 | status = B_NO_MEMORY; |
| 362 | } |
| 363 | return status; |
| 364 | } else |
| 365 | return _MakeBitmaps(bitmap); |
| 366 | } |
| 367 | |
| 368 | // SetIcon |
| 369 | status_t |
| 370 | IconButton::SetIcon(const BMimeType* fileType, bool small) |
| 371 | { |
| 372 | status_t status = fileType ? fileType->InitCheck() : B_BAD_VALUE; |
| 373 | if (status >= B_OK) { |
| 374 | BBitmap* mimeBitmap = new(nothrow) BBitmap(BRect(0.0, 0.0, 15.0, 15.0), B_CMAP8); |
| 375 | if (mimeBitmap && mimeBitmap->IsValid()) { |
| 376 | status = fileType->GetIcon(mimeBitmap, small ? B_MINI_ICON : B_LARGE_ICON); |
| 377 | if (status >= B_OK) { |
| 378 | if (BBitmap* bitmap = _ConvertToRGB32(mimeBitmap)) { |
| 379 | status = _MakeBitmaps(bitmap); |
| 380 | delete bitmap; |
| 381 | } else |
| 382 | printf("IconButton::SetIcon() - B_RGB32 bitmap is not valid\n"); |
| 383 | } else |
| 384 | printf("IconButton::SetIcon() - fileType->GetIcon() failed: %s\n", strerror(status)); |
| 385 | } else |
| 386 | printf("IconButton::SetIcon() - B_CMAP8 bitmap is not valid\n"); |
| 387 | delete mimeBitmap; |
| 388 | } else |
| 389 | printf("IconButton::SetIcon() - fileType is not valid: %s\n", strerror(status)); |
| 390 | return status; |
| 391 | } |
| 392 | |
| 393 | // SetIcon |
| 394 | status_t |
| 395 | IconButton::SetIcon(const unsigned char* bitsFromQuickRes, |
| 396 | uint32 width, uint32 height, color_space format, bool convertToBW) |
| 397 | { |
| 398 | status_t status = B_BAD_VALUE; |
| 399 | if (bitsFromQuickRes && width > 0 && height > 0) { |
| 400 | BBitmap* quickResBitmap = new(nothrow) BBitmap(BRect(0.0, 0.0, width - 1.0, height - 1.0), format); |
| 401 | status = quickResBitmap ? quickResBitmap->InitCheck() : B_ERROR; |
| 402 | if (status >= B_OK) { |
| 403 | // It doesn't look right to copy BitsLength() bytes, but bitmaps |
| 404 | // exported from QuickRes still contain their padding, so it is alright. |
| 405 | memcpy(quickResBitmap->Bits(), bitsFromQuickRes, quickResBitmap->BitsLength()); |
| 406 | if (format != B_RGB32 && format != B_RGBA32 && format != B_RGB32_BIG && format != B_RGBA32_BIG) { |
| 407 | // colorspace needs conversion |
| 408 | BBitmap* bitmap = new(nothrow) BBitmap(quickResBitmap->Bounds(), B_RGB32, true); |
| 409 | if (bitmap && bitmap->IsValid()) { |
| 410 | BView* helper = new BView(bitmap->Bounds(), "helper", |
| 411 | B_FOLLOW_NONE, B_WILL_DRAW); |
| 412 | if (bitmap->Lock()) { |
| 413 | bitmap->AddChild(helper); |
| 414 | helper->SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR)); |
| 415 | helper->FillRect(helper->Bounds()); |
| 416 | helper->SetDrawingMode(B_OP_OVER); |
| 417 | helper->DrawBitmap(quickResBitmap, BPoint(0.0, 0.0)); |
| 418 | helper->Sync(); |
| 419 | bitmap->Unlock(); |
| 420 | } |
| 421 | status = _MakeBitmaps(bitmap); |
| 422 | } else |
| 423 | printf("IconButton::SetIcon() - B_RGB32 bitmap is not valid\n"); |
| 424 | delete bitmap; |
| 425 | } else { |
| 426 | // native colorspace (32 bits) |
| 427 | if (convertToBW) { |
| 428 | // convert to gray scale icon |
| 429 | uint8* bits = (uint8*)quickResBitmap->Bits(); |
| 430 | uint32 bpr = quickResBitmap->BytesPerRow(); |
| 431 | for (uint32 y = 0; y < height; y++) { |
| 432 | uint8* handle = bits; |
| 433 | uint8 gray; |
| 434 | for (uint32 x = 0; x < width; x++) { |
| 435 | gray = uint8((116 * handle[0] + 600 * handle[1] + 308 * handle[2]) / 1024); |
| 436 | handle[0] = gray; |
| 437 | handle[1] = gray; |
| 438 | handle[2] = gray; |
| 439 | handle += 4; |
| 440 | } |
| 441 | bits += bpr; |
| 442 | } |
| 443 | } |
| 444 | status = _MakeBitmaps(quickResBitmap); |
| 445 | } |
| 446 | } else |
| 447 | printf("IconButton::SetIcon() - error allocating bitmap: %s\n", strerror(status)); |
| 448 | delete quickResBitmap; |
| 449 | } |
| 450 | return status; |
| 451 | } |
| 452 | |
| 453 | // ClearIcon |
| 454 | void |
| 455 | IconButton::ClearIcon() |
| 456 | { |
| 457 | _DeleteBitmaps(); |
| 458 | _Update(); |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | IconButton::TrimIcon(bool keepAspect) |
| 463 | { |
| 464 | if (fNormalBitmap == NULL) |
| 465 | return; |
| 466 | |
| 467 | uint8* bits = (uint8*)fNormalBitmap->Bits(); |
| 468 | uint32 bpr = fNormalBitmap->BytesPerRow(); |
| 469 | uint32 width = fNormalBitmap->Bounds().IntegerWidth() + 1; |
| 470 | uint32 height = fNormalBitmap->Bounds().IntegerHeight() + 1; |
| 471 | BRect trimmed(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN); |
| 472 | for (uint32 y = 0; y < height; y++) { |
| 473 | uint8* b = bits + 3; |
| 474 | bool rowHasAlpha = false; |
| 475 | for (uint32 x = 0; x < width; x++) { |
| 476 | if (*b) { |
| 477 | rowHasAlpha = true; |
| 478 | if (x < trimmed.left) |
| 479 | trimmed.left = x; |
| 480 | if (x > trimmed.right) |
| 481 | trimmed.right = x; |
| 482 | } |
| 483 | b += 4; |
| 484 | } |
| 485 | if (rowHasAlpha) { |
| 486 | if (y < trimmed.top) |
| 487 | trimmed.top = y; |
| 488 | if (y > trimmed.bottom) |
| 489 | trimmed.bottom = y; |
| 490 | } |
| 491 | bits += bpr; |
| 492 | } |
| 493 | if (!trimmed.IsValid()) |
| 494 | return; |
| 495 | if (keepAspect) { |
| 496 | float minInset = trimmed.left; |
| 497 | minInset = min_c(minInset, trimmed.top); |
| 498 | minInset = min_c(minInset, fNormalBitmap->Bounds().right - trimmed.right); |
| 499 | minInset = min_c(minInset, fNormalBitmap->Bounds().bottom - trimmed.bottom); |
| 500 | trimmed = fNormalBitmap->Bounds().InsetByCopy(minInset, minInset); |
| 501 | } |
| 502 | trimmed = trimmed & fNormalBitmap->Bounds(); |
| 503 | BBitmap trimmedBitmap(trimmed.OffsetToCopy(B_ORIGIN), |
| 504 | B_BITMAP_NO_SERVER_LINK, B_RGBA32); |
| 505 | bits = (uint8*)fNormalBitmap->Bits(); |
| 506 | bits += 4 * (int32)trimmed.left + bpr * (int32)trimmed.top; |
| 507 | uint8* dst = (uint8*)trimmedBitmap.Bits(); |
| 508 | uint32 trimmedWidth = trimmedBitmap.Bounds().IntegerWidth() + 1; |
| 509 | uint32 trimmedHeight = trimmedBitmap.Bounds().IntegerHeight() + 1; |
| 510 | uint32 trimmedBPR = trimmedBitmap.BytesPerRow(); |
| 511 | for (uint32 y = 0; y < trimmedHeight; y++) { |
| 512 | memcpy(dst, bits, trimmedWidth * 4); |
| 513 | dst += trimmedBPR; |
| 514 | bits += bpr; |
| 515 | } |
| 516 | SetIcon(&trimmedBitmap); |
| 517 | } |
| 518 | |
| 519 | // Bitmap |
| 520 | BBitmap* |
| 521 | IconButton::Bitmap() const |
| 522 | { |
| 523 | BBitmap* bitmap = NULL; |
| 524 | if (fNormalBitmap && fNormalBitmap->IsValid()) { |
| 525 | bitmap = new(nothrow) BBitmap(fNormalBitmap); |
| 526 | if (bitmap->IsValid()) { |
| 527 | // TODO: remove this functionality when we use real transparent bitmaps |
| 528 | uint8* bits = (uint8*)bitmap->Bits(); |
| 529 | uint32 bpr = bitmap->BytesPerRow(); |
| 530 | uint32 width = bitmap->Bounds().IntegerWidth() + 1; |
| 531 | uint32 height = bitmap->Bounds().IntegerHeight() + 1; |
| 532 | color_space format = bitmap->ColorSpace(); |
| 533 | if (format == B_CMAP8) { |
| 534 | // replace gray with magic transparent index |
| 535 | } else if (format == B_RGB32) { |
| 536 | for (uint32 y = 0; y < height; y++) { |
| 537 | uint8* bitsHandle = bits; |
| 538 | for (uint32 x = 0; x < width; x++) { |
| 539 | if (bitsHandle[0] == 216 |
| 540 | && bitsHandle[1] == 216 |
| 541 | && bitsHandle[2] == 216) { |
| 542 | bitsHandle[3] = 0; // make this pixel completely transparent |
| 543 | } |
| 544 | bitsHandle += 4; |
| 545 | } |
| 546 | bits += bpr; |
| 547 | } |
| 548 | } |
| 549 | } else { |
| 550 | delete bitmap; |
| 551 | bitmap = NULL; |
| 552 | } |
| 553 | } |
| 554 | return bitmap; |
| 555 | } |
| 556 | |
| 557 | // DrawBorder |
| 558 | bool |
| 559 | IconButton::DrawBorder() const |
| 560 | { |
| 561 | return ((IsEnabled() && (_HasFlags(STATE_INSIDE) |
| 562 | || _HasFlags(STATE_TRACKING))) || _HasFlags(STATE_FORCE_PRESSED)); |
| 563 | } |
| 564 | |
| 565 | // DrawNormalBorder |
| 566 | void |
| 567 | IconButton::DrawNormalBorder(BRect r, rgb_color background, |
| 568 | rgb_color shadow, rgb_color darkShadow, |
| 569 | rgb_color lightShadow, rgb_color light) |
| 570 | { |
| 571 | _DrawFrame(r, shadow, darkShadow, light, lightShadow); |
| 572 | } |
| 573 | |
| 574 | // DrawPressedBorder |
| 575 | void |
| 576 | IconButton::DrawPressedBorder(BRect r, rgb_color background, |
| 577 | rgb_color shadow, rgb_color darkShadow, |
| 578 | rgb_color lightShadow, rgb_color light) |
| 579 | { |
| 580 | _DrawFrame(r, shadow, light, darkShadow, background); |
| 581 | } |
| 582 | |
| 583 | // IsValid |
| 584 | bool |
| 585 | IconButton::IsValid() const |
| 586 | { |
| 587 | return (fNormalBitmap && fDisabledBitmap && fClickedBitmap |
| 588 | && fDisabledClickedBitmap |
| 589 | && fNormalBitmap->IsValid() |
| 590 | && fDisabledBitmap->IsValid() |
| 591 | && fClickedBitmap->IsValid() |
| 592 | && fDisabledClickedBitmap->IsValid()); |
| 593 | } |
| 594 | |
| 595 | // Value |
| 596 | int32 |
| 597 | IconButton::Value() const |
| 598 | { |
| 599 | return _HasFlags(STATE_PRESSED) ? B_CONTROL_ON : B_CONTROL_OFF; |
| 600 | } |
| 601 | |
| 602 | // SetValue |
| 603 | void |
| 604 | IconButton::SetValue(int32 value) |
| 605 | { |
| 606 | if (value) |
| 607 | _AddFlags(STATE_PRESSED); |
| 608 | else |
| 609 | _ClearFlags(STATE_PRESSED); |
| 610 | } |
| 611 | |
| 612 | // IsEnabled |
| 613 | bool |
| 614 | IconButton::IsEnabled() const |
| 615 | { |
| 616 | return _HasFlags(STATE_ENABLED) ? B_CONTROL_ON : B_CONTROL_OFF; |
| 617 | } |
| 618 | |
| 619 | // SetEnabled |
| 620 | void |
| 621 | IconButton::SetEnabled(bool enabled) |
| 622 | { |
| 623 | if (enabled) |
| 624 | _AddFlags(STATE_ENABLED); |
| 625 | else |
| 626 | _ClearFlags(STATE_ENABLED | STATE_TRACKING | STATE_INSIDE); |
| 627 | } |
| 628 | |
| 629 | // _ConvertToRGB32 |
| 630 | BBitmap* |
| 631 | IconButton::_ConvertToRGB32(const BBitmap* bitmap) const |
| 632 | { |
| 633 | BBitmap* convertedBitmap = new(nothrow) BBitmap(bitmap->Bounds(), |
| 634 | B_BITMAP_ACCEPTS_VIEWS, B_RGBA32); |
| 635 | if (convertedBitmap && convertedBitmap->IsValid()) { |
| 636 | memset(convertedBitmap->Bits(), 0, convertedBitmap->BitsLength()); |
| 637 | BView* helper = new BView(bitmap->Bounds(), "helper", |
| 638 | B_FOLLOW_NONE, B_WILL_DRAW); |
| 639 | if (convertedBitmap->Lock()) { |
| 640 | convertedBitmap->AddChild(helper); |
| 641 | helper->SetDrawingMode(B_OP_OVER); |
| 642 | helper->DrawBitmap(bitmap, BPoint(0.0, 0.0)); |
| 643 | helper->Sync(); |
| 644 | convertedBitmap->Unlock(); |
| 645 | } |
| 646 | } else { |
| 647 | delete convertedBitmap; |
| 648 | convertedBitmap = NULL; |
| 649 | } |
| 650 | return convertedBitmap; |
| 651 | } |
| 652 | |
| 653 | // _MakeBitmaps |
| 654 | status_t |
| 655 | IconButton::_MakeBitmaps(const BBitmap* bitmap) |
| 656 | { |
| 657 | status_t status = bitmap ? bitmap->InitCheck() : B_BAD_VALUE; |
| 658 | if (status >= B_OK) { |
| 659 | // make our own versions of the bitmap |
| 660 | BRect b(bitmap->Bounds()); |
| 661 | _DeleteBitmaps(); |
| 662 | color_space format = bitmap->ColorSpace(); |
| 663 | fNormalBitmap = new(nothrow) BBitmap(b, format); |
| 664 | fDisabledBitmap = new(nothrow) BBitmap(b, format); |
| 665 | fClickedBitmap = new(nothrow) BBitmap(b, format); |
| 666 | fDisabledClickedBitmap = new(nothrow) BBitmap(b, format); |
| 667 | if (IsValid()) { |
| 668 | // copy bitmaps from file bitmap |
| 669 | uint8* nBits = (uint8*)fNormalBitmap->Bits(); |
| 670 | uint8* dBits = (uint8*)fDisabledBitmap->Bits(); |
| 671 | uint8* cBits = (uint8*)fClickedBitmap->Bits(); |
| 672 | uint8* dcBits = (uint8*)fDisabledClickedBitmap->Bits(); |
| 673 | uint8* fBits = (uint8*)bitmap->Bits(); |
| 674 | int32 nbpr = fNormalBitmap->BytesPerRow(); |
| 675 | int32 fbpr = bitmap->BytesPerRow(); |
| 676 | int32 pixels = b.IntegerWidth() + 1; |
| 677 | int32 lines = b.IntegerHeight() + 1; |
| 678 | // nontransparent version: |
| 679 | if (format == B_RGB32 || format == B_RGB32_BIG) { |
| 680 | // iterate over color components |
| 681 | for (int32 y = 0; y < lines; y++) { |
| 682 | for (int32 x = 0; x < pixels; x++) { |
| 683 | int32 nOffset = 4 * x; |
| 684 | int32 fOffset = 4 * x; |
| 685 | nBits[nOffset + 0] = fBits[fOffset + 0]; |
| 686 | nBits[nOffset + 1] = fBits[fOffset + 1]; |
| 687 | nBits[nOffset + 2] = fBits[fOffset + 2]; |
| 688 | nBits[nOffset + 3] = 255; |
| 689 | // clicked bits are darker (lame method...) |
| 690 | cBits[nOffset + 0] = (uint8)((float)nBits[nOffset + 0] * 0.8); |
| 691 | cBits[nOffset + 1] = (uint8)((float)nBits[nOffset + 1] * 0.8); |
| 692 | cBits[nOffset + 2] = (uint8)((float)nBits[nOffset + 2] * 0.8); |
| 693 | cBits[nOffset + 3] = 255; |
| 694 | // disabled bits have less contrast (lame method...) |
| 695 | uint8 grey = 216; |
| 696 | float dist = (nBits[nOffset + 0] - grey) * 0.4; |
| 697 | dBits[nOffset + 0] = (uint8)(grey + dist); |
| 698 | dist = (nBits[nOffset + 1] - grey) * 0.4; |
| 699 | dBits[nOffset + 1] = (uint8)(grey + dist); |
| 700 | dist = (nBits[nOffset + 2] - grey) * 0.4; |
| 701 | dBits[nOffset + 2] = (uint8)(grey + dist); |
| 702 | dBits[nOffset + 3] = 255; |
| 703 | // disabled bits have less contrast (lame method...) |
| 704 | grey = 188; |
| 705 | dist = (nBits[nOffset + 0] - grey) * 0.4; |
| 706 | dcBits[nOffset + 0] = (uint8)(grey + dist); |
| 707 | dist = (nBits[nOffset + 1] - grey) * 0.4; |
| 708 | dcBits[nOffset + 1] = (uint8)(grey + dist); |
| 709 | dist = (nBits[nOffset + 2] - grey) * 0.4; |
| 710 | dcBits[nOffset + 2] = (uint8)(grey + dist); |
| 711 | dcBits[nOffset + 3] = 255; |
| 712 | } |
| 713 | nBits += nbpr; |
| 714 | dBits += nbpr; |
| 715 | cBits += nbpr; |
| 716 | dcBits += nbpr; |
| 717 | fBits += fbpr; |
| 718 | } |
| 719 | // transparent version: |
| 720 | } else if (format == B_RGBA32 || format == B_RGBA32_BIG) { |
| 721 | // iterate over color components |
| 722 | for (int32 y = 0; y < lines; y++) { |
| 723 | for (int32 x = 0; x < pixels; x++) { |
| 724 | int32 nOffset = 4 * x; |
| 725 | int32 fOffset = 4 * x; |
| 726 | nBits[nOffset + 0] = fBits[fOffset + 0]; |
| 727 | nBits[nOffset + 1] = fBits[fOffset + 1]; |
| 728 | nBits[nOffset + 2] = fBits[fOffset + 2]; |
| 729 | nBits[nOffset + 3] = fBits[fOffset + 3]; |
| 730 | // clicked bits are darker (lame method...) |
| 731 | cBits[nOffset + 0] = (uint8)(nBits[nOffset + 0] * 0.8); |
| 732 | cBits[nOffset + 1] = (uint8)(nBits[nOffset + 1] * 0.8); |
| 733 | cBits[nOffset + 2] = (uint8)(nBits[nOffset + 2] * 0.8); |
| 734 | cBits[nOffset + 3] = fBits[fOffset + 3]; |
| 735 | // disabled bits have less opacity |
| 736 | |
| 737 | uint8 grey = ((uint16)nBits[nOffset + 0] * 10 |
| 738 | + nBits[nOffset + 1] * 60 |
| 739 | + nBits[nOffset + 2] * 30) / 100; |
| 740 | float dist = (nBits[nOffset + 0] - grey) * 0.3; |
| 741 | dBits[nOffset + 0] = (uint8)(grey + dist); |
| 742 | dist = (nBits[nOffset + 1] - grey) * 0.3; |
| 743 | dBits[nOffset + 1] = (uint8)(grey + dist); |
| 744 | dist = (nBits[nOffset + 2] - grey) * 0.3; |
| 745 | dBits[nOffset + 2] = (uint8)(grey + dist); |
| 746 | dBits[nOffset + 3] = (uint8)(fBits[fOffset + 3] * 0.3); |
| 747 | // disabled bits have less contrast (lame method...) |
| 748 | dcBits[nOffset + 0] = (uint8)(dBits[nOffset + 0] * 0.8); |
| 749 | dcBits[nOffset + 1] = (uint8)(dBits[nOffset + 1] * 0.8); |
| 750 | dcBits[nOffset + 2] = (uint8)(dBits[nOffset + 2] * 0.8); |
| 751 | dcBits[nOffset + 3] = (uint8)(fBits[fOffset + 3] * 0.3); |
| 752 | } |
| 753 | nBits += nbpr; |
| 754 | dBits += nbpr; |
| 755 | cBits += nbpr; |
| 756 | dcBits += nbpr; |
| 757 | fBits += fbpr; |
| 758 | } |
| 759 | // unsupported format |
| 760 | } else { |
| 761 | printf("IconButton::_MakeBitmaps() - bitmap has unsupported colorspace\n"); |
| 762 | status = B_MISMATCHED_VALUES; |
| 763 | _DeleteBitmaps(); |
| 764 | } |
| 765 | } else { |
| 766 | printf("IconButton::_MakeBitmaps() - error allocating local bitmaps\n"); |
| 767 | status = B_NO_MEMORY; |
| 768 | _DeleteBitmaps(); |
| 769 | } |
| 770 | } else |
| 771 | printf("IconButton::_MakeBitmaps() - bitmap is not valid\n"); |
| 772 | return status; |
| 773 | } |
| 774 | |
| 775 | // _DeleteBitmaps |
| 776 | void |
| 777 | IconButton::_DeleteBitmaps() |
| 778 | { |
| 779 | delete fNormalBitmap; |
| 780 | fNormalBitmap = NULL; |
| 781 | delete fDisabledBitmap; |
| 782 | fDisabledBitmap = NULL; |
| 783 | delete fClickedBitmap; |
| 784 | fClickedBitmap = NULL; |
| 785 | delete fDisabledClickedBitmap; |
| 786 | fDisabledClickedBitmap = NULL; |
| 787 | } |
| 788 | |
| 789 | // _Update |
| 790 | void |
| 791 | IconButton::_Update() |
| 792 | { |
| 793 | if (LockLooper()) { |
| 794 | Invalidate(); |
| 795 | UnlockLooper(); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | // _AddFlags |
| 800 | void |
| 801 | IconButton::_AddFlags(uint32 flags) |
| 802 | { |
| 803 | if (!(fButtonState & flags)) { |
| 804 | fButtonState |= flags; |
| 805 | _Update(); |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | // _ClearFlags |
| 810 | void |
| 811 | IconButton::_ClearFlags(uint32 flags) |
| 812 | { |
| 813 | if (fButtonState & flags) { |
| 814 | fButtonState &= ~flags; |
| 815 | _Update(); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | // _HasFlags |
| 820 | bool |
| 821 | IconButton::_HasFlags(uint32 flags) const |
| 822 | { |
| 823 | return (fButtonState & flags); |
| 824 | } |
| 825 | |
| 826 | // _DrawFrame |
| 827 | void |
| 828 | IconButton::_DrawFrame(BRect r, rgb_color col1, rgb_color col2, |
| 829 | rgb_color col3, rgb_color col4) |
| 830 | { |
| 831 | BeginLineArray(8); |
| 832 | AddLine(BPoint(r.left, r.bottom), BPoint(r.left, r.top), col1); |
| 833 | AddLine(BPoint(r.left + 1.0, r.top), BPoint(r.right, r.top), col1); |
| 834 | AddLine(BPoint(r.right, r.top + 1.0), BPoint(r.right, r.bottom), col2); |
| 835 | AddLine(BPoint(r.right - 1.0, r.bottom), BPoint(r.left + 1.0, r.bottom), col2); |
| 836 | r.InsetBy(1.0, 1.0); |
| 837 | AddLine(BPoint(r.left, r.bottom), BPoint(r.left, r.top), col3); |
| 838 | AddLine(BPoint(r.left + 1.0, r.top), BPoint(r.right, r.top), col3); |
| 839 | AddLine(BPoint(r.right, r.top + 1.0), BPoint(r.right, r.bottom), col4); |
| 840 | AddLine(BPoint(r.right - 1.0, r.bottom), BPoint(r.left + 1.0, r.bottom), col4); |
| 841 | EndLineArray(); |
| 842 | } |