]> git.saurik.com Git - wxWidgets.git/blob - src/generic/editlbox.cpp
missing commit
[wxWidgets.git] / src / generic / editlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/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 char wxEditableListBoxNameStr[] = "editableListBox";
34
35 static const char* const 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 const char* const 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 const char* const 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 const char* const 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 const char* const 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, wxT("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 if (w < 0) w = 0;
176 SetColumnWidth(0, w);
177 }
178
179 private:
180 DECLARE_EVENT_TABLE()
181 void OnSize(wxSizeEvent& event)
182 {
183 SizeColumns();
184 event.Skip();
185 }
186 };
187
188 BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
189 EVT_SIZE(CleverListCtrl::OnSize)
190 END_EVENT_TABLE()
191
192
193 // ----------------------------------------------------------------------------
194 // wxEditableListBox
195 // ----------------------------------------------------------------------------
196
197 IMPLEMENT_CLASS(wxEditableListBox, wxPanel)
198
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();
207
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)
216 END_EVENT_TABLE()
217
218 bool wxEditableListBox::Create(wxWindow *parent, wxWindowID id,
219 const wxString& label,
220 const wxPoint& pos, const wxSize& size,
221 long style,
222 const wxString& name)
223 {
224 if (!wxPanel::Create(parent, id, pos, size, wxTAB_TRAVERSAL, name))
225 return false;
226
227 m_style = style;
228
229 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
230
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);
235
236 #ifdef __WXMSW__
237 #define BTN_BORDER 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
240 // figure out...
241 #else
242 #define BTN_BORDER 0
243 #endif
244
245 if ( m_style & wxEL_ALLOW_EDIT )
246 {
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);
249 }
250
251 if ( m_style & wxEL_ALLOW_NEW )
252 {
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);
255 }
256
257 if ( m_style & wxEL_ALLOW_DELETE )
258 {
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);
261 }
262
263 if (!(m_style & wxEL_NO_REORDER))
264 {
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);
267
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);
270 }
271
272 #if wxUSE_TOOLTIPS
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"));
278 #endif
279
280 subp->SetSizer(subsizer);
281 subsizer->Fit(subp);
282
283 sizer->Add(subp, 0, wxEXPAND);
284
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);
292
293 sizer->Add(m_listCtrl, 1, wxEXPAND);
294
295 SetSizer(sizer);
296 Layout();
297
298 return true;
299 }
300
301 void wxEditableListBox::SetStrings(const wxArrayString& strings)
302 {
303 m_listCtrl->DeleteAllItems();
304 size_t i;
305
306 for (i = 0; i < strings.GetCount(); i++)
307 m_listCtrl->InsertItem(i, strings[i]);
308
309 m_listCtrl->InsertItem(strings.GetCount(), wxEmptyString);
310 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
311 }
312
313 void wxEditableListBox::GetStrings(wxArrayString& strings) const
314 {
315 strings.Clear();
316
317 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
318 strings.Add(m_listCtrl->GetItemText(i));
319 }
320
321 void wxEditableListBox::OnItemSelected(wxListEvent& event)
322 {
323 m_selection = event.GetIndex();
324 if (!(m_style & wxEL_NO_REORDER))
325 {
326 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
327 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
328 }
329
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);
334 }
335
336 void wxEditableListBox::OnNewItem(wxCommandEvent& WXUNUSED(event))
337 {
338 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
339 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
340 m_listCtrl->EditLabel(m_selection);
341 }
342
343 void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
344 {
345 if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 &&
346 !event.GetText().empty() )
347 {
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
350 // possible:
351 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxEmptyString);
352
353 // Simulate a wxEVT_LIST_ITEM_SELECTED event for the new item,
354 // so that the buttons are enabled/disabled properly
355 wxListEvent selectionEvent(wxEVT_LIST_ITEM_SELECTED, m_listCtrl->GetId());
356 selectionEvent.m_itemIndex = event.GetIndex();
357 m_listCtrl->GetEventHandler()->ProcessEvent(selectionEvent);
358 }
359 }
360
361 void wxEditableListBox::OnDelItem(wxCommandEvent& WXUNUSED(event))
362 {
363 m_listCtrl->DeleteItem(m_selection);
364 m_listCtrl->SetItemState(m_selection,
365 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
366 }
367
368 void wxEditableListBox::OnEditItem(wxCommandEvent& WXUNUSED(event))
369 {
370 m_listCtrl->EditLabel(m_selection);
371 }
372
373 void wxEditableListBox::SwapItems(long i1, long i2)
374 {
375 // swap the text
376 wxString t1 = m_listCtrl->GetItemText(i1);
377 wxString t2 = m_listCtrl->GetItemText(i2);
378 m_listCtrl->SetItemText(i1, t2);
379 m_listCtrl->SetItemText(i2, t1);
380
381 // swap the item data
382 long d1 = m_listCtrl->GetItemData(i1);
383 long d2 = m_listCtrl->GetItemData(i2);
384 m_listCtrl->SetItemData(i1, d2);
385 m_listCtrl->SetItemData(i2, d1);
386 }
387
388
389 void wxEditableListBox::OnUpItem(wxCommandEvent& WXUNUSED(event))
390 {
391 SwapItems(m_selection - 1, m_selection);
392 m_listCtrl->SetItemState(m_selection - 1,
393 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
394 }
395
396 void wxEditableListBox::OnDownItem(wxCommandEvent& WXUNUSED(event))
397 {
398 SwapItems(m_selection + 1, m_selection);
399 m_listCtrl->SetItemState(m_selection + 1,
400 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
401 }
402
403 #endif // wxUSE_EDITABLELISTBOX