1 /* 2 * Copyright 2001-2008, Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Marc Flerackers (mflerackers@androme.be) 7 * Bill Hayden (haydentech@users.sourceforge.net) 8 * Stefano Ceccherini (stefano.ceccherini@gmail.com) 9 * Olivier Milla 10 */ 11 12 //! Display item for BMenu class 13 14 #include <ctype.h> 15 #include <stdlib.h> 16 #include <string.h> 17 18 #include <Bitmap.h> 19 #include <ControlLook.h> 20 #include <MenuItem.h> 21 #include <Shape.h> 22 #include <String.h> 23 #include <Window.h> 24 25 #include <MenuPrivate.h> 26 27 #include "utf8_functions.h" 28 29 30 const float kLightBGTint = (B_LIGHTEN_1_TINT + B_LIGHTEN_1_TINT + B_NO_TINT) / 3.0; 31 32 // map control key shortcuts to drawable Unicode characters 33 // cf. http://unicode.org/charts/PDF/U2190.pdf 34 const char *kUTF8ControlMap[] = { 35 NULL, 36 "\xe2\x86\xb8", /* B_HOME U+21B8 */ 37 NULL, NULL, 38 NULL, /* B_END */ 39 NULL, /* B_INSERT */ 40 NULL, NULL, 41 NULL, /* B_BACKSPACE */ 42 "\xe2\x86\xb9", /* B_TAB U+21B9 */ 43 "\xe2\x86\xb5", /* B_ENTER, U+21B5 */ 44 //"\xe2\x8f\x8e", /* B_ENTER, U+23CE it's the official one */ 45 NULL, /* B_PAGE_UP */ 46 NULL, /* B_PAGE_DOWN */ 47 NULL, NULL, NULL, 48 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 49 NULL, NULL, NULL, NULL, 50 "\xe2\x86\x90", /* B_LEFT_ARROW */ 51 "\xe2\x86\x92", /* B_RIGHT_ARROW */ 52 "\xe2\x86\x91", /* B_UP_ARROW */ 53 "\xe2\x86\x93", /* B_DOWN_ARROW */ 54 }; 55 56 using BPrivate::MenuPrivate; 57 58 BMenuItem::BMenuItem(const char *label, BMessage *message, char shortcut, 59 uint32 modifiers) 60 { 61 _InitData(); 62 if (label != NULL) 63 fLabel = strdup(label); 64 65 SetMessage(message); 66 67 fShortcutChar = shortcut; 68 69 if (shortcut != 0) 70 fModifiers = modifiers | B_COMMAND_KEY; 71 else 72 fModifiers = 0; 73 } 74 75 76 BMenuItem::BMenuItem(BMenu *menu, BMessage *message) 77 { 78 _InitData(); 79 SetMessage(message); 80 _InitMenuData(menu); 81 } 82 83 84 BMenuItem::BMenuItem(BMessage *data) 85 { 86 _InitData(); 87 88 if (data->HasString("_label")) { 89 const char *string; 90 91 data->FindString("_label", &string); 92 SetLabel(string); 93 } 94 95 bool disable; 96 if (data->FindBool("_disable", &disable) == B_OK) 97 SetEnabled(!disable); 98 99 bool marked; 100 if (data->FindBool("_marked", &marked) == B_OK) 101 SetMarked(marked); 102 103 int32 userTrigger; 104 if (data->FindInt32("_user_trig", &userTrigger) == B_OK) 105 SetTrigger(userTrigger); 106 107 if (data->HasInt32("_shortcut")) { 108 int32 shortcut, mods; 109 110 data->FindInt32("_shortcut", &shortcut); 111 data->FindInt32("_mods", &mods); 112 113 SetShortcut(shortcut, mods); 114 } 115 116 if (data->HasMessage("_msg")) { 117 BMessage *msg = new BMessage; 118 data->FindMessage("_msg", msg); 119 SetMessage(msg); 120 } 121 122 BMessage subMessage; 123 if (data->FindMessage("_submenu", &subMessage) == B_OK) { 124 BArchivable *object = instantiate_object(&subMessage); 125 if (object != NULL) { 126 BMenu *menu = dynamic_cast<BMenu *>(object); 127 if (menu != NULL) 128 _InitMenuData(menu); 129 } 130 } 131 } 132 133 134 BArchivable * 135 BMenuItem::Instantiate(BMessage *data) 136 { 137 if (validate_instantiation(data, "BMenuItem")) 138 return new BMenuItem(data); 139 140 return NULL; 141 } 142 143 144 status_t 145 BMenuItem::Archive(BMessage *data, bool deep) const 146 { 147 status_t ret = BArchivable::Archive(data, deep); 148 149 if (ret == B_OK && fLabel) 150 ret = data->AddString("_label", Label()); 151 152 if (ret == B_OK && !IsEnabled()) 153 ret = data->AddBool("_disable", true); 154 155 if (ret == B_OK && IsMarked()) 156 ret = data->AddBool("_marked", true); 157 158 if (ret == B_OK && fUserTrigger) 159 ret = data->AddInt32("_user_trig", fUserTrigger); 160 161 if (ret == B_OK && fShortcutChar) { 162 ret = data->AddInt32("_shortcut", fShortcutChar); 163 if (ret == B_OK) 164 ret = data->AddInt32("_mods", fModifiers); 165 } 166 167 if (ret == B_OK && Message()) 168 ret = data->AddMessage("_msg", Message()); 169 170 if (ret == B_OK && deep && fSubmenu) { 171 BMessage submenu; 172 if (fSubmenu->Archive(&submenu, true) == B_OK) 173 ret = data->AddMessage("_submenu", &submenu); 174 } 175 176 return ret; 177 } 178 179 180 BMenuItem::~BMenuItem() 181 { 182 free(fLabel); 183 delete fSubmenu; 184 } 185 186 187 void 188 BMenuItem::SetLabel(const char *string) 189 { 190 if (fLabel != NULL) { 191 free(fLabel); 192 fLabel = NULL; 193 } 194 195 if (string != NULL) 196 fLabel = strdup(string); 197 198 if (fSuper != NULL) { 199 fSuper->InvalidateLayout(); 200 201 if (fSuper->LockLooper()) { 202 fSuper->Invalidate(); 203 fSuper->UnlockLooper(); 204 } 205 } 206 } 207 208 209 void 210 BMenuItem::SetEnabled(bool state) 211 { 212 if (fEnabled == state) 213 return; 214 215 fEnabled = state; 216 217 if (fSubmenu != NULL) 218 fSubmenu->SetEnabled(state); 219 220 BMenu *menu = Menu(); 221 if (menu != NULL && menu->LockLooper()) { 222 menu->Invalidate(fBounds); 223 menu->UnlockLooper(); 224 } 225 } 226 227 228 void 229 BMenuItem::SetMarked(bool state) 230 { 231 fMark = state; 232 233 if (state && Menu() != NULL) { 234 MenuPrivate priv(Menu()); 235 priv.ItemMarked(this); 236 } 237 } 238 239 240 void 241 BMenuItem::SetTrigger(char trigger) 242 { 243 fUserTrigger = trigger; 244 245 // try uppercase letters first 246 247 const char* pos = strchr(Label(), toupper(trigger)); 248 if (pos == NULL) { 249 // take lowercase, too 250 pos = strchr(Label(), trigger); 251 } 252 if (pos != NULL) { 253 fTriggerIndex = UTF8CountChars(Label(), pos - Label()); 254 fTrigger = tolower(UTF8ToCharCode(&pos)); 255 } else { 256 fTrigger = 0; 257 fTriggerIndex = -1; 258 } 259 260 if (fSuper != NULL) 261 fSuper->InvalidateLayout(); 262 } 263 264 265 void 266 BMenuItem::SetShortcut(char ch, uint32 modifiers) 267 { 268 if (fShortcutChar != 0 && (fModifiers & B_COMMAND_KEY) && fWindow) 269 fWindow->RemoveShortcut(fShortcutChar, fModifiers); 270 271 fShortcutChar = ch; 272 273 if (ch != 0) 274 fModifiers = modifiers | B_COMMAND_KEY; 275 else 276 fModifiers = 0; 277 278 if (fShortcutChar != 0 && (fModifiers & B_COMMAND_KEY) && fWindow) 279 fWindow->AddShortcut(fShortcutChar, fModifiers, this); 280 281 if (fSuper) { 282 fSuper->InvalidateLayout(); 283 284 if (fSuper->LockLooper()) { 285 fSuper->Invalidate(); 286 fSuper->UnlockLooper(); 287 } 288 } 289 } 290 291 292 const char * 293 BMenuItem::Label() const 294 { 295 return fLabel; 296 } 297 298 299 bool 300 BMenuItem::IsEnabled() const 301 { 302 if (fSubmenu) 303 return fSubmenu->IsEnabled(); 304 305 if (!fEnabled) 306 return false; 307 308 return fSuper ? fSuper->IsEnabled() : true; 309 } 310 311 312 bool 313 BMenuItem::IsMarked() const 314 { 315 return fMark; 316 } 317 318 319 char 320 BMenuItem::Trigger() const 321 { 322 return fUserTrigger; 323 } 324 325 326 char 327 BMenuItem::Shortcut(uint32 *modifiers) const 328 { 329 if (modifiers) 330 *modifiers = fModifiers; 331 332 return fShortcutChar; 333 } 334 335 336 BMenu * 337 BMenuItem::Submenu() const 338 { 339 return fSubmenu; 340 } 341 342 343 BMenu * 344 BMenuItem::Menu() const 345 { 346 return fSuper; 347 } 348 349 350 BRect 351 BMenuItem::Frame() const 352 { 353 return fBounds; 354 } 355 356 357 void 358 BMenuItem::GetContentSize(float *width, float *height) 359 { 360 // TODO: Get rid of this. BMenu should handle this 361 // automatically. Maybe it's not even needed, since our 362 // BFont::Height() caches the value locally 363 MenuPrivate(fSuper).CacheFontInfo(); 364 365 fCachedWidth = fSuper->StringWidth(fLabel); 366 367 if (width) 368 *width = (float)ceil(fCachedWidth); 369 if (height) { 370 *height = MenuPrivate(fSuper).FontHeight(); 371 } 372 } 373 374 375 void 376 BMenuItem::TruncateLabel(float maxWidth, char *newLabel) 377 { 378 BFont font; 379 BString string(fLabel); 380 381 font.TruncateString(&string, B_TRUNCATE_MIDDLE, maxWidth); 382 383 string.CopyInto(newLabel, 0, string.Length()); 384 newLabel[string.Length()] = '\0'; 385 } 386 387 388 void 389 BMenuItem::DrawContent() 390 { 391 MenuPrivate(fSuper).CacheFontInfo(); 392 393 fSuper->MovePenBy(0, MenuPrivate(fSuper).Ascent()); 394 BPoint lineStart = fSuper->PenLocation(); 395 396 float labelWidth, labelHeight; 397 GetContentSize(&labelWidth, &labelHeight); 398 399 fSuper->SetDrawingMode(B_OP_OVER); 400 401 // truncate if needed 402 // TODO: Actually, this is still never triggered 403 if (fBounds.Width() > labelWidth) 404 fSuper->DrawString(fLabel); 405 else { 406 char *truncatedLabel = new char[strlen(fLabel) + 4]; 407 TruncateLabel(fBounds.Width(), truncatedLabel); 408 fSuper->DrawString(truncatedLabel); 409 delete[] truncatedLabel; 410 } 411 412 if (fSuper->AreTriggersEnabled() && fTriggerIndex != -1) { 413 float escapements[fTriggerIndex + 1]; 414 BFont font; 415 fSuper->GetFont(&font); 416 417 font.GetEscapements(fLabel, fTriggerIndex + 1, escapements); 418 419 for (int32 i = 0; i < fTriggerIndex; i++) 420 lineStart.x += escapements[i] * font.Size(); 421 422 lineStart.x--; 423 lineStart.y++; 424 425 BPoint lineEnd(lineStart); 426 lineEnd.x += escapements[fTriggerIndex] * font.Size(); 427 428 fSuper->StrokeLine(lineStart, lineEnd); 429 } 430 } 431 432 433 void 434 BMenuItem::Draw() 435 { 436 bool enabled = IsEnabled(); 437 bool selected = IsSelected(); 438 439 rgb_color noTint = fSuper->LowColor(); 440 rgb_color bgColor = noTint; 441 442 // set low color and fill background if selected 443 bool activated = selected && (enabled || Submenu()) 444 /*&& fSuper->fRedrawAfterSticky*/; 445 if (activated) { 446 bgColor = tint_color(bgColor, B_DARKEN_3_TINT); 447 if (be_control_look != NULL) { 448 BRect rect = Frame(); 449 be_control_look->DrawMenuItemBackground(fSuper, rect, rect, 450 noTint, BControlLook::B_ACTIVATED); 451 } else { 452 fSuper->SetLowColor(bgColor); 453 fSuper->FillRect(Frame(), B_SOLID_LOW); 454 } 455 } else { 456 fSuper->SetLowColor(bgColor); 457 } 458 459 // set high color 460 if (be_control_look != NULL) { 461 if (enabled) { 462 fSuper->SetHighColor(tint_color(fSuper->LowColor(), 463 B_DARKEN_MAX_TINT)); 464 } else { 465 fSuper->SetHighColor(tint_color(fSuper->LowColor(), 466 B_DISABLED_LABEL_TINT)); 467 } 468 } else { 469 if (enabled) 470 fSuper->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR)); 471 else 472 fSuper->SetHighColor(tint_color(bgColor, B_DISABLED_LABEL_TINT)); 473 } 474 475 // draw content 476 fSuper->MovePenTo(ContentLocation()); 477 DrawContent(); 478 479 // draw extra symbols 480 const menu_layout layout = MenuPrivate(fSuper).Layout(); 481 if (layout == B_ITEMS_IN_COLUMN) { 482 if (IsMarked()) 483 _DrawMarkSymbol(bgColor); 484 485 if (fShortcutChar) 486 _DrawShortcutSymbol(); 487 488 if (Submenu()) 489 _DrawSubmenuSymbol(bgColor); 490 } 491 492 fSuper->SetLowColor(noTint); 493 } 494 495 496 void 497 BMenuItem::Highlight(bool flag) 498 { 499 Menu()->Invalidate(Frame()); 500 } 501 502 503 bool 504 BMenuItem::IsSelected() const 505 { 506 return fSelected; 507 } 508 509 510 BPoint 511 BMenuItem::ContentLocation() const 512 { 513 const BRect &padding = MenuPrivate(fSuper).Padding(); 514 515 return BPoint(fBounds.left + padding.left, 516 fBounds.top + padding.top); 517 } 518 519 520 void BMenuItem::_ReservedMenuItem1() {} 521 void BMenuItem::_ReservedMenuItem2() {} 522 void BMenuItem::_ReservedMenuItem3() {} 523 void BMenuItem::_ReservedMenuItem4() {} 524 525 526 BMenuItem::BMenuItem(const BMenuItem &) 527 { 528 } 529 530 531 BMenuItem & 532 BMenuItem::operator=(const BMenuItem &) 533 { 534 return *this; 535 } 536 537 538 void 539 BMenuItem::_InitData() 540 { 541 fLabel = NULL; 542 fSubmenu = NULL; 543 fWindow = NULL; 544 fSuper = NULL; 545 fModifiers = 0; 546 fCachedWidth = 0; 547 fTriggerIndex = -1; 548 fUserTrigger = 0; 549 fTrigger = 0; 550 fShortcutChar = 0; 551 fMark = false; 552 fEnabled = true; 553 fSelected = false; 554 } 555 556 557 void 558 BMenuItem::_InitMenuData(BMenu *menu) 559 { 560 fSubmenu = menu; 561 562 MenuPrivate(fSubmenu).SetSuperItem(this); 563 564 BMenuItem *item = menu->FindMarked(); 565 566 if (menu->IsRadioMode() && menu->IsLabelFromMarked() && item != NULL) 567 SetLabel(item->Label()); 568 else 569 SetLabel(menu->Name()); 570 } 571 572 573 void 574 BMenuItem::Install(BWindow *window) 575 { 576 if (fSubmenu) { 577 MenuPrivate(fSubmenu).Install(window); 578 } 579 580 fWindow = window; 581 582 if (fShortcutChar != 0 && (fModifiers & B_COMMAND_KEY) && fWindow) 583 window->AddShortcut(fShortcutChar, fModifiers, this); 584 585 if (!Messenger().IsValid()) 586 SetTarget(window); 587 } 588 589 590 status_t 591 BMenuItem::Invoke(BMessage *message) 592 { 593 if (!IsEnabled()) 594 return B_ERROR; 595 596 if (fSuper->IsRadioMode()) 597 SetMarked(true); 598 599 bool notify = false; 600 uint32 kind = InvokeKind(¬ify); 601 602 BMessage clone(kind); 603 status_t err = B_BAD_VALUE; 604 605 if (!message && !notify) 606 message = Message(); 607 608 if (!message) { 609 if (!fSuper->IsWatched()) 610 return err; 611 } else 612 clone = *message; 613 614 clone.AddInt32("index", Menu()->IndexOf(this)); 615 clone.AddInt64("when", (int64)system_time()); 616 clone.AddPointer("source", this); 617 clone.AddMessenger("be:sender", BMessenger(fSuper)); 618 619 if (message) 620 err = BInvoker::Invoke(&clone); 621 622 // TODO: assynchronous messaging 623 // SendNotices(kind, &clone); 624 625 return err; 626 } 627 628 629 void 630 BMenuItem::Uninstall() 631 { 632 if (fSubmenu != NULL) { 633 MenuPrivate(fSubmenu).Uninstall(); 634 } 635 636 if (Target() == fWindow) 637 SetTarget(BMessenger()); 638 639 if (fShortcutChar != 0 && (fModifiers & B_COMMAND_KEY) != 0 640 && fWindow != NULL) 641 fWindow->RemoveShortcut(fShortcutChar, fModifiers); 642 643 fWindow = NULL; 644 } 645 646 647 void 648 BMenuItem::SetSuper(BMenu *super) 649 { 650 if (fSuper != NULL && super != NULL) 651 debugger("Error - can't add menu or menu item to more than 1 container (either menu or menubar)."); 652 653 if (fSubmenu != NULL) { 654 MenuPrivate(fSubmenu).SetSuper(super); 655 } 656 657 fSuper = super; 658 } 659 660 661 void 662 BMenuItem::Select(bool selected) 663 { 664 if (fSelected == selected) 665 return; 666 667 if (Submenu() || IsEnabled()) { 668 fSelected = selected; 669 Highlight(selected); 670 } 671 } 672 673 674 void 675 BMenuItem::_DrawMarkSymbol(rgb_color bgColor) 676 { 677 fSuper->PushState(); 678 679 BRect r(fBounds); 680 float leftMargin; 681 MenuPrivate(fSuper).GetItemMargins(&leftMargin, NULL, NULL, NULL); 682 r.right = r.left + leftMargin - 3; 683 r.left += 1; 684 685 BPoint center(floorf((r.left + r.right) / 2.0), 686 floorf((r.top + r.bottom) / 2.0)); 687 688 float size = min_c(r.Height() - 2, r.Width()); 689 r.top = floorf(center.y - size / 2 + 0.5); 690 r.bottom = floorf(center.y + size / 2 + 0.5); 691 r.left = floorf(center.x - size / 2 + 0.5); 692 r.right = floorf(center.x + size / 2 + 0.5); 693 694 BShape arrowShape; 695 center.x += 0.5; 696 center.y += 0.5; 697 size *= 0.3; 698 arrowShape.MoveTo(BPoint(center.x - size, center.y - size * 0.25)); 699 arrowShape.LineTo(BPoint(center.x - size * 0.25, center.y + size)); 700 arrowShape.LineTo(BPoint(center.x + size, center.y - size)); 701 702 fSuper->SetDrawingMode(B_OP_OVER); 703 fSuper->SetHighColor(tint_color(bgColor, B_DARKEN_MAX_TINT)); 704 fSuper->SetPenSize(2.0); 705 // NOTE: StrokeShape() offsets the shape by the current pen position, 706 // it is not documented in the BeBook, but it is true! 707 fSuper->MovePenTo(B_ORIGIN); 708 fSuper->StrokeShape(&arrowShape); 709 710 fSuper->PopState(); 711 } 712 713 714 void 715 BMenuItem::_DrawShortcutSymbol() 716 { 717 BMenu *menu = Menu(); 718 BFont font; 719 menu->GetFont(&font); 720 BPoint where = ContentLocation(); 721 where.x = fBounds.right - font.Size(); 722 723 if (fSubmenu) 724 where.x -= fBounds.Height() - 3; 725 726 const float ascent = MenuPrivate(fSuper).Ascent(); 727 if (fShortcutChar < B_SPACE && kUTF8ControlMap[(int)fShortcutChar]) 728 _DrawControlChar(fShortcutChar, where + BPoint(0, ascent)); 729 else 730 fSuper->DrawChar(fShortcutChar, where + BPoint(0, ascent)); 731 732 where.y += (fBounds.Height() - 11) / 2 - 1; 733 where.x -= 4; 734 735 if (fModifiers & B_COMMAND_KEY) { 736 const BBitmap *command = MenuPrivate::MenuItemCommand(); 737 const BRect &rect = command->Bounds(); 738 where.x -= rect.Width() + 1; 739 fSuper->DrawBitmap(command, where); 740 } 741 742 if (fModifiers & B_CONTROL_KEY) { 743 const BBitmap *control = MenuPrivate::MenuItemControl(); 744 const BRect &rect = control->Bounds(); 745 where.x -= rect.Width() + 1; 746 fSuper->DrawBitmap(control, where); 747 } 748 749 if (fModifiers & B_OPTION_KEY) { 750 const BBitmap *option = MenuPrivate::MenuItemOption(); 751 const BRect &rect = option->Bounds(); 752 where.x -= rect.Width() + 1; 753 fSuper->DrawBitmap(option, where); 754 } 755 756 if (fModifiers & B_SHIFT_KEY) { 757 const BBitmap *shift = MenuPrivate::MenuItemShift(); 758 const BRect &rect = shift->Bounds(); 759 where.x -= rect.Width() + 1; 760 fSuper->DrawBitmap(shift, where); 761 } 762 } 763 764 765 void 766 BMenuItem::_DrawSubmenuSymbol(rgb_color bgColor) 767 { 768 fSuper->PushState(); 769 770 BRect r(fBounds); 771 float rightMargin; 772 MenuPrivate(fSuper).GetItemMargins(NULL, NULL, &rightMargin, NULL); 773 r.left = r.right - rightMargin + 3; 774 r.right -= 1; 775 776 BPoint center(floorf((r.left + r.right) / 2.0), 777 floorf((r.top + r.bottom) / 2.0)); 778 779 float size = min_c(r.Height() - 2, r.Width()); 780 r.top = floorf(center.y - size / 2 + 0.5); 781 r.bottom = floorf(center.y + size / 2 + 0.5); 782 r.left = floorf(center.x - size / 2 + 0.5); 783 r.right = floorf(center.x + size / 2 + 0.5); 784 785 BShape arrowShape; 786 center.x += 0.5; 787 center.y += 0.5; 788 size *= 0.25; 789 float hSize = size * 0.7; 790 arrowShape.MoveTo(BPoint(center.x - hSize, center.y - size)); 791 arrowShape.LineTo(BPoint(center.x + hSize, center.y)); 792 arrowShape.LineTo(BPoint(center.x - hSize, center.y + size)); 793 794 fSuper->SetDrawingMode(B_OP_OVER); 795 fSuper->SetHighColor(tint_color(bgColor, B_DARKEN_MAX_TINT)); 796 fSuper->SetPenSize(ceilf(size * 0.4)); 797 // NOTE: StrokeShape() offsets the shape by the current pen position, 798 // it is not documented in the BeBook, but it is true! 799 fSuper->MovePenTo(B_ORIGIN); 800 fSuper->StrokeShape(&arrowShape); 801 802 fSuper->PopState(); 803 } 804 805 806 void 807 BMenuItem::_DrawControlChar(char shortcut, BPoint where) 808 { 809 // TODO: If needed, take another font for the control characters 810 // (or have font overlays in the app_server!) 811 const char* symbol = " "; 812 if (kUTF8ControlMap[(int)fShortcutChar]) 813 symbol = kUTF8ControlMap[(int)fShortcutChar]; 814 815 fSuper->DrawString(symbol, where); 816 } 817 818 819 void 820 BMenuItem::SetAutomaticTrigger(int32 index, uint32 trigger) 821 { 822 fTriggerIndex = index; 823 fTrigger = trigger; 824 } 825