]>
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" wxWindows 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
,
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
)
52 InsertColumn(0, _T("item"));
59 w
-= wxSystemSettings::GetSystemMetric(wxSYS_VSCROLL_X
) + 6;
65 void OnSize(wxSizeEvent
& event
)
72 BEGIN_EVENT_TABLE(CleverListCtrl
, wxListCtrl
)
73 EVT_SIZE(CleverListCtrl::OnSize
)
83 IMPLEMENT_CLASS(wxEditableListBox
, wxPanel
)
87 // ID value doesn't matter, it won't propagate out of wxEditableListBox
89 wxID_ELB_DELETE
= wxID_HIGHEST
+ 1,
97 BEGIN_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
)
107 wxEditableListBox::wxEditableListBox(wxWindow
*parent
, wxWindowID id
,
108 const wxString
& label
,
109 const wxPoint
& pos
, const wxSize
& size
,
110 const wxString
& name
)
111 : wxPanel(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
), m_edittingNew(FALSE
)
113 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
115 wxPanel
*subp
= new wxPanel(this, -1, wxDefaultPosition
, wxDefaultSize
,
116 wxSUNKEN_BORDER
| wxTAB_TRAVERSAL
);
117 wxSizer
*subsizer
= new wxBoxSizer(wxHORIZONTAL
);
118 subsizer
->Add(new wxStaticText(subp
, -1, label
), 1, wxALIGN_CENTRE_VERTICAL
| wxLEFT
, 4);
119 m_bEdit
= new wxBitmapButton(subp
, wxID_ELB_EDIT
, wxBitmap(eledit_xpm
));
120 m_bNew
= new wxBitmapButton(subp
, wxID_ELB_NEW
, wxBitmap(elnew_xpm
));
121 m_bDel
= new wxBitmapButton(subp
, wxID_ELB_DELETE
, wxBitmap(eldel_xpm
));
122 m_bUp
= new wxBitmapButton(subp
, wxID_ELB_UP
, wxBitmap(elup_xpm
));
123 m_bDown
= new wxBitmapButton(subp
, wxID_ELB_DOWN
, wxBitmap(eldown_xpm
));
124 subsizer
->Add(m_bEdit
, 0, wxALIGN_CENTRE_VERTICAL
);
125 subsizer
->Add(m_bNew
, 0, wxALIGN_CENTRE_VERTICAL
);
126 subsizer
->Add(m_bDel
, 0, wxALIGN_CENTRE_VERTICAL
);
127 subsizer
->Add(m_bUp
, 0, wxALIGN_CENTRE_VERTICAL
);
128 subsizer
->Add(m_bDown
, 0, wxALIGN_CENTRE_VERTICAL
);
130 subp
->SetAutoLayout(TRUE
);
131 subp
->SetSizer(subsizer
);
134 sizer
->Add(subp
, 0, wxEXPAND
);
135 m_listCtrl
= new CleverListCtrl(this, wxID_ELD_LISTCTRL
,
136 wxDefaultPosition
, wxDefaultSize
,
137 wxLC_REPORT
| wxLC_NO_HEADER
|
138 wxLC_SINGLE_SEL
| wxSUNKEN_BORDER
|
140 wxArrayString empty_ar
;
141 SetStrings(empty_ar
);
143 sizer
->Add(m_listCtrl
, 1, wxEXPAND
);
150 void wxEditableListBox::SetStrings(const wxArrayString
& strings
)
152 m_listCtrl
->DeleteAllItems();
155 for (i
= 0; i
< strings
.GetCount(); i
++)
156 m_listCtrl
->InsertItem(i
, strings
[i
]);
158 m_listCtrl
->InsertItem(strings
.GetCount(), _T(""));
159 m_listCtrl
->SetItemState(0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
162 void wxEditableListBox::GetStrings(wxArrayString
& strings
)
166 for (int i
= 0; i
< m_listCtrl
->GetItemCount()-1; i
++)
167 strings
.Add(m_listCtrl
->GetItemText(i
));
170 void wxEditableListBox::OnItemSelected(wxListEvent
& event
)
172 m_selection
= event
.GetIndex();
173 m_bUp
->Enable(m_selection
!= 0 && m_selection
< m_listCtrl
->GetItemCount()-1);
174 m_bDown
->Enable(m_selection
< m_listCtrl
->GetItemCount()-2);
175 m_bEdit
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
176 m_bDel
->Enable(m_selection
< m_listCtrl
->GetItemCount()-1);
179 void wxEditableListBox::OnNewItem(wxCommandEvent
& event
)
181 m_listCtrl
->SetItemState(m_listCtrl
->GetItemCount()-1,
182 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
183 m_edittingNew
= TRUE
;
184 m_listCtrl
->EditLabel(m_selection
);
187 void wxEditableListBox::OnEndLabelEdit(wxListEvent
& event
)
191 m_edittingNew
= FALSE
;
192 if (!event
.GetText().IsEmpty())
193 m_listCtrl
->InsertItem(m_listCtrl
->GetItemCount(), _T(""));
197 void wxEditableListBox::OnDelItem(wxCommandEvent
& event
)
199 m_listCtrl
->DeleteItem(m_selection
);
200 m_listCtrl
->SetItemState(m_selection
,
201 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
204 void wxEditableListBox::OnEditItem(wxCommandEvent
& event
)
206 m_listCtrl
->EditLabel(m_selection
);
209 void wxEditableListBox::OnUpItem(wxCommandEvent
& event
)
213 t1
= m_listCtrl
->GetItemText(m_selection
- 1);
214 t2
= m_listCtrl
->GetItemText(m_selection
);
215 m_listCtrl
->SetItemText(m_selection
- 1, t2
);
216 m_listCtrl
->SetItemText(m_selection
, t1
);
217 m_listCtrl
->SetItemState(m_selection
- 1,
218 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
221 void wxEditableListBox::OnDownItem(wxCommandEvent
& event
)
225 t1
= m_listCtrl
->GetItemText(m_selection
+ 1);
226 t2
= m_listCtrl
->GetItemText(m_selection
);
227 m_listCtrl
->SetItemText(m_selection
+ 1, t2
);
228 m_listCtrl
->SetItemText(m_selection
, t1
);
229 m_listCtrl
->SetItemState(m_selection
+ 1,
230 wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);