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