]>
Commit | Line | Data |
---|---|---|
f55d21eb VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: editlbox.cpp | |
3 | // Purpose: ListBox with editable items | |
4 | // Author: Vaclav Slavik | |
5 | // RCS-ID: $Id$ | |
6 | // Copyright: (c) Vaclav Slavik | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
f55d21eb VS |
10 | // For compilers that support precompilation, includes "wx/wx.h". |
11 | #include "wx/wxprec.h" | |
12 | ||
13 | #ifdef __BORLANDC__ | |
14 | #pragma hdrstop | |
15 | #endif | |
16 | ||
17 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 18 | // need because it includes almost all "standard" wxWidgets headers) |
f55d21eb VS |
19 | #ifndef WX_PRECOMP |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/gizmos/editlbox.h" | |
24 | #include "wx/sizer.h" | |
25 | #include "wx/listctrl.h" | |
26 | ||
27 | ||
28 | ||
29 | ||
30 | // list control with auto-resizable column: | |
31 | class CleverListCtrl : public wxListCtrl | |
32 | { | |
33 | public: | |
34 | CleverListCtrl(wxWindow *parent, | |
a2d49353 | 35 | wxWindowID id = wxID_ANY, |
f55d21eb VS |
36 | const wxPoint &pos = wxDefaultPosition, |
37 | const wxSize &size = wxDefaultSize, | |
38 | long style = wxLC_ICON, | |
39 | const wxValidator& validator = wxDefaultValidator, | |
52f2ad08 | 40 | const wxString &name = wxListCtrlNameStr) |
f55d21eb VS |
41 | : wxListCtrl(parent, id, pos, size, style, validator, name) |
42 | { | |
43 | CreateColumns(); | |
44 | } | |
45 | ||
46 | void CreateColumns() | |
47 | { | |
48 | InsertColumn(0, _T("item")); | |
49 | SizeColumns(); | |
50 | } | |
51 | ||
52 | void SizeColumns() | |
53 | { | |
54 | int w = GetSize().x; | |
55232d19 | 55 | #ifdef __WXMSW__ |
e1c6c6ae | 56 | w -= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 6; |
55232d19 VS |
57 | #else |
58 | w -= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X); | |
59 | #endif | |
f55d21eb VS |
60 | SetColumnWidth(0, w); |
61 | } | |
62 | ||
63 | private: | |
64 | DECLARE_EVENT_TABLE() | |
65 | void OnSize(wxSizeEvent& event) | |
66 | { | |
67 | SizeColumns(); | |
c48792de | 68 | event.Skip(); |
f55d21eb VS |
69 | } |
70 | }; | |
71 | ||
72 | BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl) | |
73 | EVT_SIZE(CleverListCtrl::OnSize) | |
74 | END_EVENT_TABLE() | |
75 | ||
f55d21eb VS |
76 | #include "eldel.xpm" |
77 | #include "eldown.xpm" | |
78 | #include "eledit.xpm" | |
79 | #include "elnew.xpm" | |
80 | #include "elup.xpm" | |
81 | ||
82 | IMPLEMENT_CLASS(wxEditableListBox, wxPanel) | |
83 | ||
f7ad3cd3 VS |
84 | // NB: generate the IDs at runtime to avoid conflict with XRCID values, |
85 | // they could cause XRCCTRL() failures in XRC-based dialogs | |
86 | const int wxID_ELB_DELETE = wxNewId(); | |
87 | const int wxID_ELB_EDIT = wxNewId(); | |
88 | const int wxID_ELB_NEW = wxNewId(); | |
89 | const int wxID_ELB_UP = wxNewId(); | |
90 | const int wxID_ELB_DOWN = wxNewId(); | |
91 | const int wxID_ELB_LISTCTRL = wxNewId(); | |
f55d21eb VS |
92 | |
93 | BEGIN_EVENT_TABLE(wxEditableListBox, wxPanel) | |
f7ad3cd3 VS |
94 | EVT_LIST_ITEM_SELECTED(wxID_ELB_LISTCTRL, wxEditableListBox::OnItemSelected) |
95 | EVT_LIST_END_LABEL_EDIT(wxID_ELB_LISTCTRL, wxEditableListBox::OnEndLabelEdit) | |
f55d21eb VS |
96 | EVT_BUTTON(wxID_ELB_NEW, wxEditableListBox::OnNewItem) |
97 | EVT_BUTTON(wxID_ELB_UP, wxEditableListBox::OnUpItem) | |
98 | EVT_BUTTON(wxID_ELB_DOWN, wxEditableListBox::OnDownItem) | |
99 | EVT_BUTTON(wxID_ELB_EDIT, wxEditableListBox::OnEditItem) | |
100 | EVT_BUTTON(wxID_ELB_DELETE, wxEditableListBox::OnDelItem) | |
101 | END_EVENT_TABLE() | |
102 | ||
103 | wxEditableListBox::wxEditableListBox(wxWindow *parent, wxWindowID id, | |
104 | const wxString& label, | |
e7d5dd02 | 105 | const wxPoint& pos, const wxSize& size, |
6187ec8f | 106 | long style, |
e7d5dd02 | 107 | const wxString& name) |
55232d19 | 108 | : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name) |
f55d21eb | 109 | { |
6187ec8f | 110 | m_style = style; |
dabbc6a5 | 111 | m_bEdit = m_bNew = m_bDel = m_bUp = m_bDown = NULL; |
dd1d4b13 | 112 | |
f55d21eb | 113 | wxSizer *sizer = new wxBoxSizer(wxVERTICAL); |
96d24601 | 114 | |
a2d49353 | 115 | wxPanel *subp = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, |
f55d21eb VS |
116 | wxSUNKEN_BORDER | wxTAB_TRAVERSAL); |
117 | wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL); | |
a2d49353 | 118 | subsizer->Add(new wxStaticText(subp, wxID_ANY, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4); |
96d24601 | 119 | |
923d52d6 VS |
120 | #ifdef __WXMSW__ |
121 | #define BTN_BORDER 4 | |
6187ec8f RD |
122 | // FIXME - why is this needed? There's some reason why sunken border is |
123 | // ignored by sizers in wxMSW but not in wxGTK that I can't | |
923d52d6 VS |
124 | // figure out... |
125 | #else | |
126 | #define BTN_BORDER 0 | |
127 | #endif | |
128 | ||
dd1d4b13 VS |
129 | if ( m_style & wxEL_ALLOW_EDIT ) |
130 | { | |
131 | m_bEdit = new wxBitmapButton(subp, wxID_ELB_EDIT, wxBitmap(eledit_xpm)); | |
132 | subsizer->Add(m_bEdit, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER); | |
133 | } | |
134 | ||
135 | if ( m_style & wxEL_ALLOW_NEW ) | |
136 | { | |
137 | m_bNew = new wxBitmapButton(subp, wxID_ELB_NEW, wxBitmap(elnew_xpm)); | |
138 | subsizer->Add(m_bNew, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER); | |
139 | } | |
6187ec8f | 140 | |
dd1d4b13 VS |
141 | if ( m_style & wxEL_ALLOW_DELETE ) |
142 | { | |
143 | m_bDel = new wxBitmapButton(subp, wxID_ELB_DELETE, wxBitmap(eldel_xpm)); | |
144 | subsizer->Add(m_bDel, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER); | |
145 | } | |
6187ec8f | 146 | |
a41b5c2f VZ |
147 | if (!(m_style & wxEL_NO_REORDER)) |
148 | { | |
149 | m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm)); | |
150 | subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER); | |
dd1d4b13 | 151 | |
a41b5c2f VZ |
152 | m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm)); |
153 | subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER); | |
154 | } | |
96d24601 | 155 | |
dd1d4b13 | 156 | #if wxUSE_TOOLTIPS |
a84c0558 VS |
157 | if ( m_bEdit ) m_bEdit->SetToolTip(_("Edit item")); |
158 | if ( m_bNew ) m_bNew->SetToolTip(_("New item")); | |
159 | if ( m_bDel ) m_bDel->SetToolTip(_("Delete item")); | |
a41b5c2f VZ |
160 | if ( m_bUp ) m_bUp->SetToolTip(_("Move up")); |
161 | if ( m_bDown ) m_bDown->SetToolTip(_("Move down")); | |
dd1d4b13 VS |
162 | #endif |
163 | ||
f55d21eb VS |
164 | subp->SetSizer(subsizer); |
165 | subsizer->Fit(subp); | |
96d24601 | 166 | |
f55d21eb | 167 | sizer->Add(subp, 0, wxEXPAND); |
6187ec8f RD |
168 | |
169 | long st = wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxSUNKEN_BORDER; | |
dd1d4b13 VS |
170 | if ( style & wxEL_ALLOW_EDIT ) |
171 | st |= wxLC_EDIT_LABELS; | |
f7ad3cd3 | 172 | m_listCtrl = new CleverListCtrl(this, wxID_ELB_LISTCTRL, |
6187ec8f | 173 | wxDefaultPosition, wxDefaultSize, st); |
f55d21eb VS |
174 | wxArrayString empty_ar; |
175 | SetStrings(empty_ar); | |
96d24601 | 176 | |
f55d21eb VS |
177 | sizer->Add(m_listCtrl, 1, wxEXPAND); |
178 | ||
f55d21eb | 179 | SetSizer(sizer); |
35975549 | 180 | Layout(); |
f55d21eb VS |
181 | } |
182 | ||
183 | void wxEditableListBox::SetStrings(const wxArrayString& strings) | |
184 | { | |
185 | m_listCtrl->DeleteAllItems(); | |
186 | size_t i; | |
96d24601 | 187 | |
f55d21eb VS |
188 | for (i = 0; i < strings.GetCount(); i++) |
189 | m_listCtrl->InsertItem(i, strings[i]); | |
96d24601 | 190 | |
dabbc6a5 | 191 | m_listCtrl->InsertItem(strings.GetCount(), wxEmptyString); |
f55d21eb VS |
192 | m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); |
193 | } | |
194 | ||
79e66317 | 195 | void wxEditableListBox::GetStrings(wxArrayString& strings) const |
f55d21eb VS |
196 | { |
197 | strings.Clear(); | |
198 | ||
199 | for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++) | |
200 | strings.Add(m_listCtrl->GetItemText(i)); | |
201 | } | |
202 | ||
203 | void wxEditableListBox::OnItemSelected(wxListEvent& event) | |
204 | { | |
205 | m_selection = event.GetIndex(); | |
a41b5c2f VZ |
206 | if (!(m_style & wxEL_NO_REORDER)) |
207 | { | |
208 | m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1); | |
209 | m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2); | |
210 | } | |
211 | ||
6187ec8f RD |
212 | if (m_style & wxEL_ALLOW_EDIT) |
213 | m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1); | |
214 | if (m_style & wxEL_ALLOW_DELETE) | |
215 | m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1); | |
f55d21eb VS |
216 | } |
217 | ||
c3f815fa | 218 | void wxEditableListBox::OnNewItem(wxCommandEvent& WXUNUSED(event)) |
f55d21eb | 219 | { |
96d24601 | 220 | m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1, |
f55d21eb | 221 | wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); |
f55d21eb VS |
222 | m_listCtrl->EditLabel(m_selection); |
223 | } | |
224 | ||
225 | void wxEditableListBox::OnEndLabelEdit(wxListEvent& event) | |
226 | { | |
dabbc6a5 | 227 | if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 && |
52f2ad08 | 228 | !event.GetText().empty() ) |
f55d21eb | 229 | { |
55232d19 VS |
230 | // The user edited last (empty) line, i.e. added new entry. We have to |
231 | // add new empty line here so that adding one more line is still | |
232 | // possible: | |
dabbc6a5 | 233 | m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxEmptyString); |
bec3b0dd WS |
234 | |
235 | // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item, | |
236 | // so that the buttons are enabled/disabled properly | |
237 | wxListEvent selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED, m_listCtrl->GetId()); | |
238 | selectionEvent.m_itemIndex = event.GetIndex(); | |
239 | m_listCtrl->GetEventHandler()->ProcessEvent(selectionEvent); | |
f55d21eb VS |
240 | } |
241 | } | |
242 | ||
c3f815fa | 243 | void wxEditableListBox::OnDelItem(wxCommandEvent& WXUNUSED(event)) |
f55d21eb VS |
244 | { |
245 | m_listCtrl->DeleteItem(m_selection); | |
96d24601 | 246 | m_listCtrl->SetItemState(m_selection, |
f55d21eb VS |
247 | wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); |
248 | } | |
249 | ||
c3f815fa | 250 | void wxEditableListBox::OnEditItem(wxCommandEvent& WXUNUSED(event)) |
f55d21eb VS |
251 | { |
252 | m_listCtrl->EditLabel(m_selection); | |
253 | } | |
254 | ||
c3f815fa | 255 | void wxEditableListBox::OnUpItem(wxCommandEvent& WXUNUSED(event)) |
f55d21eb VS |
256 | { |
257 | wxString t1, t2; | |
96d24601 | 258 | |
f55d21eb VS |
259 | t1 = m_listCtrl->GetItemText(m_selection - 1); |
260 | t2 = m_listCtrl->GetItemText(m_selection); | |
261 | m_listCtrl->SetItemText(m_selection - 1, t2); | |
262 | m_listCtrl->SetItemText(m_selection, t1); | |
263 | m_listCtrl->SetItemState(m_selection - 1, | |
264 | wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
265 | } | |
266 | ||
c3f815fa | 267 | void wxEditableListBox::OnDownItem(wxCommandEvent& WXUNUSED(event)) |
f55d21eb VS |
268 | { |
269 | wxString t1, t2; | |
96d24601 | 270 | |
f55d21eb VS |
271 | t1 = m_listCtrl->GetItemText(m_selection + 1); |
272 | t2 = m_listCtrl->GetItemText(m_selection); | |
273 | m_listCtrl->SetItemText(m_selection + 1, t2); | |
274 | m_listCtrl->SetItemText(m_selection, t1); | |
275 | m_listCtrl->SetItemState(m_selection + 1, | |
276 | wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED); | |
277 | } |