[ 1713933 ] docs for wxEditableListBox
[wxWidgets.git] / src / generic / editlbox.cpp
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
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 #if wxUSE_EDITABLELISTBOX
18
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWidgets headers)
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25 #include "wx/editlbox.h"
26 #include "wx/sizer.h"
27 #include "wx/listctrl.h"
28
29 // ============================================================================
30 // implementation
31 // ============================================================================
32
33 const wxChar wxEditableListBoxNameStr[] = wxT("editableListBox");
34
35 static char * eledit_xpm[] = {
36 "16 16 3 1",
37 " c None",
38 ". c #000000",
39 "+ c #00007F",
40 " ",
41 " ",
42 " .. .. ",
43 " . ",
44 " . ",
45 " ++++ . ++++ ",
46 " ++ . ++ ++",
47 " +++++ . ++++++",
48 " ++ ++ . ++ ",
49 " ++ ++ . ++ ++",
50 " +++++ . ++++ ",
51 " . ",
52 " . ",
53 " .. .. ",
54 " ",
55 " "};
56
57 static char * elnew_xpm[] = {
58 "16 16 5 1",
59 " c None",
60 ". c #7F7F7F",
61 "+ c #FFFFFF",
62 "@ c #FFFF00",
63 "# c #000000",
64 " ",
65 " ",
66 " . .+ .@ ",
67 " . .@.@# # # ",
68 " @.@+.... # ",
69 " ... @@ ",
70 " @ . @. # ",
71 " .# .@ ",
72 " . # ",
73 " # ",
74 " # ",
75 " # ",
76 " # ",
77 " # # # # # # ",
78 " ",
79 " "};
80
81 static char * eldel_xpm[] = {
82 "16 16 3 1",
83 " c None",
84 ". c #7F0000",
85 "+ c #FFFFFF",
86 " ",
87 " ",
88 " ",
89 " ..+ ..+ ",
90 " ....+ ..+ ",
91 " ....+ ..+ ",
92 " ...+ .+ ",
93 " .....+ ",
94 " ...+ ",
95 " .....+ ",
96 " ...+ ..+ ",
97 " ...+ ..+ ",
98 " ...+ .+ ",
99 " ...+ .+ ",
100 " . . ",
101 " "};
102
103 static char * eldown_xpm[] = {
104 "16 16 2 1",
105 " c None",
106 ". c #000000",
107 " ",
108 " ",
109 " ... ",
110 " ... ",
111 " ... ",
112 " ... ",
113 " ... ",
114 " ... ",
115 " ........... ",
116 " ......... ",
117 " ....... ",
118 " ..... ",
119 " ... ",
120 " . ",
121 " ",
122 " "};
123
124 static char * elup_xpm[] = {
125 "16 16 2 1",
126 " c None",
127 ". c #000000",
128 " ",
129 " . ",
130 " ... ",
131 " ..... ",
132 " ....... ",
133 " ......... ",
134 " ........... ",
135 " ... ",
136 " ... ",
137 " ... ",
138 " ... ",
139 " ... ",
140 " ... ",
141 " ",
142 " ",
143 " "};
144
145 // list control with auto-resizable column:
146 class CleverListCtrl : public wxListCtrl
147 {
148 public:
149 CleverListCtrl(wxWindow *parent,
150 wxWindowID id = wxID_ANY,
151 const wxPoint &pos = wxDefaultPosition,
152 const wxSize &size = wxDefaultSize,
153 long style = wxLC_ICON,
154 const wxValidator& validator = wxDefaultValidator,
155 const wxString &name = wxListCtrlNameStr)
156 : wxListCtrl(parent, id, pos, size, style, validator, name)
157 {
158 CreateColumns();
159 }
160
161 void CreateColumns()
162 {
163 InsertColumn(0, _T("item"));
164 SizeColumns();
165 }
166
167 void SizeColumns()
168 {
169 int w = GetSize().x;
170 #ifdef __WXMSW__
171 w -= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 6;
172 #else
173 w -= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
174 #endif
175 SetColumnWidth(0, w);
176 }
177
178 private:
179 DECLARE_EVENT_TABLE()
180 void OnSize(wxSizeEvent& event)
181 {
182 SizeColumns();
183 event.Skip();
184 }
185 };
186
187 BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
188 EVT_SIZE(CleverListCtrl::OnSize)
189 END_EVENT_TABLE()
190
191
192 // ----------------------------------------------------------------------------
193 // wxEditableListBox
194 // ----------------------------------------------------------------------------
195
196 IMPLEMENT_CLASS(wxEditableListBox, wxPanel)
197
198 // NB: generate the IDs at runtime to avoid conflict with XRCID values,
199 // they could cause XRCCTRL() failures in XRC-based dialogs
200 const int wxID_ELB_DELETE = wxNewId();
201 const int wxID_ELB_EDIT = wxNewId();
202 const int wxID_ELB_NEW = wxNewId();
203 const int wxID_ELB_UP = wxNewId();
204 const int wxID_ELB_DOWN = wxNewId();
205 const int wxID_ELB_LISTCTRL = wxNewId();
206
207 BEGIN_EVENT_TABLE(wxEditableListBox, wxPanel)
208 EVT_LIST_ITEM_SELECTED(wxID_ELB_LISTCTRL, wxEditableListBox::OnItemSelected)
209 EVT_LIST_END_LABEL_EDIT(wxID_ELB_LISTCTRL, wxEditableListBox::OnEndLabelEdit)
210 EVT_BUTTON(wxID_ELB_NEW, wxEditableListBox::OnNewItem)
211 EVT_BUTTON(wxID_ELB_UP, wxEditableListBox::OnUpItem)
212 EVT_BUTTON(wxID_ELB_DOWN, wxEditableListBox::OnDownItem)
213 EVT_BUTTON(wxID_ELB_EDIT, wxEditableListBox::OnEditItem)
214 EVT_BUTTON(wxID_ELB_DELETE, wxEditableListBox::OnDelItem)
215 END_EVENT_TABLE()
216
217 bool wxEditableListBox::Create(wxWindow *parent, wxWindowID id,
218 const wxString& label,
219 const wxPoint& pos, const wxSize& size,
220 long style,
221 const wxString& name)
222 {
223 if (!wxPanel::Create(parent, id, pos, size, wxTAB_TRAVERSAL, name))
224 return false;
225
226 m_style = style;
227
228 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
229
230 wxPanel *subp = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
231 wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
232 wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL);
233 subsizer->Add(new wxStaticText(subp, wxID_ANY, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4);
234
235 #ifdef __WXMSW__
236 #define BTN_BORDER 4
237 // FIXME - why is this needed? There's some reason why sunken border is
238 // ignored by sizers in wxMSW but not in wxGTK that I can't
239 // figure out...
240 #else
241 #define BTN_BORDER 0
242 #endif
243
244 if ( m_style & wxEL_ALLOW_EDIT )
245 {
246 m_bEdit = new wxBitmapButton(subp, wxID_ELB_EDIT, wxBitmap(eledit_xpm));
247 subsizer->Add(m_bEdit, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
248 }
249
250 if ( m_style & wxEL_ALLOW_NEW )
251 {
252 m_bNew = new wxBitmapButton(subp, wxID_ELB_NEW, wxBitmap(elnew_xpm));
253 subsizer->Add(m_bNew, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
254 }
255
256 if ( m_style & wxEL_ALLOW_DELETE )
257 {
258 m_bDel = new wxBitmapButton(subp, wxID_ELB_DELETE, wxBitmap(eldel_xpm));
259 subsizer->Add(m_bDel, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
260 }
261
262 if (!(m_style & wxEL_NO_REORDER))
263 {
264 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
265 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
266
267 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
268 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
269 }
270
271 #if wxUSE_TOOLTIPS
272 if ( m_bEdit ) m_bEdit->SetToolTip(_("Edit item"));
273 if ( m_bNew ) m_bNew->SetToolTip(_("New item"));
274 if ( m_bDel ) m_bDel->SetToolTip(_("Delete item"));
275 if ( m_bUp ) m_bUp->SetToolTip(_("Move up"));
276 if ( m_bDown ) m_bDown->SetToolTip(_("Move down"));
277 #endif
278
279 subp->SetSizer(subsizer);
280 subsizer->Fit(subp);
281
282 sizer->Add(subp, 0, wxEXPAND);
283
284 long st = wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxSUNKEN_BORDER;
285 if ( style & wxEL_ALLOW_EDIT )
286 st |= wxLC_EDIT_LABELS;
287 m_listCtrl = new CleverListCtrl(this, wxID_ELB_LISTCTRL,
288 wxDefaultPosition, wxDefaultSize, st);
289 wxArrayString empty_ar;
290 SetStrings(empty_ar);
291
292 sizer->Add(m_listCtrl, 1, wxEXPAND);
293
294 SetSizer(sizer);
295 Layout();
296 }
297
298 void wxEditableListBox::SetStrings(const wxArrayString& strings)
299 {
300 m_listCtrl->DeleteAllItems();
301 size_t i;
302
303 for (i = 0; i < strings.GetCount(); i++)
304 m_listCtrl->InsertItem(i, strings[i]);
305
306 m_listCtrl->InsertItem(strings.GetCount(), wxEmptyString);
307 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
308 }
309
310 void wxEditableListBox::GetStrings(wxArrayString& strings) const
311 {
312 strings.Clear();
313
314 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
315 strings.Add(m_listCtrl->GetItemText(i));
316 }
317
318 void wxEditableListBox::OnItemSelected(wxListEvent& event)
319 {
320 m_selection = event.GetIndex();
321 if (!(m_style & wxEL_NO_REORDER))
322 {
323 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
324 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
325 }
326
327 if (m_style & wxEL_ALLOW_EDIT)
328 m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1);
329 if (m_style & wxEL_ALLOW_DELETE)
330 m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1);
331 }
332
333 void wxEditableListBox::OnNewItem(wxCommandEvent& WXUNUSED(event))
334 {
335 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
336 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
337 m_listCtrl->EditLabel(m_selection);
338 }
339
340 void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
341 {
342 if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 &&
343 !event.GetText().empty() )
344 {
345 // The user edited last (empty) line, i.e. added new entry. We have to
346 // add new empty line here so that adding one more line is still
347 // possible:
348 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxEmptyString);
349
350 // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item,
351 // so that the buttons are enabled/disabled properly
352 wxListEvent selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED, m_listCtrl->GetId());
353 selectionEvent.m_itemIndex = event.GetIndex();
354 m_listCtrl->GetEventHandler()->ProcessEvent(selectionEvent);
355 }
356 }
357
358 void wxEditableListBox::OnDelItem(wxCommandEvent& WXUNUSED(event))
359 {
360 m_listCtrl->DeleteItem(m_selection);
361 m_listCtrl->SetItemState(m_selection,
362 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
363 }
364
365 void wxEditableListBox::OnEditItem(wxCommandEvent& WXUNUSED(event))
366 {
367 m_listCtrl->EditLabel(m_selection);
368 }
369
370 void wxEditableListBox::OnUpItem(wxCommandEvent& WXUNUSED(event))
371 {
372 wxString t1, t2;
373
374 t1 = m_listCtrl->GetItemText(m_selection - 1);
375 t2 = m_listCtrl->GetItemText(m_selection);
376 m_listCtrl->SetItemText(m_selection - 1, t2);
377 m_listCtrl->SetItemText(m_selection, t1);
378 m_listCtrl->SetItemState(m_selection - 1,
379 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
380 }
381
382 void wxEditableListBox::OnDownItem(wxCommandEvent& WXUNUSED(event))
383 {
384 wxString t1, t2;
385
386 t1 = m_listCtrl->GetItemText(m_selection + 1);
387 t2 = m_listCtrl->GetItemText(m_selection);
388 m_listCtrl->SetItemText(m_selection + 1, t2);
389 m_listCtrl->SetItemText(m_selection, t1);
390 m_listCtrl->SetItemState(m_selection + 1,
391 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
392 }
393
394 #endif // wxUSE_EDITABLELISTBOX