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"
29 #include "wx/rearrangectrl.h"
31 // ============================================================================
32 // wxRearrangeList implementation
33 // ============================================================================
36 WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeListNameStr
[] = "wxRearrangeList";
38 BEGIN_EVENT_TABLE(wxRearrangeList
, wxCheckListBox
)
39 EVT_CHECKLISTBOX(wxID_ANY
, wxRearrangeList::OnCheck
)
42 bool wxRearrangeList::Create(wxWindow
*parent
,
46 const wxArrayInt
& order
,
47 const wxArrayString
& items
,
49 const wxValidator
& validator
,
52 // construct the array of items in the order in which they should appear in
54 const size_t count
= items
.size();
55 wxCHECK_MSG( order
.size() == count
, false, "arrays not in sync" );
57 wxArrayString itemsInOrder
;
58 itemsInOrder
.reserve(count
);
60 for ( n
= 0; n
< count
; n
++ )
65 itemsInOrder
.push_back(items
[idx
]);
68 // do create the real control
69 if ( !wxCheckListBox::Create(parent
, id
, pos
, size
, itemsInOrder
,
70 style
, validator
, name
) )
73 // and now check all the items which should be initially checked
74 for ( n
= 0; n
< count
; n
++ )
85 bool wxRearrangeList::CanMoveCurrentUp() const
87 const int sel
= GetSelection();
88 return sel
!= wxNOT_FOUND
&& sel
!= 0;
91 bool wxRearrangeList::CanMoveCurrentDown() const
93 const int sel
= GetSelection();
94 return sel
!= wxNOT_FOUND
&& static_cast<unsigned>(sel
) != GetCount() - 1;
97 bool wxRearrangeList::MoveCurrentUp()
99 const int sel
= GetSelection();
100 if ( sel
== wxNOT_FOUND
|| sel
== 0 )
104 SetSelection(sel
- 1);
109 bool wxRearrangeList::MoveCurrentDown()
111 const int sel
= GetSelection();
112 if ( sel
== wxNOT_FOUND
|| static_cast<unsigned>(sel
) == GetCount() - 1 )
116 SetSelection(sel
+ 1);
121 void wxRearrangeList::Swap(int pos1
, int pos2
)
123 wxSwap(m_order
[pos1
], m_order
[pos2
]);
125 const wxString stringTmp
= GetString(pos1
);
126 const bool checkedTmp
= IsChecked(pos1
);
128 SetString(pos1
, GetString(pos2
));
129 Check(pos1
, IsChecked(pos2
));
131 SetString(pos2
, stringTmp
);
132 Check(pos2
, checkedTmp
);
135 void wxRearrangeList::OnCheck(wxCommandEvent
& event
)
137 // update the internal state to match the new item state
138 const int n
= event
.GetInt();
140 const bool checked
= IsChecked(n
);
141 wxASSERT_MSG( (m_order
[n
] >= 0) == !checked
,
142 "discrepancy between internal state and GUI" );
144 m_order
[n
] = ~m_order
[n
];
147 // ============================================================================
148 // wxRearrangeCtrl implementation
149 // ============================================================================
151 BEGIN_EVENT_TABLE(wxRearrangeCtrl
, wxPanel
)
152 EVT_UPDATE_UI(wxID_UP
, wxRearrangeCtrl::OnUpdateButtonUI
)
153 EVT_UPDATE_UI(wxID_DOWN
, wxRearrangeCtrl::OnUpdateButtonUI
)
155 EVT_BUTTON(wxID_UP
, wxRearrangeCtrl::OnButton
)
156 EVT_BUTTON(wxID_DOWN
, wxRearrangeCtrl::OnButton
)
159 void wxRearrangeCtrl::Init()
165 wxRearrangeCtrl::Create(wxWindow
*parent
,
169 const wxArrayInt
& order
,
170 const wxArrayString
& items
,
172 const wxValidator
& validator
,
173 const wxString
& name
)
175 // create all the windows
176 if ( !wxPanel::Create(parent
, id
, pos
, size
, wxTAB_TRAVERSAL
, name
) )
179 m_list
= new wxRearrangeList(this, wxID_ANY
,
180 wxDefaultPosition
, wxDefaultSize
,
183 wxButton
* const btnUp
= new wxButton(this, wxID_UP
);
184 wxButton
* const btnDown
= new wxButton(this, wxID_DOWN
);
186 // arrange them in a sizer
187 wxSizer
* const sizerBtns
= new wxBoxSizer(wxVERTICAL
);
188 sizerBtns
->Add(btnUp
, wxSizerFlags().Centre().Border(wxBOTTOM
));
189 sizerBtns
->Add(btnDown
, wxSizerFlags().Centre().Border(wxTOP
));
191 wxSizer
* const sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
192 sizerTop
->Add(m_list
, wxSizerFlags(1).Expand().Border(wxRIGHT
));
193 sizerTop
->Add(sizerBtns
, wxSizerFlags(0).Centre().Border(wxLEFT
));
201 void wxRearrangeCtrl::OnUpdateButtonUI(wxUpdateUIEvent
& event
)
203 event
.Enable( event
.GetId() == wxID_UP
? m_list
->CanMoveCurrentUp()
204 : m_list
->CanMoveCurrentDown() );
207 void wxRearrangeCtrl::OnButton(wxCommandEvent
& event
)
209 if ( event
.GetId() == wxID_UP
)
210 m_list
->MoveCurrentUp();
212 m_list
->MoveCurrentDown();
215 // ============================================================================
216 // wxRearrangeDialog implementation
217 // ============================================================================
220 WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeDialogNameStr
[] = "wxRearrangeDlg";
222 wxRearrangeDialog::wxRearrangeDialog(wxWindow
*parent
,
223 const wxString
& message
,
224 const wxString
& title
,
225 const wxArrayInt
& order
,
226 const wxArrayString
& items
,
228 const wxString
& name
)
229 : wxDialog(parent
, wxID_ANY
, title
,
231 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
,
234 m_ctrl
= new wxRearrangeCtrl(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
,
237 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
238 sizerTop
->Add(new wxStaticText(this, wxID_ANY
, message
),
239 wxSizerFlags().DoubleBorder());
240 sizerTop
->Add(m_ctrl
,
241 wxSizerFlags(1).Expand().Border());
242 sizerTop
->Add(CreateSeparatedButtonSizer(wxOK
| wxCANCEL
),
243 wxSizerFlags().Expand().Border());
244 SetSizerAndFit(sizerTop
);