ctrlsub.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/ctrlsub.h
  3. // Purpose: common functionality of wxItemContainer-derived controls
  4. // Author: Vadim Zeitlin
  5. // Created: 2007-07-25
  6. // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_GENERIC_CTRLSUB_H_
  10. #define _WX_GENERIC_CTRLSUB_H_
  11. #include "wx/dynarray.h"
  12. // ----------------------------------------------------------------------------
  13. // wxControlWithItemsGeneric: generic implementation of item client data
  14. // ----------------------------------------------------------------------------
  15. class wxControlWithItemsGeneric : public wxControlWithItemsBase
  16. {
  17. public:
  18. wxControlWithItemsGeneric() { }
  19. virtual void DoInitItemClientData()
  20. {
  21. m_itemsClientData.resize(GetCount(), NULL);
  22. }
  23. virtual void DoSetItemClientData(unsigned int n, void *clientData)
  24. {
  25. m_itemsClientData[n] = clientData;
  26. }
  27. virtual void *DoGetItemClientData(unsigned int n) const
  28. {
  29. return m_itemsClientData[n];
  30. }
  31. virtual void DoClear() { m_itemsClientData.clear(); }
  32. virtual void DoDeleteOneItem(unsigned int pos)
  33. {
  34. if ( HasClientData() )
  35. m_itemsClientData.RemoveAt(pos);
  36. }
  37. protected:
  38. // preallocate memory for numItems new items: this should be called from
  39. // the derived classes DoInsertItems() to speed up appending big numbers of
  40. // items with client data; it is safe to call even if we don't use client
  41. // data at all and does nothing in this case
  42. void AllocClientData(unsigned int numItems)
  43. {
  44. if ( HasClientData() )
  45. m_itemsClientData.reserve(m_itemsClientData.size() + numItems);
  46. }
  47. // this must be called by derived classes when a new item is added to the
  48. // control to add storage for the corresponding client data pointer (before
  49. // inserting many items, call AllocClientData())
  50. void InsertNewItemClientData(unsigned int pos,
  51. void **clientData,
  52. unsigned int n,
  53. wxClientDataType type)
  54. {
  55. if ( InitClientDataIfNeeded(type) )
  56. m_itemsClientData.Insert(clientData[n], pos);
  57. }
  58. // the same as InsertNewItemClientData() but for numItems consecutive items
  59. // (this can only be used if the control doesn't support sorting as
  60. // otherwise the items positions wouldn't be consecutive any more)
  61. void InsertNewItemsClientData(unsigned int pos,
  62. unsigned int numItems,
  63. void **clientData,
  64. wxClientDataType type)
  65. {
  66. if ( InitClientDataIfNeeded(type) )
  67. {
  68. // it's more efficient to insert everything at once and then update
  69. // for big number of items to avoid moving the array contents
  70. // around (which would result in O(N^2) algorithm)
  71. m_itemsClientData.Insert(NULL, pos, numItems);
  72. for ( unsigned int n = 0; n < numItems; ++n, ++pos )
  73. m_itemsClientData[pos] = clientData[n];
  74. }
  75. }
  76. // vector containing the client data pointers: it is either empty (if
  77. // client data is not used) or has the same number of elements as the
  78. // control
  79. wxArrayPtrVoid m_itemsClientData;
  80. private:
  81. // initialize client data if needed, return false if we don't have any
  82. // client data and true otherwise
  83. bool InitClientDataIfNeeded(wxClientDataType type)
  84. {
  85. if ( !HasClientData() )
  86. {
  87. if ( type == wxClientData_None )
  88. {
  89. // we didn't have the client data before and are not asked to
  90. // store it now neither
  91. return false;
  92. }
  93. // this is the first time client data is used with this control
  94. DoInitItemClientData();
  95. SetClientDataType(type);
  96. }
  97. //else: we already have client data
  98. return true;
  99. }
  100. wxDECLARE_NO_COPY_CLASS(wxControlWithItemsGeneric);
  101. };
  102. #endif // _WX_GENERIC_CTRLSUB_H_