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::KeyUp(const char* bytes, int32 numBytes) 272 \brief Hook method that is called when a keyboard key is released. 273 274 \param bytes The \a bytes representing the keys released. 275 \param numBytes The size of \a bytes. 276 277 \see BView::KeyUp() 278*/ 279 280 281/*! 282 \fn void BListView::MouseDown(BPoint point) 283 \brief Hook method that is called when a mouse button is pushed down while 284 the cursor is contained in the view. 285 286 By default this method selects items on a single click, and invokes them on a 287 double click. This method calls InitiateDrag() to allow derived classes the 288 opportunity to drag and drop items from the list. 289 290 \param point The \a point where the mouse button was pushed down. 291 292 \see BView::MouseDown() 293*/ 294 295 296/*! 297 \fn void BListView::MouseUp(BPoint where) 298 \brief Hook method that is called when a mouse button is released while 299 the cursor is contained in the view. 300 301 \param where The location that the mouse button was released. 302 303 \see BView::MouseUp() 304*/ 305 306 307/*! 308 \fn void BListView::MouseMoved(BPoint where, uint32 code, 309 const BMessage* dragMessage) 310 \brief Hook method that is called whenever the mouse cursor enters, exits 311 or moves inside the list view. 312 313 \param where The point where the mouse cursor has moved to. 314 \param code A code which indicating if the mouse entered or exited the view. 315 \param dragMessage A message containing drag and drop information. 316 317 \see BView::MouseMoved() 318*/ 319 320 321/*! 322 \fn bool BListView::InitiateDrag(BPoint point, int32 index, bool wasSelected) 323 \brief Hook method called when a drag and drop operation is initiated. 324 325 This method is used by derived classes to implement drag and drop. This method 326 is called by the MouseDown() method. If the derived class initiates the drag & 327 drop operation you should return \c true, otherwise return \c false. By default 328 this method returns \c false. 329 330 \param point Where the drag & drop operation started. 331 \param index 332 \param wasSelected Indicates whether or not the item was selected. 333 334 \returns \c true if a drag & drop operation was initiated, \c false if not. 335*/ 336 337 338/*! 339 \fn void BListView::SelectionChanged() 340 \brief Hook method that is called when the selection changes. 341 342 This method should be implemented by derived classes, the default 343 implementation does nothing. 344*/ 345 346 347//! @} 348 349 350/*! 351 \name Resize methods 352*/ 353 354 355//! @{ 356 357 358/*! 359 \fn void BListView::ResizeToPreferred() 360 \brief Resize the view to it's preferred size. 361 362 \see BView::ResizeToPreferred() 363*/ 364 365 366/*! 367 \fn void BListView::GetPreferredSize(float *_width, float *_height) 368 \brief Fill out the \a _width and \a _height parameters with the preferred 369 width and height of the list view. 370 371 \param _width The list view's preferred width is written to \a _width. 372 \param _height The list view's preferred height is written to \a _height. 373 374 \see BView::GetPreferredSize() 375*/ 376 377 378/*! 379 \fn BSize BListView::MinSize() 380 \brief Returns the minimum size of the list view. 381 382 \return The minimum size of the list view as a BSize. 383 384 \see BView::MinSize() 385*/ 386 387 388/*! 389 \fn BSize BListView::MaxSize() 390 \brief Returns the maximum size of the list view. 391 392 \return The maximum size of the list view as a BSize. 393 394 \see BView::MaxSize() 395*/ 396 397 398/*! 399 \fn BSize BListView::PreferredSize() 400 \brief Returns the preferred size of the list view. 401 402 \return The preferred size of the list view as a BSize. 403 404 \see BView::PreferredSize() 405*/ 406 407 408//! @} 409 410 411/*! 412 \fn void BListView::MakeFocus(bool focused) 413 \brief Highlight or unhighlight the selection when the list view acquires 414 or loses its focus state. 415 416 \param focused \c true to receive focus or \c false to lose it. 417 418 \see BView::MakeFocus() 419*/ 420 421 422/*! 423 \fn void BListView::SetFont(const BFont* font, uint32 mask) 424 \brief Sets the font of the list view to \a font with the font parameters set 425 by \a mask. 426 427 \param font The \a font to set the list view to. 428 \param mask A \a mask indicating which properties of \a font to set. 429 430 \see BView::SetFont() 431*/ 432 433 434/*! 435 \fn void BListView::ScrollTo(BPoint point) 436 \brief Scroll the view to the specified \a point. 437 438 \param point The location to scroll the list view to. 439 440 \see BView::ScrollTo() 441*/ 442 443 444/*! 445 \name Add and remove item methods 446*/ 447 448 449//! @{ 450 451 452/*! 453 \fn bool BListView::AddItem(BListItem *item, int32 index) 454 \brief Add an \a item to the list view at the specified \a index. 455 456 \param item The list item to add. 457 \param index The \a index of where to add the list item, if not specified the 458 item is added to the end. 459 460 \return \c true if the list item was added, \c false otherwise. 461*/ 462 463 464/*! 465 \fn bool BListView::AddList(BList* list, int32 index) 466 \brief Add a \a list of list items to the list view at the specified \a index. 467 468 \param list The \a list of list items to add. 469 \param index The \a index of where to add the list, if not specified the 470 \a list is added to the end. 471 472 \return \c true if the \a list was added, \c false otherwise. 473*/ 474 475 476/*! 477 \fn bool BListView::AddList(BList* list) 478 \fn Add a \a list of list items to the end of the list view. 479 480 \param list The \a list of list items to add. 481 482 \return \c true if the \a list was added, \c false otherwise. 483*/ 484 485 486/*! 487 \fn BListItem* BListView::RemoveItem(int32 index) 488 \brief Remove the item at \a index from the list. 489 490 \param index The \a index of the item to remove. 491 492 \return \c true if the item was removed, \c false otherwise. 493*/ 494 495 496/*! 497 \fn bool BListView::RemoveItem(BListItem* item) 498 \brief Remove the specified list item. 499 500 \param item The list item to remove. 501 502 \return \c true if the \a item was removed, \c false otherwise. 503*/ 504 505 506/*! 507 \fn bool BListView::RemoveItems(int32 index, int32 count) 508 \brief Removes the items from \a index and the next \a count items. 509 510 \param index The location to start removing items from. 511 \param count The number of items past \a index to remove. 512 513 return \c true if the \a items were removed, \c false otherwise. 514*/ 515 516 517//! @} 518 519 520/*! 521 \name Selection and Invocation message methods 522*/ 523 524 525//! @{ 526 527 528/*! 529 \fn void BListView::SetSelectionMessage(BMessage* message) 530 \brief Sets the \a message that the list view sends when a new item is selected. 531 532 \param message The selection \a message to set. 533*/ 534 535 536/*! 537 \fn void BListView::SetInvocationMessage(BMessage* message) 538 Sets the \a message that the list view sends when an item is invoked. 539 540 \param message The invocation \a message to set. 541 542 \see BInvoker::SetMessage() 543*/ 544 545 546/*! 547 \fn BMessage* BListView::InvocationMessage() const 548 \brief Returns the message that is send when an item is invoked. 549 550 \return The current invocation method as a BMessage. 551 552 \see BInvoker::Message() 553*/ 554 555 556/*! 557 \fn uint32 BListView::InvocationCommand() const 558 \brief Returns the what parameter of the current invocation method. 559 560 \returns The what parameter of the currently set invocation method. 561 562 \see BInvoker::Command() 563*/ 564 565 566/*! 567 \fn BMessage* BListView::SelectionMessage() const 568 \brief Returns the message that is send when an item is selected. 569 570 \return The current selection message as a BMessage. 571*/ 572 573 574/*! 575 \fn uint32 BListView::SelectionCommand() const 576 \brief Returns the what parameter of the message that is send when an item is 577 selected. 578 579 \return The what parameter of the current selection message. 580*/ 581 582 583//! @} 584 585 586/*! 587 \name List type methods 588*/ 589 590 591//! @{ 592 593 594/*! 595 \fn void BListView::SetListType(list_view_type type) 596 \brief Sets the list view \a type. 597 598 \param type The list view \a type to set. 599*/ 600 601 602/*! 603 \fn list_view_type BListView::ListType() const 604 \brief Returns the current list view type. 605 606 \return The list view type. 607*/ 608 609 610//! @} 611 612 613/*! 614 \name List methods 615*/ 616 617 618//! @{ 619 620 621/*! 622 \fn BListItem* BListView::ItemAt(int32 index) const 623 \brief Returns the list item at the specified \a index. 624 625 \param index 626 627 \return The list item at the specified \a index. 628*/ 629 630 631/*! 632 \fn int32 BListView::IndexOf(BListItem* item) const 633 \brief Returns the index of the specified \a item. 634 635 \param item The list item to get the index of. 636 637 \return The index of the specified \a item. 638*/ 639 640 641/*! 642 \fn int32 BListView::IndexOf(BPoint point) const 643 \brief Returns the index of the item at the specified \a point. 644 645 \param point The location of the list item to get the index of. 646 647 \return The index of the list item at the specified \a point. 648*/ 649 650 651/*! 652 \fn BListItem* BListView::FirstItem() const 653 \brief Returns the first list item. 654 655 \return The first item in the list. 656*/ 657 658 659/*! 660 \fn BListItem* BListView::LastItem() const 661 \brief Returns the last list item. 662 663 \return The last item in the list. 664*/ 665 666 667/*! 668 \fn bool BListView::HasItem(BListItem *item) const 669 \brief Returns whether or not the list contains the specified \a item. 670 671 \param item The list item to check. 672 673 \return \c true if the list item is contained in the list view, \c false 674 otherwise. 675*/ 676 677 678/*! 679 \fn int32 BListView::CountItems() const 680 \brief Returns the number of list items contained in the list view. 681 682 \return The number of list items. 683*/ 684 685 686/*! 687 \fn void BListView::MakeEmpty() 688 \brief Empties the list view of all list items. 689*/ 690 691 692/*! 693 \fn bool BListView::IsEmpty() const 694 \brief Returns whether or not the list view is empty or not. 695 696 \return \c true if the list view was empty, \c false otherwize. 697*/ 698 699 700/*! 701 \fn void BListView::DoForEach(bool (*func)(BListItem*)) 702 \brief Calls the specified function on each item in the list. 703 704 The \a func is called on the items in order starting with the item at index 0 705 and ending at the last item in the list. This method stops calling the \a func 706 once it returns \a true or the end of the list is reached. 707 708 The first argument of \a func is a pointer to the list item. 709 710 \param func The function to call on each item. 711*/ 712 713 714/*! 715 \fn void BListView::DoForEach(bool (*func)(BListItem*, void*), 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 index 0 719 and ending at the last item in the list. This method stops calling the \a func 720 once it returns \a true or the end of the list is reached. 721 722 The first argument of \a func is a pointer to the list item, \a arg is passed in 723 as the second argument. 724 725 \param func The function to call on each item. 726*/ 727 728 729/*! 730 \fn const BListItem** BListView::Items() const 731 \brief Returns a pointer to the list of list items. 732 733 \returns a pointer to the list of list items. 734*/ 735 736 737//! @} 738 739 740/*! 741 \fn void BListView::InvalidateItem(int32 index) 742 \brief Draws the list item at the specified \a index. 743 744 \param index The \a index of the list item to draw. 745*/ 746 747 748/*! 749 \name Selection methods 750*/ 751 752 753//! @{ 754 755 756/*! 757 \fn void BListView::ScrollToSelection() 758 \brief Scrolls to selected list item. 759*/ 760 761 762/*! 763 \fn void BListView::Select(int32 index, bool extend) 764 \brief Selects the list item at the specified \a index. 765 766 \param index The \a index of the item to select. 767 \param extend Whether or not to also select child items. 768*/ 769 770 771/*! 772 \fn void BListView::Select(int32 start, int32 finish, bool extend) 773 \brief Select items from \a start to \a finish. 774 775 \param start The index of the item to start the selection. 776 \param finish The index of the item to end the selection. 777 \param extend Whether or not to also select child items. 778*/ 779 780 781/*! 782 \fn bool BListView::IsItemSelected(int32 index) const 783 \brief Returns whether or not the item at \a index is selected. 784 785 \return \c true if the item was selected, \c false otherwise. 786*/ 787 788 789/*! 790 \fn int32 BListView::CurrentSelection(int32 index) const 791 \brief Returns the index of a currently selected item relative to the passed 792 in \a index. 793 794 If the index of the selected item is lower than \a index the value returned 795 is negative, if the index of the selected item is greater than \a index the 796 value returned is positive. If the index of the selected item is equal to 797 \a index then 0 is returned. 798 799 \brief index The \a index of the item to get relative to the selected item's 800 index. 801*/ 802 803 804//! @} 805 806 807/*! 808 \fn status_t BListView::Invoke(BMessage* message) 809 \brief Invoke the list view, either with the current invocation message or 810 \a message if it is specified. 811 812 \param message The message to send or \c NULL to send the current invocation 813 message. 814 815 \see BControl::Invoke() 816*/ 817 818 819/*! 820 \name Deselection methods 821*/ 822 823 824//! @{ 825 826 827/*! 828 \fn void BListView::DeselectAll() 829 \brief Deselect all items. 830*/ 831 832 833/*! 834 \fn void BListView::DeselectExcept(int32 exceptFrom, int32 exceptTo) 835 \brief Deselect all items except the items with index in the range of 836 \a exceptFrom to \a exceptTo. 837 838 \param exceptFrom The index of the start of the exception list. 839 \param exceptTo The index of the end of the exception list. 840*/ 841 842 843/*! 844 \fn void BListView::Deselect(int32 index) 845 \brief Deselect the item at \a index. 846 847 \param index The \a index of the item to deselect. 848*/ 849 850 851//! @} 852 853 854/*! 855 \fn void BListView::SortItems(int (*cmp)(const void *, const void *)) 856 \brief sort the items according the the passed in \a cmp function. 857 858 \param cmp The compare function to use to sort the items. 859*/ 860 861 862/*! 863 \fn bool BListView::SwapItems(int32 a, int32 b) 864 \brief Swap item \a a with item \a b. 865 866 \param a The index of the first item to swap. 867 \param b The index of the second item to swap. 868 869 \return \c true if the items were swapped, \c false otherwise. 870*/ 871 872 873/*! 874 \fn bool BListView::MoveItem(int32 from, int32 to) 875 \brief Move the item at index \a from to the position in the list at index \a to. 876 877 \param from The index of the item to move. 878 \param to The index to move the item to. 879 880 \return \c true if the item was moved, \c false otherwise. 881*/ 882 883 884/*! 885 \fn bool BListView::ReplaceItem(int32 index, BListItem* item) 886 \brief Replace the item at index \a index with \a item. 887 888 \param index The \a index of the item to replace. 889 \param item The \a item to replace the item at \a index with. 890 891 \return \c true if the item was replaced, \c false otherwise. 892*/ 893 894 895/*! 896 \fn BRect BListView::ItemFrame(int32 index) 897 \brief Return the frame of the item at the specified \a index. 898 899 \param index The \a index of the item to get the frame of. 900 901 \returns The frame of the item at \a index. 902*/ 903 904 905/*! 906 \fn BHandler* BListView::ResolveSpecifier(BMessage* message, int32 index, 907 BMessage* specifier, int32 form, const char* property); 908 \brief Returns the proper handler for the passed in scripting \a message. 909 910 \param message The scripting message to determine the handler. 911 \param index The index of the specifier. 912 \param specifier The message which contains the specifier. 913 \param form The 'what' field of the specifier message. 914 \param property The name of the target property. 915 916 \return The proper BHandler for the passed in scripting \a message. 917 918 \see BView::ResolveSpecifier() 919*/ 920 921 922/*! 923 \fn status_t BListView::GetSupportedSuites(BMessage* data) 924 \brief Reports the suites of messages and specifiers that derived classes 925 understand. 926 927 \param data The message to report the suite of messages and specifiers. 928 929 \see BView::GetSupportedSuites() 930*/ 931 932 933/*! 934 \fn status_t BListView::Perform(perform_code code, void* _data) 935 \brief Performs an action give a perform_code and data. (Internal Method) 936 937 \param code The perform code 938 \param _data A pointer to some data to perform on 939 940 \return A status code. 941 942 \see BView::Perform() 943*/ 944