1/* 2 * Copyright 2013 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * John Scipione, jscipione@gmail.com 7 * 8 * Corresponds to: 9 * headers/os/interface/ListView.h hrev45555 10 * src/kits/interface/ListView.cpp hrev45555 11 */ 12 13 14/*! 15 \file ListView.h 16 \ingroup interface 17 \ingroup libbe 18 \brief ListView class definition. 19*/ 20 21 22/*! 23 \class BListView 24 \ingroup interface 25 \ingroup libbe 26 \brief Displays a list of items that the user can select and invoke. 27 28 BListView's can be one of two types set by the type parameter of the constructor: 29 - \c B_SINGLE_SELECTION_LIST Can select only one item in the list at a time. 30 This is the default. 31 - \c B_MULTIPLE_SELECTION_LIST Can select any number of items by holding down 32 Option for a discontinuous selection, or Shift for a contiguous selection. 33 34 An example of a BListView looks like this: 35 \image html BListView_example.png 36 37 Click on an item to select it and double-click an item to invoke it. The 38 BListView doesn't define what it means to "invoke" an item. See 39 BListView::SetSelectionMessage() and BListView::SetInvocationMessage() to set 40 a message to be set when these actions occur. You can also select and invoke 41 items with keyboard keys such as the up and down arrow keys, Page Up and Page 42 Down and the Enter key or Space key to invoke the item. 43 44 This class is based on the BList class from the Support Kit and many of the 45 methods it uses behave similarly. 46 47 Although a BListView is scrollable, it doesn't provide scroll bars by itself. 48 You should add the BListView as a child of a BScrollView to make it scrollable. 49 50 The code to add a BListView to a BScrollView looks something like this: 51 52\code 53 BListView* list = new BListView(frame, "List", B_SINGLE_SELECTION_LIST); 54 list->AddItem(new BStringItem("Item 1")); 55 list->AddItem(new BStringItem("Item 2")); 56 ... 57 view->AddChild(new BScrollView("scroll_view", list, 58 B_FOLLOW_LEFT | B_FOLLOW_TOP, 0, false, true)); 59\endcode 60 61 \see BScrollView for more information on scrolling views. 62 \see BList in the Support Kit. 63 \see BOutlineListView 64 \see BListItem 65*/ 66 67 68/*! 69 \fn BListView::BListView(BRect frame, const char* name, list_view_type type, 70 uint32 resizingMode, uint32 flags) 71 \brief Creates a new list view. This is the non-layout constructor. 72 73 \param frame The frame rectangle of the view. 74 \param name The name of the view. 75 \param type Whether the list view supports a single selection or multiple 76 selections. 77 \param resizingMode The resizing mode flags. 78 \param flags The view flags. 79*/ 80 81 82/*! 83 \fn BListView::BListView(const char* name, list_view_type type, uint32 flags) 84 \brief Creates a new list view suitable as part of a layout with the specified 85 \a name, \a type, and \a flags. 86 87 \param name The name of the view. 88 \param type Whether the list view supports a single selection or multiple 89 selections. 90 \param flags The view flags. 91*/ 92 93 94/*! 95 \fn BListView::BListView(list_view_type type) 96 \brief Creates a new list view suitable as part of a layout. 97 98 \param type Whether the list view supports a single selection or multiple 99 selections. 100*/ 101 102 103/*! 104 \fn BListView::BListView(BMessage* archive) 105 \brief Creates a list view from an \a archive message. 106 107 \param archive The message to create the list view from. 108*/ 109 110 111/*! 112 \fn BListView::~BListView() 113 \brief Delete the list view and free the memory used. 114 115 This method does not free the list items. 116*/ 117 118 119/*! 120 \name Archiving 121*/ 122 123 124//! @{ 125 126 127/*! 128 \fn BArchivable* BListView::Instantiate(BMessage* archive) 129 \brief Create a new list view from the message \a archive. 130 131 \param archive The message to create the list view from. 132*/ 133 134 135/*! 136 \fn status_t BListView::Archive(BMessage* archive, bool deep) const 137 \brief Archive the list view to a message. 138 139 \param archive The message to archive the list view to. 140 \param deep \c true to archive child views. 141*/ 142 143 144//! @} 145 146 147/*! 148 \name Hook methods 149*/ 150 151 152//! @{ 153 154 155/*! 156 \fn void BListView::Draw(BRect updateRect) 157 \brief Hook method called to draw the contents of the text view. 158 159 You should not have to call this method directly, use Invalidate() instead. 160 161 \param updateRect The rectangular area to draw. 162 163 \see BView::Draw() 164*/ 165 166 167/*! 168 \fn void BListView::AttachedToWindow() 169 \brief Hook method called when the list view is added to the view hierarchy. 170 171 \see BView::AttachedToWindow() 172*/ 173 174 175/*! 176 \fn void BListView::DetachedFromWindow() 177 \brief Hook method that is called when the list view is removed from the 178 view hierarchy. 179 180 \see BView::DetachedFromWindow() 181*/ 182 183 184/*! 185 \fn void BListView::AllAttached() 186 \brief Hook method called once all views are attached to the view. 187 188 \see BView::AllAttached() 189*/ 190 191 192/*! 193 \fn void BListView::AllDetached() 194 \brief Hook method called once all views are detached from the view. 195 196 \see BView::AllDetached() 197*/ 198 199 200/*! 201 \fn void BListView::FrameResized(float width, float height) 202 \brief Hook method called when the list view is resized. 203 204 \param width The new \a width of the list view. 205 \param height The new \a height of the list view. 206 207 \see BView::FrameResized() 208*/ 209 210 211/*! 212 \fn void BListView::FrameMoved(BPoint new_position) 213 \brief Hook method called when the list view is moved. 214 215 \param new_position The list view's new position. 216*/ 217 218 219/*! 220 \fn void BListView::TargetedByScrollView(BScrollView *view) 221 \brief Hook method called when the list view is attached to a BScrollView. 222 223 \param view The BScrollView the list view is attached to. 224*/ 225 226 227/*! 228 \fn void BListView::WindowActivated(bool state) 229 \brief Hook method that is called when the window becomes the active window 230 or gives up that status. 231 232 \param state If \c true, window has just been activated. If \c false the 233 window has just been deactivated. 234 235 \see BView::WindowActivated() 236*/ 237 238 239/*! 240 \fn void BListView::MessageReceived(BMessage* message) 241 \brief Hook method called with a message is received by the list view. 242 243 \param message The message received by the list view. 244 245 \see BView::MessageReceived() 246*/ 247 248 249/*! 250 \fn void BListView::KeyDown(const char* bytes, int32 numBytes) 251 \brief Hook method that is called when a key is pressed while the view is 252 the focus view of the active window. 253 254 The following keys are used by the list view by default: 255 - Up Arrow Selects the previous item. 256 - Down Arrow Selects the next item. 257 - Page Up Selects the item one view height above the current item. 258 - Page Down Selects the item one view height below the current item. 259 - Home Selects the first item in the list. 260 - End Select the last item in the list. 261 - Enter and Spacebar Invokes the currently selected item. 262 263 \param bytes The \a bytes representing the keys pushed down. 264 \param numBytes The size of \a bytes. 265 266 \see BView::KeyDown() 267*/ 268 269 270/*! 271 \fn void BListView::MouseDown(BPoint point) 272 \brief Hook method that is called when a mouse button is pushed down while 273 the cursor is contained in the view. 274 275 By default this method selects items on a single click, and invokes them on a 276 double click. This method calls InitiateDrag() to allow derived classes the 277 opportunity to drag and drop items from the list. 278 279 \param point The \a point where the mouse button was pushed down. 280 281 \see BView::MouseDown() 282*/ 283 284 285/*! 286 \fn void BListView::MouseUp(BPoint where) 287 \brief Hook method that is called when a mouse button is released while 288 the cursor is contained in the view. 289 290 \param where The location that the mouse button was released. 291 292 \see BView::MouseUp() 293*/ 294 295 296/*! 297 \fn void BListView::MouseMoved(BPoint where, uint32 code, 298 const BMessage* dragMessage) 299 \brief Hook method that is called whenever the mouse cursor enters, exits 300 or moves inside the list view. 301 302 \param where The point where the mouse cursor has moved to. 303 \param code A code which indicating if the mouse entered or exited the view. 304 \param dragMessage A message containing drag and drop information. 305 306 \see BView::MouseMoved() 307*/ 308 309 310/*! 311 \fn bool BListView::InitiateDrag(BPoint point, int32 index, bool wasSelected) 312 \brief Hook method called when a drag and drop operation is initiated. 313 314 This method is used by derived classes to implement drag and drop. This method 315 is called by the MouseDown() method. If the derived class initiates the drag & 316 drop operation you should return \c true, otherwise return \c false. By default 317 this method returns \c false. 318 319 \param point Where the drag & drop operation started. 320 \param index 321 \param wasSelected Indicates whether or not the item was selected. 322 323 \returns \c true if a drag & drop operation was initiated, \c false if not. 324*/ 325 326 327/*! 328 \fn void BListView::SelectionChanged() 329 \brief Hook method that is called when the selection changes. 330 331 This method should be implemented by derived classes, the default 332 implementation does nothing. 333*/ 334 335 336//! @} 337 338 339/*! 340 \name Resize methods 341*/ 342 343 344//! @{ 345 346 347/*! 348 \fn void BListView::ResizeToPreferred() 349 \brief Resize the view to it's preferred size. 350 351 \see BView::ResizeToPreferred() 352*/ 353 354 355/*! 356 \fn void BListView::GetPreferredSize(float *_width, float *_height) 357 \brief Fill out the \a _width and \a _height parameters with the preferred 358 width and height of the list view. 359 360 \param _width The list view's preferred width is written to \a _width. 361 \param _height The list view's preferred height is written to \a _height. 362 363 \see BView::GetPreferredSize() 364*/ 365 366 367/*! 368 \fn BSize BListView::MinSize() 369 \brief Returns the minimum size of the list view. 370 371 \return The minimum size of the list view as a BSize. 372 373 \see BView::MinSize() 374*/ 375 376 377/*! 378 \fn BSize BListView::MaxSize() 379 \brief Returns the maximum size of the list view. 380 381 \return The maximum size of the list view as a BSize. 382 383 \see BView::MaxSize() 384*/ 385 386 387/*! 388 \fn BSize BListView::PreferredSize() 389 \brief Returns the preferred size of the list view. 390 391 \return The preferred size of the list view as a BSize. 392 393 \see BView::PreferredSize() 394*/ 395 396 397//! @} 398 399 400/*! 401 \fn void BListView::MakeFocus(bool focused) 402 \brief Highlight or unhighlight the selection when the list view acquires 403 or loses its focus state. 404 405 \param focused \c true to receive focus or \c false to lose it. 406 407 \see BView::MakeFocus() 408*/ 409 410 411/*! 412 \fn void BListView::SetFont(const BFont* font, uint32 mask) 413 \brief Sets the font of the list view to \a font with the font parameters set 414 by \a mask. 415 416 \param font The \a font to set the list view to. 417 \param mask A \a mask indicating which properties of \a font to set. 418 419 \see BView::SetFont() 420*/ 421 422 423/*! 424 \fn void BListView::ScrollTo(BPoint point) 425 \brief Scroll the view to the specified \a point. 426 427 \param point The location to scroll the list view to. 428 429 \see BView::ScrollTo() 430*/ 431 432 433/*! 434 \name Add and remove item methods 435*/ 436 437 438//! @{ 439 440 441/*! 442 \fn bool BListView::AddItem(BListItem *item, int32 index) 443 \brief Add an \a item to the list view at the specified \a index. 444 445 \param item The list item to add. 446 \param index The \a index of where to add the list item, if not specified the 447 item is added to the end. 448 449 \return \c true if the list item was added, \c false otherwise. 450*/ 451 452 453/*! 454 \fn bool BListView::AddList(BList* list, int32 index) 455 \brief Add a \a list of list items to the list view at the specified \a index. 456 457 \param list The \a list of list items to add. 458 \param index The \a index of where to add the list, if not specified the 459 \a list is added to the end. 460 461 \return \c true if the \a list was added, \c false otherwise. 462*/ 463 464 465/*! 466 \fn bool BListView::AddList(BList* list) 467 \brief Add a \a list of list items to the end of the list view. 468 469 \param list The \a list of list items to add. 470 471 \return \c true if the \a list was added, \c false otherwise. 472*/ 473 474 475/*! 476 \fn BListItem* BListView::RemoveItem(int32 index) 477 \brief Remove the item at \a index from the list. 478 479 \param index The \a index of the item to remove. 480 481 \return \c true if the item was removed, \c false otherwise. 482*/ 483 484 485/*! 486 \fn bool BListView::RemoveItem(BListItem* item) 487 \brief Remove the specified list item. 488 489 \param item The list item to remove. 490 491 \return \c true if the \a item was removed, \c false otherwise. 492*/ 493 494 495/*! 496 \fn bool BListView::RemoveItems(int32 index, int32 count) 497 \brief Removes the items from \a index and the next \a count items. 498 499 \param index The location to start removing items from. 500 \param count The number of items past \a index to remove. 501 502 return \c true if the \a items were removed, \c false otherwise. 503*/ 504 505 506//! @} 507 508 509/*! 510 \name Selection and Invocation message methods 511*/ 512 513 514//! @{ 515 516 517/*! 518 \fn void BListView::SetSelectionMessage(BMessage* message) 519 \brief Sets the \a message that the list view sends when a new item is selected. 520 521 \param message The selection \a message to set. 522*/ 523 524 525/*! 526 \fn void BListView::SetInvocationMessage(BMessage* message) 527 Sets the \a message that the list view sends when an item is invoked. 528 529 \param message The invocation \a message to set. 530 531 \see BInvoker::SetMessage() 532*/ 533 534 535/*! 536 \fn BMessage* BListView::InvocationMessage() const 537 \brief Returns the message that is send when an item is invoked. 538 539 \return The current invocation method as a BMessage. 540 541 \see BInvoker::Message() 542*/ 543 544 545/*! 546 \fn uint32 BListView::InvocationCommand() const 547 \brief Returns the what parameter of the current invocation method. 548 549 \returns The what parameter of the currently set invocation method. 550 551 \see BInvoker::Command() 552*/ 553 554 555/*! 556 \fn BMessage* BListView::SelectionMessage() const 557 \brief Returns the message that is send when an item is selected. 558 559 \return The current selection message as a BMessage. 560*/ 561 562 563/*! 564 \fn uint32 BListView::SelectionCommand() const 565 \brief Returns the what parameter of the message that is send when an item is 566 selected. 567 568 \return The what parameter of the current selection message. 569*/ 570 571 572//! @} 573 574 575/*! 576 \name List type methods 577*/ 578 579 580//! @{ 581 582 583/*! 584 \fn void BListView::SetListType(list_view_type type) 585 \brief Sets the list view \a type. 586 587 \param type The list view \a type to set. 588*/ 589 590 591/*! 592 \fn list_view_type BListView::ListType() const 593 \brief Returns the current list view type. 594 595 \return The list view type. 596*/ 597 598 599//! @} 600 601 602/*! 603 \name List methods 604*/ 605 606 607//! @{ 608 609 610/*! 611 \fn BListItem* BListView::ItemAt(int32 index) const 612 \brief Returns the list item at the specified \a index. 613 614 \param index 615 616 \return The list item at the specified \a index. 617*/ 618 619 620/*! 621 \fn int32 BListView::IndexOf(BListItem* item) const 622 \brief Returns the index of the specified \a item. 623 624 \param item The list item to get the index of. 625 626 \return The index of the specified \a item. 627*/ 628 629 630/*! 631 \fn int32 BListView::IndexOf(BPoint point) const 632 \brief Returns the index of the item at the specified \a point. 633 634 \param point The location of the list item to get the index of. 635 636 \return The index of the list item at the specified \a point. 637*/ 638 639 640/*! 641 \fn BListItem* BListView::FirstItem() const 642 \brief Returns the first list item. 643 644 \return The first item in the list. 645*/ 646 647 648/*! 649 \fn BListItem* BListView::LastItem() const 650 \brief Returns the last list item. 651 652 \return The last item in the list. 653*/ 654 655 656/*! 657 \fn bool BListView::HasItem(BListItem *item) const 658 \brief Returns whether or not the list contains the specified \a item. 659 660 \param item The list item to check. 661 662 \return \c true if the list item is contained in the list view, \c false 663 otherwise. 664*/ 665 666 667/*! 668 \fn int32 BListView::CountItems() const 669 \brief Returns the number of list items contained in the list view. 670 671 \return The number of list items. 672*/ 673 674 675/*! 676 \fn void BListView::MakeEmpty() 677 \brief Empties the list view of all list items. 678*/ 679 680 681/*! 682 \fn bool BListView::IsEmpty() const 683 \brief Returns whether or not the list view is empty or not. 684 685 \return \c true if the list view was empty, \c false otherwize. 686*/ 687 688 689/*! 690 \fn void BListView::DoForEach(bool (*func)(BListItem* item)) 691 \brief Calls the specified function on each item in the list. 692 693 The \a func is called on the items in order starting with the item at index 0 694 and ending at the last item in the list. This method stops calling the \a func 695 once it returns \a true or the end of the list is reached. 696 697 The first argument of \a func is a pointer to the list item. 698 699 \param func The function to call on each item. 700*/ 701 702 703/*! 704 \fn void BListView::DoForEach(bool (*func)(BListItem* item, void* arg), 705 void* arg) 706 \brief Calls the specified function on each item in the list. 707 708 The \a func is called on the items in order starting with the item at index 0 709 and ending at the last item in the list. This method stops calling the \a func 710 once it returns \a true or the end of the list is reached. 711 712 The first argument of \a func is a pointer to the list item, \a arg is passed in 713 as the second argument. 714 715 \param func The function to call on each item. 716 \param arg The second argument of the function. 717*/ 718 719 720/*! 721 \fn const BListItem** BListView::Items() const 722 \brief Returns a pointer to the list of list items. 723 724 \returns a pointer to the list of list items. 725*/ 726 727 728//! @} 729 730 731/*! 732 \fn void BListView::InvalidateItem(int32 index) 733 \brief Draws the list item at the specified \a index. 734 735 \param index The \a index of the list item to draw. 736*/ 737 738 739/*! 740 \name Selection methods 741*/ 742 743 744//! @{ 745 746 747/*! 748 \fn void BListView::ScrollToSelection() 749 \brief Scrolls to selected list item. 750*/ 751 752 753/*! 754 \fn void BListView::Select(int32 index, bool extend) 755 \brief Selects the list item at the specified \a index. 756 757 \param index The \a index of the item to select. 758 \param extend Whether or not to also select child items. 759*/ 760 761 762/*! 763 \fn void BListView::Select(int32 start, int32 finish, bool extend) 764 \brief Select items from \a start to \a finish. 765 766 \param start The index of the item to start the selection. 767 \param finish The index of the item to end the selection. 768 \param extend Whether or not to also select child items. 769*/ 770 771 772/*! 773 \fn bool BListView::IsItemSelected(int32 index) const 774 \brief Returns whether or not the item at \a index is selected. 775 776 \return \c true if the item was selected, \c false otherwise. 777*/ 778 779 780/*! 781 \fn int32 BListView::CurrentSelection(int32 index) const 782 \brief Returns the index of a currently selected item relative to the passed 783 in \a index. 784 785 If the index of the selected item is lower than \a index the value returned 786 is negative, if the index of the selected item is greater than \a index the 787 value returned is positive. If the index of the selected item is equal to 788 \a index then 0 is returned. 789 790 \brief index The \a index of the item to get relative to the selected item's 791 index. 792*/ 793 794 795//! @} 796 797 798/*! 799 \fn status_t BListView::Invoke(BMessage* message) 800 \brief Invoke the list view, either with the current invocation message or 801 \a message if it is specified. 802 803 \param message The message to send or \c NULL to send the current invocation 804 message. 805 806 \see BControl::Invoke() 807*/ 808 809 810/*! 811 \name Deselection methods 812*/ 813 814 815//! @{ 816 817 818/*! 819 \fn void BListView::DeselectAll() 820 \brief Deselect all items. 821*/ 822 823 824/*! 825 \fn void BListView::DeselectExcept(int32 exceptFrom, int32 exceptTo) 826 \brief Deselect all items except the items with index in the range of 827 \a exceptFrom to \a exceptTo. 828 829 \param exceptFrom The index of the start of the exception list. 830 \param exceptTo The index of the end of the exception list. 831*/ 832 833 834/*! 835 \fn void BListView::Deselect(int32 index) 836 \brief Deselect the item at \a index. 837 838 \param index The \a index of the item to deselect. 839*/ 840 841 842//! @} 843 844 845/*! 846 \fn void BListView::SortItems(int (*cmp)(const void *, const void *)) 847 \brief sort the items according the the passed in \a cmp function. 848 849 \param cmp The compare function to use to sort the items. 850*/ 851 852 853/*! 854 \fn bool BListView::SwapItems(int32 a, int32 b) 855 \brief Swap item \a a with item \a b. 856 857 \param a The index of the first item to swap. 858 \param b The index of the second item to swap. 859 860 \return \c true if the items were swapped, \c false otherwise. 861*/ 862 863 864/*! 865 \fn bool BListView::MoveItem(int32 from, int32 to) 866 \brief Move the item at index \a from to the position in the list at index \a to. 867 868 \param from The index of the item to move. 869 \param to The index to move the item to. 870 871 \return \c true if the item was moved, \c false otherwise. 872*/ 873 874 875/*! 876 \fn bool BListView::ReplaceItem(int32 index, BListItem* item) 877 \brief Replace the item at index \a index with \a item. 878 879 \param index The \a index of the item to replace. 880 \param item The \a item to replace the item at \a index with. 881 882 \return \c true if the item was replaced, \c false otherwise. 883*/ 884 885 886/*! 887 \fn BRect BListView::ItemFrame(int32 index) 888 \brief Return the frame of the item at the specified \a index. 889 890 \param index The \a index of the item to get the frame of. 891 892 \returns The frame of the item at \a index. 893*/ 894 895 896/*! 897 \fn BHandler* BListView::ResolveSpecifier(BMessage* message, int32 index, 898 BMessage* specifier, int32 form, const char* property); 899 \brief Returns the proper handler for the passed in scripting \a message. 900 901 \param message The scripting message to determine the handler. 902 \param index The index of the specifier. 903 \param specifier The message which contains the specifier. 904 \param form The 'what' field of the specifier message. 905 \param property The name of the target property. 906 907 \return The proper BHandler for the passed in scripting \a message. 908 909 \see BView::ResolveSpecifier() 910*/ 911 912 913/*! 914 \fn status_t BListView::GetSupportedSuites(BMessage* data) 915 \brief Reports the suites of messages and specifiers that derived classes 916 understand. 917 918 \param data The message to report the suite of messages and specifiers. 919 920 \see BView::GetSupportedSuites() 921*/ 922 923 924/*! 925 \fn status_t BListView::Perform(perform_code code, void* _data) 926 \brief Performs an action give a perform_code and data. (Internal Method) 927 928 \param code The perform code 929 \param _data A pointer to some data to perform on 930 931 \return A status code. 932 933 \see BView::Perform() 934*/ 935