]>
git.saurik.com Git - wxWidgets.git/blob - contrib/src/gizmos/editlbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: ListBox with editable items
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "editlbox.h"
14 // For compilers that support precompilation, includes "wx/wx.h".
15 #include "wx/wxprec.h"
21 // for all others, include the necessary headers (this file is usually all you
22 // need because it includes almost all "standard" wxWidgets headers)
27 #include "wx/gizmos/editlbox.h"
29 #include "wx/listctrl.h"
34 // list control with auto-resizable column:
35 class CleverListCtrl
: public wxListCtrl
38 CleverListCtrl(wxWindow
*parent
,
39 wxWindowID id
= wxID_ANY
,
40 const wxPoint
&pos
= wxDefaultPosition
,
41 const wxSize
&size
= wxDefaultSize
,
42 long style
= wxLC_ICON
,
43 const wxValidator
& validator
= wxDefaultValidator
,
44 const wxString
&name
= _T("listctrl"))
45 : wxListCtrl(parent
, id
, pos
, size
, style
, validator
, name
)
52 InsertColumn(0, _T("item"));
60 w
-= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
) + 6;
62 w
-= 2*wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
69 void OnSize(wxSizeEvent
& event
)
76 BEGIN_EVENT_TABLE(CleverListCtrl
, wxListCtrl
)
77 EVT_SIZE(CleverListCtrl::OnSize
)
86 IMPLEMENT_CLASS(wxEditableListBox
, wxPanel
)
90 // ID value doesn't matter, it won't propagate out of wxEditableListBox
92 wxID_ELB_DELETE
= wxID_HIGHEST
+ 1,
100 BEGIN_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
)
110 wxEditableListBox::wxEditableListBox(wxWindow
*parent
, wxWindowID id
,
111 const wxString
& label
,
112 const wxPoint
& pos
, const wxSize
& size
,
114 const wxString
& name
)
115 : wxPanel(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
)
118 m_bEdit
= m_bNew
= m_bDel
= m_bUp
= m_bDown
= NULL
;
120 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
122 wxPanel
*subp
= new wxPanel(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
123 wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
124 wxSizer
*subsizer
= new wxBoxSizer(wxHORIZONTAL
);
125 subsizer
->Add(new wxStaticText(subp
, wxID_ANY
, label
), 1, wxALIGN_CENTRE_VERTICAL
| wxLEFT
, 4);
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
136 if ( m_style
& wxEL_ALLOW_EDIT
)
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
);
142 if ( m_style
& wxEL_ALLOW_NEW
)
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
);
148 if ( m_style
& wxEL_ALLOW_DELETE
)
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
);
154 m_bUp
= new wxBitmapButton(subp
, wxID_ELB_UP
, wxBitmap(elup_xpm
));
155 subsizer
->Add(m_bUp
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
157 m_bDown
= new wxBitmapButton(subp
, wxID_ELB_DOWN
, wxBitmap(eldown_xpm
));
158 subsizer
->Add(m_bDown
, 0, wxALIGN_CENTRE_VERTICAL
| wxTOP
| wxBOTTOM
, BTN_BORDER
);
161 if ( m_bEdit
) m_bEdit
->SetToolTip(_("Edit item"));
162 if ( m_bNew
) m_bNew
->SetToolTip(_("New item"));
163 if ( m_bDel
) m_bDel
->SetToolTip(_("Delete item"));
164 m_bUp
->SetToolTip(_("Move up"));
165 m_bDown
->SetToolTip(_("Move down"));
168 subp
->SetSizer(subsizer
);
171 sizer
->Add(subp
, 0, wxEXPAND
);
173 long st
= wxLC_REPORT
| wxLC_NO_HEADER
| wxLC_SINGLE_SEL
| wxSUNKEN_BORDER
;
174 if ( style
& wxEL_ALLOW_EDIT
)
175 st
|= wxLC_EDIT_LABELS
;
176 m_listCtrl
= new CleverListCtrl(this, wxID_ELD_LISTCTRL
,
177 wxDefaultPosition
, wxDefaultSize
, st
);
178 wxArrayString empty_ar
;
179 SetStrings(empty_ar
);
181 sizer
->Add(m_listCtrl
, 1, wxEXPAND
);
187 void wxEditableListBox::SetStrings(const wxArrayString
& strings
)
189 m_listCtrl
->DeleteAllItems();
192 for (i
= 0; i
< strings
.GetCount(); i
++)
193 m_listCtrl
->InsertItem(i
, strings
[i
]);
195 m_listCtrl
->InsertItem(strings
.GetCount(), wxEmptyString
);
196 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
199 void wxEditableListBox::GetStrings(wxArrayString
& strings
)
203 for (int i
= 0; i
< m_listCtrl
->GetItemCount()-1; i
++)
204 strings
.Add(m_listCtrl
->GetItemText(i
));
207 void wxEditableListBox::OnItemSelected(wxListEvent
& event
)
209 m_selection
= event
.GetIndex();
210 m_bUp
->Enable(m_selection
!= 0 && m_selection
< m_listCtrl
->GetItemCount()-1);
211 m_bDown
->Enable(m_selection
< m_listCtrl
->GetItemCount()-2);
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);
218 void wxEditableListBox::OnNewItem(wxCommandEvent
& WXUNUSED(event
))
220 m_listCtrl
->SetItemState(m_listCtrl
->GetItemCount()-1,
221 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
222 m_listCtrl
->EditLabel(m_selection
);
225 void wxEditableListBox::OnEndLabelEdit(wxListEvent
& event
)
227 if ( event
.GetIndex() == m_listCtrl
->GetItemCount()-1 &&
228 !event
.GetText().IsEmpty() )
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
233 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), wxEmptyString
);
237 void wxEditableListBox::OnDelItem(wxCommandEvent
& WXUNUSED(event
))
239 m_listCtrl
->DeleteItem(m_selection
);
240 m_listCtrl
->SetItemState(m_selection
,
241 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
244 void wxEditableListBox::OnEditItem(wxCommandEvent
& WXUNUSED(event
))
246 m_listCtrl
->EditLabel(m_selection
);
249 void wxEditableListBox::OnUpItem(wxCommandEvent
& WXUNUSED(event
))
253 t1
= m_listCtrl
->GetItemText(m_selection
- 1);
254 t2
= m_listCtrl
->GetItemText(m_selection
);
255 m_listCtrl
->SetItemText(m_selection
- 1, t2
);
256 m_listCtrl
->SetItemText(m_selection
, t1
);
257 m_listCtrl
->SetItemState(m_selection
- 1,
258 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
261 void wxEditableListBox::OnDownItem(wxCommandEvent
& WXUNUSED(event
))
265 t1
= m_listCtrl
->GetItemText(m_selection
+ 1);
266 t2
= m_listCtrl
->GetItemText(m_selection
);
267 m_listCtrl
->SetItemText(m_selection
+ 1, t2
);
268 m_listCtrl
->SetItemText(m_selection
, t1
);
269 m_listCtrl
->SetItemState(m_selection
+ 1,
270 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);