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