1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: ListBox with editable items
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_EDITABLELISTBOX
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWidgets headers)
25 #include "wx/editlbox.h"
27 #include "wx/listctrl.h"
29 // ============================================================================
31 // ============================================================================
33 const char wxEditableListBoxNameStr
[] = "editableListBox";
35 static const char* const eledit_xpm
[] = {
57 static const char* const elnew_xpm
[] = {
81 static const char* const eldel_xpm
[] = {
103 static const char* const eldown_xpm
[] = {
124 static const char* const elup_xpm
[] = {
145 // list control with auto-resizable column:
146 class CleverListCtrl
: public wxListCtrl
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
)
163 InsertColumn(0, wxT("item"));
171 w
-= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
) + 6;
173 w
-= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
176 SetColumnWidth(0, w
);
180 DECLARE_EVENT_TABLE()
181 void OnSize(wxSizeEvent
& event
)
188 BEGIN_EVENT_TABLE(CleverListCtrl
, wxListCtrl
)
189 EVT_SIZE(CleverListCtrl::OnSize
)
193 // ----------------------------------------------------------------------------
195 // ----------------------------------------------------------------------------
197 IMPLEMENT_CLASS(wxEditableListBox
, wxPanel
)
199 // NB: generate the IDs at runtime to avoid conflict with XRCID values,
200 // they could cause XRCCTRL() failures in XRC-based dialogs
201 const wxWindowIDRef wxID_ELB_DELETE
= wxWindow::NewControlId();
202 const wxWindowIDRef wxID_ELB_EDIT
= wxWindow::NewControlId();
203 const wxWindowIDRef wxID_ELB_NEW
= wxWindow::NewControlId();
204 const wxWindowIDRef wxID_ELB_UP
= wxWindow::NewControlId();
205 const wxWindowIDRef wxID_ELB_DOWN
= wxWindow::NewControlId();
206 const wxWindowIDRef wxID_ELB_LISTCTRL
= wxWindow::NewControlId();
208 BEGIN_EVENT_TABLE(wxEditableListBox
, wxPanel
)
209 EVT_LIST_ITEM_SELECTED(wxID_ELB_LISTCTRL
, wxEditableListBox::OnItemSelected
)
210 EVT_LIST_END_LABEL_EDIT(wxID_ELB_LISTCTRL
, wxEditableListBox::OnEndLabelEdit
)
211 EVT_BUTTON(wxID_ELB_NEW
, wxEditableListBox::OnNewItem
)
212 EVT_BUTTON(wxID_ELB_UP
, wxEditableListBox::OnUpItem
)
213 EVT_BUTTON(wxID_ELB_DOWN
, wxEditableListBox::OnDownItem
)
214 EVT_BUTTON(wxID_ELB_EDIT
, wxEditableListBox::OnEditItem
)
215 EVT_BUTTON(wxID_ELB_DELETE
, wxEditableListBox::OnDelItem
)
218 bool wxEditableListBox::Create(wxWindow
*parent
, wxWindowID id
,
219 const wxString
& label
,
220 const wxPoint
& pos
, const wxSize
& size
,
222 const wxString
& name
)
224 if (!wxPanel::Create(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
))
229 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
231 wxPanel
*subp
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
232 wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
233 wxSizer
*subsizer
= new wxBoxSizer(wxHORIZONTAL
);
234 subsizer
->Add(new wxStaticText(subp
, wxID_ANY
, label
), 1, wxALIGN_CENTRE_VERTICAL
| wxLEFT
, 4);
238 // FIXME - why is this needed? There's some reason why sunken border is
239 // ignored by sizers in wxMSW but not in wxGTK that I can't
245 if ( m_style
& wxEL_ALLOW_EDIT
)
247 m_bEdit
= new wxBitmapButton(subp
, wxID_ELB_EDIT
, wxBitmap(eledit_xpm
));
248 subsizer
->Add(m_bEdit
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
251 if ( m_style
& wxEL_ALLOW_NEW
)
253 m_bNew
= new wxBitmapButton(subp
, wxID_ELB_NEW
, wxBitmap(elnew_xpm
));
254 subsizer
->Add(m_bNew
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
257 if ( m_style
& wxEL_ALLOW_DELETE
)
259 m_bDel
= new wxBitmapButton(subp
, wxID_ELB_DELETE
, wxBitmap(eldel_xpm
));
260 subsizer
->Add(m_bDel
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
263 if (!(m_style
& wxEL_NO_REORDER
))
265 m_bUp
= new wxBitmapButton(subp
, wxID_ELB_UP
, wxBitmap(elup_xpm
));
266 subsizer
->Add(m_bUp
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
268 m_bDown
= new wxBitmapButton(subp
, wxID_ELB_DOWN
, wxBitmap(eldown_xpm
));
269 subsizer
->Add(m_bDown
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
273 if ( m_bEdit
) m_bEdit
->SetToolTip(_("Edit item"));
274 if ( m_bNew
) m_bNew
->SetToolTip(_("New item"));
275 if ( m_bDel
) m_bDel
->SetToolTip(_("Delete item"));
276 if ( m_bUp
) m_bUp
->SetToolTip(_("Move up"));
277 if ( m_bDown
) m_bDown
->SetToolTip(_("Move down"));
280 subp
->SetSizer(subsizer
);
283 sizer
->Add(subp
, 0, wxEXPAND
);
285 long st
= wxLC_REPORT
| wxLC_NO_HEADER
| wxLC_SINGLE_SEL
| wxSUNKEN_BORDER
;
286 if ( style
& wxEL_ALLOW_EDIT
)
287 st
|= wxLC_EDIT_LABELS
;
288 m_listCtrl
= new CleverListCtrl(this, wxID_ELB_LISTCTRL
,
289 wxDefaultPosition
, wxDefaultSize
, st
);
290 wxArrayString empty_ar
;
291 SetStrings(empty_ar
);
293 sizer
->Add(m_listCtrl
, 1, wxEXPAND
);
301 void wxEditableListBox::SetStrings(const wxArrayString
& strings
)
303 m_listCtrl
->DeleteAllItems();
306 for (i
= 0; i
< strings
.GetCount(); i
++)
307 m_listCtrl
->InsertItem(i
, strings
[i
]);
309 m_listCtrl
->InsertItem(strings
.GetCount(), wxEmptyString
);
310 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
313 void wxEditableListBox::GetStrings(wxArrayString
& strings
) const
317 for (int i
= 0; i
< m_listCtrl
->GetItemCount()-1; i
++)
318 strings
.Add(m_listCtrl
->GetItemText(i
));
321 void wxEditableListBox::OnItemSelected(wxListEvent
& event
)
323 m_selection
= event
.GetIndex();
324 if (!(m_style
& wxEL_NO_REORDER
))
326 m_bUp
->Enable(m_selection
!= 0 && m_selection
< m_listCtrl
->GetItemCount()-1);
327 m_bDown
->Enable(m_selection
< m_listCtrl
->GetItemCount()-2);
330 if (m_style
& wxEL_ALLOW_EDIT
)
331 m_bEdit
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
332 if (m_style
& wxEL_ALLOW_DELETE
)
333 m_bDel
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
336 void wxEditableListBox::OnNewItem(wxCommandEvent
& WXUNUSED(event
))
338 m_listCtrl
->SetItemState(m_listCtrl
->GetItemCount()-1,
339 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
340 m_listCtrl
->EditLabel(m_selection
);
343 void wxEditableListBox::OnEndLabelEdit(wxListEvent
& event
)
345 if ( event
.GetIndex() == m_listCtrl
->GetItemCount()-1 &&
346 !event
.GetText().empty() )
348 // The user edited last (empty) line, i.e. added new entry. We have to
349 // add new empty line here so that adding one more line is still
351 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxEmptyString
);
353 // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item,
354 // so that the buttons are enabled/disabled properly
355 wxListEvent
selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED
, m_listCtrl
->GetId());
356 selectionEvent
.m_itemIndex
= event
.GetIndex();
357 m_listCtrl
->GetEventHandler()->ProcessEvent(selectionEvent
);
361 void wxEditableListBox::OnDelItem(wxCommandEvent
& WXUNUSED(event
))
363 m_listCtrl
->DeleteItem(m_selection
);
364 m_listCtrl
->SetItemState(m_selection
,
365 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
368 void wxEditableListBox::OnEditItem(wxCommandEvent
& WXUNUSED(event
))
370 m_listCtrl
->EditLabel(m_selection
);
373 void wxEditableListBox::OnUpItem(wxCommandEvent
& WXUNUSED(event
))
377 t1
= m_listCtrl
->GetItemText(m_selection
- 1);
378 t2
= m_listCtrl
->GetItemText(m_selection
);
379 m_listCtrl
->SetItemText(m_selection
- 1, t2
);
380 m_listCtrl
->SetItemText(m_selection
, t1
);
381 m_listCtrl
->SetItemState(m_selection
- 1,
382 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
385 void wxEditableListBox::OnDownItem(wxCommandEvent
& WXUNUSED(event
))
389 t1
= m_listCtrl
->GetItemText(m_selection
+ 1);
390 t2
= m_listCtrl
->GetItemText(m_selection
);
391 m_listCtrl
->SetItemText(m_selection
+ 1, t2
);
392 m_listCtrl
->SetItemText(m_selection
, t1
);
393 m_listCtrl
->SetItemState(m_selection
+ 1,
394 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
397 #endif // wxUSE_EDITABLELISTBOX