]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/gizmos/editlbox.cpp
Updating MSW file list.
[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
f55d21eb
VS
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
be5a51fb 18// need because it includes almost all "standard" wxWidgets headers)
f55d21eb
VS
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,
a2d49353 35 wxWindowID id = wxID_ANY,
f55d21eb
VS
36 const wxPoint &pos = wxDefaultPosition,
37 const wxSize &size = wxDefaultSize,
38 long style = wxLC_ICON,
39 const wxValidator& validator = wxDefaultValidator,
52f2ad08 40 const wxString &name = wxListCtrlNameStr)
f55d21eb
VS
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;
55232d19 55#ifdef __WXMSW__
e1c6c6ae 56 w -= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 6;
55232d19
VS
57#else
58 w -= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
59#endif
f55d21eb
VS
60 SetColumnWidth(0, w);
61 }
62
63private:
64 DECLARE_EVENT_TABLE()
65 void OnSize(wxSizeEvent& event)
66 {
67 SizeColumns();
c48792de 68 event.Skip();
f55d21eb
VS
69 }
70};
71
72BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
73 EVT_SIZE(CleverListCtrl::OnSize)
74END_EVENT_TABLE()
75
f55d21eb
VS
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
f7ad3cd3
VS
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();
f55d21eb
VS
92
93BEGIN_EVENT_TABLE(wxEditableListBox, wxPanel)
f7ad3cd3
VS
94 EVT_LIST_ITEM_SELECTED(wxID_ELB_LISTCTRL, wxEditableListBox::OnItemSelected)
95 EVT_LIST_END_LABEL_EDIT(wxID_ELB_LISTCTRL, wxEditableListBox::OnEndLabelEdit)
f55d21eb
VS
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,
e7d5dd02 105 const wxPoint& pos, const wxSize& size,
6187ec8f 106 long style,
e7d5dd02 107 const wxString& name)
55232d19 108 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name)
f55d21eb 109{
6187ec8f 110 m_style = style;
dabbc6a5 111 m_bEdit = m_bNew = m_bDel = m_bUp = m_bDown = NULL;
dd1d4b13 112
f55d21eb 113 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
96d24601 114
a2d49353 115 wxPanel *subp = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
f55d21eb
VS
116 wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
117 wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL);
a2d49353 118 subsizer->Add(new wxStaticText(subp, wxID_ANY, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4);
96d24601 119
923d52d6
VS
120#ifdef __WXMSW__
121 #define BTN_BORDER 4
6187ec8f
RD
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
923d52d6
VS
124 // figure out...
125#else
126 #define BTN_BORDER 0
127#endif
128
dd1d4b13
VS
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 }
6187ec8f 140
dd1d4b13
VS
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 }
6187ec8f 146
dd1d4b13 147 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
923d52d6 148 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
dd1d4b13
VS
149
150 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
923d52d6 151 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
96d24601 152
dd1d4b13 153#if wxUSE_TOOLTIPS
a84c0558
VS
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"));
dd1d4b13
VS
159#endif
160
f55d21eb
VS
161 subp->SetSizer(subsizer);
162 subsizer->Fit(subp);
96d24601 163
f55d21eb 164 sizer->Add(subp, 0, wxEXPAND);
6187ec8f
RD
165
166 long st = wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxSUNKEN_BORDER;
dd1d4b13
VS
167 if ( style & wxEL_ALLOW_EDIT )
168 st |= wxLC_EDIT_LABELS;
f7ad3cd3 169 m_listCtrl = new CleverListCtrl(this, wxID_ELB_LISTCTRL,
6187ec8f 170 wxDefaultPosition, wxDefaultSize, st);
f55d21eb
VS
171 wxArrayString empty_ar;
172 SetStrings(empty_ar);
96d24601 173
f55d21eb
VS
174 sizer->Add(m_listCtrl, 1, wxEXPAND);
175
f55d21eb 176 SetSizer(sizer);
35975549 177 Layout();
f55d21eb
VS
178}
179
180void wxEditableListBox::SetStrings(const wxArrayString& strings)
181{
182 m_listCtrl->DeleteAllItems();
183 size_t i;
96d24601 184
f55d21eb
VS
185 for (i = 0; i < strings.GetCount(); i++)
186 m_listCtrl->InsertItem(i, strings[i]);
96d24601 187
dabbc6a5 188 m_listCtrl->InsertItem(strings.GetCount(), wxEmptyString);
f55d21eb
VS
189 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
190}
191
192void 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
200void 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);
6187ec8f
RD
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);
f55d21eb
VS
209}
210
c3f815fa 211void wxEditableListBox::OnNewItem(wxCommandEvent& WXUNUSED(event))
f55d21eb 212{
96d24601 213 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
f55d21eb 214 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
f55d21eb
VS
215 m_listCtrl->EditLabel(m_selection);
216}
217
218void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
219{
dabbc6a5 220 if ( event.GetIndex() == m_listCtrl->GetItemCount()-1 &&
52f2ad08 221 !event.GetText().empty() )
f55d21eb 222 {
55232d19
VS
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:
dabbc6a5 226 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxEmptyString);
bec3b0dd
WS
227
228 // Simulate a wxEVT_COMMAND_LIST_ITEM_SELECTED event for the new item,
229 // so that the buttons are enabled/disabled properly
230 wxListEvent selectionEvent(wxEVT_COMMAND_LIST_ITEM_SELECTED, m_listCtrl->GetId());
231 selectionEvent.m_itemIndex = event.GetIndex();
232 m_listCtrl->GetEventHandler()->ProcessEvent(selectionEvent);
f55d21eb
VS
233 }
234}
235
c3f815fa 236void wxEditableListBox::OnDelItem(wxCommandEvent& WXUNUSED(event))
f55d21eb
VS
237{
238 m_listCtrl->DeleteItem(m_selection);
96d24601 239 m_listCtrl->SetItemState(m_selection,
f55d21eb
VS
240 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
241}
242
c3f815fa 243void wxEditableListBox::OnEditItem(wxCommandEvent& WXUNUSED(event))
f55d21eb
VS
244{
245 m_listCtrl->EditLabel(m_selection);
246}
247
c3f815fa 248void wxEditableListBox::OnUpItem(wxCommandEvent& WXUNUSED(event))
f55d21eb
VS
249{
250 wxString t1, t2;
96d24601 251
f55d21eb
VS
252 t1 = m_listCtrl->GetItemText(m_selection - 1);
253 t2 = m_listCtrl->GetItemText(m_selection);
254 m_listCtrl->SetItemText(m_selection - 1, t2);
255 m_listCtrl->SetItemText(m_selection, t1);
256 m_listCtrl->SetItemState(m_selection - 1,
257 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
258}
259
c3f815fa 260void wxEditableListBox::OnDownItem(wxCommandEvent& WXUNUSED(event))
f55d21eb
VS
261{
262 wxString t1, t2;
96d24601 263
f55d21eb
VS
264 t1 = m_listCtrl->GetItemText(m_selection + 1);
265 t2 = m_listCtrl->GetItemText(m_selection);
266 m_listCtrl->SetItemText(m_selection + 1, t2);
267 m_listCtrl->SetItemText(m_selection, t1);
268 m_listCtrl->SetItemState(m_selection + 1,
269 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
270}