KiCad PCB EDA Suite
confirm.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2007 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 1992-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #include <wx/stockitem.h>
31 #include <wx/richmsgdlg.h>
32 
33 #include <confirm.h>
34 #include <bitmaps.h>
35 #include <html_messagebox.h>
36 #include <dialog_exit_base.h>
37 
38 #include <functional>
39 
40 
42 {
43 public:
44  DIALOG_EXIT( wxWindow *aParent, const wxString& aMessage ) :
45  DIALOG_EXIT_BASE( aParent )
46  {
47  m_bitmap->SetBitmap( KiBitmap( dialog_warning_xpm ) );
48 
49  if( !aMessage.IsEmpty() )
50  m_TextInfo->SetLabel( aMessage );
51 
52  GetSizer()->Fit( this );
53  GetSizer()->SetSizeHints( this );
54  };
55 
56 private:
57  void OnSaveAndExit( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
58  void OnExitNoSave( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
59 };
60 
61 
62 int DisplayExitDialog( wxWindow* parent, const wxString& aMessage )
63 {
64  DIALOG_EXIT dlg( parent, aMessage );
65 
66  int ret = dlg.ShowModal();
67 
68  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
69  return ret;
70 }
71 
72 
73 // DisplayError should be deprecated, use DisplayErrorMessage instead
74 void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
75 {
76  wxMessageDialog* dialog;
77 
78  int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
79 
80  dialog = new wxMessageDialog( parent, text, _( "Warning" ),
81  wxOK | wxCENTRE | wxRESIZE_BORDER | icon );
82 
83  dialog->ShowModal();
84  dialog->Destroy();
85 }
86 
87 
88 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
89 {
90  wxRichMessageDialog* dlg;
91 
92  dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
93  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR );
94 
95  if( !aExtraInfo.IsEmpty() )
96  {
97  dlg->ShowDetailedText( aExtraInfo );
98  }
99 
100  dlg->ShowModal();
101  dlg->Destroy();
102 }
103 
104 
105 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
106 {
107  wxRichMessageDialog* dlg;
108 
109  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
110  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_INFORMATION );
111 
112  if( !aExtraInfo.IsEmpty() )
113  {
114  dlg->ShowDetailedText( aExtraInfo );
115  }
116 
117  dlg->ShowModal();
118  dlg->Destroy();
119 }
120 
121 
122 bool IsOK( wxWindow* aParent, const wxString& aMessage )
123 {
124  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
125  wxYES_NO | wxCENTRE | wxICON_QUESTION );
126 
127  return dlg.ShowModal() == wxID_YES;
128 }
129 
130 
132 {
133 public:
134  DIALOG_YES_NO_CANCEL( wxWindow *aParent,
135  const wxString& aPrimaryMessage,
136  const wxString& aSecondaryMessage = wxEmptyString,
137  const wxString& aYesButtonText = wxEmptyString,
138  const wxString& aNoButtonText = wxEmptyString,
139  const wxString& aCancelButtonText = wxEmptyString ) :
140  DIALOG_EXIT( aParent, aSecondaryMessage )
141  {
142  m_TextInfo->SetLabel( aPrimaryMessage );
143 
144  if( aSecondaryMessage.IsEmpty() )
145  m_staticText2->Hide();
146 
147  m_buttonSaveAndExit->SetLabel( aYesButtonText.IsEmpty() ? wxGetStockLabel( wxID_YES ) :
148  aYesButtonText );
149  m_buttonExitNoSave->SetLabel( aNoButtonText.IsEmpty() ? wxGetStockLabel( wxID_NO ) :
150  aNoButtonText );
151  m_buttonCancel->SetLabel( aCancelButtonText.IsEmpty() ? wxGetStockLabel( wxID_CANCEL ) :
152  aCancelButtonText );
153  GetSizer()->Fit( this );
154  GetSizer()->SetSizeHints( this );
155  };
156 };
157 
158 
159 int YesNoCancelDialog( wxWindow* aParent,
160  const wxString& aPrimaryMessage,
161  const wxString& aSecondaryMessage,
162  const wxString& aYesButtonText,
163  const wxString& aNoButtonText,
164  const wxString& aCancelButtonText )
165 {
166  DIALOG_YES_NO_CANCEL dlg( aParent, aPrimaryMessage, aSecondaryMessage,
167  aYesButtonText, aNoButtonText, aCancelButtonText );
168 
169  return dlg.ShowModal();
170 }
171 
172 
173 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage, const wxArrayString& aOptions )
174 {
175  int ret = -1;
176  wxDialog* dlg = new wxDialog( aParent, wxID_ANY, aTitle );
177 
178  wxBoxSizer* boxSizer = new wxBoxSizer( wxVERTICAL );
179 
180  if( !aMessage.IsEmpty() )
181  boxSizer->Add( new wxStaticText( dlg, wxID_ANY, aMessage ), 0, wxEXPAND | wxALL, 5 );
182 
183  std::vector<wxRadioButton*> radioButtons;
184  radioButtons.reserve( aOptions.Count() );
185 
186  for( const wxString& option : aOptions )
187  {
188  radioButtons.emplace_back( new wxRadioButton( dlg, wxID_ANY, _( option ) ) );
189  boxSizer->Add( radioButtons.back(), 0, wxEXPAND | wxALL, 5 );
190  }
191 
192  wxStdDialogButtonSizer* m_sdboxSizer = new wxStdDialogButtonSizer();
193  m_sdboxSizer->AddButton( new wxButton( dlg, wxID_OK ) );
194  m_sdboxSizer->AddButton( new wxButton( dlg, wxID_CANCEL ) );
195  m_sdboxSizer->Realize();
196  boxSizer->Add( m_sdboxSizer, 1, wxEXPAND | wxALL, 5 );
197 
198  dlg->SetSizer( boxSizer );
199  dlg->Layout();
200  boxSizer->Fit( dlg );
201  boxSizer->SetSizeHints( dlg );
202  dlg->Centre( wxBOTH );
203 
204  if( dlg->ShowModal() == wxID_OK )
205  {
206  for( unsigned int i = 0; i < radioButtons.size(); ++i )
207  {
208  if( radioButtons[i]->GetValue() )
209  {
210  ret = i;
211  break;
212  }
213  }
214  }
215  else
216  {
217  ret = -1;
218  }
219 
220  dlg->Destroy();
221 
222  return ret;
223 }
224 
225 
226 class DIALOG_MULTI_OPTIONS : public wxDialog
227 {
228 public:
229  DIALOG_MULTI_OPTIONS( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage,
230  const wxArrayString& aOptions )
231  : wxDialog( aParent, wxID_ANY, aTitle )
232  {
233  SetSizeHints( wxDefaultSize, wxDefaultSize );
234 
235  wxBoxSizer* boxSizer = new wxBoxSizer( wxVERTICAL );
236 
237  if( !aMessage.IsEmpty() )
238  boxSizer->Add( new wxStaticText( this, wxID_ANY, aMessage ), 0, wxEXPAND | wxALL, 5 );
239 
240  m_checklist = new wxCheckListBox( this, wxID_ANY );
241 
242  for( const wxString& option : aOptions )
243  m_checklist->Append( option );
244 
245  boxSizer->Add( m_checklist, 1, wxEXPAND | wxALL, 5 );
246 
247  wxBoxSizer* btnSizer = new wxBoxSizer( wxHORIZONTAL );
248  wxButton* selectAll = new wxButton( this, wxID_ANY, _( "Select All" ) );
249  btnSizer->Add( selectAll, 1, wxEXPAND | wxALL, 5 );
250  wxButton* unselectAll = new wxButton( this, wxID_ANY, _( "Unselect All" ) );
251  btnSizer->Add( unselectAll, 1, wxEXPAND | wxALL, 5 );
252  boxSizer->Add( btnSizer, 0, wxEXPAND | wxALL, 5 );
253 
254  wxStdDialogButtonSizer* m_sdboxSizer = new wxStdDialogButtonSizer();
255  m_sdboxSizer->AddButton( new wxButton( this, wxID_OK ) );
256  m_sdboxSizer->AddButton( new wxButton( this, wxID_CANCEL ) );
257  m_sdboxSizer->Realize();
258  boxSizer->Add( m_sdboxSizer, 0, wxEXPAND | wxALL, 5 );
259 
260  SetSizer( boxSizer );
261  Layout();
262  boxSizer->Fit( this );
263  boxSizer->SetSizeHints( this );
264  Centre( wxBOTH );
265 
266  selectAll->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MULTI_OPTIONS::selectAll, this );
267  unselectAll->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MULTI_OPTIONS::unselectAll, this );
268  }
269 
270  std::vector<int> GetSelection() const
271  {
272  std::vector<int> ret;
273 
274  for( unsigned int i = 0; i < m_checklist->GetCount(); ++i )
275  {
276  if( m_checklist->IsChecked( i ) )
277  ret.push_back( i );
278  }
279 
280  return ret;
281  }
282 
283  void SetCheckboxes( bool aValue )
284  {
285  for( unsigned int i = 0; i < m_checklist->GetCount(); ++i )
286  m_checklist->Check( i, aValue );
287  }
288 
289 protected:
290  wxCheckListBox* m_checklist;
291 
292  void selectAll( wxCommandEvent& aEvent )
293  {
294  SetCheckboxes( true );
295  }
296 
297  void unselectAll( wxCommandEvent& aEvent )
298  {
299  SetCheckboxes( false );
300  }
301 };
302 
303 
304 std::pair<bool, std::vector<int>> SelectMultipleOptions( wxWindow* aParent, const wxString& aTitle,
305  const wxString& aMessage, const wxArrayString& aOptions, bool aDefaultState )
306 {
307  std::vector<int> ret;
308  bool clickedOk;
309  DIALOG_MULTI_OPTIONS dlg( aParent, aTitle, aMessage, aOptions );
310  dlg.SetCheckboxes( aDefaultState );
311 
312  if( dlg.ShowModal() == wxID_OK )
313  {
314  ret = dlg.GetSelection();
315  clickedOk = true;
316  }
317  else
318  {
319  clickedOk = false;
320  }
321 
322  return std::make_pair( clickedOk, ret );
323 }
324 
325 
326 std::pair<bool, std::vector<int>> SelectMultipleOptions( wxWindow* aParent, const wxString& aTitle,
327  const wxString& aMessage, const std::vector<std::string>& aOptions, bool aDefaultState )
328 {
329  wxArrayString array;
330 
331  for( const auto& option : aOptions )
332  array.Add( option );
333 
334  return SelectMultipleOptions( aParent, aTitle, aMessage, array, aDefaultState );
335 }
void unselectAll(wxCommandEvent &aEvent)
Definition: confirm.cpp:297
DIALOG_EXIT(wxWindow *aParent, const wxString &aMessage)
Definition: confirm.cpp:44
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:88
This file is part of the common library.
DIALOG_MULTI_OPTIONS(wxWindow *aParent, const wxString &aTitle, const wxString &aMessage, const wxArrayString &aOptions)
Definition: confirm.cpp:229
DIALOG_YES_NO_CANCEL(wxWindow *aParent, const wxString &aPrimaryMessage, const wxString &aSecondaryMessage=wxEmptyString, const wxString &aYesButtonText=wxEmptyString, const wxString &aNoButtonText=wxEmptyString, const wxString &aCancelButtonText=wxEmptyString)
Definition: confirm.cpp:134
void OnExitNoSave(wxCommandEvent &event) override
Definition: confirm.cpp:58
wxStaticText * m_TextInfo
void SetCheckboxes(bool aValue)
Definition: confirm.cpp:283
Class DIALOG_EXIT_BASE.
wxButton * m_buttonSaveAndExit
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:78
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
wxCheckListBox * m_checklist
Definition: confirm.cpp:290
wxButton * m_buttonCancel
wxStaticBitmap * m_bitmap
int DisplayExitDialog(wxWindow *parent, const wxString &aMessage)
Function DisplayExitDialog displays a dialog with 3 buttons: Save and Exit Cancel Exit without save...
Definition: confirm.cpp:62
std::pair< bool, std::vector< int > > SelectMultipleOptions(wxWindow *aParent, const wxString &aTitle, const wxString &aMessage, const wxArrayString &aOptions, bool aDefaultState)
Displays a dialog with checkboxes asking the user to select one or more options.
Definition: confirm.cpp:304
wxButton * m_buttonExitNoSave
void selectAll(wxCommandEvent &aEvent)
Definition: confirm.cpp:292
int SelectSingleOption(wxWindow *aParent, const wxString &aTitle, const wxString &aMessage, const wxArrayString &aOptions)
Displays a dialog with radioboxes asking the user to select an option.
Definition: confirm.cpp:173
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:105
std::vector< int > GetSelection() const
Definition: confirm.cpp:270
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:74
int YesNoCancelDialog(wxWindow *aParent, const wxString &aPrimaryMessage, const wxString &aSecondaryMessage, const wxString &aYesButtonText, const wxString &aNoButtonText, const wxString &aCancelButtonText)
Function YesNoCancelDialog displays a yes/no/cancel dialog with aMessage and returns the user respons...
Definition: confirm.cpp:159
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:122
wxStaticText * m_staticText2
void OnSaveAndExit(wxCommandEvent &event) override
Definition: confirm.cpp:57