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 #include <unordered_map>
40 
41 // Set of dialogs that have been chosen not to be shown again
42 static std::unordered_map<unsigned long, int> doNotShowAgainDlgs;
43 
44 
45 KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage,
46  const wxString& aCaption, long aStyle )
47  : wxRichMessageDialog( aParent, aMessage, aCaption, aStyle | wxCENTRE )
48 {
49  setHash();
50 }
51 
52 
53 KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage,
54  KD_TYPE aType, const wxString& aCaption )
55  : wxRichMessageDialog( aParent, aMessage, getCaption( aType, aCaption ), getStyle( aType ) )
56 {
57  setHash();
58 }
59 
60 
62 {
63  return doNotShowAgainDlgs.count( m_hash ) > 0;
64 }
65 
66 
68 {
69  doNotShowAgainDlgs.erase( m_hash );
70 }
71 
72 
73 bool KIDIALOG::Show( bool aShow )
74 {
75  // Check if this dialog should be shown to the user
76  auto it = doNotShowAgainDlgs.find( m_hash );
77 
78  if( it != doNotShowAgainDlgs.end() )
79  return it->second;
80 
81  bool ret = wxRichMessageDialog::Show();
82 
83  // Has the user asked not to show the dialog again
84  if( IsCheckBoxChecked() )
86 
87  return ret;
88 }
89 
90 
92 {
93  // Check if this dialog should be shown to the user
94  auto it = doNotShowAgainDlgs.find( m_hash );
95 
96  if( it != doNotShowAgainDlgs.end() )
97  return it->second;
98 
99  int ret = wxRichMessageDialog::ShowModal();
100 
101  // Has the user asked not to show the dialog again
102  if( IsCheckBoxChecked() )
103  doNotShowAgainDlgs[m_hash] = ret;
104 
105  return ret;
106 }
107 
108 
110 {
111  std::size_t h1 = std::hash<wxString>{}( GetMessage() );
112  std::size_t h2 = std::hash<wxString>{}( GetTitle() );
113  m_hash = h1 ^ ( h2 << 1 );
114 }
115 
116 
117 wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
118 {
119  if( !aCaption.IsEmpty() )
120  return aCaption;
121 
122  switch( aType )
123  {
124  case KD_NONE: /* fall through */
125  case KD_INFO: return _( "Message" );
126  case KD_QUESTION: return _( "Question" );
127  case KD_WARNING: return _( "Warning" );
128  case KD_ERROR: return _( "Error" );
129  }
130 
131  return wxEmptyString;
132 }
133 
134 
136 {
137  long style = wxOK | wxCENTRE;
138 
139  switch( aType )
140  {
141  case KD_NONE: break;
142  case KD_INFO: style |= wxICON_INFORMATION; break;
143  case KD_QUESTION: style |= wxICON_QUESTION; break;
144  case KD_WARNING: style |= wxICON_WARNING; break;
145  case KD_ERROR: style |= wxICON_ERROR; break;
146  }
147 
148  return style;
149 }
150 
151 
153 {
154 public:
155  DIALOG_EXIT( wxWindow *aParent, const wxString& aMessage ) :
156  DIALOG_EXIT_BASE( aParent )
157  {
158  m_bitmap->SetBitmap( KiBitmap( dialog_warning_xpm ) );
159 
160  if( !aMessage.IsEmpty() )
161  m_TextInfo->SetLabel( aMessage );
162 
163  GetSizer()->Fit( this );
164  GetSizer()->SetSizeHints( this );
165  };
166 
167 private:
168  void OnSaveAndExit( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
169  void OnExitNoSave( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
170 };
171 
172 
173 int DisplayExitDialog( wxWindow* parent, const wxString& aMessage )
174 {
175  DIALOG_EXIT dlg( parent, aMessage );
176 
177  int ret = dlg.ShowModal();
178 
179  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
180  return ret;
181 }
182 
183 
184 // DisplayError should be deprecated, use DisplayErrorMessage instead
185 void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
186 {
187  wxMessageDialog* dialog;
188 
189  int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
190 
191  dialog = new wxMessageDialog( parent, text, _( "Warning" ),
192  wxOK | wxCENTRE | wxRESIZE_BORDER | icon );
193 
194  dialog->ShowModal();
195  dialog->Destroy();
196 }
197 
198 
199 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
200 {
201  wxRichMessageDialog* dlg;
202 
203  dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
204  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR );
205 
206  if( !aExtraInfo.IsEmpty() )
207  {
208  dlg->ShowDetailedText( aExtraInfo );
209  }
210 
211  dlg->ShowModal();
212  dlg->Destroy();
213 }
214 
215 
216 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
217 {
218  wxRichMessageDialog* dlg;
219 
220  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
221  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_INFORMATION );
222 
223  if( !aExtraInfo.IsEmpty() )
224  {
225  dlg->ShowDetailedText( aExtraInfo );
226  }
227 
228  dlg->ShowModal();
229  dlg->Destroy();
230 }
231 
232 
233 bool IsOK( wxWindow* aParent, const wxString& aMessage )
234 {
235  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
236  wxYES_NO | wxCENTRE | wxICON_QUESTION );
237 
238  return dlg.ShowModal() == wxID_YES;
239 }
240 
241 
243 {
244 public:
245  DIALOG_YES_NO_CANCEL( wxWindow *aParent,
246  const wxString& aPrimaryMessage,
247  const wxString& aSecondaryMessage = wxEmptyString,
248  const wxString& aYesButtonText = wxEmptyString,
249  const wxString& aNoButtonText = wxEmptyString,
250  const wxString& aCancelButtonText = wxEmptyString ) :
251  DIALOG_EXIT( aParent, aSecondaryMessage )
252  {
253  m_TextInfo->SetLabel( aPrimaryMessage );
254 
255  if( aSecondaryMessage.IsEmpty() )
256  m_staticText2->Hide();
257 
258  m_buttonSaveAndExit->SetLabel( aYesButtonText.IsEmpty() ? wxGetStockLabel( wxID_YES ) :
259  aYesButtonText );
260  m_buttonExitNoSave->SetLabel( aNoButtonText.IsEmpty() ? wxGetStockLabel( wxID_NO ) :
261  aNoButtonText );
262  m_buttonCancel->SetLabel( aCancelButtonText.IsEmpty() ? wxGetStockLabel( wxID_CANCEL ) :
263  aCancelButtonText );
264  GetSizer()->Fit( this );
265  GetSizer()->SetSizeHints( this );
266  };
267 };
268 
269 
270 int YesNoCancelDialog( wxWindow* aParent,
271  const wxString& aPrimaryMessage,
272  const wxString& aSecondaryMessage,
273  const wxString& aYesButtonText,
274  const wxString& aNoButtonText,
275  const wxString& aCancelButtonText )
276 {
277  DIALOG_YES_NO_CANCEL dlg( aParent, aPrimaryMessage, aSecondaryMessage,
278  aYesButtonText, aNoButtonText, aCancelButtonText );
279 
280  return dlg.ShowModal();
281 }
282 
283 
284 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage, const wxArrayString& aOptions )
285 {
286  wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
287 
288  if( dlg.ShowModal() != wxID_OK )
289  return -1;
290 
291  return dlg.GetSelection();
292 }
293 
294 
295 class DIALOG_MULTI_OPTIONS : public wxMultiChoiceDialog
296 {
297 public:
298  DIALOG_MULTI_OPTIONS( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage,
299  const wxArrayString& aOptions )
300  : wxMultiChoiceDialog( aParent, aMessage, aTitle, aOptions ),
301  m_optionsCount( aOptions.GetCount() )
302  {
303  wxBoxSizer* btnSizer = new wxBoxSizer( wxHORIZONTAL );
304  wxButton* selectAll = new wxButton( this, wxID_ANY, _( "Select All" ) );
305  btnSizer->Add( selectAll, 1, wxEXPAND | wxALL, 5 );
306  wxButton* unselectAll = new wxButton( this, wxID_ANY, _( "Unselect All" ) );
307  btnSizer->Add( unselectAll, 1, wxEXPAND | wxALL, 5 );
308  auto sizer = GetSizer();
309  sizer->Insert( sizer->GetItemCount() - 1, btnSizer, 0, wxEXPAND | wxALL, 0 );
310 
311  Layout();
312  sizer->Fit( this );
313  sizer->SetSizeHints( this );
314  Centre( wxBOTH );
315 
316  selectAll->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MULTI_OPTIONS::selectAll, this );
317  unselectAll->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MULTI_OPTIONS::unselectAll, this );
318  }
319 
320  void SetCheckboxes( bool aValue )
321  {
322  wxArrayInt selIdxs;
323 
324  if( aValue ) // select all indices
325  {
326  for( int i = 0; i < m_optionsCount; ++i )
327  selIdxs.Add( i );
328  }
329 
330  SetSelections( selIdxs );
331  }
332 
333 protected:
336 
337  void selectAll( wxCommandEvent& aEvent )
338  {
339  SetCheckboxes( true );
340  }
341 
342  void unselectAll( wxCommandEvent& aEvent )
343  {
344  SetCheckboxes( false );
345  }
346 };
347 
348 
349 std::pair<bool, wxArrayInt> SelectMultipleOptions( wxWindow* aParent, const wxString& aTitle,
350  const wxString& aMessage, const wxArrayString& aOptions, bool aDefaultState )
351 {
352  DIALOG_MULTI_OPTIONS dlg( aParent, aTitle, aMessage, aOptions );
353  dlg.Layout();
354  dlg.SetCheckboxes( aDefaultState );
355 
356  wxArrayInt ret;
357  bool clickedOk = ( dlg.ShowModal() == wxID_OK );
358 
359  if( clickedOk )
360  ret = dlg.GetSelections();
361 
362  return std::make_pair( clickedOk, ret );
363 }
364 
365 
366 std::pair<bool, wxArrayInt> SelectMultipleOptions( wxWindow* aParent, const wxString& aTitle,
367  const wxString& aMessage, const std::vector<std::string>& aOptions, bool aDefaultState )
368 {
369  wxArrayString array;
370 
371  for( const auto& option : aOptions )
372  array.Add( option );
373 
374  return SelectMultipleOptions( aParent, aTitle, aMessage, array, aDefaultState );
375 }
void unselectAll(wxCommandEvent &aEvent)
Definition: confirm.cpp:342
DIALOG_EXIT(wxWindow *aParent, const wxString &aMessage)
Definition: confirm.cpp:155
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:199
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:298
KD_TYPE
Dialog type. Selects appropriate icon and default dialog title
Definition: confirm.h:47
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:245
void OnExitNoSave(wxCommandEvent &event) override
Definition: confirm.cpp:169
void SetCheckboxes(bool aValue)
Definition: confirm.cpp:320
void setHash()
Sets the dialog hash value
Definition: confirm.cpp:109
std::pair< bool, wxArrayInt > 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:349
Class DIALOG_EXIT_BASE.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:78
static std::unordered_map< unsigned long, int > doNotShowAgainDlgs
Definition: confirm.cpp:42
static wxString getCaption(KD_TYPE aType, const wxString &aCaption)
Definition: confirm.cpp:117
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
bool DoNotShowAgain() const
Checks the &#39;do not show again&#39; setting for the dialog
Definition: confirm.cpp:61
bool Show(bool aShow=true) override
Definition: confirm.cpp:73
static long getStyle(KD_TYPE aType)
Definition: confirm.cpp:135
void ForceShowAgain()
Definition: confirm.cpp:67
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:173
size_t i
Definition: json11.cpp:597
KIDIALOG(wxWindow *aParent, const wxString &aMessage, const wxString &aCaption, long aStyle=wxOK)
Definition: confirm.cpp:45
int ShowModal() override
Definition: confirm.cpp:91
int m_optionsCount
Number of displayed options
Definition: confirm.cpp:335
void selectAll(wxCommandEvent &aEvent)
Definition: confirm.cpp:337
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:284
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:216
unsigned long m_hash
Unique identifier of the dialog
Definition: confirm.h:70
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:185
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:270
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:233
void OnSaveAndExit(wxCommandEvent &event) override
Definition: confirm.cpp:168