rearrangectrl.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/rearrangectrl.h
  3. // Purpose: various controls for rearranging the items interactively
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-12-15
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_REARRANGECTRL_H_
  10. #define _WX_REARRANGECTRL_H_
  11. #include "wx/checklst.h"
  12. #if wxUSE_REARRANGECTRL
  13. #include "wx/panel.h"
  14. #include "wx/dialog.h"
  15. #include "wx/arrstr.h"
  16. extern WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeListNameStr[];
  17. extern WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeDialogNameStr[];
  18. // ----------------------------------------------------------------------------
  19. // wxRearrangeList: a (check) list box allowing to move items around
  20. // ----------------------------------------------------------------------------
  21. // This class works allows to change the order of the items shown in it as well
  22. // as to check or uncheck them individually. The data structure used to allow
  23. // this is the order array which contains the items indices indexed by their
  24. // position with an added twist that the unchecked items are represented by the
  25. // bitwise complement of the corresponding index (for any architecture using
  26. // two's complement for negative numbers representation (i.e. just about any at
  27. // all) this means that a checked item N is represented by -N-1 in unchecked
  28. // state).
  29. //
  30. // So, for example, the array order [1 -3 0] used in conjunction with the items
  31. // array ["first", "second", "third"] means that the items are displayed in the
  32. // order "second", "third", "first" and the "third" item is unchecked while the
  33. // other two are checked.
  34. class WXDLLIMPEXP_CORE wxRearrangeList : public wxCheckListBox
  35. {
  36. public:
  37. // ctors and such
  38. // --------------
  39. // default ctor, call Create() later
  40. wxRearrangeList() { }
  41. // ctor creating the control, the arguments are the same as for
  42. // wxCheckListBox except for the extra order array which defines the
  43. // (initial) display order of the items as well as their statuses, see the
  44. // description above
  45. wxRearrangeList(wxWindow *parent,
  46. wxWindowID id,
  47. const wxPoint& pos,
  48. const wxSize& size,
  49. const wxArrayInt& order,
  50. const wxArrayString& items,
  51. long style = 0,
  52. const wxValidator& validator = wxDefaultValidator,
  53. const wxString& name = wxRearrangeListNameStr)
  54. {
  55. Create(parent, id, pos, size, order, items, style, validator, name);
  56. }
  57. // Create() function takes the same parameters as the base class one and
  58. // the order array determining the initial display order
  59. bool Create(wxWindow *parent,
  60. wxWindowID id,
  61. const wxPoint& pos,
  62. const wxSize& size,
  63. const wxArrayInt& order,
  64. const wxArrayString& items,
  65. long style = 0,
  66. const wxValidator& validator = wxDefaultValidator,
  67. const wxString& name = wxRearrangeListNameStr);
  68. // items order
  69. // -----------
  70. // get the current items order; the returned array uses the same convention
  71. // as the one passed to the ctor
  72. const wxArrayInt& GetCurrentOrder() const { return m_order; }
  73. // return true if the current item can be moved up or down (i.e. just that
  74. // it's not the first or the last one)
  75. bool CanMoveCurrentUp() const;
  76. bool CanMoveCurrentDown() const;
  77. // move the current item one position up or down, return true if it was moved
  78. // or false if the current item was the first/last one and so nothing was done
  79. bool MoveCurrentUp();
  80. bool MoveCurrentDown();
  81. private:
  82. // swap two items at the given positions in the listbox
  83. void Swap(int pos1, int pos2);
  84. // event handler for item checking/unchecking
  85. void OnCheck(wxCommandEvent& event);
  86. // the current order array
  87. wxArrayInt m_order;
  88. DECLARE_EVENT_TABLE()
  89. wxDECLARE_NO_COPY_CLASS(wxRearrangeList);
  90. };
  91. // ----------------------------------------------------------------------------
  92. // wxRearrangeCtrl: composite control containing a wxRearrangeList and buttons
  93. // ----------------------------------------------------------------------------
  94. class WXDLLIMPEXP_CORE wxRearrangeCtrl : public wxPanel
  95. {
  96. public:
  97. // ctors/Create function are the same as for wxRearrangeList
  98. wxRearrangeCtrl()
  99. {
  100. Init();
  101. }
  102. wxRearrangeCtrl(wxWindow *parent,
  103. wxWindowID id,
  104. const wxPoint& pos,
  105. const wxSize& size,
  106. const wxArrayInt& order,
  107. const wxArrayString& items,
  108. long style = 0,
  109. const wxValidator& validator = wxDefaultValidator,
  110. const wxString& name = wxRearrangeListNameStr)
  111. {
  112. Init();
  113. Create(parent, id, pos, size, order, items, style, validator, name);
  114. }
  115. bool Create(wxWindow *parent,
  116. wxWindowID id,
  117. const wxPoint& pos,
  118. const wxSize& size,
  119. const wxArrayInt& order,
  120. const wxArrayString& items,
  121. long style = 0,
  122. const wxValidator& validator = wxDefaultValidator,
  123. const wxString& name = wxRearrangeListNameStr);
  124. // get the underlying listbox
  125. wxRearrangeList *GetList() const { return m_list; }
  126. private:
  127. // common part of all ctors
  128. void Init();
  129. // event handlers for the buttons
  130. void OnUpdateButtonUI(wxUpdateUIEvent& event);
  131. void OnButton(wxCommandEvent& event);
  132. wxRearrangeList *m_list;
  133. DECLARE_EVENT_TABLE()
  134. wxDECLARE_NO_COPY_CLASS(wxRearrangeCtrl);
  135. };
  136. // ----------------------------------------------------------------------------
  137. // wxRearrangeDialog: dialog containing a wxRearrangeCtrl
  138. // ----------------------------------------------------------------------------
  139. class WXDLLIMPEXP_CORE wxRearrangeDialog : public wxDialog
  140. {
  141. public:
  142. // default ctor, use Create() later
  143. wxRearrangeDialog() { Init(); }
  144. // ctor for the dialog: message is shown inside the dialog itself, order
  145. // and items are passed to wxRearrangeList used internally
  146. wxRearrangeDialog(wxWindow *parent,
  147. const wxString& message,
  148. const wxString& title,
  149. const wxArrayInt& order,
  150. const wxArrayString& items,
  151. const wxPoint& pos = wxDefaultPosition,
  152. const wxString& name = wxRearrangeDialogNameStr)
  153. {
  154. Init();
  155. Create(parent, message, title, order, items, pos, name);
  156. }
  157. bool Create(wxWindow *parent,
  158. const wxString& message,
  159. const wxString& title,
  160. const wxArrayInt& order,
  161. const wxArrayString& items,
  162. const wxPoint& pos = wxDefaultPosition,
  163. const wxString& name = wxRearrangeDialogNameStr);
  164. // methods for the dialog customization
  165. // add extra contents to the dialog below the wxRearrangeCtrl part: the
  166. // given window (usually a wxPanel containing more control inside it) must
  167. // have the dialog as its parent and will be inserted into it at the right
  168. // place by this method
  169. void AddExtraControls(wxWindow *win);
  170. // return the wxRearrangeList control used by the dialog
  171. wxRearrangeList *GetList() const;
  172. // get the order of items after it was modified by the user
  173. wxArrayInt GetOrder() const;
  174. private:
  175. // common part of all ctors
  176. void Init() { m_ctrl = NULL; }
  177. wxRearrangeCtrl *m_ctrl;
  178. wxDECLARE_NO_COPY_CLASS(wxRearrangeDialog);
  179. };
  180. #endif // wxUSE_REARRANGECTRL
  181. #endif // _WX_REARRANGECTRL_H_