ctrlsub.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: ctrlsub.h
  3. // Purpose: interface of wxControlWithItems
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxItemContainerImmutable
  9. wxItemContainer defines an interface which is implemented by all controls
  10. which have string subitems each of which may be selected.
  11. It is decomposed in wxItemContainerImmutable which omits all methods
  12. adding/removing items and is used by wxRadioBox and wxItemContainer itself.
  13. Note that this is not a control, it's a mixin interface that classes
  14. have to derive from in addition to wxControl or wxWindow.
  15. Examples: wxListBox, wxCheckListBox, wxChoice and wxComboBox (which
  16. implements an extended interface deriving from this one)
  17. @library{wxcore}
  18. @category{ctrl}
  19. @see wxControlWithItems, wxItemContainer
  20. */
  21. class wxItemContainerImmutable
  22. {
  23. public:
  24. /// Constructor
  25. wxItemContainerImmutable();
  26. //@{
  27. /**
  28. Returns the number of items in the control.
  29. @see IsEmpty()
  30. */
  31. virtual unsigned int GetCount() const = 0;
  32. /**
  33. Returns @true if the control is empty or @false if it has some items.
  34. @see GetCount()
  35. */
  36. bool IsEmpty() const;
  37. /**
  38. Returns the label of the item with the given index.
  39. @param n
  40. The zero-based index.
  41. @return The label of the item or an empty string if the position was
  42. invalid.
  43. */
  44. virtual wxString GetString(unsigned int n) const = 0;
  45. /**
  46. Returns the array of the labels of all items in the control.
  47. */
  48. wxArrayString GetStrings() const;
  49. /**
  50. Sets the label for the given item.
  51. @param n
  52. The zero-based item index.
  53. @param string
  54. The label to set.
  55. */
  56. virtual void SetString(unsigned int n, const wxString& string) = 0;
  57. /**
  58. Finds an item whose label matches the given string.
  59. @param string
  60. String to find.
  61. @param caseSensitive
  62. Whether search is case sensitive (default is not).
  63. @return The zero-based position of the item, or wxNOT_FOUND if the
  64. string was not found.
  65. */
  66. virtual int FindString(const wxString& string, bool caseSensitive = false) const;
  67. //@}
  68. /// @name Selection
  69. //@{
  70. /**
  71. Sets the selection to the given item @a n or removes the selection
  72. entirely if @a n == @c wxNOT_FOUND.
  73. Note that this does not cause any command events to be emitted nor does
  74. it deselect any other items in the controls which support multiple
  75. selections.
  76. @param n
  77. The string position to select, starting from zero.
  78. @see SetString(), SetStringSelection()
  79. */
  80. virtual void SetSelection(int n) = 0;
  81. /**
  82. Returns the index of the selected item or @c wxNOT_FOUND if no item is
  83. selected.
  84. @return The position of the current selection.
  85. @remarks This method can be used with single selection list boxes only,
  86. you should use wxListBox::GetSelections() for the list
  87. boxes with wxLB_MULTIPLE style.
  88. @see SetSelection(), GetStringSelection()
  89. */
  90. virtual int GetSelection() const = 0;
  91. /**
  92. Selects the item with the specified string in the control.
  93. This method doesn't cause any command events to be emitted.
  94. Notice that this method is case-insensitive, i.e. the string is
  95. compared with all the elements of the control case-insensitively and
  96. the first matching entry is selected, even if it doesn't have exactly
  97. the same case as this string and there is an exact match afterwards.
  98. @param string
  99. The string to select.
  100. @return @true if the specified string has been selected, @false if it
  101. wasn't found in the control.
  102. */
  103. bool SetStringSelection(const wxString& string);
  104. /**
  105. Returns the label of the selected item or an empty string if no item is
  106. selected.
  107. @see GetSelection()
  108. */
  109. virtual wxString GetStringSelection() const;
  110. /**
  111. This is the same as SetSelection() and exists only because it is
  112. slightly more natural for controls which support multiple selection.
  113. */
  114. void Select(int n);
  115. //@}
  116. };
  117. /**
  118. @class wxItemContainer
  119. This class is an abstract base class for some wxWidgets controls which
  120. contain several items such as wxListBox, wxCheckListBox, wxComboBox or
  121. wxChoice. It defines an interface which is implemented by all controls
  122. which have string subitems each of which may be selected.
  123. wxItemContainer extends wxItemContainerImmutable interface with methods
  124. for adding/removing items.
  125. It defines the methods for accessing the controls items and although each
  126. of the derived classes implements them differently, they still all conform
  127. to the same interface.
  128. The items in a wxItemContainer have (non-empty) string labels and,
  129. optionally, client data associated with them. Client data may be of two
  130. different kinds: either simple untyped (@c void *) pointers which are
  131. simply stored by the control but not used in any way by it, or typed
  132. pointers (wxClientData*) which are owned by the control meaning that the
  133. typed client data (and only it) will be deleted when an item is deleted
  134. using Delete() or the entire control is cleared using Clear(), which also
  135. happens when it is destroyed.
  136. Finally note that in the same control all items must have client data of
  137. the same type (typed or untyped), if any. This type is determined by the
  138. first call to Append() (the version with client data pointer) or
  139. SetClientData().
  140. Note that this is not a control, it's a mixin interface that classes
  141. have to derive from in addition to wxControl or wxWindow. Convenience
  142. class wxControlWithItems is provided for this purpose.
  143. @library{wxcore}
  144. @category{ctrl}
  145. @see wxControlWithItems, wxItemContainerImmutable
  146. */
  147. class wxItemContainer : public wxItemContainerImmutable
  148. {
  149. public:
  150. //@{
  151. /**
  152. Appends item into the control.
  153. @param item
  154. String to add.
  155. @return The return value is the index of the newly inserted item.
  156. Note that this may be different from the last one if the
  157. control is sorted (e.g. has @c wxLB_SORT or @c wxCB_SORT
  158. style).
  159. */
  160. int Append(const wxString& item);
  161. /**
  162. Appends item into the control.
  163. @param item
  164. String to add.
  165. @param clientData
  166. Pointer to client data to associate with the new item.
  167. @return The return value is the index of the newly inserted item.
  168. Note that this may be different from the last one if the
  169. control is sorted (e.g. has @c wxLB_SORT or @c wxCB_SORT
  170. style).
  171. */
  172. int Append(const wxString& item, void* clientData);
  173. /**
  174. Appends item into the control.
  175. @param item
  176. String to add.
  177. @param clientData
  178. Pointer to client data to associate with the new item.
  179. @return The return value is the index of the newly inserted item.
  180. Note that this may be different from the last one if the
  181. control is sorted (e.g. has @c wxLB_SORT or @c wxCB_SORT
  182. style).
  183. */
  184. int Append(const wxString& item, wxClientData* clientData);
  185. /**
  186. Appends several items at once into the control.
  187. Notice that calling this method is usually much faster than appending
  188. them one by one if you need to add a lot of items.
  189. @param items
  190. Array of strings to insert.
  191. */
  192. int Append(const wxArrayString& items);
  193. /**
  194. Appends several items at once into the control.
  195. Notice that calling this method is usually much faster than appending
  196. them one by one if you need to add a lot of items.
  197. @param items
  198. Array of strings to insert.
  199. @param clientData
  200. Array of client data pointers of the same size as @a items to
  201. associate with the new items.
  202. */
  203. int Append(const wxArrayString& items, void **clientData);
  204. /**
  205. Appends several items at once into the control.
  206. Notice that calling this method is usually much faster than appending
  207. them one by one if you need to add a lot of items.
  208. @param items
  209. Array of strings to insert.
  210. @param clientData
  211. Array of client data pointers of the same size as @a items to
  212. associate with the new items.
  213. */
  214. int Append(const wxArrayString& items, wxClientData **clientData);
  215. /**
  216. Appends several items at once into the control.
  217. Notice that calling this method is usually much faster than appending
  218. them one by one if you need to add a lot of items.
  219. @param n
  220. Number of items in the @a items array.
  221. @param items
  222. Array of strings of size @a n.
  223. */
  224. int Append(unsigned int n, const wxString* items);
  225. /**
  226. Appends several items at once into the control.
  227. Notice that calling this method is usually much faster than appending
  228. them one by one if you need to add a lot of items.
  229. @param n
  230. Number of items in the @a items array.
  231. @param items
  232. Array of strings of size @a n.
  233. @param clientData
  234. Array of client data pointers of size @a n to associate with the
  235. new items.
  236. */
  237. int Append(unsigned int n, const wxString* items,
  238. void** clientData);
  239. /**
  240. Appends several items at once into the control.
  241. Notice that calling this method is usually much faster than appending
  242. them one by one if you need to add a lot of items.
  243. @param n
  244. Number of items in the @a items array.
  245. @param items
  246. Array of strings of size @a n.
  247. @param clientData
  248. Array of client data pointers of size @a n to associate with the
  249. new items.
  250. */
  251. int Append(unsigned int n, const wxString* items,
  252. wxClientData** clientData);
  253. //@}
  254. /**
  255. Removes all items from the control.
  256. Clear() also deletes the client data of the existing items if it is
  257. owned by the control.
  258. */
  259. void Clear();
  260. /**
  261. Deletes an item from the control.
  262. The client data associated with the item will be also deleted if it is
  263. owned by the control. Note that it is an error (signalled by an assert
  264. failure in debug builds) to remove an item with the index negative or
  265. greater or equal than the number of items in the control.
  266. @param n
  267. The zero-based item index.
  268. @see Clear()
  269. */
  270. void Delete(unsigned int n);
  271. /**
  272. Returns the client object associated with the given item and transfers
  273. its ownership to the caller.
  274. This method, unlike GetClientObject(), expects the caller to delete the
  275. returned pointer. It also replaces the internally stored pointer with
  276. @NULL, i.e. completely detaches the client object pointer from the
  277. control.
  278. It's an error to call this method unless HasClientObjectData() returns
  279. @true.
  280. @param n
  281. The zero-based item index.
  282. @return The associated client object pointer to be deleted by caller or
  283. @NULL.
  284. @since 2.9.2
  285. */
  286. wxClientData *DetachClientObject(unsigned int n);
  287. /**
  288. Returns true, if either untyped data (@c void*) or object data (wxClientData*)
  289. is associated with the items of the control.
  290. */
  291. bool HasClientData() const;
  292. /**
  293. Returns true, if object data is associated with the items of the
  294. control.
  295. Object data pointers have the type @c wxClientData* instead of @c void*
  296. and, importantly, are owned by the control, i.e. will be deleted by it,
  297. unlike their untyped counterparts.
  298. */
  299. bool HasClientObjectData() const;
  300. /**
  301. Returns true, if untyped data (@c void*)
  302. is associated with the items of the control.
  303. */
  304. bool HasClientUntypedData() const;
  305. //@{
  306. /**
  307. Returns a pointer to the client data associated with the given item (if
  308. any). It is an error to call this function for a control which doesn't
  309. have untyped client data at all although it is OK to call it even if
  310. the given item doesn't have any client data associated with it (but
  311. other items do).
  312. @param n
  313. The zero-based position of the item.
  314. @return A pointer to the client data, or @NULL if not present.
  315. */
  316. void* GetClientData(unsigned int n) const;
  317. /**
  318. Returns a pointer to the client data associated with the given item (if
  319. any). It is an error to call this function for a control which doesn't
  320. have typed client data at all although it is OK to call it even if the
  321. given item doesn't have any client data associated with it (but other
  322. items do).
  323. Notice that the returned pointer is still owned by the control and will
  324. be deleted by it, use DetachClientObject() if you want to remove the
  325. pointer from the control.
  326. @param n
  327. The zero-based position of the item.
  328. @return A pointer to the client data, or @NULL if not present.
  329. */
  330. wxClientData* GetClientObject(unsigned int n) const;
  331. /**
  332. Associates the given untyped client data pointer with the given item.
  333. Note that it is an error to call this function if any typed client data
  334. pointers had been associated with the control items before.
  335. @param n
  336. The zero-based item index.
  337. @param data
  338. The client data to associate with the item.
  339. */
  340. void SetClientData(unsigned int n, void* data);
  341. /**
  342. Associates the given typed client data pointer with the given item: the
  343. @a data object will be deleted when the item is deleted (either
  344. explicitly by using Delete() or implicitly when the control itself is
  345. destroyed). Note that it is an error to call this function if any
  346. untyped client data pointers had been associated with the control items
  347. before.
  348. @param n
  349. The zero-based item index.
  350. @param data
  351. The client data to associate with the item.
  352. */
  353. void SetClientObject(unsigned int n, wxClientData* data);
  354. //@}
  355. //@{
  356. /**
  357. Inserts item into the control.
  358. @param item
  359. String to add.
  360. @param pos
  361. Position to insert item before, zero based.
  362. @return The return value is the index of the newly inserted item.
  363. If the insertion failed for some reason, -1 is returned.
  364. */
  365. int Insert(const wxString& item, unsigned int pos);
  366. /**
  367. Inserts item into the control.
  368. @param item
  369. String to add.
  370. @param pos
  371. Position to insert item before, zero based.
  372. @param clientData
  373. Pointer to client data to associate with the new item.
  374. @return The return value is the index of the newly inserted item.
  375. If the insertion failed for some reason, -1 is returned.
  376. */
  377. int Insert(const wxString& item, unsigned int pos, void* clientData);
  378. /**
  379. Inserts item into the control.
  380. @param item
  381. String to add.
  382. @param pos
  383. Position to insert item before, zero based.
  384. @param clientData
  385. Pointer to client data to associate with the new item.
  386. @return The return value is the index of the newly inserted item.
  387. If the insertion failed for some reason, -1 is returned.
  388. */
  389. int Insert(const wxString& item, unsigned int pos,
  390. wxClientData* clientData);
  391. /**
  392. Inserts several items at once into the control.
  393. Notice that calling this method is usually much faster than inserting
  394. them one by one if you need to insert a lot of items.
  395. @param items
  396. Array of strings to insert.
  397. @param pos
  398. Position to insert the items before, zero based.
  399. @return The return value is the index of the last inserted item.
  400. If the insertion failed for some reason, -1 is returned.
  401. */
  402. int Insert(const wxArrayString& items, unsigned int pos);
  403. /**
  404. Inserts several items at once into the control.
  405. Notice that calling this method is usually much faster than inserting
  406. them one by one if you need to insert a lot of items.
  407. @param items
  408. Array of strings to insert.
  409. @param pos
  410. Position to insert the items before, zero based.
  411. @param clientData
  412. Array of client data pointers of the same size as @a items to
  413. associate with the new items.
  414. @return The return value is the index of the last inserted item.
  415. If the insertion failed for some reason, -1 is returned.
  416. */
  417. int Insert(const wxArrayString& items, unsigned int pos,
  418. void **clientData);
  419. /**
  420. Inserts several items at once into the control.
  421. Notice that calling this method is usually much faster than inserting
  422. them one by one if you need to insert a lot of items.
  423. @param items
  424. Array of strings to insert.
  425. @param pos
  426. Position to insert the items before, zero based.
  427. @param clientData
  428. Array of client data pointers of the same size as @a items to
  429. associate with the new items.
  430. @return The return value is the index of the last inserted item.
  431. If the insertion failed for some reason, -1 is returned.
  432. */
  433. int Insert(const wxArrayString& items, unsigned int pos,
  434. wxClientData **clientData);
  435. /**
  436. Inserts several items at once into the control.
  437. Notice that calling this method is usually much faster than inserting
  438. them one by one if you need to insert a lot of items.
  439. @param n
  440. Number of items in the @a items array.
  441. @param items
  442. Array of strings of size @a n.
  443. @param pos
  444. Position to insert the items before, zero based.
  445. @return The return value is the index of the last inserted item.
  446. If the insertion failed for some reason, -1 is returned.
  447. */
  448. int Insert(unsigned int n, const wxString* items,
  449. unsigned int pos);
  450. /**
  451. Inserts several items at once into the control.
  452. Notice that calling this method is usually much faster than inserting
  453. them one by one if you need to insert a lot of items.
  454. @param n
  455. Number of items in the @a items array.
  456. @param items
  457. Array of strings of size @a n.
  458. @param pos
  459. Position to insert the new items before, zero based.
  460. @param clientData
  461. Array of client data pointers of size @a n to associate with the
  462. new items.
  463. @return The return value is the index of the last inserted item.
  464. If the insertion failed for some reason, -1 is returned.
  465. */
  466. int Insert(unsigned int n, const wxString* items,
  467. unsigned int pos,
  468. void** clientData);
  469. /**
  470. Inserts several items at once into the control.
  471. Notice that calling this method is usually much faster than inserting
  472. them one by one if you need to insert a lot of items.
  473. @param n
  474. Number of items in the @a items array.
  475. @param items
  476. Array of strings of size @a n.
  477. @param pos
  478. Position to insert the new items before, zero based.
  479. @param clientData
  480. Array of client data pointers of size @a n to associate with the
  481. new items.
  482. @return The return value is the index of the last inserted item.
  483. If the insertion failed for some reason, -1 is returned.
  484. */
  485. int Insert(unsigned int n, const wxString* items,
  486. unsigned int pos,
  487. wxClientData** clientData);
  488. //@}
  489. //@{
  490. /**
  491. Replaces the current control contents with the given items.
  492. Notice that calling this method is usually much faster than appending
  493. them one by one if you need to add a lot of items.
  494. @param items
  495. Array of strings to insert.
  496. */
  497. void Set(const wxArrayString& items);
  498. /**
  499. Replaces the current control contents with the given items.
  500. Notice that calling this method is usually much faster than appending
  501. them one by one if you need to add a lot of items.
  502. @param items
  503. Array of strings to insert.
  504. @param clientData
  505. Array of client data pointers of the same size as @a items to
  506. associate with the new items.
  507. */
  508. void Set(const wxArrayString& items, void **clientData);
  509. /**
  510. Replaces the current control contents with the given items.
  511. Notice that calling this method is usually much faster than appending
  512. them one by one if you need to add a lot of items.
  513. @param items
  514. Array of strings to insert.
  515. @param clientData
  516. Array of client data pointers of the same size as @a items to
  517. associate with the new items.
  518. */
  519. void Set(const wxArrayString& items, wxClientData **clientData);
  520. /**
  521. Replaces the current control contents with the given items.
  522. Notice that calling this method is usually much faster than appending
  523. them one by one if you need to add a lot of items.
  524. @param n
  525. Number of items in the @a items array.
  526. @param items
  527. Array of strings of size @a n.
  528. */
  529. void Set(unsigned int n, const wxString* items);
  530. /**
  531. Replaces the current control contents with the given items.
  532. Notice that calling this method is usually much faster than appending
  533. them one by one if you need to add a lot of items.
  534. @param n
  535. Number of items in the @a items array.
  536. @param items
  537. Array of strings of size @a n.
  538. @param clientData
  539. Array of client data pointers of size @a n to associate with the
  540. new items.
  541. */
  542. void Set(unsigned int n, const wxString* items, void** clientData);
  543. /**
  544. Replaces the current control contents with the given items.
  545. Notice that calling this method is usually much faster than appending
  546. them one by one if you need to add a lot of items.
  547. @param n
  548. Number of items in the @a items array.
  549. @param items
  550. Array of strings of size @a n.
  551. @param clientData
  552. Array of client data pointers of size @a n to associate with the
  553. new items.
  554. */
  555. void Set(unsigned int n, const wxString* items, wxClientData** clientData);
  556. //@}
  557. };
  558. /**
  559. @class wxControlWithItems
  560. This is convenience class that derives from both wxControl and
  561. wxItemContainer. It is used as basis for some wxWidgets controls
  562. (wxChoice and wxListBox).
  563. @library{wxcore}
  564. @category{ctrl}
  565. @see wxItemContainer, wxItemContainerImmutable
  566. */
  567. class wxControlWithItems : public wxControl, public wxItemContainer
  568. {
  569. };