]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/gizmos/editlbox.cpp
fixed MSVC compilation and warnings
[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;
e1c6c6ae 59 w -= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X) + 6;
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
76
77#include "eldel.xpm"
78#include "eldown.xpm"
79#include "eledit.xpm"
80#include "elnew.xpm"
81#include "elup.xpm"
82
83IMPLEMENT_CLASS(wxEditableListBox, wxPanel)
84
96d24601 85enum
f55d21eb
VS
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
97BEGIN_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)
105END_EVENT_TABLE()
106
107wxEditableListBox::wxEditableListBox(wxWindow *parent, wxWindowID id,
108 const wxString& label,
e7d5dd02 109 const wxPoint& pos, const wxSize& size,
6187ec8f 110 long style,
e7d5dd02
VS
111 const wxString& name)
112 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name), m_edittingNew(FALSE)
f55d21eb 113{
6187ec8f 114 m_style = style;
f55d21eb 115 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
96d24601 116
f55d21eb
VS
117 wxPanel *subp = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize,
118 wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
119 wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL);
120 subsizer->Add(new wxStaticText(subp, -1, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4);
121 m_bEdit = new wxBitmapButton(subp, wxID_ELB_EDIT, wxBitmap(eledit_xpm));
122 m_bNew = new wxBitmapButton(subp, wxID_ELB_NEW, wxBitmap(elnew_xpm));
123 m_bDel = new wxBitmapButton(subp, wxID_ELB_DELETE, wxBitmap(eldel_xpm));
124 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
125 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
96d24601 126
6ceccedd 127#if wxUSE_TOOLTIPS
96d24601
RD
128 m_bEdit->SetToolTip(wxT("Edit item"));
129 m_bNew->SetToolTip(wxT("New item"));
130 m_bDel->SetToolTip(wxT("Delete item"));
131 m_bUp->SetToolTip(wxT("Move up"));
132 m_bDown->SetToolTip(wxT("Move down"));
6ceccedd 133#endif
96d24601 134
923d52d6
VS
135#ifdef __WXMSW__
136 #define BTN_BORDER 4
6187ec8f
RD
137 // FIXME - why is this needed? There's some reason why sunken border is
138 // ignored by sizers in wxMSW but not in wxGTK that I can't
923d52d6
VS
139 // figure out...
140#else
141 #define BTN_BORDER 0
142#endif
143
144 subsizer->Add(m_bEdit, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
145 subsizer->Add(m_bNew, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
146 subsizer->Add(m_bDel, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
6187ec8f
RD
147
148 if (!(m_style & wxEL_ALLOW_EDIT))
149 m_bEdit->Show(FALSE);
150 if (!(m_style & wxEL_ALLOW_NEW))
151 m_bNew->Show(FALSE);
152 if (!(m_style & wxEL_ALLOW_DELETE))
153 m_bDel->Show(FALSE);
154
923d52d6
VS
155 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
156 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL | wxTOP | wxBOTTOM, BTN_BORDER);
96d24601 157
f55d21eb
VS
158 subp->SetAutoLayout(TRUE);
159 subp->SetSizer(subsizer);
160 subsizer->Fit(subp);
96d24601 161
f55d21eb 162 sizer->Add(subp, 0, wxEXPAND);
6187ec8f
RD
163
164 long st = wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxSUNKEN_BORDER;
165 if (style & wxEL_ALLOW_EDIT)
166 st |= wxLC_EDIT_LABELS;
96d24601 167 m_listCtrl = new CleverListCtrl(this, wxID_ELD_LISTCTRL,
6187ec8f 168 wxDefaultPosition, wxDefaultSize, st);
f55d21eb
VS
169 wxArrayString empty_ar;
170 SetStrings(empty_ar);
96d24601 171
f55d21eb
VS
172 sizer->Add(m_listCtrl, 1, wxEXPAND);
173
174 SetAutoLayout(TRUE);
175 SetSizer(sizer);
35975549 176 Layout();
f55d21eb
VS
177}
178
179void wxEditableListBox::SetStrings(const wxArrayString& strings)
180{
181 m_listCtrl->DeleteAllItems();
182 size_t i;
96d24601 183
f55d21eb
VS
184 for (i = 0; i < strings.GetCount(); i++)
185 m_listCtrl->InsertItem(i, strings[i]);
96d24601
RD
186
187 m_listCtrl->InsertItem(strings.GetCount(), _T(""));
f55d21eb
VS
188 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
189}
190
191void wxEditableListBox::GetStrings(wxArrayString& strings)
192{
193 strings.Clear();
194
195 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
196 strings.Add(m_listCtrl->GetItemText(i));
197}
198
199void wxEditableListBox::OnItemSelected(wxListEvent& event)
200{
201 m_selection = event.GetIndex();
202 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
203 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
6187ec8f
RD
204 if (m_style & wxEL_ALLOW_EDIT)
205 m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1);
206 if (m_style & wxEL_ALLOW_DELETE)
207 m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1);
f55d21eb
VS
208}
209
210void wxEditableListBox::OnNewItem(wxCommandEvent& event)
211{
96d24601 212 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
f55d21eb
VS
213 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
214 m_edittingNew = TRUE;
215 m_listCtrl->EditLabel(m_selection);
216}
217
218void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
219{
220 if (m_edittingNew)
221 {
222 m_edittingNew = FALSE;
223 if (!event.GetText().IsEmpty())
f55d21eb 224 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T(""));
f55d21eb
VS
225 }
226}
227
228void wxEditableListBox::OnDelItem(wxCommandEvent& event)
229{
230 m_listCtrl->DeleteItem(m_selection);
96d24601 231 m_listCtrl->SetItemState(m_selection,
f55d21eb
VS
232 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
233}
234
235void wxEditableListBox::OnEditItem(wxCommandEvent& event)
236{
237 m_listCtrl->EditLabel(m_selection);
238}
239
240void wxEditableListBox::OnUpItem(wxCommandEvent& event)
241{
242 wxString t1, t2;
96d24601 243
f55d21eb
VS
244 t1 = m_listCtrl->GetItemText(m_selection - 1);
245 t2 = m_listCtrl->GetItemText(m_selection);
246 m_listCtrl->SetItemText(m_selection - 1, t2);
247 m_listCtrl->SetItemText(m_selection, t1);
248 m_listCtrl->SetItemState(m_selection - 1,
249 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
250}
251
252void wxEditableListBox::OnDownItem(wxCommandEvent& event)
253{
254 wxString t1, t2;
96d24601 255
f55d21eb
VS
256 t1 = m_listCtrl->GetItemText(m_selection + 1);
257 t2 = m_listCtrl->GetItemText(m_selection);
258 m_listCtrl->SetItemText(m_selection + 1, t2);
259 m_listCtrl->SetItemText(m_selection, t1);
260 m_listCtrl->SetItemState(m_selection + 1,
261 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
262}