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