listbox.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: listbox.h
  3. // Purpose: interface of wxListBox
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxListBox
  9. A listbox is used to select one or more of a list of strings.
  10. The strings are displayed in a scrolling box, with the selected string(s)
  11. marked in reverse video. A listbox can be single selection (if an item is
  12. selected, the previous selection is removed) or multiple selection
  13. (clicking an item toggles the item on or off independently of other
  14. selections).
  15. List box elements are numbered from zero and while the maximal number of
  16. elements is unlimited, it is usually better to use a virtual control, not
  17. requiring to add all the items to it at once, such as wxDataViewCtrl or
  18. wxListCtrl with @c wxLC_VIRTUAL style, once more than a few hundreds items
  19. need to be displayed because this control is not optimized, neither from
  20. performance nor from user interface point of view, for large number of
  21. items.
  22. Notice that currently @c TAB characters in list box items text are not
  23. handled consistently under all platforms, so they should be replaced by
  24. spaces to display strings properly everywhere. The list box doesn't
  25. support any other control characters at all.
  26. @beginStyleTable
  27. @style{wxLB_SINGLE}
  28. Single-selection list.
  29. @style{wxLB_MULTIPLE}
  30. Multiple-selection list: the user can toggle multiple items on and off.
  31. This is the same as wxLB_EXTENDED in wxGTK2 port.
  32. @style{wxLB_EXTENDED}
  33. Extended-selection list: the user can extend the selection by using
  34. @c SHIFT or @c CTRL keys together with the cursor movement keys or
  35. the mouse.
  36. @style{wxLB_HSCROLL}
  37. Create horizontal scrollbar if contents are too wide (Windows only).
  38. @style{wxLB_ALWAYS_SB}
  39. Always show a vertical scrollbar.
  40. @style{wxLB_NEEDED_SB}
  41. Only create a vertical scrollbar if needed.
  42. @style{wxLB_NO_SB}
  43. Don't create vertical scrollbar (wxMSW only).
  44. @style{wxLB_SORT}
  45. The listbox contents are sorted in alphabetical order.
  46. @endStyleTable
  47. Note that @c wxLB_SINGLE, @c wxLB_MULTIPLE and @c wxLB_EXTENDED styles are
  48. mutually exclusive and you can specify at most one of them (single selection
  49. is the default). See also @ref overview_windowstyles.
  50. @beginEventEmissionTable{wxCommandEvent}
  51. @event{EVT_LISTBOX(id, func)}
  52. Process a @c wxEVT_LISTBOX event, when an item on the
  53. list is selected or the selection changes.
  54. @event{EVT_LISTBOX_DCLICK(id, func)}
  55. Process a @c wxEVT_LISTBOX_DCLICK event, when the listbox
  56. is double-clicked.
  57. @endEventTable
  58. @library{wxcore}
  59. @category{ctrl}
  60. @appearance{listbox}
  61. @see wxEditableListBox, wxChoice, wxComboBox, wxListCtrl, wxCommandEvent
  62. */
  63. class wxListBox : public wxControl,
  64. public wxItemContainer
  65. {
  66. public:
  67. /**
  68. Default constructor.
  69. */
  70. wxListBox();
  71. /**
  72. Constructor, creating and showing a list box.
  73. @param parent
  74. The parent window.
  75. @param id
  76. The ID of this control. A value of @c wxID_ANY indicates a default value.
  77. @param pos
  78. The initial position.
  79. If ::wxDefaultPosition is specified then a default position is chosen.
  80. @param size
  81. The initial size.
  82. If ::wxDefaultSize is specified then the window is sized appropriately.
  83. @param n
  84. Number of strings with which to initialise the control.
  85. @param choices
  86. The strings to use to initialize the control.
  87. @param style
  88. Window style. See wxListBox.
  89. @param validator
  90. The validator for this control.
  91. @param name
  92. The name of this class.
  93. @beginWxPerlOnly
  94. Not supported by wxPerl.
  95. @endWxPerlOnly
  96. */
  97. wxListBox(wxWindow* parent, wxWindowID id,
  98. const wxPoint& pos = wxDefaultPosition,
  99. const wxSize& size = wxDefaultSize,
  100. int n = 0,
  101. const wxString choices[] = NULL,
  102. long style = 0,
  103. const wxValidator& validator = wxDefaultValidator,
  104. const wxString& name = wxListBoxNameStr);
  105. /**
  106. Constructor, creating and showing a list box.
  107. See the other wxListBox() constructor; the only difference is that
  108. this overload takes a wxArrayString instead of a pointer to an array
  109. of wxString.
  110. @beginWxPerlOnly
  111. Use an array reference for the @a choices parameter.
  112. @endWxPerlOnly
  113. */
  114. wxListBox(wxWindow* parent, wxWindowID id,
  115. const wxPoint& pos,
  116. const wxSize& size,
  117. const wxArrayString& choices,
  118. long style = 0,
  119. const wxValidator& validator = wxDefaultValidator,
  120. const wxString& name = wxListBoxNameStr);
  121. /**
  122. Destructor, destroying the list box.
  123. */
  124. virtual ~wxListBox();
  125. //@{
  126. /**
  127. Creates the listbox for two-step construction.
  128. See wxListBox() for further details.
  129. */
  130. bool Create(wxWindow *parent, wxWindowID id,
  131. const wxPoint& pos = wxDefaultPosition,
  132. const wxSize& size = wxDefaultSize,
  133. int n = 0, const wxString choices[] = NULL,
  134. long style = 0,
  135. const wxValidator& validator = wxDefaultValidator,
  136. const wxString& name = wxListBoxNameStr);
  137. bool Create(wxWindow *parent, wxWindowID id,
  138. const wxPoint& pos,
  139. const wxSize& size,
  140. const wxArrayString& choices,
  141. long style = 0,
  142. const wxValidator& validator = wxDefaultValidator,
  143. const wxString& name = wxListBoxNameStr);
  144. //@}
  145. /**
  146. Deselects an item in the list box.
  147. @param n
  148. The zero-based item to deselect.
  149. @remarks This applies to multiple selection listboxes only.
  150. */
  151. void Deselect(int n);
  152. virtual void SetSelection(int n);
  153. virtual int GetSelection() const;
  154. virtual bool SetStringSelection(const wxString& s, bool select);
  155. virtual bool SetStringSelection(const wxString& s);
  156. /**
  157. Fill an array of ints with the positions of the currently selected items.
  158. @param selections
  159. A reference to an wxArrayInt instance that is used to store the result of
  160. the query.
  161. @return The number of selections.
  162. @remarks Use this with a multiple selection listbox.
  163. @beginWxPerlOnly
  164. In wxPerl this method takes no parameters and return the
  165. selected items as a list.
  166. @endWxPerlOnly
  167. @see wxControlWithItems::GetSelection, wxControlWithItems::GetStringSelection,
  168. wxControlWithItems::SetSelection
  169. */
  170. virtual int GetSelections(wxArrayInt& selections) const;
  171. /**
  172. Returns the item located at @a point, or @c wxNOT_FOUND if there
  173. is no item located at @a point.
  174. It is currently implemented for wxMSW, wxMac and wxGTK2 ports.
  175. @param point
  176. Point of item (in client coordinates) to obtain
  177. @return Item located at point, or wxNOT_FOUND if unimplemented or the
  178. item does not exist.
  179. @since 2.7.0
  180. */
  181. int HitTest(const wxPoint& point) const;
  182. /**
  183. @overload
  184. */
  185. int HitTest(int x, int y) const;
  186. /**
  187. Insert the given number of strings before the specified position.
  188. @param nItems
  189. Number of items in the array items
  190. @param items
  191. Labels of items to be inserted
  192. @param pos
  193. Position before which to insert the items: if pos is 0 the
  194. items will be inserted in the beginning of the listbox
  195. @beginWxPerlOnly
  196. Not supported by wxPerl.
  197. @endWxPerlOnly
  198. */
  199. void InsertItems(unsigned int nItems, const wxString *items,
  200. unsigned int pos);
  201. /**
  202. Insert the given number of strings before the specified position.
  203. @param items
  204. Labels of items to be inserted
  205. @param pos
  206. Position before which to insert the items: if pos is @c 0 the
  207. items will be inserted in the beginning of the listbox
  208. @beginWxPerlOnly
  209. Use an array reference for the @a items parameter.
  210. @endWxPerlOnly
  211. */
  212. void InsertItems(const wxArrayString& items,
  213. unsigned int pos);
  214. /**
  215. Determines whether an item is selected.
  216. @param n
  217. The zero-based item index.
  218. @return @true if the given item is selected, @false otherwise.
  219. */
  220. virtual bool IsSelected(int n) const;
  221. /**
  222. Set the specified item to be the first visible item.
  223. @param n
  224. The zero-based item index that should be visible.
  225. */
  226. void SetFirstItem(int n);
  227. /**
  228. Set the specified item to be the first visible item.
  229. @param string
  230. The string that should be visible.
  231. */
  232. void SetFirstItem(const wxString& string);
  233. /**
  234. Ensure that the item with the given index is currently shown.
  235. Scroll the listbox if necessary.
  236. This method is currently only implemented in wxGTK and wxOSX and does
  237. nothing in other ports.
  238. @see SetFirstItem()
  239. */
  240. virtual void EnsureVisible(int n);
  241. /**
  242. Return true if the listbox has ::wxLB_SORT style.
  243. This method is mostly meant for internal use only.
  244. */
  245. virtual bool IsSorted() const;
  246. // NOTE: Phoenix needs to see the implementation of pure virtuals so it
  247. // knows that this class is not abstract.
  248. virtual unsigned int GetCount() const;
  249. virtual wxString GetString(unsigned int n) const;
  250. virtual void SetString(unsigned int n, const wxString& s);
  251. virtual int FindString(const wxString& s, bool bCase = false) const;
  252. };