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 // for all others, include the necessary headers (this file is usually all you
18 // need because it includes almost all "standard" wxWidgets headers)
23 #include "wx/editlbox.h"
25 #include "wx/listctrl.h"
27 static char * eledit_xpm
[] = {
49 static char * elnew_xpm
[] = {
73 static char * eldel_xpm
[] = {
95 static char * eldown_xpm
[] = {
116 static char * elup_xpm
[] = {
137 // list control with auto-resizable column:
138 class CleverListCtrl
: public wxListCtrl
141 CleverListCtrl(wxWindow
*parent
,
142 wxWindowID id
= wxID_ANY
,
143 const wxPoint
&pos
= wxDefaultPosition
,
144 const wxSize
&size
= wxDefaultSize
,
145 long style
= wxLC_ICON
,
146 const wxValidator
& validator
= wxDefaultValidator
,
147 const wxString
&name
= wxListCtrlNameStr
)
148 : wxListCtrl(parent
, id
, pos
, size
, style
, validator
, name
)
155 InsertColumn(0, _T("item"));
163 w
-= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
) + 6;
165 w
-= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
167 SetColumnWidth(0, w
);
171 DECLARE_EVENT_TABLE()
172 void OnSize(wxSizeEvent
& event
)
179 BEGIN_EVENT_TABLE(CleverListCtrl
, wxListCtrl
)
180 EVT_SIZE(CleverListCtrl::OnSize
)
183 IMPLEMENT_CLASS(wxEditableListBox
, wxPanel
)
185 // NB: generate the IDs at runtime to avoid conflict with XRCID values,
186 // they could cause XRCCTRL() failures in XRC-based dialogs
187 const int wxID_ELB_DELETE
= wxNewId();
188 const int wxID_ELB_EDIT
= wxNewId();
189 const int wxID_ELB_NEW
= wxNewId();
190 const int wxID_ELB_UP
= wxNewId();
191 const int wxID_ELB_DOWN
= wxNewId();
192 const int wxID_ELB_LISTCTRL
= wxNewId();
194 BEGIN_EVENT_TABLE(wxEditableListBox
, wxPanel
)
195 EVT_LIST_ITEM_SELECTED(wxID_ELB_LISTCTRL
, wxEditableListBox::OnItemSelected
)
196 EVT_LIST_END_LABEL_EDIT(wxID_ELB_LISTCTRL
, wxEditableListBox::OnEndLabelEdit
)
197 EVT_BUTTON(wxID_ELB_NEW
, wxEditableListBox::OnNewItem
)
198 EVT_BUTTON(wxID_ELB_UP
, wxEditableListBox::OnUpItem
)
199 EVT_BUTTON(wxID_ELB_DOWN
, wxEditableListBox::OnDownItem
)
200 EVT_BUTTON(wxID_ELB_EDIT
, wxEditableListBox::OnEditItem
)
201 EVT_BUTTON(wxID_ELB_DELETE
, wxEditableListBox::OnDelItem
)
204 wxEditableListBox::wxEditableListBox(wxWindow
*parent
, wxWindowID id
,
205 const wxString
& label
,
206 const wxPoint
& pos
, const wxSize
& size
,
208 const wxString
& name
)
209 : wxPanel(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
)
212 m_bEdit
= m_bNew
= m_bDel
= m_bUp
= m_bDown
= NULL
;
214 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
216 wxPanel
*subp
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
217 wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
218 wxSizer
*subsizer
= new wxBoxSizer(wxHORIZONTAL
);
219 subsizer
->Add(new wxStaticText(subp
, wxID_ANY
, label
), 1, wxALIGN_CENTRE_VERTICAL
| wxLEFT
, 4);
223 // FIXME - why is this needed? There's some reason why sunken border is
224 // ignored by sizers in wxMSW but not in wxGTK that I can't
230 if ( m_style
& wxEL_ALLOW_EDIT
)
232 m_bEdit
= new wxBitmapButton(subp
, wxID_ELB_EDIT
, wxBitmap(eledit_xpm
));
233 subsizer
->Add(m_bEdit
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
236 if ( m_style
& wxEL_ALLOW_NEW
)
238 m_bNew
= new wxBitmapButton(subp
, wxID_ELB_NEW
, wxBitmap(elnew_xpm
));
239 subsizer
->Add(m_bNew
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
242 if ( m_style
& wxEL_ALLOW_DELETE
)
244 m_bDel
= new wxBitmapButton(subp
, wxID_ELB_DELETE
, wxBitmap(eldel_xpm
));
245 subsizer
->Add(m_bDel
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
248 if (!(m_style
& wxEL_NO_REORDER
))
250 m_bUp
= new wxBitmapButton(subp
, wxID_ELB_UP
, wxBitmap(elup_xpm
));
251 subsizer
->Add(m_bUp
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
253 m_bDown
= new wxBitmapButton(subp
, wxID_ELB_DOWN
, wxBitmap(eldown_xpm
));
254 subsizer
->Add(m_bDown
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
258 if ( m_bEdit
) m_bEdit
->SetToolTip(_("Edit item"));
259 if ( m_bNew
) m_bNew
->SetToolTip(_("New item"));
260 if ( m_bDel
) m_bDel
->SetToolTip(_("Delete item"));
261 if ( m_bUp
) m_bUp
->SetToolTip(_("Move up"));
262 if ( m_bDown
) m_bDown
->SetToolTip(_("Move down"));
265 subp
->SetSizer(subsizer
);
268 sizer
->Add(subp
, 0, wxEXPAND
);
270 long st
= wxLC_REPORT
| wxLC_NO_HEADER
| wxLC_SINGLE_SEL
| wxSUNKEN_BORDER
;
271 if ( style
& wxEL_ALLOW_EDIT
)
272 st
|= wxLC_EDIT_LABELS
;
273 m_listCtrl
= new CleverListCtrl(this, wxID_ELB_LISTCTRL
,
274 wxDefaultPosition
, wxDefaultSize
, st
);
275 wxArrayString empty_ar
;
276 SetStrings(empty_ar
);
278 sizer
->Add(m_listCtrl
, 1, wxEXPAND
);
284 void wxEditableListBox::SetStrings(const wxArrayString
& strings
)
286 m_listCtrl
->DeleteAllItems();
289 for (i
= 0; i
< strings
.GetCount(); i
++)
290 m_listCtrl
->InsertItem(i
, strings
[i
]);
292 m_listCtrl
->InsertItem(strings
.GetCount(), wxEmptyString
);
293 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
296 void wxEditableListBox::GetStrings(wxArrayString
& strings
) const
300 for (int i
= 0; i
< m_listCtrl
->GetItemCount()-1; i
++)
301 strings
.Add(m_listCtrl
->GetItemText(i
));
304 void wxEditableListBox::OnItemSelected(wxListEvent
& event
)
306 m_selection
= event
.GetIndex();
307 if (!(m_style
& wxEL_NO_REORDER
))
309 m_bUp
->Enable(m_selection
!= 0 && m_selection
< m_listCtrl
->GetItemCount()-1);
310 m_bDown
->Enable(m_selection
< m_listCtrl
->GetItemCount()-2);
313 if (m_style
& wxEL_ALLOW_EDIT
)
314 m_bEdit
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
315 if (m_style
& wxEL_ALLOW_DELETE
)
316 m_bDel
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
319 void wxEditableListBox::OnNewItem(wxCommandEvent
& WXUNUSED(event
))
321 m_listCtrl
->SetItemState(m_listCtrl
->GetItemCount()-1,
322 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
323 m_listCtrl
->EditLabel(m_selection
);
326 void wxEditableListBox::OnEndLabelEdit(wxListEvent
& event
)
328 if ( event
.GetIndex() == m_listCtrl
->GetItemCount()-1 &&
329 !event
.GetText().empty() )
331 // The user edited last (empty) line, i.e. added new entry. We have to
332 // add new empty line here so that adding one more line is still
334 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxEmptyString
);
336 // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item,
337 // so that the buttons are enabled/disabled properly
338 wxListEvent
selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED
, m_listCtrl
->GetId());
339 selectionEvent
.m_itemIndex
= event
.GetIndex();
340 m_listCtrl
->GetEventHandler()->ProcessEvent(selectionEvent
);
344 void wxEditableListBox::OnDelItem(wxCommandEvent
& WXUNUSED(event
))
346 m_listCtrl
->DeleteItem(m_selection
);
347 m_listCtrl
->SetItemState(m_selection
,
348 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
351 void wxEditableListBox::OnEditItem(wxCommandEvent
& WXUNUSED(event
))
353 m_listCtrl
->EditLabel(m_selection
);
356 void wxEditableListBox::OnUpItem(wxCommandEvent
& WXUNUSED(event
))
360 t1
= m_listCtrl
->GetItemText(m_selection
- 1);
361 t2
= m_listCtrl
->GetItemText(m_selection
);
362 m_listCtrl
->SetItemText(m_selection
- 1, t2
);
363 m_listCtrl
->SetItemText(m_selection
, t1
);
364 m_listCtrl
->SetItemState(m_selection
- 1,
365 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
368 void wxEditableListBox::OnDownItem(wxCommandEvent
& WXUNUSED(event
))
372 t1
= m_listCtrl
->GetItemText(m_selection
+ 1);
373 t2
= m_listCtrl
->GetItemText(m_selection
);
374 m_listCtrl
->SetItemText(m_selection
+ 1, t2
);
375 m_listCtrl
->SetItemText(m_selection
, t1
);
376 m_listCtrl
->SetItemState(m_selection
+ 1,
377 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);