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