rearrangectrl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/rearrangectrl.h
  3. // Purpose: interface of wxRearrangeList
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-12-15
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. /**
  10. @class wxRearrangeList
  11. A listbox-like control allowing the user to rearrange the items and to
  12. enable or disable them.
  13. This class allows to change the order of the items shown in it as well as
  14. to check or uncheck them individually. The data structure used to allow
  15. this is the order array which contains the items indices indexed by their
  16. position with an added twist that the unchecked items are represented by
  17. the bitwise complement of the corresponding index (for any architecture
  18. using two's complement for negative numbers representation (i.e. just about
  19. any at all) this means that a checked item N is represented by -N-1 in
  20. unchecked state). In practice this means that you must apply the C bitwise
  21. complement operator when constructing the order array, e.g.
  22. @code
  23. wxArrayInt order;
  24. order.push_back(0); // checked item #0
  25. order.push_back(~1); // unchecked item #1
  26. @endcode
  27. So, for example, the array order [1 -3 0] used in conjunction with the
  28. items array ["first", "second", "third"] means that the items order is
  29. "second", "third", "first" and the "third" item is unchecked while the
  30. other two are checked.
  31. This convention is used both for the order argument of the control ctor or
  32. Create() and for the array returned from GetCurrentOrder().
  33. Usually this control will be used together with other controls allowing to
  34. move the items around in it interactively. The simplest possible solution
  35. is to use wxRearrangeCtrl which combines it with two standard buttons to
  36. move the current item up or down.
  37. @since 2.9.0
  38. @library{wxcore}
  39. @category{ctrl}
  40. */
  41. class wxRearrangeList : public wxCheckListBox
  42. {
  43. public:
  44. /**
  45. Default constructor.
  46. Create() must be called later to effectively create the control.
  47. */
  48. wxRearrangeList();
  49. /**
  50. Constructor really creating the control.
  51. Please see Create() for the parameters description.
  52. */
  53. wxRearrangeList(wxWindow *parent,
  54. wxWindowID id,
  55. const wxPoint& pos,
  56. const wxSize& size,
  57. const wxArrayInt& order,
  58. const wxArrayString& items,
  59. long style = 0,
  60. const wxValidator& validator = wxDefaultValidator,
  61. const wxString& name = wxRearrangeListNameStr);
  62. /**
  63. Effectively creates the window for an object created using the default
  64. constructor.
  65. This function is very similar to wxCheckListBox::Create() except that
  66. it has an additional parameter specifying the initial order of the
  67. items. Please see the class documentation for the explanation of the
  68. conventions used by the @a order argument.
  69. @param parent
  70. The parent window, must be non-@NULL.
  71. @param id
  72. The window identifier.
  73. @param pos
  74. The initial window position.
  75. @param size
  76. The initial window size.
  77. @param order
  78. Array specifying the initial order of the items in @a items array.
  79. @param items
  80. The items to display in the list.
  81. @param style
  82. The control style, there are no special styles for this class but
  83. the base class styles can be used here.
  84. @param validator
  85. Optional window validator.
  86. @param name
  87. Optional window name.
  88. */
  89. bool Create(wxWindow *parent,
  90. wxWindowID id,
  91. const wxPoint& pos,
  92. const wxSize& size,
  93. const wxArrayInt& order,
  94. const wxArrayString& items,
  95. long style = 0,
  96. const wxValidator& validator = wxDefaultValidator,
  97. const wxString& name = wxRearrangeListNameStr);
  98. /**
  99. Return the current order of the items.
  100. The order may be different from the one passed to the constructor if
  101. MoveCurrentUp() or MoveCurrentDown() were called.
  102. */
  103. const wxArrayInt& GetCurrentOrder() const;
  104. /**
  105. Return @true if the currently selected item can be moved up.
  106. This function is useful for EVT_UPDATE_UI handler for the standard "Up"
  107. button often used together with this control and wxRearrangeCtrl uses
  108. it in this way.
  109. @return
  110. @true if the currently selected item can be moved up in the
  111. listbox, @false if there is no selection or the current item is the
  112. first one.
  113. @see CanMoveCurrentDown()
  114. */
  115. bool CanMoveCurrentUp() const;
  116. /**
  117. Return @true if the currently selected item can be moved down.
  118. @see CanMoveCurrentUp()
  119. */
  120. bool CanMoveCurrentDown() const;
  121. /**
  122. Move the currently selected item one position above.
  123. This method is useful to implement the standard "Up" button behaviour
  124. and wxRearrangeCtrl uses it for this.
  125. @return
  126. @true if the item was moved or @false if this couldn't be done.
  127. @see MoveCurrentDown()
  128. */
  129. bool MoveCurrentUp();
  130. /**
  131. Move the currently selected item one position below.
  132. @see MoveCurrentUp()
  133. */
  134. bool MoveCurrentDown();
  135. };
  136. /**
  137. @class wxRearrangeCtrl
  138. A composite control containing a wxRearrangeList and the buttons allowing
  139. to move the items in it.
  140. This control is in fact a panel containing the wxRearrangeList control and
  141. the "Up" and "Down" buttons to move the currently selected item up or down.
  142. It is used as the main part of a wxRearrangeDialog.
  143. @since 2.9.0
  144. @library{wxcore}
  145. @category{ctrl}
  146. */
  147. class wxRearrangeCtrl : public wxPanel
  148. {
  149. public:
  150. /**
  151. Default constructor.
  152. Create() must be called later to effectively create the control.
  153. */
  154. wxRearrangeCtrl();
  155. /**
  156. Constructor really creating the control.
  157. Please see Create() for the parameters description.
  158. */
  159. wxRearrangeCtrl(wxWindow *parent,
  160. wxWindowID id,
  161. const wxPoint& pos,
  162. const wxSize& size,
  163. const wxArrayInt& order,
  164. const wxArrayString& items,
  165. long style = 0,
  166. const wxValidator& validator = wxDefaultValidator,
  167. const wxString& name = wxRearrangeListNameStr);
  168. /**
  169. Effectively creates the window for an object created using the default
  170. constructor.
  171. The parameters of this method are the same as for
  172. wxRearrangeList::Create().
  173. */
  174. bool Create(wxWindow *parent,
  175. wxWindowID id,
  176. const wxPoint& pos,
  177. const wxSize& size,
  178. const wxArrayInt& order,
  179. const wxArrayString& items,
  180. long style = 0,
  181. const wxValidator& validator = wxDefaultValidator,
  182. const wxString& name = wxRearrangeListNameStr);
  183. /**
  184. Return the listbox which is the main part of this control.
  185. */
  186. wxRearrangeList *GetList() const;
  187. };
  188. /**
  189. @class wxRearrangeDialog
  190. A dialog allowing the user to rearrange the specified items.
  191. This dialog can be used to allow the user to modify the order of the items
  192. and to enable or disable them individually. For example:
  193. @code
  194. wxArrayString items;
  195. items.push_back("meat");
  196. items.push_back("fish");
  197. items.push_back("fruits");
  198. items.push_back("beer");
  199. wxArrayInt order;
  200. order.push_back(3);
  201. order.push_back(0);
  202. order.push_back(1);
  203. order.push_back(2);
  204. wxRearrangeDialog dlg(NULL,
  205. "You can also uncheck the items you don't like "
  206. "at all.",
  207. "Sort the items in order of preference",
  208. order, items);
  209. if ( dlg.ShowModal() == wxID_OK ) {
  210. order = dlg.GetOrder();
  211. for ( size_t n = 0; n < order.size(); n++ ) {
  212. if ( order[n] >= 0 ) {
  213. wxLogMessage("Your most preferred item is \"%s\"",
  214. items[order[n]]);
  215. break;
  216. }
  217. }
  218. }
  219. @endcode
  220. @since 2.9.0
  221. @library{wxcore}
  222. @category{cmndlg}
  223. */
  224. class wxRearrangeDialog : public wxDialog
  225. {
  226. public:
  227. /**
  228. Default constructor.
  229. Create() must be called later to effectively create the control.
  230. */
  231. wxRearrangeDialog();
  232. /**
  233. Constructor creating the dialog.
  234. Please see Create() for the parameters description.
  235. */
  236. wxRearrangeDialog(wxWindow *parent,
  237. const wxString& message,
  238. const wxString& title,
  239. const wxArrayInt& order,
  240. const wxArrayString& items,
  241. const wxPoint& pos = wxDefaultPosition,
  242. const wxString& name = wxRearrangeDialogNameStr);
  243. /**
  244. Effectively creates the dialog for an object created using the default
  245. constructor.
  246. @param parent
  247. The dialog parent, possibly @NULL.
  248. @param message
  249. The message shown inside the dialog itself, above the items list.
  250. @param title
  251. The title of the dialog.
  252. @param order
  253. The initial order of the items in the convention used by
  254. wxRearrangeList.
  255. @param items
  256. The items to show in the dialog.
  257. @param pos
  258. Optional dialog position.
  259. @param name
  260. Optional dialog name.
  261. @return
  262. @true if the dialog was successfully created or @false if creation
  263. failed.
  264. */
  265. bool Create(wxWindow *parent,
  266. const wxString& message,
  267. const wxString& title,
  268. const wxArrayInt& order,
  269. const wxArrayString& items,
  270. const wxPoint& pos = wxDefaultPosition,
  271. const wxString& name = wxRearrangeDialogNameStr);
  272. /**
  273. Customize the dialog by adding extra controls to it.
  274. This function adds the given @a win to the dialog, putting it just
  275. below the part occupied by wxRearrangeCtrl. It must be called after
  276. creating the dialog and you will typically need to process the events
  277. generated by the extra controls for them to do something useful.
  278. For example:
  279. @code
  280. class MyRearrangeDialog : public wxRearrangeDialog
  281. {
  282. public:
  283. MyRearrangeDialog(wxWindow *parent, ...)
  284. : wxRearrangeDialog(parent, ...)
  285. {
  286. wxPanel *panel = new wxPanel(this);
  287. wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
  288. sizer->Add(new wxStaticText(panel, wxID_ANY,
  289. "Column width in pixels:"));
  290. sizer->Add(new wxTextCtrl(panel, wxID_ANY, ""));
  291. panel->SetSizer(sizer);
  292. AddExtraControls(panel);
  293. }
  294. ... code to update the text control with the currently selected
  295. item width and to react to its changes omitted ...
  296. };
  297. @endcode
  298. See also the complete example of a custom rearrange dialog in the
  299. dialogs sample.
  300. @param win
  301. The window containing the extra controls. It must have this dialog
  302. as its parent.
  303. */
  304. void AddExtraControls(wxWindow *win);
  305. /**
  306. Return the list control used by the dialog.
  307. @see wxRearrangeCtrl::GetList()
  308. */
  309. wxRearrangeList *GetList() const;
  310. /**
  311. Return the array describing the order of items after it was modified by
  312. the user.
  313. Please notice that the array will contain negative items if any items
  314. were unchecked. See wxRearrangeList for more information about the
  315. convention used for this array.
  316. */
  317. wxArrayInt GetOrder() const;
  318. };