Ticket #5525: splitLayout.patch
File splitLayout.patch, 15.3 KB (added by , 14 years ago) |
---|
-
src/kits/interface/SplitLayout.cpp
5 5 6 6 #include "SplitLayout.h" 7 7 8 #include <new> 8 9 #include <stdio.h> 9 10 10 11 #include <LayoutItem.h> 11 12 #include <LayoutUtils.h> 13 #include <Message.h> 12 14 #include <View.h> 13 15 14 16 #include "OneElementLayouter.h" 15 17 #include "SimpleLayouter.h" 16 18 17 // ItemLayoutInfo 19 // archivng constants 20 namespace { 21 const char* kInfoCollapsibleField = "BSplitLayout::info::collapsible"; 22 const char* kInfoWeightField = "BSplitLayout::info::weight"; 23 const char* kSpacingField = "BSplitLayout::spacing"; 24 const char* kSplitterSizeField = "BSplitLayout::splitterSize"; 25 const char* kIsVerticalField = "BSplitLayout::vertical"; 26 const char* kTopInsetField = "BSplitLayout::TopInset"; 27 const char* kBottomInsetField = "BSplitLayout::BottomInset"; 28 const char* kLeftInsetField = "BSplitLayout::LeftInset"; 29 const char* kRightInsetField = "BSplitLayout::RightInset"; 30 } 31 32 18 33 class BSplitLayout::ItemLayoutInfo { 19 34 public: 20 35 float weight; … … 25 40 bool isCollapsible; 26 41 27 42 ItemLayoutInfo() 28 : weight(1.0f), 29 layoutFrame(0, 0, -1, -1), 30 min(), 31 max(), 32 isVisible(true), 33 isCollapsible(true) 43 : 44 weight(1.0f), 45 layoutFrame(0, 0, -1, -1), 46 min(), 47 max(), 48 isVisible(true), 49 isCollapsible(true) 34 50 { 35 51 } 36 52 }; 37 53 38 // ValueRange 54 39 55 class BSplitLayout::ValueRange { 40 56 public: 41 57 int32 sumValue; // including spacing … … 47 63 int32 nextSize; 48 64 }; 49 65 50 // SplitterItem 66 51 67 class BSplitLayout::SplitterItem : public BLayoutItem { 52 68 public: 53 69 SplitterItem(BSplitLayout* layout) 54 : fLayout(layout), 55 fFrame() 70 : 71 fLayout(layout), 72 fFrame() 56 73 { 57 74 } 58 75 76 59 77 virtual BSize MinSize() 60 78 { 61 79 if (fLayout->Orientation() == B_HORIZONTAL) … … 128 146 BRect fFrame; 129 147 }; 130 148 131 132 149 // #pragma mark - 133 150 134 151 135 // constructor 152 136 153 BSplitLayout::BSplitLayout(enum orientation orientation, 137 154 float spacing) 138 : fOrientation(orientation), 139 fLeftInset(0), 140 fRightInset(0), 141 fTopInset(0), 142 fBottomInset(0), 143 fSplitterSize(6), 144 fSpacing(spacing), 155 : 156 fOrientation(orientation), 157 fLeftInset(0), 158 fRightInset(0), 159 fTopInset(0), 160 fBottomInset(0), 161 fSplitterSize(6), 162 fSpacing(spacing), 145 163 146 147 148 149 150 164 fSplitterItems(), 165 fVisibleItems(), 166 fMin(), 167 fMax(), 168 fPreferred(), 151 169 152 153 154 155 170 fHorizontalLayouter(NULL), 171 fVerticalLayouter(NULL), 172 fHorizontalLayoutInfo(NULL), 173 fVerticalLayoutInfo(NULL), 156 174 157 158 159 175 fHeightForWidthItems(), 176 fHeightForWidthVerticalLayouter(NULL), 177 fHeightForWidthHorizontalLayoutInfo(NULL), 160 178 161 179 fLayoutValid(false), 162 180 163 164 165 166 167 181 fCachedHeightForWidthWidth(-2), 182 fHeightForWidthVerticalLayouterWidth(-2), 183 fCachedMinHeightForWidth(-1), 184 fCachedMaxHeightForWidth(-1), 185 fCachedPreferredHeightForWidth(-1), 168 186 169 170 171 172 187 fDraggingStartPoint(), 188 fDraggingStartValue(0), 189 fDraggingCurrentValue(0), 190 fDraggingSplitterIndex(-1) 173 191 { 174 192 } 175 193 176 // destructor 194 195 BSplitLayout::BSplitLayout(BMessage* from) 196 : 197 BLayout(from), 198 fSplitterItems(), 199 fVisibleItems(), 200 fMin(), 201 fMax(), 202 fPreferred(), 203 204 fHorizontalLayouter(NULL), 205 fVerticalLayouter(NULL), 206 fHorizontalLayoutInfo(NULL), 207 fVerticalLayoutInfo(NULL), 208 209 fHeightForWidthItems(), 210 fHeightForWidthVerticalLayouter(NULL), 211 fHeightForWidthHorizontalLayoutInfo(NULL), 212 213 fLayoutValid(false), 214 215 fCachedHeightForWidthWidth(-2), 216 fHeightForWidthVerticalLayouterWidth(-2), 217 fCachedMinHeightForWidth(-1), 218 fCachedMaxHeightForWidth(-1), 219 fCachedPreferredHeightForWidth(-1), 220 221 fDraggingStartPoint(), 222 fDraggingStartValue(0), 223 fDraggingCurrentValue(0), 224 fDraggingSplitterIndex(-1) 225 { 226 bool isVertical; 227 from->FindBool(kIsVerticalField, &isVertical); 228 fOrientation = (isVertical) ? B_VERTICAL : B_HORIZONTAL ; 229 230 from->FindFloat(kLeftInsetField, &fLeftInset); 231 from->FindFloat(kRightInsetField, &fRightInset); 232 from->FindFloat(kTopInsetField, &fTopInset); 233 from->FindFloat(kBottomInsetField, &fBottomInset); 234 from->FindFloat(kSplitterSizeField, &fSplitterSize); 235 from->FindFloat(kSpacingField, &fSpacing); 236 } 237 238 177 239 BSplitLayout::~BSplitLayout() 178 240 { 179 241 } 180 242 181 // SetInsets 243 182 244 void 183 245 BSplitLayout::SetInsets(float left, float top, float right, float bottom) 184 246 { … … 190 252 InvalidateLayout(); 191 253 } 192 254 193 // GetInsets 255 194 256 void 195 257 BSplitLayout::GetInsets(float* left, float* top, float* right, 196 258 float* bottom) const … … 205 267 *bottom = fBottomInset; 206 268 } 207 269 208 // Spacing 270 209 271 float 210 272 BSplitLayout::Spacing() const 211 273 { 212 274 return fSpacing; 213 275 } 214 276 215 // SetSpacing 277 216 278 void 217 279 BSplitLayout::SetSpacing(float spacing) 218 280 { … … 223 285 } 224 286 } 225 287 226 // Orientation 288 227 289 orientation 228 290 BSplitLayout::Orientation() const 229 291 { 230 292 return fOrientation; 231 293 } 232 294 233 // SetOrientation 295 234 296 void 235 297 BSplitLayout::SetOrientation(enum orientation orientation) 236 298 { … … 241 303 } 242 304 } 243 305 244 // SplitterSize 306 245 307 float 246 308 BSplitLayout::SplitterSize() const 247 309 { 248 310 return fSplitterSize; 249 311 } 250 312 251 // SetSplitterSize 313 252 314 void 253 315 BSplitLayout::SetSplitterSize(float size) 254 316 { … … 259 321 } 260 322 } 261 323 262 // AddView 324 263 325 BLayoutItem* 264 326 BSplitLayout::AddView(BView* child) 265 327 { 266 328 return BLayout::AddView(child); 267 329 } 268 330 269 // AddView 331 270 332 BLayoutItem* 271 333 BSplitLayout::AddView(int32 index, BView* child) 272 334 { 273 335 return BLayout::AddView(index, child); 274 336 } 275 337 276 // AddView 338 277 339 BLayoutItem* 278 340 BSplitLayout::AddView(BView* child, float weight) 279 341 { 280 342 return AddView(-1, child, weight); 281 343 } 282 344 283 // AddView 345 284 346 BLayoutItem* 285 347 BSplitLayout::AddView(int32 index, BView* child, float weight) 286 348 { … … 291 353 return item; 292 354 } 293 355 294 // AddItem 356 295 357 bool 296 358 BSplitLayout::AddItem(BLayoutItem* item) 297 359 { 298 360 return BLayout::AddItem(item); 299 361 } 300 362 301 // AddItem 363 302 364 bool 303 365 BSplitLayout::AddItem(int32 index, BLayoutItem* item) 304 366 { 305 367 return BLayout::AddItem(index, item); 306 368 } 307 369 308 // AddItem 370 309 371 bool 310 372 BSplitLayout::AddItem(BLayoutItem* item, float weight) 311 373 { 312 374 return AddItem(-1, item, weight); 313 375 } 314 376 315 // AddItem 377 316 378 bool 317 379 BSplitLayout::AddItem(int32 index, BLayoutItem* item, float weight) 318 380 { … … 323 385 return success; 324 386 } 325 387 326 // ItemWeight 388 327 389 float 328 390 BSplitLayout::ItemWeight(int32 index) const 329 391 { … … 333 395 return ItemWeight(ItemAt(index)); 334 396 } 335 397 336 // ItemWeight 398 337 399 float 338 400 BSplitLayout::ItemWeight(BLayoutItem* item) const 339 401 { … … 342 404 return 0; 343 405 } 344 406 345 // SetItemWeight 407 346 408 void 347 409 BSplitLayout::SetItemWeight(int32 index, float weight, bool invalidateLayout) 348 410 { … … 366 428 InvalidateLayout(); 367 429 } 368 430 369 // SetItemWeight 431 370 432 void 371 433 BSplitLayout::SetItemWeight(BLayoutItem* item, float weight) 372 434 { … … 374 436 info->weight = weight; 375 437 } 376 438 377 // SetCollapsible 439 378 440 void 379 441 BSplitLayout::SetCollapsible(bool collapsible) 380 442 { 381 443 SetCollapsible(0, CountItems() - 1, collapsible); 382 444 } 383 445 384 // SetCollapsible 446 385 447 void 386 448 BSplitLayout::SetCollapsible(int32 index, bool collapsible) 387 449 { 388 450 SetCollapsible(index, index, collapsible); 389 451 } 390 452 391 // SetCollapsible 453 392 454 void 393 455 BSplitLayout::SetCollapsible(int32 first, int32 last, bool collapsible) 394 456 { … … 401 463 _ItemLayoutInfo(ItemAt(i))->isCollapsible = collapsible; 402 464 } 403 465 404 // MinSize 466 405 467 BSize 406 468 BSplitLayout::MinSize() 407 469 { … … 410 472 return _AddInsets(fMin); 411 473 } 412 474 413 // MaxSize 475 414 476 BSize 415 477 BSplitLayout::MaxSize() 416 478 { … … 419 481 return _AddInsets(fMax); 420 482 } 421 483 422 // PreferredSize 484 423 485 BSize 424 486 BSplitLayout::PreferredSize() 425 487 { … … 428 490 return _AddInsets(fPreferred); 429 491 } 430 492 431 // Alignment 493 432 494 BAlignment 433 495 BSplitLayout::Alignment() 434 496 { 435 497 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT); 436 498 } 437 499 438 // HasHeightForWidth 500 439 501 bool 440 502 BSplitLayout::HasHeightForWidth() 441 503 { … … 444 506 return !fHeightForWidthItems.IsEmpty(); 445 507 } 446 508 447 // GetHeightForWidth 509 448 510 void 449 511 BSplitLayout::GetHeightForWidth(float width, float* min, float* max, 450 512 float* preferred) … … 457 519 _AddInsets(min, max, preferred); 458 520 } 459 521 460 // InvalidateLayout 522 461 523 void 462 524 BSplitLayout::InvalidateLayout() 463 525 { 464 526 _InvalidateLayout(true); 465 527 } 466 528 467 // LayoutView 529 468 530 void 469 531 BSplitLayout::LayoutView() 470 532 { … … 543 605 fLayoutValid = true; 544 606 } 545 607 546 // SplitterItemFrame 608 547 609 BRect 548 610 BSplitLayout::SplitterItemFrame(int32 index) const 549 611 { … … 552 614 return BRect(); 553 615 } 554 616 555 // IsAboveSplitter 617 556 618 bool 557 619 BSplitLayout::IsAboveSplitter(const BPoint& point) const 558 620 { 559 621 return _SplitterItemAt(point) != NULL; 560 622 } 561 623 562 // StartDraggingSplitter 624 563 625 bool 564 626 BSplitLayout::StartDraggingSplitter(BPoint point) 565 627 { … … 589 651 return false; 590 652 } 591 653 592 // DragSplitter 654 593 655 bool 594 656 BSplitLayout::DragSplitter(BPoint point) 595 657 { … … 608 670 fDraggingStartValue + valueDiff); 609 671 } 610 672 611 // StopDraggingSplitter 673 612 674 bool 613 675 BSplitLayout::StopDraggingSplitter() 614 676 { … … 623 685 return true; 624 686 } 625 687 626 // DraggedSplitter 688 627 689 int32 628 690 BSplitLayout::DraggedSplitter() const 629 691 { 630 692 return fDraggingSplitterIndex; 631 693 } 632 694 633 // ItemAdded 695 696 status_t 697 BSplitLayout::Archive(BMessage* into, bool deep) const 698 { 699 BArchiver archiver(into); 700 status_t err = BLayout::Archive(into, deep); 701 702 if (err == B_OK) 703 into->AddBool(kIsVerticalField, fOrientation == B_VERTICAL); 704 705 if (err == B_OK) 706 err = into->AddFloat(kLeftInsetField, fLeftInset); 707 708 if (err == B_OK) 709 err = into->AddFloat(kRightInsetField, fRightInset); 710 711 if (err == B_OK) 712 err = into->AddFloat(kTopInsetField, fTopInset); 713 714 if (err == B_OK) 715 err = into->AddFloat(kBottomInsetField, fBottomInset); 716 717 if (err == B_OK) 718 err = into->AddFloat(kSplitterSizeField, fSplitterSize); 719 720 if (err == B_OK) 721 err = into->AddFloat(kSpacingField, fSpacing); 722 723 return archiver.Finish(err); 724 } 725 726 727 BArchivable* 728 BSplitLayout::Instantiate(BMessage* from) 729 { 730 if (validate_instantiation(from, "BSplitLayout")) 731 return new(std::nothrow) BSplitLayout(from); 732 return NULL; 733 } 734 735 736 status_t 737 BSplitLayout::ItemArchived(BMessage* into, BLayoutItem* item, int32 index) const 738 { 739 ItemLayoutInfo* info = (ItemLayoutInfo*)item->LayoutData(); 740 741 if (!info) // TODO: remove this check when AddItem() returns a bool 742 return B_ERROR; 743 744 status_t err = into->AddFloat(kInfoWeightField, info->weight); 745 if (err == B_OK) 746 err = into->AddBool(kInfoCollapsibleField, info->isCollapsible); 747 748 return err; 749 } 750 751 752 status_t 753 BSplitLayout::ItemUnarchived(const BMessage* from, 754 BLayoutItem* item, int32 index) 755 { 756 ItemLayoutInfo* info = _ItemLayoutInfo(item); 757 status_t err = from->FindFloat(kInfoWeightField, index, &info->weight); 758 759 if (err == B_OK) { 760 bool* collapsible = &info->isCollapsible; 761 err = from->FindBool(kInfoCollapsibleField, index, collapsible); 762 } 763 return err; 764 } 765 766 634 767 void 635 768 BSplitLayout::ItemAdded(BLayoutItem* item) 636 769 { … … 643 776 SetItemWeight(item, 1); 644 777 } 645 778 646 // ItemRemoved 779 647 780 void 648 781 BSplitLayout::ItemRemoved(BLayoutItem* item) 649 782 { … … 658 791 item->SetLayoutData(NULL); 659 792 } 660 793 661 // _InvalidateLayout 794 662 795 void 663 796 BSplitLayout::_InvalidateLayout(bool invalidateView) 664 797 { … … 680 813 fLayoutValid = false; 681 814 } 682 815 683 // _InvalidateCachedHeightForWidth 816 684 817 void 685 818 BSplitLayout::_InvalidateCachedHeightForWidth() 686 819 { … … 694 827 fHeightForWidthVerticalLayouterWidth = -2; 695 828 } 696 829 697 // _SplitterItemAt 830 698 831 BSplitLayout::SplitterItem* 699 832 BSplitLayout::_SplitterItemAt(const BPoint& point, int32* index) const 700 833 { … … 711 844 return NULL; 712 845 } 713 846 714 // _SplitterItemAt 847 715 848 BSplitLayout::SplitterItem* 716 849 BSplitLayout::_SplitterItemAt(int32 index) const 717 850 { 718 851 return (SplitterItem*)fSplitterItems.ItemAt(index); 719 852 } 720 853 721 // _GetSplitterValueRange 854 722 855 void 723 856 BSplitLayout::_GetSplitterValueRange(int32 index, ValueRange& range) 724 857 { … … 747 880 range.sumValue += (int32)fSpacing; 748 881 } 749 882 750 // _SplitterValue 883 751 884 int32 752 885 BSplitLayout::_SplitterValue(int32 index) const 753 886 { … … 761 894 return 0; 762 895 } 763 896 764 // _LayoutItem 897 765 898 void 766 899 BSplitLayout::_LayoutItem(BLayoutItem* item, BRect frame, bool visible) 767 900 { … … 790 923 item->AlignInFrame(frame); 791 924 } 792 925 793 // _LayoutItem 926 794 927 void 795 928 BSplitLayout::_LayoutItem(BLayoutItem* item, ItemLayoutInfo* info) 796 929 { … … 806 939 807 940 item->AlignInFrame(info->layoutFrame); 808 941 942 // TODO: shouldn't this be done (as needed) by item->SetVisible(); 809 943 // if the item became visible, we need to update its internal layout 810 944 if (visibilityChanged) { 811 945 if (BView* itemView = item->View()) … … 813 947 } 814 948 } 815 949 816 // _SetSplitterValue 950 817 951 bool 818 952 BSplitLayout::_SetSplitterValue(int32 index, int32 value) 819 953 { … … 1042 1176 return true; 1043 1177 } 1044 1178 1045 // _ItemLayoutInfo 1179 1046 1180 BSplitLayout::ItemLayoutInfo* 1047 1181 BSplitLayout::_ItemLayoutInfo(BLayoutItem* item) const 1048 1182 { … … 1055 1189 return info; 1056 1190 } 1057 1191 1058 // _UpdateSplitterWeights 1192 1059 1193 void 1060 1194 BSplitLayout::_UpdateSplitterWeights() 1061 1195 { … … 1078 1212 _InvalidateCachedHeightForWidth(); 1079 1213 } 1080 1214 1081 // _ValidateMinMax 1215 1082 1216 void 1083 1217 BSplitLayout::_ValidateMinMax() 1084 1218 { … … 1152 1286 view->ResetLayoutInvalidation(); 1153 1287 } 1154 1288 1155 // _InternalGetHeightForWidth 1289 1156 1290 void 1157 1291 BSplitLayout::_InternalGetHeightForWidth(float width, bool realLayout, 1158 1292 float* minHeight, float* maxHeight, float* preferredHeight) … … 1221 1355 *preferredHeight = fCachedPreferredHeightForWidth; 1222 1356 } 1223 1357 1224 // _SplitterSpace 1358 1225 1359 float 1226 1360 BSplitLayout::_SplitterSpace() const 1227 1361 { … … 1235 1369 return space; 1236 1370 } 1237 1371 1238 // _AddInsets 1372 1239 1373 BSize 1240 1374 BSplitLayout::_AddInsets(BSize size) 1241 1375 { … … 1253 1387 return size; 1254 1388 } 1255 1389 1256 // _AddInsets 1390 1257 1391 void 1258 1392 BSplitLayout::_AddInsets(float* minHeight, float* maxHeight, 1259 1393 float* preferredHeight) … … 1269 1403 *preferredHeight = BLayoutUtils::AddDistances(*preferredHeight, insets); 1270 1404 } 1271 1405 1272 // _SubtractInsets 1406 1273 1407 BSize 1274 1408 BSplitLayout::_SubtractInsets(BSize size) 1275 1409 { -
src/kits/interface/SplitLayout.h
1 1 /* 2 * Copyright 2006 , Haiku Inc.2 * Copyright 2006-2010, Haiku Inc. 3 3 * Distributed under the terms of the MIT License. 4 4 */ 5 5 #ifndef _SPLIT_LAYOUT_H … … 25 25 public: 26 26 BSplitLayout(enum orientation orientation, 27 27 float spacing = 0.0f); 28 BSplitLayout(BMessage* from); 28 29 virtual ~BSplitLayout(); 29 30 30 31 void SetInsets(float left, float top, float right, … … 87 88 bool StopDraggingSplitter(); 88 89 int32 DraggedSplitter() const; 89 90 91 // archiving methods 92 virtual status_t Archive(BMessage* into, bool deep = true) const; 93 static BArchivable* Instantiate(BMessage* from); 94 95 virtual status_t ItemArchived(BMessage* into, BLayoutItem* item, 96 int32 index) const; 97 virtual status_t ItemUnarchived(const BMessage* from, 98 BLayoutItem* item, int32 index); 99 90 100 protected: 91 101 virtual void ItemAdded(BLayoutItem* item); 92 102 virtual void ItemRemoved(BLayoutItem* item); … … 121 131 122 132 void _ValidateMinMax(); 123 133 124 void 134 void _InternalGetHeightForWidth(float width, 125 135 bool realLayout, float* minHeight, 126 136 float* maxHeight, float* preferredHeight); 127 137