1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/rearrangectrl.cpp
3 // Purpose: implementation of classes in wx/rearrangectrl.h
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
27 #include "wx/button.h"
28 #include "wx/stattext.h"
32 #include "wx/rearrangectrl.h"
34 // ============================================================================
35 // wxRearrangeList implementation
36 // ============================================================================
39 WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeListNameStr
[] = "wxRearrangeList";
41 BEGIN_EVENT_TABLE(wxRearrangeList
, wxCheckListBox
)
42 EVT_CHECKLISTBOX(wxID_ANY
, wxRearrangeList::OnCheck
)
45 bool wxRearrangeList::Create(wxWindow
*parent
,
49 const wxArrayInt
& order
,
50 const wxArrayString
& items
,
52 const wxValidator
& validator
,
55 // construct the array of items in the order in which they should appear in
57 const size_t count
= items
.size();
58 wxCHECK_MSG( order
.size() == count
, false, "arrays not in sync" );
60 wxArrayString itemsInOrder
;
61 itemsInOrder
.reserve(count
);
63 for ( n
= 0; n
< count
; n
++ )
68 itemsInOrder
.push_back(items
[idx
]);
71 // do create the real control
72 if ( !wxCheckListBox::Create(parent
, id
, pos
, size
, itemsInOrder
,
73 style
, validator
, name
) )
76 // and now check all the items which should be initially checked
77 for ( n
= 0; n
< count
; n
++ )
88 bool wxRearrangeList::CanMoveCurrentUp() const
90 const int sel
= GetSelection();
91 return sel
!= wxNOT_FOUND
&& sel
!= 0;
94 bool wxRearrangeList::CanMoveCurrentDown() const
96 const int sel
= GetSelection();
97 return sel
!= wxNOT_FOUND
&& static_cast<unsigned>(sel
) != GetCount() - 1;
100 bool wxRearrangeList::MoveCurrentUp()
102 const int sel
= GetSelection();
103 if ( sel
== wxNOT_FOUND
|| sel
== 0 )
107 SetSelection(sel
- 1);
112 bool wxRearrangeList::MoveCurrentDown()
114 const int sel
= GetSelection();
115 if ( sel
== wxNOT_FOUND
|| static_cast<unsigned>(sel
) == GetCount() - 1 )
119 SetSelection(sel
+ 1);
124 void wxRearrangeList::Swap(int pos1
, int pos2
)
126 wxSwap(m_order
[pos1
], m_order
[pos2
]);
128 const wxString stringTmp
= GetString(pos1
);
129 const bool checkedTmp
= IsChecked(pos1
);
131 SetString(pos1
, GetString(pos2
));
132 Check(pos1
, IsChecked(pos2
));
134 SetString(pos2
, stringTmp
);
135 Check(pos2
, checkedTmp
);
138 void wxRearrangeList::OnCheck(wxCommandEvent
& event
)
140 // update the internal state to match the new item state
141 const int n
= event
.GetInt();
143 m_order
[n
] = ~m_order
[n
];
145 wxASSERT_MSG( (m_order
[n
] >= 0) == IsChecked(n
),
146 "discrepancy between internal state and GUI" );
149 // ============================================================================
150 // wxRearrangeCtrl implementation
151 // ============================================================================
153 BEGIN_EVENT_TABLE(wxRearrangeCtrl
, wxPanel
)
154 EVT_UPDATE_UI(wxID_UP
, wxRearrangeCtrl::OnUpdateButtonUI
)
155 EVT_UPDATE_UI(wxID_DOWN
, wxRearrangeCtrl::OnUpdateButtonUI
)
157 EVT_BUTTON(wxID_UP
, wxRearrangeCtrl::OnButton
)
158 EVT_BUTTON(wxID_DOWN
, wxRearrangeCtrl::OnButton
)
161 void wxRearrangeCtrl::Init()
167 wxRearrangeCtrl::Create(wxWindow
*parent
,
171 const wxArrayInt
& order
,
172 const wxArrayString
& items
,
174 const wxValidator
& validator
,
175 const wxString
& name
)
177 // create all the windows
178 if ( !wxPanel::Create(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
) )
181 m_list
= new wxRearrangeList(this, wxID_ANY
,
182 wxDefaultPosition
, wxDefaultSize
,
185 wxButton
* const btnUp
= new wxButton(this, wxID_UP
);
186 wxButton
* const btnDown
= new wxButton(this, wxID_DOWN
);
188 // arrange them in a sizer
189 wxSizer
* const sizerBtns
= new wxBoxSizer(wxVERTICAL
);
190 sizerBtns
->Add(btnUp
, wxSizerFlags().Centre().Border(wxBOTTOM
));
191 sizerBtns
->Add(btnDown
, wxSizerFlags().Centre().Border(wxTOP
));
193 wxSizer
* const sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
194 sizerTop
->Add(m_list
, wxSizerFlags(1).Expand().Border(wxRIGHT
));
195 sizerTop
->Add(sizerBtns
, wxSizerFlags(0).Centre().Border(wxLEFT
));
203 void wxRearrangeCtrl::OnUpdateButtonUI(wxUpdateUIEvent
& event
)
205 event
.Enable( event
.GetId() == wxID_UP
? m_list
->CanMoveCurrentUp()
206 : m_list
->CanMoveCurrentDown() );
209 void wxRearrangeCtrl::OnButton(wxCommandEvent
& event
)
211 if ( event
.GetId() == wxID_UP
)
212 m_list
->MoveCurrentUp();
214 m_list
->MoveCurrentDown();
217 // ============================================================================
218 // wxRearrangeDialog implementation
219 // ============================================================================
222 WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeDialogNameStr
[] = "wxRearrangeDlg";
224 wxRearrangeDialog::wxRearrangeDialog(wxWindow
*parent
,
225 const wxString
& message
,
226 const wxString
& title
,
227 const wxArrayInt
& order
,
228 const wxArrayString
& items
,
230 const wxString
& name
)
231 : wxDialog(parent
, wxID_ANY
, title
,
233 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
,
236 m_ctrl
= new wxRearrangeCtrl(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
239 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
240 sizerTop
->Add(new wxStaticText(this, wxID_ANY
, message
),
241 wxSizerFlags().DoubleBorder());
242 sizerTop
->Add(m_ctrl
,
243 wxSizerFlags(1).Expand().Border());
244 sizerTop
->Add(CreateSeparatedButtonSizer(wxOK
| wxCANCEL
),
245 wxSizerFlags().Expand().Border());
246 SetSizerAndFit(sizerTop
);