vlbox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: vlbox.h
  3. // Purpose: interface of wxVListBox
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxVListBox
  9. wxVListBox is a wxListBox-like control with the following two main
  10. differences from a regular wxListBox: it can have an arbitrarily huge
  11. number of items because it doesn't store them itself but uses the
  12. OnDrawItem() callback to draw them (so it is a virtual listbox) and its
  13. items can have variable height as determined by OnMeasureItem() (so it is
  14. also a listbox with the lines of variable height).
  15. Also, as a consequence of its virtual nature, it doesn't have any methods
  16. to append or insert items in it as it isn't necessary to do it: you just
  17. have to call SetItemCount() to tell the control how many items it should
  18. display. Of course, this also means that you will never use this class
  19. directly because it has pure virtual functions, but will need to derive
  20. your own class from it (for example, wxHtmlListBox).
  21. However it emits the same events as wxListBox and the same event macros may
  22. be used with it. Since wxVListBox does not store its items itself, the
  23. events will only contain the index, not any contents such as the string of
  24. an item.
  25. @library{wxcore}
  26. @category{ctrl}
  27. @see wxSimpleHtmlListBox, wxHtmlListBox
  28. */
  29. class wxVListBox : public wxVScrolledWindow
  30. {
  31. public:
  32. /**
  33. Default constructor, you must call Create() later.
  34. */
  35. wxVListBox();
  36. /**
  37. Normal constructor which calls Create() internally.
  38. */
  39. wxVListBox(wxWindow* parent, wxWindowID id = wxID_ANY,
  40. const wxPoint& pos = wxDefaultPosition,
  41. const wxSize& size = wxDefaultSize,
  42. long style = 0, const wxString& name = wxVListBoxNameStr);
  43. /**
  44. Destructor.
  45. */
  46. virtual ~wxVListBox();
  47. /**
  48. Deletes all items from the control.
  49. */
  50. void Clear();
  51. /**
  52. Creates the control. To finish creating it you also should call
  53. SetItemCount() to let it know about the number of items it contains.
  54. The only special style which may be used with wxVListBox is
  55. @c wxLB_MULTIPLE which indicates that the listbox should support
  56. multiple selection.
  57. @return @true on success or @false if the control couldn't be created.
  58. */
  59. bool Create(wxWindow* parent, wxWindowID id = wxID_ANY,
  60. const wxPoint& pos = wxDefaultPosition,
  61. const wxSize& size = wxDefaultSize, long style = 0,
  62. const wxString& name = wxVListBoxNameStr);
  63. /**
  64. Deselects all the items in the listbox. This method is only valid for
  65. multi selection listboxes.
  66. @return @true if any items were changed, i.e. if there had been any
  67. selected items before, or @false if all the items were already
  68. deselected.
  69. @see SelectAll(), Select()
  70. */
  71. bool DeselectAll();
  72. /**
  73. Returns the index of the first selected item in the listbox or
  74. @c wxNOT_FOUND if no items are currently selected.
  75. @a cookie is an opaque parameter which should be passed to the
  76. subsequent calls to GetNextSelected(). It is needed in order to allow
  77. parallel iterations over the selected items.
  78. Here is a typical example of using these functions:
  79. @code
  80. unsigned long cookie;
  81. int item = hlbox->GetFirstSelected(cookie);
  82. while ( item != wxNOT_FOUND )
  83. {
  84. // ... process item ...
  85. item = hlbox->GetNextSelected(cookie);
  86. }
  87. @endcode
  88. This method is only valid for multi selection listboxes.
  89. */
  90. int GetFirstSelected(unsigned long& cookie) const;
  91. /**
  92. Get the number of items in the control.
  93. @see SetItemCount()
  94. */
  95. size_t GetItemCount() const;
  96. /**
  97. Returns the margins used by the control. The @c x field of the returned
  98. point is the horizontal margin and the @c y field is the vertical one.
  99. @see SetMargins()
  100. */
  101. wxPoint GetMargins() const;
  102. /**
  103. Returns the rectangle occupied by this item in physical coordinates.
  104. If the item is not currently visible, returns an empty rectangle.
  105. @since 2.9.0
  106. */
  107. wxRect GetItemRect(size_t item) const;
  108. /**
  109. Returns the index of the next selected item or @c wxNOT_FOUND if there
  110. are no more.
  111. This method is only valid for multi selection listboxes.
  112. @see GetFirstSelected()
  113. */
  114. int GetNextSelected(unsigned long& cookie) const;
  115. /**
  116. Returns the number of the items currently selected.
  117. It is valid for both single and multi selection controls. In the former
  118. case it may only return 0 or 1 however.
  119. @see IsSelected(), GetFirstSelected(), GetNextSelected()
  120. */
  121. size_t GetSelectedCount() const;
  122. /**
  123. Get the currently selected item or @c wxNOT_FOUND if there is no
  124. selection.
  125. */
  126. int GetSelection() const;
  127. /**
  128. Returns the background colour used for the selected cells. By default
  129. the standard system colour is used.
  130. @see wxSystemSettings::GetColour(), SetSelectionBackground()
  131. */
  132. const wxColour& GetSelectionBackground() const;
  133. /**
  134. Returns @true if the listbox was created with @c wxLB_MULTIPLE style
  135. and so supports multiple selection or @false if it is a single
  136. selection listbox.
  137. */
  138. bool HasMultipleSelection() const;
  139. /**
  140. Returns @true if this item is the current one, @false otherwise.
  141. The current item is always the same as selected one for the single
  142. selection listbox and in this case this method is equivalent to
  143. IsSelected() but they are different for multi selection listboxes where
  144. many items may be selected but only one (at most) is current.
  145. */
  146. bool IsCurrent(size_t item) const;
  147. /**
  148. Returns @true if this item is selected, @false otherwise.
  149. */
  150. bool IsSelected(size_t item) const;
  151. /**
  152. Selects or deselects the specified item which must be valid (i.e.\ not
  153. equal to @c wxNOT_FOUND).
  154. @return @true if the items selection status has changed or @false
  155. otherwise.
  156. This function is only valid for the multiple selection listboxes, use
  157. SetSelection() for the single selection ones.
  158. */
  159. bool Select(size_t item, bool select = true);
  160. /**
  161. Selects all the items in the listbox.
  162. @return @true if any items were changed, i.e. if there had been any
  163. unselected items before, or @false if all the items were
  164. already selected.
  165. This method is only valid for multi selection listboxes.
  166. @see DeselectAll(), Select()
  167. */
  168. bool SelectAll();
  169. /**
  170. Selects all items in the specified range which may be given in any
  171. order.
  172. @return @true if the items selection status has changed or @false
  173. otherwise.
  174. This method is only valid for multi selection listboxes.
  175. @see SelectAll(), Select()
  176. */
  177. bool SelectRange(size_t from, size_t to);
  178. /**
  179. Set the number of items to be shown in the control.
  180. This is just a synonym for wxVScrolledWindow::SetRowCount().
  181. */
  182. virtual void SetItemCount(size_t count);
  183. //@{
  184. /**
  185. Set the margins: horizontal margin is the distance between the window
  186. border and the item contents while vertical margin is half of the
  187. distance between items.
  188. By default both margins are 0.
  189. */
  190. void SetMargins(const wxPoint& pt);
  191. void SetMargins(wxCoord x, wxCoord y);
  192. //@}
  193. /**
  194. Set the selection to the specified item, if it is -1 the selection is
  195. unset. The selected item will be automatically scrolled into view if it
  196. isn't currently visible.
  197. This method may be used both with single and multiple selection
  198. listboxes.
  199. */
  200. void SetSelection(int selection);
  201. /**
  202. Sets the colour to be used for the selected cells background. The
  203. background of the standard cells may be changed by simply calling
  204. wxWindow::SetBackgroundColour().
  205. @note Using a non-default background colour may result in control
  206. having an appearance different from the similar native controls
  207. and should be avoided in general.
  208. @see GetSelectionBackground()
  209. */
  210. void SetSelectionBackground(const wxColour& col);
  211. /**
  212. Toggles the state of the specified @a item, i.e.\ selects it if it was
  213. unselected and deselects it if it was selected.
  214. This method is only valid for multi selection listboxes.
  215. @see Select()
  216. */
  217. void Toggle(size_t item);
  218. protected:
  219. /**
  220. The derived class must implement this function to actually draw the
  221. item with the given index on the provided DC.
  222. @param dc
  223. The device context to use for drawing.
  224. @param rect
  225. The bounding rectangle for the item being drawn (DC clipping
  226. region is set to this rectangle before calling this function).
  227. @param n
  228. The index of the item to be drawn.
  229. @todo Change this function signature to non-const.
  230. */
  231. virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const = 0;
  232. /**
  233. This method is used to draw the item's background and, maybe, a border
  234. around it.
  235. The base class version implements a reasonable default behaviour which
  236. consists in drawing the selected item with the standard background
  237. colour and drawing a border around the item if it is either selected or
  238. current.
  239. @todo Change this function signature to non-const.
  240. */
  241. virtual void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
  242. /**
  243. This method may be used to draw separators between the lines. The
  244. rectangle passed to it may be modified, typically to deflate it a bit
  245. before passing to OnDrawItem().
  246. The base class version of this method doesn't do anything.
  247. @param dc
  248. The device context to use for drawing.
  249. @param rect
  250. The bounding rectangle for the item.
  251. @param n
  252. The index of the item.
  253. @todo Change this function signature to non-const.
  254. */
  255. virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
  256. /**
  257. The derived class must implement this method to return the height of
  258. the specified item (in pixels).
  259. */
  260. virtual wxCoord OnMeasureItem(size_t n) const = 0;
  261. };