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/gizmos/editlbox.h"
25 #include "wx/listctrl.h"
30 // list control with auto-resizable column:
31 class CleverListCtrl
: public wxListCtrl
34 CleverListCtrl(wxWindow
*parent
,
35 wxWindowID id
= wxID_ANY
,
36 const wxPoint
&pos
= wxDefaultPosition
,
37 const wxSize
&size
= wxDefaultSize
,
38 long style
= wxLC_ICON
,
39 const wxValidator
& validator
= wxDefaultValidator
,
40 const wxString
&name
= wxListCtrlNameStr
)
41 : wxListCtrl(parent
, id
, pos
, size
, style
, validator
, name
)
48 InsertColumn(0, _T("item"));
56 w
-= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
) + 6;
58 w
-= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
65 void OnSize(wxSizeEvent
& event
)
72 BEGIN_EVENT_TABLE(CleverListCtrl
, wxListCtrl
)
73 EVT_SIZE(CleverListCtrl::OnSize
)
82 IMPLEMENT_CLASS(wxEditableListBox
, wxPanel
)
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();
93 BEGIN_EVENT_TABLE(wxEditableListBox
, wxPanel
)
94 EVT_LIST_ITEM_SELECTED(wxID_ELB_LISTCTRL
, wxEditableListBox::OnItemSelected
)
95 EVT_LIST_END_LABEL_EDIT(wxID_ELB_LISTCTRL
, wxEditableListBox::OnEndLabelEdit
)
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
)
103 wxEditableListBox::wxEditableListBox(wxWindow
*parent
, wxWindowID id
,
104 const wxString
& label
,
105 const wxPoint
& pos
, const wxSize
& size
,
107 const wxString
& name
)
108 : wxPanel(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
)
111 m_bEdit
= m_bNew
= m_bDel
= m_bUp
= m_bDown
= NULL
;
113 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
115 wxPanel
*subp
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
116 wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
117 wxSizer
*subsizer
= new wxBoxSizer(wxHORIZONTAL
);
118 subsizer
->Add(new wxStaticText(subp
, wxID_ANY
, label
), 1, wxALIGN_CENTRE_VERTICAL
| wxLEFT
, 4);
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
129 if ( m_style
& wxEL_ALLOW_EDIT
)
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
);
135 if ( m_style
& wxEL_ALLOW_NEW
)
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
);
141 if ( m_style
& wxEL_ALLOW_DELETE
)
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
);
147 m_bUp
= new wxBitmapButton(subp
, wxID_ELB_UP
, wxBitmap(elup_xpm
));
148 subsizer
->Add(m_bUp
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
150 m_bDown
= new wxBitmapButton(subp
, wxID_ELB_DOWN
, wxBitmap(eldown_xpm
));
151 subsizer
->Add(m_bDown
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
154 if ( m_bEdit
) m_bEdit
->SetToolTip(_("Edit item"));
155 if ( m_bNew
) m_bNew
->SetToolTip(_("New item"));
156 if ( m_bDel
) m_bDel
->SetToolTip(_("Delete item"));
157 m_bUp
->SetToolTip(_("Move up"));
158 m_bDown
->SetToolTip(_("Move down"));
161 subp
->SetSizer(subsizer
);
164 sizer
->Add(subp
, 0, wxEXPAND
);
166 long st
= wxLC_REPORT
| wxLC_NO_HEADER
| wxLC_SINGLE_SEL
| wxSUNKEN_BORDER
;
167 if ( style
& wxEL_ALLOW_EDIT
)
168 st
|= wxLC_EDIT_LABELS
;
169 m_listCtrl
= new CleverListCtrl(this, wxID_ELB_LISTCTRL
,
170 wxDefaultPosition
, wxDefaultSize
, st
);
171 wxArrayString empty_ar
;
172 SetStrings(empty_ar
);
174 sizer
->Add(m_listCtrl
, 1, wxEXPAND
);
180 void wxEditableListBox::SetStrings(const wxArrayString
& strings
)
182 m_listCtrl
->DeleteAllItems();
185 for (i
= 0; i
< strings
.GetCount(); i
++)
186 m_listCtrl
->InsertItem(i
, strings
[i
]);
188 m_listCtrl
->InsertItem(strings
.GetCount(), wxEmptyString
);
189 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
192 void wxEditableListBox::GetStrings(wxArrayString
& strings
)
196 for (int i
= 0; i
< m_listCtrl
->GetItemCount()-1; i
++)
197 strings
.Add(m_listCtrl
->GetItemText(i
));
200 void wxEditableListBox::OnItemSelected(wxListEvent
& event
)
202 m_selection
= event
.GetIndex();
203 m_bUp
->Enable(m_selection
!= 0 && m_selection
< m_listCtrl
->GetItemCount()-1);
204 m_bDown
->Enable(m_selection
< m_listCtrl
->GetItemCount()-2);
205 if (m_style
& wxEL_ALLOW_EDIT
)
206 m_bEdit
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
207 if (m_style
& wxEL_ALLOW_DELETE
)
208 m_bDel
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
211 void wxEditableListBox::OnNewItem(wxCommandEvent
& WXUNUSED(event
))
213 m_listCtrl
->SetItemState(m_listCtrl
->GetItemCount()-1,
214 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
215 m_listCtrl
->EditLabel(m_selection
);
218 void wxEditableListBox::OnEndLabelEdit(wxListEvent
& event
)
220 if ( event
.GetIndex() == m_listCtrl
->GetItemCount()-1 &&
221 !event
.GetText().empty() )
223 // The user edited last (empty) line, i.e. added new entry. We have to
224 // add new empty line here so that adding one more line is still
226 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxEmptyString
);
228 // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item,
229 // so that the buttons are enabled/disabled properly
230 wxListEvent
selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED
, m_listCtrl
->GetId());
231 selectionEvent
.m_itemIndex
= event
.GetIndex();
232 m_listCtrl
->GetEventHandler()->ProcessEvent(selectionEvent
);
236 void wxEditableListBox::OnDelItem(wxCommandEvent
& WXUNUSED(event
))
238 m_listCtrl
->DeleteItem(m_selection
);
239 m_listCtrl
->SetItemState(m_selection
,
240 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
243 void wxEditableListBox::OnEditItem(wxCommandEvent
& WXUNUSED(event
))
245 m_listCtrl
->EditLabel(m_selection
);
248 void wxEditableListBox::OnUpItem(wxCommandEvent
& WXUNUSED(event
))
252 t1
= m_listCtrl
->GetItemText(m_selection
- 1);
253 t2
= m_listCtrl
->GetItemText(m_selection
);
254 m_listCtrl
->SetItemText(m_selection
- 1, t2
);
255 m_listCtrl
->SetItemText(m_selection
, t1
);
256 m_listCtrl
->SetItemState(m_selection
- 1,
257 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
260 void wxEditableListBox::OnDownItem(wxCommandEvent
& WXUNUSED(event
))
264 t1
= m_listCtrl
->GetItemText(m_selection
+ 1);
265 t2
= m_listCtrl
->GetItemText(m_selection
);
266 m_listCtrl
->SetItemText(m_selection
+ 1, t2
);
267 m_listCtrl
->SetItemText(m_selection
, t1
);
268 m_listCtrl
->SetItemState(m_selection
+ 1,
269 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);