]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/gizmos/editlbox.cpp
wxX11:
[wxWidgets.git] / contrib / src / gizmos / editlbox.cpp
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
22 // need because it includes almost all "standard" wxWindows headers)
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,
39 wxWindowID id = -1,
40 const wxPoint &pos = wxDefaultPosition,
41 const wxSize &size = wxDefaultSize,
42 long style = wxLC_ICON,
43 const wxValidator& validator = wxDefaultValidator,
44 const wxString &name = "listctrl")
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;
59 w -= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 6;
60 SetColumnWidth(0, w);
61 }
62
63 private:
64 DECLARE_EVENT_TABLE()
65 void OnSize(wxSizeEvent& event)
66 {
67 SizeColumns();
68 event.Skip();
69 }
70 };
71
72 BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
73 EVT_SIZE(CleverListCtrl::OnSize)
74 END_EVENT_TABLE()
75
76
77 #include "eldel.xpm"
78 #include "eldown.xpm"
79 #include "eledit.xpm"
80 #include "elnew.xpm"
81 #include "elup.xpm"
82
83 IMPLEMENT_CLASS(wxEditableListBox, wxPanel)
84
85 enum
86 {
87 // ID value doesn't matter, it won't propagate out of wxEditableListBox
88 // instance
89 wxID_ELB_DELETE = wxID_HIGHEST + 1,
90 wxID_ELB_NEW,
91 wxID_ELB_UP,
92 wxID_ELB_DOWN,
93 wxID_ELB_EDIT,
94 wxID_ELD_LISTCTRL
95 };
96
97 BEGIN_EVENT_TABLE(wxEditableListBox, wxPanel)
98 EVT_LIST_ITEM_SELECTED(wxID_ELD_LISTCTRL, wxEditableListBox::OnItemSelected)
99 EVT_LIST_END_LABEL_EDIT(wxID_ELD_LISTCTRL, wxEditableListBox::OnEndLabelEdit)
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,
109 const wxPoint& pos, const wxSize& size,
110 const wxString& name)
111 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name), m_edittingNew(FALSE)
112 {
113 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
114
115 wxPanel *subp = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize,
116 wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
117 wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL);
118 subsizer->Add(new wxStaticText(subp, -1, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4);
119 m_bEdit = new wxBitmapButton(subp, wxID_ELB_EDIT, wxBitmap(eledit_xpm));
120 m_bNew = new wxBitmapButton(subp, wxID_ELB_NEW, wxBitmap(elnew_xpm));
121 m_bDel = new wxBitmapButton(subp, wxID_ELB_DELETE, wxBitmap(eldel_xpm));
122 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
123 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
124
125 #if wxUSE_TOOLTIPS
126 m_bEdit->SetToolTip(wxT("Edit item"));
127 m_bNew->SetToolTip(wxT("New item"));
128 m_bDel->SetToolTip(wxT("Delete item"));
129 m_bUp->SetToolTip(wxT("Move up"));
130 m_bDown->SetToolTip(wxT("Move down"));
131 #endif
132
133 subsizer->Add(m_bEdit, 0, wxALIGN_CENTRE_VERTICAL);
134 subsizer->Add(m_bNew, 0, wxALIGN_CENTRE_VERTICAL);
135 subsizer->Add(m_bDel, 0, wxALIGN_CENTRE_VERTICAL);
136 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL);
137 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL);
138
139 subp->SetAutoLayout(TRUE);
140 subp->SetSizer(subsizer);
141 subsizer->Fit(subp);
142
143 sizer->Add(subp, 0, wxEXPAND);
144 m_listCtrl = new CleverListCtrl(this, wxID_ELD_LISTCTRL,
145 wxDefaultPosition, wxDefaultSize,
146 wxLC_REPORT | wxLC_NO_HEADER |
147 wxLC_SINGLE_SEL | wxSUNKEN_BORDER |
148 wxLC_EDIT_LABELS);
149 wxArrayString empty_ar;
150 SetStrings(empty_ar);
151
152 sizer->Add(m_listCtrl, 1, wxEXPAND);
153
154 SetAutoLayout(TRUE);
155 SetSizer(sizer);
156 Layout();
157 }
158
159 void wxEditableListBox::SetStrings(const wxArrayString& strings)
160 {
161 m_listCtrl->DeleteAllItems();
162 size_t i;
163
164 for (i = 0; i < strings.GetCount(); i++)
165 m_listCtrl->InsertItem(i, strings[i]);
166
167 m_listCtrl->InsertItem(strings.GetCount(), _T(""));
168 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
169 }
170
171 void wxEditableListBox::GetStrings(wxArrayString& strings)
172 {
173 strings.Clear();
174
175 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
176 strings.Add(m_listCtrl->GetItemText(i));
177 }
178
179 void wxEditableListBox::OnItemSelected(wxListEvent& event)
180 {
181 m_selection = event.GetIndex();
182 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
183 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
184 m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1);
185 m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1);
186 }
187
188 void wxEditableListBox::OnNewItem(wxCommandEvent& event)
189 {
190 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
191 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
192 m_edittingNew = TRUE;
193 m_listCtrl->EditLabel(m_selection);
194 }
195
196 void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
197 {
198 if (m_edittingNew)
199 {
200 m_edittingNew = FALSE;
201 if (!event.GetText().IsEmpty())
202 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T(""));
203 }
204 }
205
206 void wxEditableListBox::OnDelItem(wxCommandEvent& event)
207 {
208 m_listCtrl->DeleteItem(m_selection);
209 m_listCtrl->SetItemState(m_selection,
210 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
211 }
212
213 void wxEditableListBox::OnEditItem(wxCommandEvent& event)
214 {
215 m_listCtrl->EditLabel(m_selection);
216 }
217
218 void wxEditableListBox::OnUpItem(wxCommandEvent& event)
219 {
220 wxString t1, t2;
221
222 t1 = m_listCtrl->GetItemText(m_selection - 1);
223 t2 = m_listCtrl->GetItemText(m_selection);
224 m_listCtrl->SetItemText(m_selection - 1, t2);
225 m_listCtrl->SetItemText(m_selection, t1);
226 m_listCtrl->SetItemState(m_selection - 1,
227 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
228 }
229
230 void wxEditableListBox::OnDownItem(wxCommandEvent& event)
231 {
232 wxString t1, t2;
233
234 t1 = m_listCtrl->GetItemText(m_selection + 1);
235 t2 = m_listCtrl->GetItemText(m_selection);
236 m_listCtrl->SetItemText(m_selection + 1, t2);
237 m_listCtrl->SetItemText(m_selection, t1);
238 m_listCtrl->SetItemState(m_selection + 1,
239 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
240 }