1 /* 2 * Copyright (c) 2001-2007, Haiku, Inc. 3 * Distributed under the terms of the MIT license. 4 * 5 * Authors: 6 * Marc Flerackers (mflerackers@androme.be) 7 * Stephan Aßmus <superstippi@gmx.de> 8 * DarkWyrm <bpmagic@columbus.rr.com> 9 * Axel Dörfler, axeld@pinc-software.de 10 */ 11 12 13 #include <Box.h> 14 15 #include <stdio.h> 16 #include <stdlib.h> 17 #include <string.h> 18 19 #include <Layout.h> 20 #include <LayoutUtils.h> 21 #include <Message.h> 22 #include <Region.h> 23 24 25 struct BBox::LayoutData { 26 LayoutData() 27 : valid(false) 28 { 29 } 30 31 BRect label_box; // label box (label string or label view); in case 32 // of a label string not including descent 33 BRect insets; // insets induced by border and label 34 BSize min; 35 BSize max; 36 BSize preferred; 37 bool valid; // validity the other fields 38 }; 39 40 41 BBox::BBox(BRect frame, const char *name, uint32 resizingMode, uint32 flags, 42 border_style border) 43 : BView(frame, name, resizingMode, flags | B_WILL_DRAW | B_FRAME_EVENTS), 44 fStyle(border) 45 { 46 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 47 SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 48 49 _InitObject(); 50 } 51 52 53 BBox::BBox(const char* name, uint32 flags, border_style border, BView* child) 54 : BView(BRect(0, 0, -1, -1), name, B_FOLLOW_NONE, 55 flags | B_WILL_DRAW | B_FRAME_EVENTS | B_SUPPORTS_LAYOUT), 56 fStyle(border) 57 { 58 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 59 SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 60 61 _InitObject(); 62 63 if (child) 64 AddChild(child); 65 } 66 67 68 BBox::BBox(border_style border, BView* child) 69 : BView(BRect(0, 0, -1, -1), NULL, B_FOLLOW_NONE, 70 B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP | B_SUPPORTS_LAYOUT), 71 fStyle(border) 72 { 73 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 74 SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 75 76 _InitObject(); 77 78 if (child) 79 AddChild(child); 80 } 81 82 83 BBox::BBox(BMessage *archive) 84 : BView(archive), 85 fStyle(B_FANCY_BORDER) 86 { 87 _InitObject(archive); 88 } 89 90 91 BBox::~BBox() 92 { 93 _ClearLabel(); 94 95 delete fLayoutData; 96 } 97 98 99 BArchivable * 100 BBox::Instantiate(BMessage *archive) 101 { 102 if (validate_instantiation(archive, "BBox")) 103 return new BBox(archive); 104 105 return NULL; 106 } 107 108 109 status_t 110 BBox::Archive(BMessage *archive, bool deep) const 111 { 112 status_t ret = BView::Archive(archive, deep); 113 114 if (fLabel && ret == B_OK) 115 ret = archive->AddString("_label", fLabel); 116 117 if (fLabelView && ret == B_OK) 118 ret = archive->AddBool("_lblview", true); 119 120 if (fStyle != B_FANCY_BORDER && ret == B_OK) 121 ret = archive->AddInt32("_style", fStyle); 122 123 return ret; 124 } 125 126 127 void 128 BBox::SetBorder(border_style border) 129 { 130 if (border == fStyle) 131 return; 132 133 fStyle = border; 134 135 InvalidateLayout(); 136 137 if (Window() != NULL && LockLooper()) { 138 Invalidate(); 139 UnlockLooper(); 140 } 141 } 142 143 144 border_style 145 BBox::Border() const 146 { 147 return fStyle; 148 } 149 150 151 //! This function is not part of the R5 API and is not yet finalized yet 152 float 153 BBox::TopBorderOffset() 154 { 155 _ValidateLayoutData(); 156 157 if (fLabel != NULL || fLabelView != NULL) 158 return fLayoutData->label_box.Height() / 2; 159 160 return 0; 161 } 162 163 164 //! This function is not part of the R5 API and is not yet finalized yet 165 BRect 166 BBox::InnerFrame() 167 { 168 _ValidateLayoutData(); 169 170 BRect frame(Bounds()); 171 frame.left += fLayoutData->insets.left; 172 frame.top += fLayoutData->insets.top; 173 frame.right -= fLayoutData->insets.right; 174 frame.bottom -= fLayoutData->insets.bottom; 175 176 return frame; 177 } 178 179 180 void 181 BBox::SetLabel(const char *string) 182 { 183 _ClearLabel(); 184 185 if (string) 186 fLabel = strdup(string); 187 188 InvalidateLayout(); 189 190 if (Window()) 191 Invalidate(); 192 } 193 194 195 status_t 196 BBox::SetLabel(BView *viewLabel) 197 { 198 _ClearLabel(); 199 200 if (viewLabel) { 201 fLabelView = viewLabel; 202 fLabelView->MoveTo(10.0f, 0.0f); 203 AddChild(fLabelView, ChildAt(0)); 204 } 205 206 InvalidateLayout(); 207 208 if (Window()) 209 Invalidate(); 210 211 return B_OK; 212 } 213 214 215 const char * 216 BBox::Label() const 217 { 218 return fLabel; 219 } 220 221 222 BView * 223 BBox::LabelView() const 224 { 225 return fLabelView; 226 } 227 228 229 void 230 BBox::Draw(BRect updateRect) 231 { 232 _ValidateLayoutData(); 233 234 PushState(); 235 236 BRect labelBox = BRect(0, 0, 0, 0); 237 if (fLabel != NULL) { 238 labelBox = fLayoutData->label_box; 239 BRegion update(updateRect); 240 update.Exclude(labelBox); 241 242 ConstrainClippingRegion(&update); 243 } else if (fLabelView != NULL) 244 labelBox = fLabelView->Bounds(); 245 246 switch (fStyle) { 247 case B_FANCY_BORDER: 248 _DrawFancy(labelBox); 249 break; 250 251 case B_PLAIN_BORDER: 252 _DrawPlain(labelBox); 253 break; 254 255 default: 256 break; 257 } 258 259 if (fLabel) { 260 ConstrainClippingRegion(NULL); 261 262 font_height fontHeight; 263 GetFontHeight(&fontHeight); 264 265 SetHighColor(0, 0, 0); 266 DrawString(fLabel, BPoint(10.0f, ceilf(fontHeight.ascent))); 267 } 268 269 PopState(); 270 } 271 272 273 void 274 BBox::AttachedToWindow() 275 { 276 if (Parent()) { 277 SetViewColor(Parent()->ViewColor()); 278 SetLowColor(Parent()->ViewColor()); 279 } 280 281 // The box could have been resized in the mean time 282 fBounds = Bounds(); 283 } 284 285 286 void 287 BBox::DetachedFromWindow() 288 { 289 BView::DetachedFromWindow(); 290 } 291 292 293 void 294 BBox::AllAttached() 295 { 296 BView::AllAttached(); 297 } 298 299 300 void 301 BBox::AllDetached() 302 { 303 BView::AllDetached(); 304 } 305 306 307 void 308 BBox::FrameResized(float width, float height) 309 { 310 BRect bounds(Bounds()); 311 312 // invalidate the regions that the app_server did not 313 // (for removing the previous or drawing the new border) 314 if (fStyle != B_NO_BORDER) { 315 316 int32 borderSize = fStyle == B_PLAIN_BORDER ? 0 : 1; 317 318 BRect invalid(bounds); 319 if (fBounds.right < bounds.right) { 320 // enlarging 321 invalid.left = fBounds.right - borderSize; 322 invalid.right = fBounds.right; 323 324 Invalidate(invalid); 325 } else if (fBounds.right > bounds.right) { 326 // shrinking 327 invalid.left = bounds.right - borderSize; 328 329 Invalidate(invalid); 330 } 331 332 invalid = bounds; 333 if (fBounds.bottom < bounds.bottom) { 334 // enlarging 335 invalid.top = fBounds.bottom - borderSize; 336 invalid.bottom = fBounds.bottom; 337 338 Invalidate(invalid); 339 } else if (fBounds.bottom > bounds.bottom) { 340 // shrinking 341 invalid.top = bounds.bottom - borderSize; 342 343 Invalidate(invalid); 344 } 345 } 346 347 fBounds.right = bounds.right; 348 fBounds.bottom = bounds.bottom; 349 } 350 351 352 void 353 BBox::MessageReceived(BMessage *message) 354 { 355 BView::MessageReceived(message); 356 } 357 358 359 void 360 BBox::MouseDown(BPoint point) 361 { 362 BView::MouseDown(point); 363 } 364 365 366 void 367 BBox::MouseUp(BPoint point) 368 { 369 BView::MouseUp(point); 370 } 371 372 373 void 374 BBox::WindowActivated(bool active) 375 { 376 BView::WindowActivated(active); 377 } 378 379 380 void 381 BBox::MouseMoved(BPoint point, uint32 transit, const BMessage *message) 382 { 383 BView::MouseMoved(point, transit, message); 384 } 385 386 387 void 388 BBox::FrameMoved(BPoint newLocation) 389 { 390 BView::FrameMoved(newLocation); 391 } 392 393 394 BHandler * 395 BBox::ResolveSpecifier(BMessage *message, int32 index, 396 BMessage *specifier, int32 what, 397 const char *property) 398 { 399 return BView::ResolveSpecifier(message, index, specifier, what, property); 400 } 401 402 403 void 404 BBox::ResizeToPreferred() 405 { 406 float width, height; 407 GetPreferredSize(&width, &height); 408 409 // make sure the box don't get smaller than it already is 410 if (width < Bounds().Width()) 411 width = Bounds().Width(); 412 if (height < Bounds().Height()) 413 height = Bounds().Height(); 414 415 BView::ResizeTo(width, height); 416 } 417 418 419 void 420 BBox::GetPreferredSize(float *_width, float *_height) 421 { 422 _ValidateLayoutData(); 423 424 if (_width) 425 *_width = fLayoutData->preferred.width; 426 if (_height) 427 *_height = fLayoutData->preferred.height; 428 } 429 430 431 void 432 BBox::MakeFocus(bool focused) 433 { 434 BView::MakeFocus(focused); 435 } 436 437 438 status_t 439 BBox::GetSupportedSuites(BMessage *message) 440 { 441 return BView::GetSupportedSuites(message); 442 } 443 444 445 status_t 446 BBox::Perform(perform_code d, void *arg) 447 { 448 return BView::Perform(d, arg); 449 } 450 451 452 BSize 453 BBox::MinSize() 454 { 455 _ValidateLayoutData(); 456 457 BSize size = (GetLayout() ? GetLayout()->MinSize() : fLayoutData->min); 458 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size); 459 } 460 461 462 BSize 463 BBox::MaxSize() 464 { 465 _ValidateLayoutData(); 466 467 BSize size = (GetLayout() ? GetLayout()->MaxSize() : fLayoutData->max); 468 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size); 469 } 470 471 472 BSize 473 BBox::PreferredSize() 474 { 475 _ValidateLayoutData(); 476 477 BSize size = (GetLayout() ? GetLayout()->PreferredSize() 478 : fLayoutData->preferred); 479 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), size); 480 } 481 482 483 void 484 BBox::InvalidateLayout(bool descendants) 485 { 486 fLayoutData->valid = false; 487 BView::InvalidateLayout(descendants); 488 } 489 490 491 void 492 BBox::DoLayout() 493 { 494 // Bail out, if we shan't do layout. 495 if (!(Flags() & B_SUPPORTS_LAYOUT)) 496 return; 497 498 // If the user set a layout, we let the base class version call its 499 // hook. 500 if (GetLayout()) { 501 BView::DoLayout(); 502 return; 503 } 504 505 _ValidateLayoutData(); 506 507 // layout the label view 508 if (fLabelView) { 509 fLabelView->MoveTo(fLayoutData->label_box.LeftTop()); 510 fLabelView->ResizeTo(fLayoutData->label_box.Size()); 511 } 512 513 // layout the child 514 if (BView* child = _Child()) { 515 BRect frame(Bounds()); 516 frame.left += fLayoutData->insets.left; 517 frame.top += fLayoutData->insets.top; 518 frame.right -= fLayoutData->insets.right; 519 frame.bottom -= fLayoutData->insets.bottom; 520 521 BLayoutUtils::AlignInFrame(child, frame); 522 } 523 } 524 525 526 void BBox::_ReservedBox1() {} 527 void BBox::_ReservedBox2() {} 528 529 530 BBox & 531 BBox::operator=(const BBox &) 532 { 533 return *this; 534 } 535 536 537 void 538 BBox::_InitObject(BMessage* archive) 539 { 540 fBounds = Bounds(); 541 542 fLabel = NULL; 543 fLabelView = NULL; 544 fLayoutData = new LayoutData; 545 546 int32 flags = 0; 547 548 BFont font(be_bold_font); 549 550 if (!archive || !archive->HasString("_fname")) 551 flags = B_FONT_FAMILY_AND_STYLE; 552 553 if (!archive || !archive->HasFloat("_fflt")) 554 flags |= B_FONT_SIZE; 555 556 if (flags != 0) 557 SetFont(&font, flags); 558 559 if (archive != NULL) { 560 const char *string; 561 if (archive->FindString("_label", &string) == B_OK) 562 SetLabel(string); 563 564 bool fancy; 565 int32 style; 566 567 if (archive->FindBool("_style", &fancy) == B_OK) 568 fStyle = fancy ? B_FANCY_BORDER : B_PLAIN_BORDER; 569 else if (archive->FindInt32("_style", &style) == B_OK) 570 fStyle = (border_style)style; 571 572 bool hasLabelView; 573 if (archive->FindBool("_lblview", &hasLabelView) == B_OK) 574 fLabelView = ChildAt(0); 575 } 576 } 577 578 579 void 580 BBox::_DrawPlain(BRect labelBox) 581 { 582 BRect rect = Bounds(); 583 rect.top += TopBorderOffset(); 584 585 rgb_color shadow = tint_color(ViewColor(), B_DARKEN_3_TINT); 586 587 if (rect.Height() == 0.0 || rect.Width() == 0.0) { 588 // used as separator 589 SetHighColor(shadow); 590 StrokeLine(rect.LeftTop(),rect.RightBottom()); 591 } else { 592 // used as box 593 rgb_color light = tint_color(ViewColor(), B_LIGHTEN_MAX_TINT); 594 BeginLineArray(4); 595 AddLine(BPoint(rect.left, rect.bottom), 596 BPoint(rect.left, rect.top), light); 597 AddLine(BPoint(rect.left + 1.0f, rect.top), 598 BPoint(rect.right, rect.top), light); 599 AddLine(BPoint(rect.left + 1.0f, rect.bottom), 600 BPoint(rect.right, rect.bottom), shadow); 601 AddLine(BPoint(rect.right, rect.bottom - 1.0f), 602 BPoint(rect.right, rect.top + 1.0f), shadow); 603 EndLineArray(); 604 } 605 } 606 607 608 void 609 BBox::_DrawFancy(BRect labelBox) 610 { 611 BRect rect = Bounds(); 612 rect.top += TopBorderOffset(); 613 614 rgb_color light = tint_color(ViewColor(), B_LIGHTEN_MAX_TINT); 615 rgb_color shadow = tint_color(ViewColor(), B_DARKEN_3_TINT); 616 617 if (rect.Height() == 1.0) { 618 // used as horizontal separator 619 BeginLineArray(2); 620 AddLine(BPoint(rect.left, rect.top), 621 BPoint(rect.right, rect.top), shadow); 622 AddLine(BPoint(rect.left, rect.bottom), 623 BPoint(rect.right, rect.bottom), light); 624 EndLineArray(); 625 } else if (rect.Width() == 1.0) { 626 // used as vertical separator 627 BeginLineArray(2); 628 AddLine(BPoint(rect.left, rect.top), 629 BPoint(rect.left, rect.bottom), shadow); 630 AddLine(BPoint(rect.right, rect.top), 631 BPoint(rect.right, rect.bottom), light); 632 EndLineArray(); 633 } else { 634 // used as box 635 BeginLineArray(8); 636 AddLine(BPoint(rect.left, rect.bottom - 1.0), 637 BPoint(rect.left, rect.top), shadow); 638 AddLine(BPoint(rect.left + 1.0, rect.top), 639 BPoint(rect.right - 1.0, rect.top), shadow); 640 AddLine(BPoint(rect.left, rect.bottom), 641 BPoint(rect.right, rect.bottom), light); 642 AddLine(BPoint(rect.right, rect.bottom - 1.0), 643 BPoint(rect.right, rect.top), light); 644 645 rect.InsetBy(1.0, 1.0); 646 647 AddLine(BPoint(rect.left, rect.bottom - 1.0), 648 BPoint(rect.left, rect.top), light); 649 AddLine(BPoint(rect.left + 1.0, rect.top), 650 BPoint(rect.right - 1.0, rect.top), light); 651 AddLine(BPoint(rect.left, rect.bottom), 652 BPoint(rect.right, rect.bottom), shadow); 653 AddLine(BPoint(rect.right, rect.bottom - 1.0), 654 BPoint(rect.right, rect.top), shadow); 655 EndLineArray(); 656 } 657 } 658 659 660 void 661 BBox::_ClearLabel() 662 { 663 fBounds.top = 0; 664 665 if (fLabel) { 666 free(fLabel); 667 fLabel = NULL; 668 } else if (fLabelView) { 669 fLabelView->RemoveSelf(); 670 delete fLabelView; 671 fLabelView = NULL; 672 } 673 } 674 675 676 BView* 677 BBox::_Child() const 678 { 679 for (int32 i = 0; BView* view = ChildAt(i); i++) { 680 if (view != fLabelView) 681 return view; 682 } 683 684 return NULL; 685 } 686 687 688 void 689 BBox::_ValidateLayoutData() 690 { 691 if (fLayoutData->valid) 692 return; 693 694 // compute the label box, width and height 695 bool label = true; 696 float labelHeight = 0; // height of the label (pixel count) 697 if (fLabel) { 698 // leave 6 pixels of the frame, and have a gap of 4 pixels between 699 // the frame and the text on either side 700 font_height fontHeight; 701 GetFontHeight(&fontHeight); 702 fLayoutData->label_box.Set(6.0f, 0, 14.0f + StringWidth(fLabel), 703 ceilf(fontHeight.ascent)); 704 labelHeight = ceilf(fontHeight.ascent + fontHeight.descent) + 1; 705 } else if (fLabelView) { 706 // the label view is placed at (0, 10) at its preferred size 707 BSize size = fLabelView->PreferredSize(); 708 fLayoutData->label_box.Set(10, 0, 10 + size.width, size.height); 709 labelHeight = size.height + 1; 710 } else { 711 label = false; 712 } 713 714 // border 715 switch (fStyle) { 716 case B_PLAIN_BORDER: 717 fLayoutData->insets.Set(1, 1, 1, 1); 718 break; 719 case B_FANCY_BORDER: 720 fLayoutData->insets.Set(2, 2, 2, 2); 721 break; 722 case B_NO_BORDER: 723 default: 724 fLayoutData->insets.Set(0, 0, 0, 0); 725 break; 726 } 727 728 // if there's a label, the top inset will be dictated by the label 729 if (label && labelHeight > fLayoutData->insets.top) 730 fLayoutData->insets.top = labelHeight; 731 732 // total number of pixel the border adds 733 float addWidth = fLayoutData->insets.left + fLayoutData->insets.right; 734 float addHeight = fLayoutData->insets.top + fLayoutData->insets.bottom; 735 736 // compute the minimal width induced by the label 737 float minWidth; 738 if (label) 739 minWidth = fLayoutData->label_box.right + fLayoutData->insets.right; 740 else 741 minWidth = addWidth - 1; 742 743 // finally consider the child constraints, if we shall support layout 744 BView* child = _Child(); 745 if (child && (Flags() & B_SUPPORTS_LAYOUT)) { 746 BSize min = child->MinSize(); 747 BSize max = child->MaxSize(); 748 BSize preferred = child->PreferredSize(); 749 750 min.width += addWidth; 751 min.height += addHeight; 752 preferred.width += addWidth; 753 preferred.height += addHeight; 754 max.width = BLayoutUtils::AddDistances(max.width, addWidth - 1); 755 max.height = BLayoutUtils::AddDistances(max.height, addHeight - 1); 756 757 if (min.width < minWidth) 758 min.width = minWidth; 759 BLayoutUtils::FixSizeConstraints(min, max, preferred); 760 761 fLayoutData->min = min; 762 fLayoutData->max = max; 763 fLayoutData->preferred = preferred; 764 } else { 765 fLayoutData->min.Set(minWidth, addHeight - 1); 766 fLayoutData->max.Set(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED); 767 fLayoutData->preferred = fLayoutData->min; 768 } 769 770 fLayoutData->valid = true; 771 } 772 773