]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/gizmos/editlbox.cpp
Project file update
[wxWidgets.git] / contrib / src / gizmos / editlbox.cpp
CommitLineData
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
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:
35class CleverListCtrl : public wxListCtrl
36{
37public:
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;
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
67private:
68 DECLARE_EVENT_TABLE()
69 void OnSize(wxSizeEvent& event)
70 {
71 SizeColumns();
c48792de 72 event.Skip();
f55d21eb
VS
73 }
74};
75
76BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
77 EVT_SIZE(CleverListCtrl::OnSize)
78END_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
86IMPLEMENT_CLASS(wxEditableListBox, wxPanel)
87
96d24601 88enum
f55d21eb
VS
89{
90 // ID value doesn't matter, it won't propagate out of wxEditableListBox
91 // instance
92 wxID_ELB_DELETE = wxID_HIGHEST + 1,
93 wxID_ELB_NEW,
94 wxID_ELB_UP,
95 wxID_ELB_DOWN,
96 wxID_ELB_EDIT,
97 wxID_ELD_LISTCTRL
98};
99
100BEGIN_EVENT_TABLE(wxEditableListBox, wxPanel)
101 EVT_LIST_ITEM_SELECTED(wxID_ELD_LISTCTRL, wxEditableListBox::OnItemSelected)
102 EVT_LIST_END_LABEL_EDIT(wxID_ELD_LISTCTRL, wxEditableListBox::OnEndLabelEdit)
103 EVT_BUTTON(wxID_ELB_NEW, wxEditableListBox::OnNewItem)
104 EVT_BUTTON(wxID_ELB_UP, wxEditableListBox::OnUpItem)
105 EVT_BUTTON(wxID_ELB_DOWN, wxEditableListBox::OnDownItem)
106 EVT_BUTTON(wxID_ELB_EDIT, wxEditableListBox::OnEditItem)
107 EVT_BUTTON(wxID_ELB_DELETE, wxEditableListBox::OnDelItem)
108END_EVENT_TABLE()
109
110wxEditableListBox::wxEditableListBox(wxWindow *parent, wxWindowID id,
111 const wxString& label,
e7d5dd02 112 const wxPoint& pos, const wxSize& size,
6187ec8f 113 long style,
e7d5dd02 114 const wxString& name)
55232d19 115 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name)
f55d21eb 116{
6187ec8f 117 m_style = style;
dd1d4b13
VS
118 m_bEdit = m_bNew = m_bDel = m_bUp = m_bDown = NULL;
119
f55d21eb 120 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
96d24601 121
f55d21eb
VS
122 wxPanel *subp = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize,
123 wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
124 wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL);
125 subsizer->Add(new wxStaticText(subp, -1, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4);
96d24601 126
923d52d6
VS
127#ifdef __WXMSW__
128 #define BTN_BORDER 4
6187ec8f
RD
129 // FIXME - why is this needed? There's some reason why sunken border is
130 // ignored by sizers in wxMSW but not in wxGTK that I can't
923d52d6
VS
131 // figure out...
132#else
133 #define BTN_BORDER 0
134#endif
135
dd1d4b13
VS
136 if ( m_style & wxEL_ALLOW_EDIT )
137 {
138 m_bEdit = new wxBitmapButton(subp, wxID_ELB_EDIT, wxBitmap(eledit_xpm));
139 subsizer->Add(m_bEdit, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
140 }
141
142 if ( m_style & wxEL_ALLOW_NEW )
143 {
144 m_bNew = new wxBitmapButton(subp, wxID_ELB_NEW, wxBitmap(elnew_xpm));
145 subsizer->Add(m_bNew, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
146 }
6187ec8f 147
dd1d4b13
VS
148 if ( m_style & wxEL_ALLOW_DELETE )
149 {
150 m_bDel = new wxBitmapButton(subp, wxID_ELB_DELETE, wxBitmap(eldel_xpm));
151 subsizer->Add(m_bDel, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
152 }
6187ec8f 153
dd1d4b13 154 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
923d52d6 155 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
dd1d4b13
VS
156
157 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
923d52d6 158 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
96d24601 159
dd1d4b13
VS
160#if wxUSE_TOOLTIPS
161 if ( m_bEdit ) m_bEdit->SetToolTip(wxT("Edit item"));
162 if ( m_bNew ) m_bNew->SetToolTip(wxT("New item"));
163 if ( m_bDel ) m_bDel->SetToolTip(wxT("Delete item"));
164 m_bUp->SetToolTip(wxT("Move up"));
165 m_bDown->SetToolTip(wxT("Move down"));
166#endif
167
f55d21eb
VS
168 subp->SetAutoLayout(TRUE);
169 subp->SetSizer(subsizer);
170 subsizer->Fit(subp);
96d24601 171
f55d21eb 172 sizer->Add(subp, 0, wxEXPAND);
6187ec8f
RD
173
174 long st = wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxSUNKEN_BORDER;
dd1d4b13
VS
175 if ( style & wxEL_ALLOW_EDIT )
176 st |= wxLC_EDIT_LABELS;
96d24601 177 m_listCtrl = new CleverListCtrl(this, wxID_ELD_LISTCTRL,
6187ec8f 178 wxDefaultPosition, wxDefaultSize, st);
f55d21eb
VS
179 wxArrayString empty_ar;
180 SetStrings(empty_ar);
96d24601 181
f55d21eb
VS
182 sizer->Add(m_listCtrl, 1, wxEXPAND);
183
184 SetAutoLayout(TRUE);
185 SetSizer(sizer);
35975549 186 Layout();
f55d21eb
VS
187}
188
189void wxEditableListBox::SetStrings(const wxArrayString& strings)
190{
191 m_listCtrl->DeleteAllItems();
192 size_t i;
96d24601 193
f55d21eb
VS
194 for (i = 0; i < strings.GetCount(); i++)
195 m_listCtrl->InsertItem(i, strings[i]);
96d24601
RD
196
197 m_listCtrl->InsertItem(strings.GetCount(), _T(""));
f55d21eb
VS
198 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
199}
200
201void wxEditableListBox::GetStrings(wxArrayString& strings)
202{
203 strings.Clear();
204
205 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
206 strings.Add(m_listCtrl->GetItemText(i));
207}
208
209void wxEditableListBox::OnItemSelected(wxListEvent& event)
210{
211 m_selection = event.GetIndex();
212 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
213 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
6187ec8f
RD
214 if (m_style & wxEL_ALLOW_EDIT)
215 m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1);
216 if (m_style & wxEL_ALLOW_DELETE)
217 m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1);
f55d21eb
VS
218}
219
220void wxEditableListBox::OnNewItem(wxCommandEvent& event)
221{
96d24601 222 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
f55d21eb 223 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
f55d21eb
VS
224 m_listCtrl->EditLabel(m_selection);
225}
226
227void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
228{
55232d19
VS
229 if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 &&
230 !event.GetText().IsEmpty() )
f55d21eb 231 {
55232d19
VS
232 // The user edited last (empty) line, i.e. added new entry. We have to
233 // add new empty line here so that adding one more line is still
234 // possible:
235 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T(""));
f55d21eb
VS
236 }
237}
238
239void wxEditableListBox::OnDelItem(wxCommandEvent& event)
240{
241 m_listCtrl->DeleteItem(m_selection);
96d24601 242 m_listCtrl->SetItemState(m_selection,
f55d21eb
VS
243 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
244}
245
246void wxEditableListBox::OnEditItem(wxCommandEvent& event)
247{
248 m_listCtrl->EditLabel(m_selection);
249}
250
251void wxEditableListBox::OnUpItem(wxCommandEvent& event)
252{
253 wxString t1, t2;
96d24601 254
f55d21eb
VS
255 t1 = m_listCtrl->GetItemText(m_selection - 1);
256 t2 = m_listCtrl->GetItemText(m_selection);
257 m_listCtrl->SetItemText(m_selection - 1, t2);
258 m_listCtrl->SetItemText(m_selection, t1);
259 m_listCtrl->SetItemState(m_selection - 1,
260 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
261}
262
263void wxEditableListBox::OnDownItem(wxCommandEvent& event)
264{
265 wxString t1, t2;
96d24601 266
f55d21eb
VS
267 t1 = m_listCtrl->GetItemText(m_selection + 1);
268 t2 = m_listCtrl->GetItemText(m_selection);
269 m_listCtrl->SetItemText(m_selection + 1, t2);
270 m_listCtrl->SetItemText(m_selection, t1);
271 m_listCtrl->SetItemState(m_selection + 1,
272 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
273}