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