]> git.saurik.com Git - wxWidgets.git/blame_incremental - contrib/src/gizmos/editlbox.cpp
suppress unused parameter warning
[wxWidgets.git] / contrib / src / gizmos / editlbox.cpp
... / ...
CommitLineData
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:
31class CleverListCtrl : public wxListCtrl
32{
33public:
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
63private:
64 DECLARE_EVENT_TABLE()
65 void OnSize(wxSizeEvent& event)
66 {
67 SizeColumns();
68 event.Skip();
69 }
70};
71
72BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
73 EVT_SIZE(CleverListCtrl::OnSize)
74END_EVENT_TABLE()
75
76#include "eldel.xpm"
77#include "eldown.xpm"
78#include "eledit.xpm"
79#include "elnew.xpm"
80#include "elup.xpm"
81
82IMPLEMENT_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
86const int wxID_ELB_DELETE = wxNewId();
87const int wxID_ELB_EDIT = wxNewId();
88const int wxID_ELB_NEW = wxNewId();
89const int wxID_ELB_UP = wxNewId();
90const int wxID_ELB_DOWN = wxNewId();
91const int wxID_ELB_LISTCTRL = wxNewId();
92
93BEGIN_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)
101END_EVENT_TABLE()
102
103wxEditableListBox::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 if (!(m_style & wxEL_NO_REORDER))
148 {
149 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
150 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
151
152 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
153 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
154 }
155
156#if wxUSE_TOOLTIPS
157 if ( m_bEdit ) m_bEdit->SetToolTip(_("Edit item"));
158 if ( m_bNew ) m_bNew->SetToolTip(_("New item"));
159 if ( m_bDel ) m_bDel->SetToolTip(_("Delete item"));
160 if ( m_bUp ) m_bUp->SetToolTip(_("Move up"));
161 if ( m_bDown ) m_bDown->SetToolTip(_("Move down"));
162#endif
163
164 subp->SetSizer(subsizer);
165 subsizer->Fit(subp);
166
167 sizer->Add(subp, 0, wxEXPAND);
168
169 long st = wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxSUNKEN_BORDER;
170 if ( style & wxEL_ALLOW_EDIT )
171 st |= wxLC_EDIT_LABELS;
172 m_listCtrl = new CleverListCtrl(this, wxID_ELB_LISTCTRL,
173 wxDefaultPosition, wxDefaultSize, st);
174 wxArrayString empty_ar;
175 SetStrings(empty_ar);
176
177 sizer->Add(m_listCtrl, 1, wxEXPAND);
178
179 SetSizer(sizer);
180 Layout();
181}
182
183void wxEditableListBox::SetStrings(const wxArrayString& strings)
184{
185 m_listCtrl->DeleteAllItems();
186 size_t i;
187
188 for (i = 0; i < strings.GetCount(); i++)
189 m_listCtrl->InsertItem(i, strings[i]);
190
191 m_listCtrl->InsertItem(strings.GetCount(), wxEmptyString);
192 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
193}
194
195void wxEditableListBox::GetStrings(wxArrayString& strings) const
196{
197 strings.Clear();
198
199 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
200 strings.Add(m_listCtrl->GetItemText(i));
201}
202
203void wxEditableListBox::OnItemSelected(wxListEvent& event)
204{
205 m_selection = event.GetIndex();
206 if (!(m_style & wxEL_NO_REORDER))
207 {
208 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
209 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
210 }
211
212 if (m_style & wxEL_ALLOW_EDIT)
213 m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1);
214 if (m_style & wxEL_ALLOW_DELETE)
215 m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1);
216}
217
218void wxEditableListBox::OnNewItem(wxCommandEvent& WXUNUSED(event))
219{
220 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
221 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
222 m_listCtrl->EditLabel(m_selection);
223}
224
225void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
226{
227 if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 &&
228 !event.GetText().empty() )
229 {
230 // The user edited last (empty) line, i.e. added new entry. We have to
231 // add new empty line here so that adding one more line is still
232 // possible:
233 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxEmptyString);
234
235 // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item,
236 // so that the buttons are enabled/disabled properly
237 wxListEvent selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED, m_listCtrl->GetId());
238 selectionEvent.m_itemIndex = event.GetIndex();
239 m_listCtrl->GetEventHandler()->ProcessEvent(selectionEvent);
240 }
241}
242
243void wxEditableListBox::OnDelItem(wxCommandEvent& WXUNUSED(event))
244{
245 m_listCtrl->DeleteItem(m_selection);
246 m_listCtrl->SetItemState(m_selection,
247 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
248}
249
250void wxEditableListBox::OnEditItem(wxCommandEvent& WXUNUSED(event))
251{
252 m_listCtrl->EditLabel(m_selection);
253}
254
255void wxEditableListBox::OnUpItem(wxCommandEvent& WXUNUSED(event))
256{
257 wxString t1, t2;
258
259 t1 = m_listCtrl->GetItemText(m_selection - 1);
260 t2 = m_listCtrl->GetItemText(m_selection);
261 m_listCtrl->SetItemText(m_selection - 1, t2);
262 m_listCtrl->SetItemText(m_selection, t1);
263 m_listCtrl->SetItemState(m_selection - 1,
264 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
265}
266
267void wxEditableListBox::OnDownItem(wxCommandEvent& WXUNUSED(event))
268{
269 wxString t1, t2;
270
271 t1 = m_listCtrl->GetItemText(m_selection + 1);
272 t2 = m_listCtrl->GetItemText(m_selection);
273 m_listCtrl->SetItemText(m_selection + 1, t2);
274 m_listCtrl->SetItemText(m_selection, t1);
275 m_listCtrl->SetItemState(m_selection + 1,
276 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
277}