tb_select.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_SELECT_H
  6. #define TB_SELECT_H
  7. #include "tb_window.h"
  8. #include "tb_scroll_container.h"
  9. #include "tb_select_item.h"
  10. namespace tb {
  11. class TBMenuWindow;
  12. typedef int (*TBSelectListSortCallback)(TBSelectItemSource *source, const int *a, const int *b);
  13. /** TBSelectList shows a scrollable list of items provided by a TBSelectItemSource. */
  14. class TBSelectList : public TBWidget, public TBSelectItemViewer
  15. {
  16. public:
  17. // For safe typecasting
  18. TBOBJECT_SUBCLASS(TBSelectList, TBWidget);
  19. TBSelectList();
  20. ~TBSelectList();
  21. /** Get the default item source for this widget. This source can be used to add
  22. items of type TBGenericStringItem to this widget.
  23. It is the item source that is fed from resource files.
  24. If you need to add other types of items, or if you want to share item sources
  25. between several TBSelectDropDown/TBSelectList widgets, use SetSource using a
  26. external item source. */
  27. TBGenericStringItemSource *GetDefaultSource() { return &m_default_source; }
  28. /** Set filter string so only matching items will be showed.
  29. Set nullptr or empty string to remove filter and show all items. */
  30. void SetFilter(const char *filter);
  31. const char *GetFilter() const { return m_filter; }
  32. void SetSortCallback(TBSelectListSortCallback callback) { m_sort_callback = callback; }
  33. /** Set the language string id for the header. The header is shown
  34. at the top of the list when only a subset of all items are shown. */
  35. void SetHeaderString(const TBID& id);
  36. /** Make the list update its items to reflect the items from the
  37. in the current source. The update will take place next time
  38. the list is validated. */
  39. void InvalidateList();
  40. /** Make sure the list is reflecting the current items in the source. */
  41. void ValidateList();
  42. /** The value is the selected item. In lists with multiple selectable
  43. items it's the item that is the current focus. */
  44. virtual void SetValue(int value);
  45. virtual int GetValue() { return m_value; }
  46. /** Get the ID of the selected item, or 0 if there is no item selected. */
  47. TBID GetSelectedItemID();
  48. /** Change the value to a non disabled item that is visible with the current
  49. filter. Returns true if it successfully found another item.
  50. Valid keys:
  51. TB_KEY_UP - Previous item.
  52. TB_KEY_DOWN - Next item.
  53. TB_KEY_HOME - First item.
  54. TB_KEY_END - Last item. */
  55. bool ChangeValue(SPECIAL_KEY key);
  56. /** Set the selected state of the item at the given index. If you want
  57. to unselect the previously selected item, use SetValue. */
  58. void SelectItem(int index, bool selected);
  59. TBWidget *GetItemWidget(int index);
  60. bool GetItemSelected(int index);
  61. /** Scroll to the current selected item. The scroll may be delayed until
  62. the items has been layouted if the layout is currently invalid. */
  63. void ScrollToSelectedItem();
  64. /** Return the scrollcontainer used in this list. */
  65. TBScrollContainer *GetScrollContainer() { return &m_container; }
  66. TBID GetItemID(int index) const;
  67. int GetNumItems() const;
  68. void SetUIListView(bool value) { m_ui_list_view = value; }
  69. virtual void OnInflate(const INFLATE_INFO &info);
  70. virtual void OnSkinChanged();
  71. virtual void OnProcess();
  72. virtual void OnProcessAfterChildren();
  73. virtual bool OnEvent(const TBWidgetEvent &ev);
  74. // == TBSelectItemViewer ==================================================
  75. virtual void OnSourceChanged();
  76. virtual void OnItemChanged(int index);
  77. virtual void OnItemAdded(int index);
  78. virtual void OnItemRemoved(int index);
  79. virtual void OnAllItemsRemoved();
  80. protected:
  81. TBScrollContainer m_container;
  82. TBLayout m_layout;
  83. TBGenericStringItemSource m_default_source;
  84. int m_value;
  85. TBStr m_filter;
  86. bool m_list_is_invalid;
  87. bool m_scroll_to_current;
  88. bool m_ui_list_view;
  89. TBID m_header_lng_string_id;
  90. private:
  91. TBSelectListSortCallback m_sort_callback;
  92. TBWidget *CreateAndAddItemAfter(int index, TBWidget *reference);
  93. void SelectAllItems(bool select = false);
  94. };
  95. /** TBSelectDropdown shows a button that opens a popup with a TBSelectList with items
  96. provided by a TBSelectItemSource. */
  97. class TBSelectDropdown : public TBButton, public TBSelectItemViewer
  98. {
  99. public:
  100. // For safe typecasting
  101. TBOBJECT_SUBCLASS(TBSelectDropdown, TBButton);
  102. TBSelectDropdown();
  103. ~TBSelectDropdown();
  104. /** Get the default item source for this widget. This source can be used to add
  105. items of type TBGenericStringItem to this widget.
  106. It is the item source that is fed from resource files.
  107. If you need to add other types of items, or if you want to share item sources
  108. between several TBSelectDropDown/TBSelectList widgets, use SetSource using a
  109. external item source. */
  110. TBGenericStringItemSource *GetDefaultSource() { return &m_default_source; }
  111. /** Set the selected item. */
  112. virtual void SetValue(int value);
  113. virtual int GetValue() { return m_value; }
  114. /** Get the ID of the selected item, or 0 if there is no item selected. */
  115. TBID GetSelectedItemID();
  116. /** Open the window if the model has items. */
  117. void OpenWindow();
  118. /** Close the window if it is open. */
  119. void CloseWindow();
  120. /** Return the menu window if it's open, or nullptr. */
  121. TBMenuWindow *GetMenuIfOpen() const;
  122. virtual void OnInflate(const INFLATE_INFO &info);
  123. virtual bool OnEvent(const TBWidgetEvent &ev);
  124. // == TBSelectItemViewer ==================================================
  125. virtual void OnSourceChanged();
  126. virtual void OnItemChanged(int index);
  127. virtual void OnItemAdded(int index) {}
  128. virtual void OnItemRemoved(int index) {}
  129. virtual void OnAllItemsRemoved() {}
  130. protected:
  131. TBGenericStringItemSource m_default_source;
  132. TBSkinImage m_arrow;
  133. int m_value;
  134. TBWidgetSafePointer m_window_pointer; ///< Points to the dropdown window if opened
  135. };
  136. }; // namespace tb
  137. #endif // TB_SELECT_H