]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/gizmos/editlbox.cpp
Enabling the Pop Menu fix to peek at all the command messages. Required adding a...
[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;
59 w -= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X) + 6;
60 SetColumnWidth(0, w);
61 }
62
63private:
64 DECLARE_EVENT_TABLE()
65 void OnSize(wxSizeEvent& event)
66 {
67 SizeColumns();
68 }
69};
70
71BEGIN_EVENT_TABLE(CleverListCtrl, wxListCtrl)
72 EVT_SIZE(CleverListCtrl::OnSize)
73END_EVENT_TABLE()
74
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
84enum
85{
86 // ID value doesn't matter, it won't propagate out of wxEditableListBox
87 // instance
88 wxID_ELB_DELETE = wxID_HIGHEST + 1,
89 wxID_ELB_NEW,
90 wxID_ELB_UP,
91 wxID_ELB_DOWN,
92 wxID_ELB_EDIT,
93 wxID_ELD_LISTCTRL
94};
95
96BEGIN_EVENT_TABLE(wxEditableListBox, wxPanel)
97 EVT_LIST_ITEM_SELECTED(wxID_ELD_LISTCTRL, wxEditableListBox::OnItemSelected)
98 EVT_LIST_END_LABEL_EDIT(wxID_ELD_LISTCTRL, wxEditableListBox::OnEndLabelEdit)
99 EVT_BUTTON(wxID_ELB_NEW, wxEditableListBox::OnNewItem)
100 EVT_BUTTON(wxID_ELB_UP, wxEditableListBox::OnUpItem)
101 EVT_BUTTON(wxID_ELB_DOWN, wxEditableListBox::OnDownItem)
102 EVT_BUTTON(wxID_ELB_EDIT, wxEditableListBox::OnEditItem)
103 EVT_BUTTON(wxID_ELB_DELETE, wxEditableListBox::OnDelItem)
104END_EVENT_TABLE()
105
106wxEditableListBox::wxEditableListBox(wxWindow *parent, wxWindowID id,
107 const wxString& label,
e7d5dd02
VS
108 const wxPoint& pos, const wxSize& size,
109 const wxString& name)
110 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL, name), m_edittingNew(FALSE)
f55d21eb
VS
111{
112 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
113
114 wxPanel *subp = new wxPanel(this, -1, wxDefaultPosition, wxDefaultSize,
115 wxSUNKEN_BORDER | wxTAB_TRAVERSAL);
116 wxSizer *subsizer = new wxBoxSizer(wxHORIZONTAL);
117 subsizer->Add(new wxStaticText(subp, -1, label), 1, wxALIGN_CENTRE_VERTICAL | wxLEFT, 4);
118 m_bEdit = new wxBitmapButton(subp, wxID_ELB_EDIT, wxBitmap(eledit_xpm));
119 m_bNew = new wxBitmapButton(subp, wxID_ELB_NEW, wxBitmap(elnew_xpm));
120 m_bDel = new wxBitmapButton(subp, wxID_ELB_DELETE, wxBitmap(eldel_xpm));
121 m_bUp = new wxBitmapButton(subp, wxID_ELB_UP, wxBitmap(elup_xpm));
122 m_bDown = new wxBitmapButton(subp, wxID_ELB_DOWN, wxBitmap(eldown_xpm));
123 subsizer->Add(m_bEdit, 0, wxALIGN_CENTRE_VERTICAL);
124 subsizer->Add(m_bNew, 0, wxALIGN_CENTRE_VERTICAL);
125 subsizer->Add(m_bDel, 0, wxALIGN_CENTRE_VERTICAL);
126 subsizer->Add(m_bUp, 0, wxALIGN_CENTRE_VERTICAL);
127 subsizer->Add(m_bDown, 0, wxALIGN_CENTRE_VERTICAL);
128
129 subp->SetAutoLayout(TRUE);
130 subp->SetSizer(subsizer);
131 subsizer->Fit(subp);
132
133 sizer->Add(subp, 0, wxEXPAND);
134 m_listCtrl = new CleverListCtrl(this, wxID_ELD_LISTCTRL,
135 wxDefaultPosition, wxDefaultSize,
136 wxLC_REPORT | wxLC_NO_HEADER |
cad599a1
VS
137 wxLC_SINGLE_SEL | wxSUNKEN_BORDER |
138 wxLC_EDIT_LABELS);
f55d21eb
VS
139 wxArrayString empty_ar;
140 SetStrings(empty_ar);
141
142 sizer->Add(m_listCtrl, 1, wxEXPAND);
143
144 SetAutoLayout(TRUE);
145 SetSizer(sizer);
cad599a1 146 Layout();
f55d21eb
VS
147}
148
149void wxEditableListBox::SetStrings(const wxArrayString& strings)
150{
151 m_listCtrl->DeleteAllItems();
152 size_t i;
153
154 for (i = 0; i < strings.GetCount(); i++)
155 m_listCtrl->InsertItem(i, strings[i]);
156
157 m_listCtrl->InsertItem(strings.GetCount(), _T(""));
158 m_listCtrl->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
159}
160
161void wxEditableListBox::GetStrings(wxArrayString& strings)
162{
163 strings.Clear();
164
165 for (int i = 0; i < m_listCtrl->GetItemCount()-1; i++)
166 strings.Add(m_listCtrl->GetItemText(i));
167}
168
169void wxEditableListBox::OnItemSelected(wxListEvent& event)
170{
171 m_selection = event.GetIndex();
172 m_bUp->Enable(m_selection != 0 && m_selection < m_listCtrl->GetItemCount()-1);
173 m_bDown->Enable(m_selection < m_listCtrl->GetItemCount()-2);
174 m_bEdit->Enable(m_selection < m_listCtrl->GetItemCount()-1);
175 m_bDel->Enable(m_selection < m_listCtrl->GetItemCount()-1);
176}
177
178void wxEditableListBox::OnNewItem(wxCommandEvent& event)
179{
180 m_listCtrl->SetItemState(m_listCtrl->GetItemCount()-1,
181 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
182 m_edittingNew = TRUE;
183 m_listCtrl->EditLabel(m_selection);
184}
185
186void wxEditableListBox::OnEndLabelEdit(wxListEvent& event)
187{
188 if (m_edittingNew)
189 {
190 m_edittingNew = FALSE;
191 if (!event.GetText().IsEmpty())
f55d21eb 192 m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), _T(""));
f55d21eb
VS
193 }
194}
195
196void wxEditableListBox::OnDelItem(wxCommandEvent& event)
197{
198 m_listCtrl->DeleteItem(m_selection);
199 m_listCtrl->SetItemState(m_selection,
200 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
201}
202
203void wxEditableListBox::OnEditItem(wxCommandEvent& event)
204{
205 m_listCtrl->EditLabel(m_selection);
206}
207
208void wxEditableListBox::OnUpItem(wxCommandEvent& event)
209{
210 wxString t1, t2;
211
212 t1 = m_listCtrl->GetItemText(m_selection - 1);
213 t2 = m_listCtrl->GetItemText(m_selection);
214 m_listCtrl->SetItemText(m_selection - 1, t2);
215 m_listCtrl->SetItemText(m_selection, t1);
216 m_listCtrl->SetItemState(m_selection - 1,
217 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
218}
219
220void wxEditableListBox::OnDownItem(wxCommandEvent& event)
221{
222 wxString t1, t2;
223
224 t1 = m_listCtrl->GetItemText(m_selection + 1);
225 t2 = m_listCtrl->GetItemText(m_selection);
226 m_listCtrl->SetItemText(m_selection + 1, t2);
227 m_listCtrl->SetItemText(m_selection, t1);
228 m_listCtrl->SetItemState(m_selection + 1,
229 wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
230}