ctrlsub.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/ctrlsub.h (read: "wxConTRoL with SUBitems")
  3. // Purpose: wxControlWithItems interface
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 22.10.99
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_CTRLSUB_H_BASE_
  11. #define _WX_CTRLSUB_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_CONTROLS
  14. #include "wx/arrstr.h"
  15. #include "wx/control.h" // base class
  16. // ----------------------------------------------------------------------------
  17. // wxItemContainer defines an interface which is implemented by all controls
  18. // which have string subitems each of which may be selected.
  19. //
  20. // It is decomposed in wxItemContainerImmutable which omits all methods
  21. // adding/removing items and is used by wxRadioBox and wxItemContainer itself.
  22. //
  23. // Examples: wxListBox, wxCheckListBox, wxChoice and wxComboBox (which
  24. // implements an extended interface deriving from this one)
  25. // ----------------------------------------------------------------------------
  26. class WXDLLIMPEXP_CORE wxItemContainerImmutable
  27. {
  28. public:
  29. wxItemContainerImmutable() { }
  30. virtual ~wxItemContainerImmutable();
  31. // accessing strings
  32. // -----------------
  33. virtual unsigned int GetCount() const = 0;
  34. bool IsEmpty() const { return GetCount() == 0; }
  35. virtual wxString GetString(unsigned int n) const = 0;
  36. wxArrayString GetStrings() const;
  37. virtual void SetString(unsigned int n, const wxString& s) = 0;
  38. // finding string natively is either case sensitive or insensitive
  39. // but never both so fall back to this base version for not
  40. // supported search type
  41. virtual int FindString(const wxString& s, bool bCase = false) const
  42. {
  43. unsigned int count = GetCount();
  44. for ( unsigned int i = 0; i < count ; ++i )
  45. {
  46. if (GetString(i).IsSameAs( s , bCase ))
  47. return (int)i;
  48. }
  49. return wxNOT_FOUND;
  50. }
  51. // selection
  52. // ---------
  53. virtual void SetSelection(int n) = 0;
  54. virtual int GetSelection() const = 0;
  55. // set selection to the specified string, return false if not found
  56. bool SetStringSelection(const wxString& s);
  57. // return the selected string or empty string if none
  58. virtual wxString GetStringSelection() const;
  59. // this is the same as SetSelection( for single-selection controls but
  60. // reads better for multi-selection ones
  61. void Select(int n) { SetSelection(n); }
  62. protected:
  63. // check that the index is valid
  64. bool IsValid(unsigned int n) const { return n < GetCount(); }
  65. bool IsValidInsert(unsigned int n) const { return n <= GetCount(); }
  66. };
  67. // ----------------------------------------------------------------------------
  68. // wxItemContainer extends wxItemContainerImmutable interface with methods
  69. // for adding/removing items.
  70. //
  71. // Classes deriving from this one must override DoInsertItems() to implement
  72. // adding items to the control. This can often be implemented more efficiently
  73. // than simply looping over the elements and inserting them but if this is not
  74. // the case, the generic DoInsertItemsInLoop can be used in implementation, but
  75. // in this case DoInsertItem() needs to be overridden.
  76. // ----------------------------------------------------------------------------
  77. class WXDLLIMPEXP_CORE wxItemContainer : public wxItemContainerImmutable
  78. {
  79. private:
  80. // AppendItems() and InsertItems() helpers just call DoAppend/InsertItems()
  81. // after doing some checks
  82. //
  83. // NB: they're defined here so that they're inlined when used in public part
  84. int AppendItems(const wxArrayStringsAdapter& items,
  85. void **clientData,
  86. wxClientDataType type)
  87. {
  88. if ( items.IsEmpty() )
  89. return wxNOT_FOUND;
  90. return DoAppendItems(items, clientData, type);
  91. }
  92. int AppendItems(const wxArrayStringsAdapter& items)
  93. {
  94. return AppendItems(items, NULL, wxClientData_None);
  95. }
  96. int AppendItems(const wxArrayStringsAdapter& items, void **clientData)
  97. {
  98. wxASSERT_MSG( GetClientDataType() != wxClientData_Object,
  99. wxT("can't mix different types of client data") );
  100. return AppendItems(items, clientData, wxClientData_Void);
  101. }
  102. int AppendItems(const wxArrayStringsAdapter& items,
  103. wxClientData **clientData)
  104. {
  105. wxASSERT_MSG( GetClientDataType() != wxClientData_Void,
  106. wxT("can't mix different types of client data") );
  107. return AppendItems(items, reinterpret_cast<void **>(clientData),
  108. wxClientData_Object);
  109. }
  110. int InsertItems(const wxArrayStringsAdapter& items,
  111. unsigned int pos,
  112. void **clientData,
  113. wxClientDataType type)
  114. {
  115. wxASSERT_MSG( !IsSorted(), wxT("can't insert items in sorted control") );
  116. wxCHECK_MSG( pos <= GetCount(), wxNOT_FOUND,
  117. wxT("position out of range") );
  118. // not all derived classes handle empty arrays correctly in
  119. // DoInsertItems() and besides it really doesn't make much sense to do
  120. // this (for append it could correspond to creating an initially empty
  121. // control but why would anybody need to insert 0 items?)
  122. wxCHECK_MSG( !items.IsEmpty(), wxNOT_FOUND,
  123. wxT("need something to insert") );
  124. return DoInsertItems(items, pos, clientData, type);
  125. }
  126. int InsertItems(const wxArrayStringsAdapter& items, unsigned int pos)
  127. {
  128. return InsertItems(items, pos, NULL, wxClientData_None);
  129. }
  130. int InsertItems(const wxArrayStringsAdapter& items,
  131. unsigned int pos,
  132. void **clientData)
  133. {
  134. wxASSERT_MSG( GetClientDataType() != wxClientData_Object,
  135. wxT("can't mix different types of client data") );
  136. return InsertItems(items, pos, clientData, wxClientData_Void);
  137. }
  138. int InsertItems(const wxArrayStringsAdapter& items,
  139. unsigned int pos,
  140. wxClientData **clientData)
  141. {
  142. wxASSERT_MSG( GetClientDataType() != wxClientData_Void,
  143. wxT("can't mix different types of client data") );
  144. return InsertItems(items, pos,
  145. reinterpret_cast<void **>(clientData),
  146. wxClientData_Object);
  147. }
  148. public:
  149. wxItemContainer() { m_clientDataItemsType = wxClientData_None; }
  150. virtual ~wxItemContainer();
  151. // adding items
  152. // ------------
  153. // append single item, return its position in the control (which can be
  154. // different from the last one if the control is sorted)
  155. int Append(const wxString& item)
  156. { return AppendItems(item); }
  157. int Append(const wxString& item, void *clientData)
  158. { return AppendItems(item, &clientData); }
  159. int Append(const wxString& item, wxClientData *clientData)
  160. { return AppendItems(item, &clientData); }
  161. // append several items at once to the control, return the position of the
  162. // last item appended
  163. int Append(const wxArrayString& items)
  164. { return AppendItems(items); }
  165. int Append(const wxArrayString& items, void **clientData)
  166. { return AppendItems(items, clientData); }
  167. int Append(const wxArrayString& items, wxClientData **clientData)
  168. { return AppendItems(items, clientData); }
  169. int Append(unsigned int n, const wxString *items)
  170. { return AppendItems(wxArrayStringsAdapter(n, items)); }
  171. int Append(unsigned int n, const wxString *items, void **clientData)
  172. { return AppendItems(wxArrayStringsAdapter(n, items), clientData); }
  173. int Append(unsigned int n,
  174. const wxString *items,
  175. wxClientData **clientData)
  176. { return AppendItems(wxArrayStringsAdapter(n, items), clientData); }
  177. // only for RTTI needs (separate name)
  178. void AppendString(const wxString& item)
  179. { Append(item); }
  180. // inserting items: not for sorted controls!
  181. // -----------------------------------------
  182. // insert single item at the given position, return its effective position
  183. int Insert(const wxString& item, unsigned int pos)
  184. { return InsertItems(item, pos); }
  185. int Insert(const wxString& item, unsigned int pos, void *clientData)
  186. { return InsertItems(item, pos, &clientData); }
  187. int Insert(const wxString& item, unsigned int pos, wxClientData *clientData)
  188. { return InsertItems(item, pos, &clientData); }
  189. // insert several items at once into the control, return the index of the
  190. // last item inserted
  191. int Insert(const wxArrayString& items, unsigned int pos)
  192. { return InsertItems(items, pos); }
  193. int Insert(const wxArrayString& items, unsigned int pos, void **clientData)
  194. { return InsertItems(items, pos, clientData); }
  195. int Insert(const wxArrayString& items,
  196. unsigned int pos,
  197. wxClientData **clientData)
  198. { return InsertItems(items, pos, clientData); }
  199. int Insert(unsigned int n, const wxString *items, unsigned int pos)
  200. { return InsertItems(wxArrayStringsAdapter(n, items), pos); }
  201. int Insert(unsigned int n,
  202. const wxString *items,
  203. unsigned int pos,
  204. void **clientData)
  205. { return InsertItems(wxArrayStringsAdapter(n, items), pos, clientData); }
  206. int Insert(unsigned int n,
  207. const wxString *items,
  208. unsigned int pos,
  209. wxClientData **clientData)
  210. { return InsertItems(wxArrayStringsAdapter(n, items), pos, clientData); }
  211. // replacing items
  212. // ---------------
  213. void Set(const wxArrayString& items)
  214. { Clear(); Append(items); }
  215. void Set(const wxArrayString& items, void **clientData)
  216. { Clear(); Append(items, clientData); }
  217. void Set(const wxArrayString& items, wxClientData **clientData)
  218. { Clear(); Append(items, clientData); }
  219. void Set(unsigned int n, const wxString *items)
  220. { Clear(); Append(n, items); }
  221. void Set(unsigned int n, const wxString *items, void **clientData)
  222. { Clear(); Append(n, items, clientData); }
  223. void Set(unsigned int n, const wxString *items, wxClientData **clientData)
  224. { Clear(); Append(n, items, clientData); }
  225. // deleting items
  226. // --------------
  227. void Clear();
  228. void Delete(unsigned int pos);
  229. // various accessors
  230. // -----------------
  231. // The control may maintain its items in a sorted order in which case
  232. // items are automatically inserted at the right position when they are
  233. // inserted or appended. Derived classes have to override this method if
  234. // they implement sorting, typically by returning HasFlag(wxXX_SORT)
  235. virtual bool IsSorted() const { return false; }
  236. // client data stuff
  237. // -----------------
  238. void SetClientData(unsigned int n, void* clientData);
  239. void* GetClientData(unsigned int n) const;
  240. // SetClientObject() takes ownership of the pointer, GetClientObject()
  241. // returns it but keeps the ownership while DetachClientObject() expects
  242. // the caller to delete the pointer and also resets the internally stored
  243. // one to NULL for this item
  244. void SetClientObject(unsigned int n, wxClientData* clientData);
  245. wxClientData* GetClientObject(unsigned int n) const;
  246. wxClientData* DetachClientObject(unsigned int n);
  247. // return the type of client data stored in this control: usually it just
  248. // returns m_clientDataItemsType but must be overridden in the controls
  249. // which delegate their client data storage to another one (e.g. wxChoice
  250. // in wxUniv which stores data in wxListBox which it uses anyhow); don't
  251. // forget to override SetClientDataType() if you override this one
  252. //
  253. // NB: for this to work no code should ever access m_clientDataItemsType
  254. // directly but only via this function!
  255. virtual wxClientDataType GetClientDataType() const
  256. { return m_clientDataItemsType; }
  257. bool HasClientData() const
  258. { return GetClientDataType() != wxClientData_None; }
  259. bool HasClientObjectData() const
  260. { return GetClientDataType() == wxClientData_Object; }
  261. bool HasClientUntypedData() const
  262. { return GetClientDataType() == wxClientData_Void; }
  263. protected:
  264. // there is usually no need to override this method but you can do it if it
  265. // is more convenient to only do "real" insertions in DoInsertItems() and
  266. // to implement items appending here (in which case DoInsertItems() should
  267. // call this method if pos == GetCount() as it can still be called in this
  268. // case if public Insert() is called with such position)
  269. virtual int DoAppendItems(const wxArrayStringsAdapter& items,
  270. void **clientData,
  271. wxClientDataType type)
  272. {
  273. return DoInsertItems(items, GetCount(), clientData, type);
  274. }
  275. // this method must be implemented to insert the items into the control at
  276. // position pos which can be GetCount() meaning that the items should be
  277. // appended; for the sorted controls the position can be ignored
  278. //
  279. // the derived classes typically use AssignNewItemClientData() to
  280. // associate the data with the items as they're being inserted
  281. //
  282. // the method should return the index of the position the last item was
  283. // inserted into or wxNOT_FOUND if an error occurred
  284. virtual int DoInsertItems(const wxArrayStringsAdapter & items,
  285. unsigned int pos,
  286. void **clientData,
  287. wxClientDataType type) = 0;
  288. // before the client data is set for the first time for the control which
  289. // hadn't had it before, DoInitItemClientData() is called which gives the
  290. // derived class the possibility to initialize its client data storage only
  291. // when client data is really used
  292. virtual void DoInitItemClientData() { }
  293. virtual void DoSetItemClientData(unsigned int n, void *clientData) = 0;
  294. virtual void *DoGetItemClientData(unsigned int n) const = 0;
  295. virtual void DoClear() = 0;
  296. virtual void DoDeleteOneItem(unsigned int pos) = 0;
  297. // methods useful for the derived classes which don't have any better way
  298. // of adding multiple items to the control than doing it one by one: such
  299. // classes should call DoInsertItemsInLoop() from their DoInsert() and
  300. // override DoInsertOneItem() to perform the real insertion
  301. virtual int DoInsertOneItem(const wxString& item, unsigned int pos);
  302. int DoInsertItemsInLoop(const wxArrayStringsAdapter& items,
  303. unsigned int pos,
  304. void **clientData,
  305. wxClientDataType type);
  306. // helper for DoInsertItems(): n is the index into clientData, pos is the
  307. // position of the item in the control
  308. void AssignNewItemClientData(unsigned int pos,
  309. void **clientData,
  310. unsigned int n,
  311. wxClientDataType type);
  312. // free the client object associated with the item at given position and
  313. // set it to NULL (must only be called if HasClientObjectData())
  314. void ResetItemClientObject(unsigned int n);
  315. // set the type of the client data stored in this control: override this if
  316. // you override GetClientDataType()
  317. virtual void SetClientDataType(wxClientDataType clientDataItemsType)
  318. {
  319. m_clientDataItemsType = clientDataItemsType;
  320. }
  321. private:
  322. // the type of the client data for the items
  323. wxClientDataType m_clientDataItemsType;
  324. };
  325. // Inheriting directly from a wxWindow-derived class and wxItemContainer
  326. // unfortunately introduces an ambiguity for all GetClientXXX() methods as they
  327. // are inherited twice: the "global" versions from wxWindow and the per-item
  328. // versions taking the index from wxItemContainer.
  329. //
  330. // So we need to explicitly resolve them and this helper template class is
  331. // provided to do it. To use it, simply inherit from wxWindowWithItems<Window,
  332. // Container> instead of Window and Container interface directly.
  333. template <class W, class C>
  334. class wxWindowWithItems : public W, public C
  335. {
  336. public:
  337. typedef W BaseWindowClass;
  338. typedef C BaseContainerInterface;
  339. wxWindowWithItems() { }
  340. void SetClientData(void *data)
  341. { BaseWindowClass::SetClientData(data); }
  342. void *GetClientData() const
  343. { return BaseWindowClass::GetClientData(); }
  344. void SetClientObject(wxClientData *data)
  345. { BaseWindowClass::SetClientObject(data); }
  346. wxClientData *GetClientObject() const
  347. { return BaseWindowClass::GetClientObject(); }
  348. void SetClientData(unsigned int n, void* clientData)
  349. { wxItemContainer::SetClientData(n, clientData); }
  350. void* GetClientData(unsigned int n) const
  351. { return wxItemContainer::GetClientData(n); }
  352. void SetClientObject(unsigned int n, wxClientData* clientData)
  353. { wxItemContainer::SetClientObject(n, clientData); }
  354. wxClientData* GetClientObject(unsigned int n) const
  355. { return wxItemContainer::GetClientObject(n); }
  356. };
  357. class WXDLLIMPEXP_CORE wxControlWithItemsBase :
  358. public wxWindowWithItems<wxControl, wxItemContainer>
  359. {
  360. public:
  361. wxControlWithItemsBase() { }
  362. // usually the controls like list/combo boxes have their own background
  363. // colour
  364. virtual bool ShouldInheritColours() const { return false; }
  365. // Implementation only from now on.
  366. // Generate an event of the given type for the selection change.
  367. void SendSelectionChangedEvent(wxEventType eventType);
  368. protected:
  369. // fill in the client object or data field of the event as appropriate
  370. //
  371. // calls InitCommandEvent() and, if n != wxNOT_FOUND, also sets the per
  372. // item client data
  373. void InitCommandEventWithItems(wxCommandEvent& event, int n);
  374. private:
  375. wxDECLARE_NO_COPY_CLASS(wxControlWithItemsBase);
  376. };
  377. // define the platform-specific wxControlWithItems class
  378. #if defined(__WXMSW__)
  379. #include "wx/msw/ctrlsub.h"
  380. #elif defined(__WXMOTIF__)
  381. #include "wx/motif/ctrlsub.h"
  382. #else
  383. class WXDLLIMPEXP_CORE wxControlWithItems : public wxControlWithItemsBase
  384. {
  385. public:
  386. wxControlWithItems() { }
  387. private:
  388. DECLARE_ABSTRACT_CLASS(wxControlWithItems)
  389. wxDECLARE_NO_COPY_CLASS(wxControlWithItems);
  390. };
  391. #endif
  392. #endif // wxUSE_CONTROLS
  393. #endif // _WX_CTRLSUB_H_BASE_