Code::Blocks  SVN r11506
multiselectdlg.cpp
Go to the documentation of this file.
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: 11366 $
6  * $Id: multiselectdlg.cpp 11366 2018-04-12 07:02:45Z fuscated $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/multiselectdlg.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include <wx/button.h>
14  #include <wx/checklst.h>
15  #include <wx/msgdlg.h>
16  #include <wx/stattext.h>
17  #include <wx/xrc/xmlres.h>
18  #include "globals.h"
19 #endif
20 
21 #include "multiselectdlg.h"
22 
23 BEGIN_EVENT_TABLE(MultiSelectDlg, wxScrollingDialog)
24  EVT_CHECKLISTBOX(XRCID("lstItems"), MultiSelectDlg::OnItemToggle)
25  EVT_BUTTON(XRCID("btnSelectWild"), MultiSelectDlg::OnWildcard)
26  EVT_BUTTON(XRCID("btnToggle"), MultiSelectDlg::OnToggle)
27  EVT_BUTTON(XRCID("btnSelectAll"), MultiSelectDlg::OnSelectAll)
28  EVT_BUTTON(XRCID("btnDeselectAll"), MultiSelectDlg::OnDeselectAll)
29 END_EVENT_TABLE()
30 
32  const wxArrayString& items,
33  const wxString& wildcard,
34  const wxString& label,
35  const wxString& title)
36 {
37  //ctor
38  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgGenericMultiSelect"),_T("wxScrollingDialog"));
39 
40  SetTitle(title);
41  XRCCTRL(*this, "lblLabel", wxStaticText)->SetLabel(label);
42  Init(items, wildcard);
43 
44  wxButton *buttonOK = XRCCTRL(*this, "wxID_OK", wxButton);
45  buttonOK->SetDefault();
46  buttonOK->SetFocus();
47 }
48 
50  const wxArrayString& items,
51  bool selectall,
52  const wxString& label,
53  const wxString& title)
54 {
55  //ctor
56  wxXmlResource::Get()->LoadObject(this, parent, _T("dlgGenericMultiSelect"),_T("wxScrollingDialog"));
57  XRCCTRL(*this, "wxID_OK", wxButton)->SetDefault();
58 
59  SetTitle(title);
60  XRCCTRL(*this, "lblLabel", wxStaticText)->SetLabel(label);
61  Init(items, selectall ? _T("*") : _T(""));
62 
63  wxButton *buttonOK = XRCCTRL(*this, "wxID_OK", wxButton);
64  buttonOK->SetDefault();
65  buttonOK->SetFocus();
66 }
67 
69 {
70  //dtor
71 }
72 
73 void MultiSelectDlg::Init(const wxArrayString& items, const wxString& wildcard)
74 {
75  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
76  for (size_t i = 0; i < items.GetCount(); ++i)
77  lst->Append(items[i]);
78 
79  SelectWildCard(wildcard);
80 }
81 
83 {
84  int count = 0;
85  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
86  for (size_t i = 0; i < lst->GetCount(); ++i)
87  {
88  if (lst->IsChecked(i))
89  ++count;
90  }
91  wxString msg;
92  msg << _("Selected: ") << wxString::Format(_T("%d"), count);
93  XRCCTRL(*this, "lblStatus", wxStaticText)->SetLabel(msg);
94 }
95 
97 {
98  wxArrayString ret;
99  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
100  for (size_t i = 0; i < lst->GetCount(); ++i)
101  {
102  if (lst->IsChecked(i))
103  ret.Add(lst->GetString(i));
104  }
105  return ret;
106 }
107 
109 {
110  wxArrayInt ret;
111  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
112  for (size_t i = 0; i < lst->GetCount(); ++i)
113  {
114  if (lst->IsChecked(i))
115  ret.Add(i);
116  }
117  return ret;
118 }
119 
120 void MultiSelectDlg::SelectWildCard(const wxString& wild, bool select, bool clearOld)
121 {
122  if (wild.IsEmpty())
123  return;
124  wxArrayString wilds = GetArrayFromString(wild, _T(";"));
125  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
126  for (size_t i = 0; i < lst->GetCount(); ++i)
127  {
128  if (clearOld || !lst->IsChecked(i))
129  {
130  wxString entry = lst->GetString(i).Lower();
131  bool MatchesWildCard = false;
132  for (unsigned int x = 0; x < wilds.GetCount(); ++x)
133  {
134  if (entry.Matches(wilds[x].Lower()))
135  {
136  lst->Check(i, select);
137  MatchesWildCard = true;
138  break;
139  }
140  }
141  // did not match the wildcard and was selected ( == in the old list)
142  // and we want those to be removed (clearOld) -> uncheck
143  if (!MatchesWildCard && clearOld && lst->IsChecked(i))
144  lst->Check(i, false);
145  }
146  }
147  UpdateStatus();
148 }
149 
151 {
152  wxString wild = cbGetTextFromUser(_("Enter a selection wildcard\n(e.g. \"dlg*.cpp\" "
153  "would select all files starting with \"dlg\" and "
154  "ending in \".cpp\")\nSeparate multiple masks with \";\":"),
155  _("Wildcard selection"));
156  if (wild.IsEmpty())
157  return;
158 
159  // Do not ask to un-select before if there are no items selected
160  bool ask_clear = false;
161  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
162  for (size_t i = 0; i < lst->GetCount(); ++i)
163  {
164  if (lst->IsChecked(i))
165  {
166  ask_clear = true;
167  break;
168  }
169  }
170 
171  bool clear = false;
172  if (ask_clear)
173  clear = cbMessageBox(_("Do you want to clear the previous selections?"),
174  _("Question"),
175  wxICON_QUESTION | wxYES_NO, this) == wxID_YES;
176  SelectWildCard(wild, true, clear);
177 }
178 
180 {
181  UpdateStatus();
182 }
183 
185 {
186  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
187  for (size_t i = 0; i < lst->GetCount(); ++i)
188  lst->Check(i, !lst->IsChecked(i));
189 
190  UpdateStatus();
191 }
192 
194 {
195  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
196  for (size_t i = 0; i < lst->GetCount(); ++i)
197  lst->Check(i, true);
198 
199  UpdateStatus();
200 }
201 
203 {
204  wxCheckListBox* lst = XRCCTRL(*this, "lstItems", wxCheckListBox);
205  for (size_t i = 0; i < lst->GetCount(); ++i)
206  lst->Check(i, false);
207 
208  UpdateStatus();
209 }
wxArrayString GetSelectedStrings() const
DLLIMPORT wxArrayString GetArrayFromString(const wxString &text, const wxString &separator=DEFAULT_ARRAY_SEP, bool trimSpaces=true)
Definition: globals.cpp:134
bool Matches(const wxString &mask) const
#define wxICON_QUESTION
wxString Lower() const
void OnSelectAll(wxCommandEvent &event)
virtual wxString GetString(unsigned int n) const
DLLIMPORT wxString cbGetTextFromUser(const wxString &message, const wxString &caption=cbGetTextFromUserPromptStr, const wxString &default_value=wxEmptyString, wxWindow *parent=NULL, int x=wxDefaultCoord, int y=wxDefaultCoord, bool centre=true)
Definition: globals.cpp:1465
#define _T(string)
#define wxYES_NO
bool IsChecked(unsigned int item) const
~MultiSelectDlg() override
void OnItemToggle(wxCommandEvent &event)
MultiSelectDlg(wxWindow *parent, const wxArrayString &items, const wxString &wildcard, const wxString &label=_("Select items:"), const wxString &title=_("Multiple selection"))
wxArrayInt GetSelectedIndices() const
void Check(unsigned int item, bool check=true)
void SelectWildCard(const wxString &wild, bool select=true, bool clearOld=false)
const wxString & _(const wxString &string)
wxArray< int > wxArrayInt
void OnWildcard(wxCommandEvent &event)
bool IsEmpty() const
void Init(const wxArrayString &items, const wxString &wildcard)
void OnToggle(wxCommandEvent &event)
size_t Add(const wxString &str, size_t copies=1)
size_t GetCount() const
void OnDeselectAll(wxCommandEvent &event)
static wxXmlResource * Get()
static wxString Format(const wxString &format,...)
wxObject * LoadObject(wxWindow *parent, const wxString &name, const wxString &classname)
virtual unsigned int GetCount() const
DLLIMPORT int cbMessageBox(const wxString &message, const wxString &caption=wxEmptyString, int style=wxOK, wxWindow *parent=NULL, int x=-1, int y=-1)
wxMessageBox wrapper.
Definition: globals.cpp:1395