1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 11846 $
6  * $Id: editarrayorderdlg.cpp 11846 2019-09-08 22:37:55Z fuscated $
7  * $HeadURL: svn://svn.code.sf.net/p/codeblocks/code/branches/release-20.xx/src/sdk/editarrayorderdlg.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13     #include <wx/xrc/xmlres.h>
14     #include <wx/intl.h>
15     #include <wx/button.h>
16     #include <wx/listbox.h>
17 #endif
18 
19 #include "editarrayorderdlg.h" // class's header file
20 
BEGIN_EVENT_TABLE(EditArrayOrderDlg,wxScrollingDialog)21 BEGIN_EVENT_TABLE(EditArrayOrderDlg, wxScrollingDialog)
22     EVT_UPDATE_UI( -1, EditArrayOrderDlg::OnUpdateUI)
23     EVT_BUTTON(XRCID("btnMoveUp"), EditArrayOrderDlg::OnMoveUp)
24     EVT_BUTTON(XRCID("btnMoveDown"), EditArrayOrderDlg::OnMoveDown)
25 END_EVENT_TABLE()
26 
27 // class constructor
28 EditArrayOrderDlg::EditArrayOrderDlg(wxWindow* parent, const wxArrayString& array)
29     : m_Array(array)
30 {
31     wxXmlResource::Get()->LoadObject(this, parent, _T("dlgEditArrayOrder"),_T("wxScrollingDialog"));
32     DoFillList();
33 
34     XRCCTRL(*this, "wxID_OK", wxButton)->SetDefault();
35     wxListBox* list = XRCCTRL(*this, "lstItems", wxListBox);
36     list->SetFocus();
37 }
38 
39 // class destructor
~EditArrayOrderDlg()40 EditArrayOrderDlg::~EditArrayOrderDlg()
41 {
42 }
43 
DoFillList()44 void EditArrayOrderDlg::DoFillList()
45 {
46     wxListBox* list = XRCCTRL(*this, "lstItems", wxListBox);
47     list->Clear();
48     for (unsigned int i = 0; i < m_Array.GetCount(); ++i)
49         list->Append(m_Array[i]);
50 }
51 
OnUpdateUI(wxUpdateUIEvent & WXUNUSED (event))52 void EditArrayOrderDlg::OnUpdateUI(wxUpdateUIEvent& WXUNUSED(event))
53 {
54     wxListBox* list = XRCCTRL(*this, "lstItems", wxListBox);
55 
56     XRCCTRL(*this, "btnMoveUp", wxButton)->Enable(list->GetSelection() > 0);
57     XRCCTRL(*this, "btnMoveDown", wxButton)->Enable(list->GetSelection() >= 0 && list->GetSelection() < (int)list->GetCount() - 1);
58 }
59 
OnMoveUp(wxCommandEvent & WXUNUSED (event))60 void EditArrayOrderDlg::OnMoveUp(wxCommandEvent& WXUNUSED(event))
61 {
62     wxListBox* list = XRCCTRL(*this, "lstItems", wxListBox);
63     int sel = list->GetSelection();
64 
65     if (sel > 0)
66     {
67         wxString tmp = list->GetString(sel);
68         list->Delete(sel);
69         list->InsertItems(1, &tmp, sel - 1);
70         list->SetSelection(sel - 1);
71     }
72 }
73 
OnMoveDown(wxCommandEvent & WXUNUSED (event))74 void EditArrayOrderDlg::OnMoveDown(wxCommandEvent& WXUNUSED(event))
75 {
76     wxListBox* list = XRCCTRL(*this, "lstItems", wxListBox);
77     int sel = list->GetSelection();
78 
79     if (sel < (int)list->GetCount() - 1)
80     {
81         wxString tmp = list->GetString(sel);
82         list->Delete(sel);
83         list->InsertItems(1, &tmp, sel + 1);
84         list->SetSelection(sel + 1);
85     }
86 }
87 
EndModal(int retCode)88 void EditArrayOrderDlg::EndModal(int retCode)
89 {
90     if (retCode == wxID_OK)
91     {
92         wxListBox* list = XRCCTRL(*this, "lstItems", wxListBox);
93 
94         m_Array.Clear();
95         for (int i = 0; i < (int)list->GetCount(); ++i)
96             m_Array.Add(list->GetString(i));
97     }
98 
99     wxScrollingDialog::EndModal(retCode);
100 }
101 
102