]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/gizmos/editlbox.cpp
make pen/brush parameters const
[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 // 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 // for all others, include the necessary headers (this file is usually all you
18 // need because it includes almost all "standard" wxWidgets headers)
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/gizmos/editlbox.h"
24 #include "wx/sizer.h"
25 #include "wx/listctrl.h"
26
27
28
29
30 // list control with auto-resizable column:
31 class CleverListCtrl : public wxListCtrl
32 {
33 public:
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)
42 {
43 CreateColumns();
44 }
45
46 void CreateColumns()
47 {
48 InsertColumn(0, _T("item"));
49 SizeColumns();
50 }
51
52 void SizeColumns()
53 {
54 int w = GetSize().x;
55 #ifdef __WXMSW__
56 w -= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 6;
57 #else
58 w -= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
59 #endif
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 #include "eldel.xpm"
77 #include "eldown.xpm"
78 #include "eledit.xpm"
79 #include "elnew.xpm"
80 #include "elup.xpm"
81
82 IMPLEMENT_CLASS(wxEditableListBox, wxPanel)
83
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();
92
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)
101 END_EVENT_TABLE()
102
103 wxEditableListBox::wxEditableListBox(wxWindow *parent, wxWindowID id,
104 const wxString& label,
105 const wxPoint& pos, const wxSize& size,
106 long style,
107 const wxString& name)
108 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name)
109 {
110 m_style = style;
111 m_bEdit = m_bNew = m_bDel = m_bUp = m_bDown = NULL;
112
113 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
114
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);
119
120 #ifdef __WXMSW__
121 #define BTN_BORDER 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
124 // figure out...
125 #else
126 #define BTN_BORDER 0
127 #endif
128
129 if ( m_style & wxEL_ALLOW_EDIT )
130 {
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);
133 }
134
135 if ( m_style & wxEL_ALLOW_NEW )
136 {
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);
139 }
140
141 if ( m_style & wxEL_ALLOW_DELETE )
142 {
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);
145 }
146
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);
149
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);
152
153 #if wxUSE_TOOLTIPS
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"));
159 #endif
160
161 subp->SetSizer(subsizer);
162 subsizer->Fit(subp);
163
164 sizer->Add(subp, 0, wxEXPAND);
165
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);
173
174 sizer->Add(m_listCtrl, 1, wxEXPAND);
175
176 SetSizer(sizer);
177 Layout();
178 }
179
180 void wxEditableListBox::SetStrings(const wxArrayString& strings)
181 {
182 m_listCtrl->DeleteAllItems();
183 size_t i;
184
185 for (i = 0; i < strings.GetCount(); i++)
186 m_listCtrl->InsertItem(i, strings[i]);
187
188 m_listCtrl->InsertItem(strings.GetCount(), wxEmptyString);
189 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
190 }
191
192 void wxEditableListBox::GetStrings(wxArrayString& strings)
193 {
194 strings.Clear();
195
196 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
197 strings.Add(m_listCtrl->GetItemText(i));
198 }
199
200 void wxEditableListBox::OnItemSelected(wxListEvent& event)
201 {
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);
209 }
210
211 void wxEditableListBox::OnNewItem(wxCommandEvent& WXUNUSED(event))
212 {
213 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
214 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
215 m_listCtrl->EditLabel(m_selection);
216 }
217
218 void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
219 {
220 if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 &&
221 !event.GetText().empty() )
222 {
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
225 // possible:
226 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxEmptyString);
227 }
228 }
229
230 void wxEditableListBox::OnDelItem(wxCommandEvent& WXUNUSED(event))
231 {
232 m_listCtrl->DeleteItem(m_selection);
233 m_listCtrl->SetItemState(m_selection,
234 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
235 }
236
237 void wxEditableListBox::OnEditItem(wxCommandEvent& WXUNUSED(event))
238 {
239 m_listCtrl->EditLabel(m_selection);
240 }
241
242 void wxEditableListBox::OnUpItem(wxCommandEvent& WXUNUSED(event))
243 {
244 wxString t1, t2;
245
246 t1 = m_listCtrl->GetItemText(m_selection - 1);
247 t2 = m_listCtrl->GetItemText(m_selection);
248 m_listCtrl->SetItemText(m_selection - 1, t2);
249 m_listCtrl->SetItemText(m_selection, t1);
250 m_listCtrl->SetItemState(m_selection - 1,
251 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
252 }
253
254 void wxEditableListBox::OnDownItem(wxCommandEvent& WXUNUSED(event))
255 {
256 wxString t1, t2;
257
258 t1 = m_listCtrl->GetItemText(m_selection + 1);
259 t2 = m_listCtrl->GetItemText(m_selection);
260 m_listCtrl->SetItemText(m_selection + 1, t2);
261 m_listCtrl->SetItemText(m_selection, t1);
262 m_listCtrl->SetItemState(m_selection + 1,
263 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
264 }