vlbox.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/vlbox.h
  3. // Purpose: wxVListBox is a virtual listbox with lines of variable height
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 31.05.03
  7. // Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_VLBOX_H_
  11. #define _WX_VLBOX_H_
  12. #include "wx/vscroll.h" // base class
  13. #include "wx/bitmap.h"
  14. class WXDLLIMPEXP_FWD_CORE wxSelectionStore;
  15. extern WXDLLIMPEXP_DATA_CORE(const char) wxVListBoxNameStr[];
  16. // ----------------------------------------------------------------------------
  17. // wxVListBox
  18. // ----------------------------------------------------------------------------
  19. /*
  20. This class has two main differences from a regular listbox: it can have an
  21. arbitrarily huge number of items because it doesn't store them itself but
  22. uses OnDrawItem() callback to draw them and its items can have variable
  23. height as determined by OnMeasureItem().
  24. It emits the same events as wxListBox and the same event macros may be used
  25. with it.
  26. */
  27. class WXDLLIMPEXP_CORE wxVListBox : public wxVScrolledWindow
  28. {
  29. public:
  30. // constructors and such
  31. // ---------------------
  32. // default constructor, you must call Create() later
  33. wxVListBox() { Init(); }
  34. // normal constructor which calls Create() internally
  35. wxVListBox(wxWindow *parent,
  36. wxWindowID id = wxID_ANY,
  37. const wxPoint& pos = wxDefaultPosition,
  38. const wxSize& size = wxDefaultSize,
  39. long style = 0,
  40. const wxString& name = wxVListBoxNameStr)
  41. {
  42. Init();
  43. (void)Create(parent, id, pos, size, style, name);
  44. }
  45. // really creates the control and sets the initial number of items in it
  46. // (which may be changed later with SetItemCount())
  47. //
  48. // the only special style which may be specified here is wxLB_MULTIPLE
  49. //
  50. // returns true on success or false if the control couldn't be created
  51. bool Create(wxWindow *parent,
  52. wxWindowID id = wxID_ANY,
  53. const wxPoint& pos = wxDefaultPosition,
  54. const wxSize& size = wxDefaultSize,
  55. long style = 0,
  56. const wxString& name = wxVListBoxNameStr);
  57. // dtor does some internal cleanup (deletes m_selStore if any)
  58. virtual ~wxVListBox();
  59. // accessors
  60. // ---------
  61. // get the number of items in the control
  62. size_t GetItemCount() const { return GetRowCount(); }
  63. // does this control use multiple selection?
  64. bool HasMultipleSelection() const { return m_selStore != NULL; }
  65. // get the currently selected item or wxNOT_FOUND if there is no selection
  66. //
  67. // this method is only valid for the single selection listboxes
  68. int GetSelection() const
  69. {
  70. wxASSERT_MSG( !HasMultipleSelection(),
  71. wxT("GetSelection() can't be used with wxLB_MULTIPLE") );
  72. return m_current;
  73. }
  74. // is this item the current one?
  75. bool IsCurrent(size_t item) const { return item == (size_t)m_current; }
  76. #ifdef __WXUNIVERSAL__
  77. bool IsCurrent() const { return wxVScrolledWindow::IsCurrent(); }
  78. #endif
  79. // is this item selected?
  80. bool IsSelected(size_t item) const;
  81. // get the number of the selected items (maybe 0)
  82. //
  83. // this method is valid for both single and multi selection listboxes
  84. size_t GetSelectedCount() const;
  85. // get the first selected item, returns wxNOT_FOUND if none
  86. //
  87. // cookie is an opaque parameter which should be passed to
  88. // GetNextSelected() later
  89. //
  90. // this method is only valid for the multi selection listboxes
  91. int GetFirstSelected(unsigned long& cookie) const;
  92. // get next selection item, return wxNOT_FOUND if no more
  93. //
  94. // cookie must be the same parameter that was passed to GetFirstSelected()
  95. // before
  96. //
  97. // this method is only valid for the multi selection listboxes
  98. int GetNextSelected(unsigned long& cookie) const;
  99. // get the margins around each item
  100. wxPoint GetMargins() const { return m_ptMargins; }
  101. // get the background colour of selected cells
  102. const wxColour& GetSelectionBackground() const { return m_colBgSel; }
  103. // get the item rect, returns empty rect if the item is not visible
  104. wxRect GetItemRect(size_t n) const;
  105. // operations
  106. // ----------
  107. // set the number of items to be shown in the control
  108. //
  109. // this is just a synonym for wxVScrolledWindow::SetRowCount()
  110. virtual void SetItemCount(size_t count);
  111. // delete all items from the control
  112. void Clear() { SetItemCount(0); }
  113. // set the selection to the specified item, if it is wxNOT_FOUND the
  114. // selection is unset
  115. //
  116. // this function is only valid for the single selection listboxes
  117. void SetSelection(int selection);
  118. // selects or deselects the specified item which must be valid (i.e. not
  119. // equal to wxNOT_FOUND)
  120. //
  121. // return true if the items selection status has changed or false
  122. // otherwise
  123. //
  124. // this function is only valid for the multiple selection listboxes
  125. bool Select(size_t item, bool select = true);
  126. // selects the items in the specified range whose end points may be given
  127. // in any order
  128. //
  129. // return true if any items selection status has changed, false otherwise
  130. //
  131. // this function is only valid for the single selection listboxes
  132. bool SelectRange(size_t from, size_t to);
  133. // toggle the selection of the specified item (must be valid)
  134. //
  135. // this function is only valid for the multiple selection listboxes
  136. void Toggle(size_t item) { Select(item, !IsSelected(item)); }
  137. // select all items in the listbox
  138. //
  139. // the return code indicates if any items were affected by this operation
  140. // (true) or if nothing has changed (false)
  141. bool SelectAll() { return DoSelectAll(true); }
  142. // unselect all items in the listbox
  143. //
  144. // the return code has the same meaning as for SelectAll()
  145. bool DeselectAll() { return DoSelectAll(false); }
  146. // set the margins: horizontal margin is the distance between the window
  147. // border and the item contents while vertical margin is half of the
  148. // distance between items
  149. //
  150. // by default both margins are 0
  151. void SetMargins(const wxPoint& pt);
  152. void SetMargins(wxCoord x, wxCoord y) { SetMargins(wxPoint(x, y)); }
  153. // change the background colour of the selected cells
  154. void SetSelectionBackground(const wxColour& col);
  155. // refreshes only the selected items
  156. void RefreshSelected();
  157. virtual wxVisualAttributes GetDefaultAttributes() const
  158. {
  159. return GetClassDefaultAttributes(GetWindowVariant());
  160. }
  161. static wxVisualAttributes
  162. GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
  163. protected:
  164. virtual wxBorder GetDefaultBorder() const { return wxBORDER_THEME; }
  165. // the derived class must implement this function to actually draw the item
  166. // with the given index on the provided DC
  167. virtual void OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const = 0;
  168. // the derived class must implement this method to return the height of the
  169. // specified item
  170. virtual wxCoord OnMeasureItem(size_t n) const = 0;
  171. // this method may be used to draw separators between the lines; note that
  172. // the rectangle may be modified, typically to deflate it a bit before
  173. // passing to OnDrawItem()
  174. //
  175. // the base class version doesn't do anything
  176. virtual void OnDrawSeparator(wxDC& dc, wxRect& rect, size_t n) const;
  177. // this method is used to draw the items background and, maybe, a border
  178. // around it
  179. //
  180. // the base class version implements a reasonable default behaviour which
  181. // consists in drawing the selected item with the standard background
  182. // colour and drawing a border around the item if it is either selected or
  183. // current
  184. virtual void OnDrawBackground(wxDC& dc, const wxRect& rect, size_t n) const;
  185. // we implement OnGetRowHeight() in terms of OnMeasureItem() because this
  186. // allows us to add borders to the items easily
  187. //
  188. // this function is not supposed to be overridden by the derived classes
  189. virtual wxCoord OnGetRowHeight(size_t line) const;
  190. // event handlers
  191. void OnPaint(wxPaintEvent& event);
  192. void OnKeyDown(wxKeyEvent& event);
  193. void OnLeftDown(wxMouseEvent& event);
  194. void OnLeftDClick(wxMouseEvent& event);
  195. void OnSetOrKillFocus(wxFocusEvent& event);
  196. void OnSize(wxSizeEvent& event);
  197. // common part of all ctors
  198. void Init();
  199. // send the wxEVT_LISTBOX event
  200. void SendSelectedEvent();
  201. virtual void InitEvent(wxCommandEvent& event, int n);
  202. // common implementation of SelectAll() and DeselectAll()
  203. bool DoSelectAll(bool select);
  204. // change the current item (in single selection listbox it also implicitly
  205. // changes the selection); current may be wxNOT_FOUND in which case there
  206. // will be no current item any more
  207. //
  208. // return true if the current item changed, false otherwise
  209. bool DoSetCurrent(int current);
  210. // flags for DoHandleItemClick
  211. enum
  212. {
  213. ItemClick_Shift = 1, // item shift-clicked
  214. ItemClick_Ctrl = 2, // ctrl
  215. ItemClick_Kbd = 4 // item selected from keyboard
  216. };
  217. // common part of keyboard and mouse handling processing code
  218. void DoHandleItemClick(int item, int flags);
  219. // paint the background of the given item using the provided colour if it's
  220. // valid, otherwise just return false and do nothing (this is used by
  221. // OnDrawBackground())
  222. bool DoDrawSolidBackground(const wxColour& col,
  223. wxDC& dc,
  224. const wxRect& rect,
  225. size_t n) const;
  226. private:
  227. // the current item or wxNOT_FOUND
  228. //
  229. // if m_selStore == NULL this is also the selected item, otherwise the
  230. // selections are managed by m_selStore
  231. int m_current;
  232. // the anchor of the selection for the multiselection listboxes:
  233. // shift-clicking an item extends the selection from m_anchor to the item
  234. // clicked, for example
  235. //
  236. // always wxNOT_FOUND for single selection listboxes
  237. int m_anchor;
  238. // the object managing our selected items if not NULL
  239. wxSelectionStore *m_selStore;
  240. // margins
  241. wxPoint m_ptMargins;
  242. // the selection bg colour
  243. wxColour m_colBgSel;
  244. DECLARE_EVENT_TABLE()
  245. wxDECLARE_NO_COPY_CLASS(wxVListBox);
  246. DECLARE_ABSTRACT_CLASS(wxVListBox)
  247. };
  248. #endif // _WX_VLBOX_H_