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