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-2019 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 
25 #include <wx/stockitem.h>
26 #include <wx/richmsgdlg.h>
27 #include <wx/choicdlg.h>
28 #include <confirm.h>
30 #include <functional>
31 #include <unordered_map>
32 
33 // Set of dialogs that have been chosen not to be shown again
34 static std::unordered_map<unsigned long, int> doNotShowAgainDlgs;
35 
36 
37 KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage, const wxString& aCaption,
38  long aStyle )
39  : wxRichMessageDialog( aParent, aMessage, aCaption, aStyle | wxCENTRE | wxSTAY_ON_TOP ),
40  m_hash( 0 ),
41  m_cancelMeansCancel( true )
42 {
43 }
44 
45 
46 KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage, KD_TYPE aType,
47  const wxString& aCaption )
48  : wxRichMessageDialog( aParent, aMessage, getCaption( aType, aCaption ), getStyle( aType ) ),
49  m_hash( 0 ),
50  m_cancelMeansCancel( true )
51 {
52 }
53 
54 
55 void KIDIALOG::DoNotShowCheckbox( wxString aUniqueId, int line )
56 {
57  ShowCheckBox( _( "Do not show again" ), false );
58 
59  m_hash = std::hash<wxString>{}( aUniqueId ) + line;
60 }
61 
62 
64 {
65  return doNotShowAgainDlgs.count( m_hash ) > 0;
66 }
67 
68 
70 {
71  doNotShowAgainDlgs.erase( m_hash );
72 }
73 
74 
75 bool KIDIALOG::Show( bool aShow )
76 {
77  // We should check the do-not-show-again setting only when the dialog is displayed
78  if( aShow )
79  {
80  // Check if this dialog should be shown to the user
81  auto it = doNotShowAgainDlgs.find( m_hash );
82 
83  if( it != doNotShowAgainDlgs.end() )
84  return it->second;
85  }
86 
87  int ret = wxRichMessageDialog::Show( aShow );
88 
89  // Has the user asked not to show the dialog again?
90  // Note that we don't save a Cancel value unless the Cancel button is being used for some
91  // other function (which is actually more common than it being used for Cancel).
92  if( IsCheckBoxChecked() && (!m_cancelMeansCancel || ret != wxID_CANCEL ) )
94 
95  return ret;
96 }
97 
98 
100 {
101  // Check if this dialog should be shown to the user
102  auto it = doNotShowAgainDlgs.find( m_hash );
103 
104  if( it != doNotShowAgainDlgs.end() )
105  return it->second;
106 
107  int ret = wxRichMessageDialog::ShowModal();
108 
109  // Has the user asked not to show the dialog again?
110  // Note that we don't save a Cancel value unless the Cancel button is being used for some
111  // other function (which is actually more common than it being used for Cancel).
112  if( IsCheckBoxChecked() && (!m_cancelMeansCancel || ret != wxID_CANCEL ) )
113  doNotShowAgainDlgs[m_hash] = ret;
114 
115  return ret;
116 }
117 
118 
119 wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
120 {
121  if( !aCaption.IsEmpty() )
122  return aCaption;
123 
124  switch( aType )
125  {
126  case KD_NONE: /* fall through */
127  case KD_INFO: return _( "Message" );
128  case KD_QUESTION: return _( "Question" );
129  case KD_WARNING: return _( "Warning" );
130  case KD_ERROR: return _( "Error" );
131  }
132 
133  return wxEmptyString;
134 }
135 
136 
138 {
139  long style = wxOK | wxCENTRE | wxSTAY_ON_TOP;
140 
141  switch( aType )
142  {
143  case KD_NONE: break;
144  case KD_INFO: style |= wxICON_INFORMATION; break;
145  case KD_QUESTION: style |= wxICON_QUESTION; break;
146  case KD_WARNING: style |= wxICON_WARNING; break;
147  case KD_ERROR: style |= wxICON_ERROR; break;
148  }
149 
150  return style;
151 }
152 
153 
154 int UnsavedChangesDialog( wxWindow* parent, wxString aMessage, bool* aApplyToAll )
155 {
156  static bool s_apply_to_all = false;
157 
158  wxRichMessageDialog dlg( parent, aMessage, wxEmptyString,
159  wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
160  dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
161  dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
162 
163  if( aApplyToAll )
164  dlg.ShowCheckBox( _( "Apply to all" ), s_apply_to_all );
165 
166  int ret = dlg.ShowModal();
167 
168  if( aApplyToAll )
169  {
170  *aApplyToAll = dlg.IsCheckBoxChecked();
171  s_apply_to_all = dlg.IsCheckBoxChecked();
172  }
173 
174  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
175  return ret;
176 }
177 
178 
179 int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage )
180 {
181 #ifdef __APPLE__
182  // wxWidgets gets the button order wrong on Mac so use the other dialog.
183  return UnsavedChangesDialog( parent, aMessage, nullptr );
184 #else
185  wxMessageDialog dlg( parent, aMessage, wxEmptyString,
186  wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
187  dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
188  dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
189 
190  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
191  return dlg.ShowModal();
192 #endif
193 }
194 
195 
196 bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
197 {
198  wxMessageDialog dlg( parent, aMessage, wxEmptyString,
199  wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
200  dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
201  dlg.SetOKCancelLabels( _( "Revert" ), _( "Cancel" ) );
202 
203  return dlg.ShowModal() == wxID_OK;
204 }
205 
206 
207 bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
208  const std::function<bool()>& aSaveFunction )
209 {
210  switch( UnsavedChangesDialog( aParent, aMessage ) )
211  {
212  case wxID_YES: return aSaveFunction();
213  case wxID_NO: return true;
214  default:
215  case wxID_CANCEL: return false;
216  }
217 }
218 
219 
220 int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
221  wxString aDetailedMessage, wxString aOKLabel, wxString aCancelLabel,
222  bool* aApplyToAll )
223 {
224  wxRichMessageDialog dlg( aParent, aMessage, aWarning,
225  wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
226 
227  if( aOKLabel.IsEmpty() )
228  aOKLabel = _( "OK" );
229 
230  if( aCancelLabel.IsEmpty() )
231  aCancelLabel = _( "Cancel" );
232 
233  dlg.SetOKCancelLabels( aOKLabel, aCancelLabel );
234 
235  if( !aDetailedMessage.IsEmpty() )
236  dlg.SetExtendedMessage( aDetailedMessage );
237 
238  if( aApplyToAll )
239  dlg.ShowCheckBox( _( "Apply to all" ), true );
240 
241  int ret = dlg.ShowModal();
242 
243  if( aApplyToAll )
244  *aApplyToAll = dlg.IsCheckBoxChecked();
245 
246  // Returns wxID_OK or wxID_CANCEL
247  return ret;
248 }
249 
250 
251 // DisplayError should be deprecated, use DisplayErrorMessage instead
252 void DisplayError( wxWindow* aParent, const wxString& aText, int aDisplayTime )
253 {
254  wxMessageDialog* dlg;
255  int icon = aDisplayTime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
256 
257  dlg = new wxMessageDialog( aParent, aText, _( "Warning" ),
258  wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
259 
260  dlg->ShowModal();
261  dlg->Destroy();
262 }
263 
264 
265 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
266 {
267  wxMessageDialog* dlg;
268 
269  dlg = new wxMessageDialog( aParent, aText, _( "Error" ),
270  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR | wxSTAY_ON_TOP );
271 
272  if( !aExtraInfo.IsEmpty() )
273  dlg->SetExtendedMessage( aExtraInfo );
274 
275  dlg->ShowModal();
276  dlg->Destroy();
277 }
278 
279 
280 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
281 {
282  wxRichMessageDialog* dlg;
283  int icon = wxICON_INFORMATION;
284 
285  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
286  wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
287 
288  if( !aExtraInfo.IsEmpty() )
289  dlg->ShowDetailedText( aExtraInfo );
290 
291  dlg->ShowModal();
292  dlg->Destroy();
293 }
294 
295 
296 bool IsOK( wxWindow* aParent, const wxString& aMessage )
297 {
298  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
299  wxYES_NO | wxCENTRE | wxICON_QUESTION | wxSTAY_ON_TOP );
300  dlg.SetEscapeId( wxID_NO );
301 
302  return dlg.ShowModal() == wxID_YES;
303 }
304 
305 
306 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
307  const wxString& aMessage, const wxArrayString& aOptions )
308 {
309  wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
310 
311  if( dlg.ShowModal() != wxID_OK )
312  return -1;
313 
314  return dlg.GetSelection();
315 }
316 
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:252
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Display a dialog with Save, Cancel and Discard Changes buttons.
Definition: confirm.cpp:207
void DoNotShowCheckbox(wxString file, int line)
Checks the 'do not show again' setting for the dialog.
Definition: confirm.cpp:55
bool ConfirmRevertDialog(wxWindow *parent, const wxString &aMessage)
Display a confirmation dialog for a revert action.
Definition: confirm.cpp:196
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:265
This file is part of the common library.
KD_TYPE
< Dialog type. Selects appropriate icon and default dialog title
Definition: confirm.h:49
static std::unordered_map< unsigned long, int > doNotShowAgainDlgs
Definition: confirm.cpp:34
static wxString getCaption(KD_TYPE aType, const wxString &aCaption)
Definition: confirm.cpp:119
bool Show(bool aShow=true) override
Definition: confirm.cpp:75
static long getStyle(KD_TYPE aType)
Definition: confirm.cpp:137
void ForceShowAgain()
Definition: confirm.cpp:69
int UnsavedChangesDialog(wxWindow *parent, wxString aMessage, bool *aApplyToAll)
A specialized version of HandleUnsavedChanges which handles an apply-to-all checkbox.
Definition: confirm.cpp:154
bool DoNotShowAgain() const
Definition: confirm.cpp:63
int OKOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, wxString aDetailedMessage, wxString aOKLabel, wxString aCancelLabel, bool *aApplyToAll)
Display a warning dialog with aMessage and returns the user response.
Definition: confirm.cpp:220
#define _(s)
Definition: 3d_actions.cpp:33
KIDIALOG(wxWindow *aParent, const wxString &aMessage, const wxString &aCaption, long aStyle=wxOK)
Definition: confirm.cpp:37
int ShowModal() override
Definition: confirm.cpp:99
bool m_cancelMeansCancel
Definition: confirm.h:79
int SelectSingleOption(wxWindow *aParent, const wxString &aTitle, const wxString &aMessage, const wxArrayString &aOptions)
Display a dialog with radioboxes asking the user to select an option.
Definition: confirm.cpp:306
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:280
unsigned long m_hash
Definition: confirm.h:78
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:296