selstore.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/selstore.h
  3. // Purpose: wxSelectionStore stores selected items in a control
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 08.06.03 (extracted from src/generic/listctrl.cpp)
  7. // Copyright: (c) 2000-2003 Vadim Zeitlin <vadim@wxwidgets.org>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_SELSTORE_H_
  11. #define _WX_SELSTORE_H_
  12. #include "wx/dynarray.h"
  13. // ----------------------------------------------------------------------------
  14. // wxSelectedIndices is just a sorted array of indices
  15. // ----------------------------------------------------------------------------
  16. inline int CMPFUNC_CONV wxUIntCmp(unsigned n1, unsigned n2)
  17. {
  18. return (int)(n1 - n2);
  19. }
  20. WX_DEFINE_SORTED_EXPORTED_ARRAY_CMP_INT(unsigned, wxUIntCmp, wxSelectedIndices);
  21. // ----------------------------------------------------------------------------
  22. // wxSelectionStore is used to store the selected items in the virtual
  23. // controls, i.e. it is well suited for storing even when the control contains
  24. // a huge (practically infinite) number of items.
  25. //
  26. // Of course, internally it still has to store the selected items somehow (as
  27. // an array currently) but the advantage is that it can handle the selection
  28. // of all items (common operation) efficiently and that it could be made even
  29. // smarter in the future (e.g. store the selections as an array of ranges +
  30. // individual items) without changing its API.
  31. // ----------------------------------------------------------------------------
  32. class WXDLLIMPEXP_CORE wxSelectionStore
  33. {
  34. public:
  35. wxSelectionStore() : m_itemsSel(wxUIntCmp) { Init(); }
  36. // set the total number of items we handle
  37. void SetItemCount(unsigned count);
  38. // special case of SetItemCount(0)
  39. void Clear() { m_itemsSel.Clear(); m_count = 0; m_defaultState = false; }
  40. // must be called when a new item is inserted/added
  41. void OnItemAdd(unsigned WXUNUSED(item)) { wxFAIL_MSG( wxT("TODO") ); }
  42. // must be called when an item is deleted
  43. void OnItemDelete(unsigned item);
  44. // select one item, use SelectRange() insted if possible!
  45. //
  46. // returns true if the items selection really changed
  47. bool SelectItem(unsigned item, bool select = true);
  48. // select the range of items (inclusive)
  49. //
  50. // return true and fill the itemsChanged array with the indices of items
  51. // which have changed state if "few" of them did, otherwise return false
  52. // (meaning that too many items changed state to bother counting them
  53. // individually)
  54. bool SelectRange(unsigned itemFrom, unsigned itemTo,
  55. bool select = true,
  56. wxArrayInt *itemsChanged = NULL);
  57. // return true if the given item is selected
  58. bool IsSelected(unsigned item) const;
  59. // return the total number of selected items
  60. unsigned GetSelectedCount() const
  61. {
  62. return m_defaultState ? m_count - m_itemsSel.GetCount()
  63. : m_itemsSel.GetCount();
  64. }
  65. private:
  66. // (re)init
  67. void Init() { m_count = 0; m_defaultState = false; }
  68. // the total number of items we handle
  69. unsigned m_count;
  70. // the default state: normally, false (i.e. off) but maybe set to true if
  71. // there are more selected items than non selected ones - this allows to
  72. // handle selection of all items efficiently
  73. bool m_defaultState;
  74. // the array of items whose selection state is different from default
  75. wxSelectedIndices m_itemsSel;
  76. wxDECLARE_NO_COPY_CLASS(wxSelectionStore);
  77. };
  78. #endif // _WX_SELSTORE_H_