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 
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 
37 #include <functional>
38 #include <unordered_map>
39 
40 // Set of dialogs that have been chosen not to be shown again
41 static std::unordered_map<unsigned long, int> doNotShowAgainDlgs;
42 
43 
44 KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage,
45  const wxString& aCaption, long aStyle )
46  : wxRichMessageDialog( aParent, aMessage, aCaption, aStyle | wxCENTRE | wxSTAY_ON_TOP ),
47  m_hash( 0 )
48 {
49 }
50 
51 
52 KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage,
53  KD_TYPE aType, const wxString& aCaption )
54  : wxRichMessageDialog( aParent, aMessage, getCaption( aType, aCaption ), getStyle( aType ) ),
55  m_hash( 0 )
56 {
57 }
58 
59 
60 void KIDIALOG::DoNotShowCheckbox( wxString aUniqueId, int line )
61 {
62  ShowCheckBox( _( "Do not show again" ), false );
63 
64  m_hash = std::hash<wxString>{}( aUniqueId ) + line;
65 }
66 
67 
69 {
70  return doNotShowAgainDlgs.count( m_hash ) > 0;
71 }
72 
73 
75 {
76  doNotShowAgainDlgs.erase( m_hash );
77 }
78 
79 
80 bool KIDIALOG::Show( bool aShow )
81 {
82  // We should check the do-not-show-again setting only when the dialog is displayed
83  if( aShow )
84  {
85  // Check if this dialog should be shown to the user
86  auto it = doNotShowAgainDlgs.find( m_hash );
87 
88  if( it != doNotShowAgainDlgs.end() )
89  return it->second;
90  }
91 
92  bool ret = wxRichMessageDialog::Show( aShow );
93 
94  // Has the user asked not to show the dialog again
95  if( IsCheckBoxChecked() )
97 
98  return ret;
99 }
100 
101 
103 {
104  // Check if this dialog should be shown to the user
105  auto it = doNotShowAgainDlgs.find( m_hash );
106 
107  if( it != doNotShowAgainDlgs.end() )
108  return it->second;
109 
110  int ret = wxRichMessageDialog::ShowModal();
111 
112  // Has the user asked not to show the dialog again
113  if( IsCheckBoxChecked() )
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  const wxString& aOKLabel, const wxString& aCancelLabel, bool* aApplyToAll )
223 {
224  wxRichMessageDialog dlg( aParent, aMessage, wxEmptyString,
225  wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
226  dlg.ShowDetailedText( _( "If you don't save, all your changes will be permanently lost." ) );
227  dlg.SetOKCancelLabels( aOKLabel, aCancelLabel );
228 
229  if( aApplyToAll )
230  dlg.ShowCheckBox( _( "Apply to all" ), true );
231 
232  int ret = dlg.ShowModal();
233 
234  if( aApplyToAll )
235  *aApplyToAll = dlg.IsCheckBoxChecked();
236 
237  // Returns wxID_OK or wxID_CANCEL
238  return ret;
239 }
240 
241 
242 // DisplayError should be deprecated, use DisplayErrorMessage instead
243 void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
244 {
245  wxMessageDialog* dialog;
246 
247  int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
248 
249  dialog = new wxMessageDialog( parent, text, _( "Warning" ),
250  wxOK | wxCENTRE | wxRESIZE_BORDER |
251  icon | wxSTAY_ON_TOP );
252 
253  dialog->ShowModal();
254  dialog->Destroy();
255 }
256 
257 
258 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
259 {
260  wxRichMessageDialog* dlg;
261 
262  dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
263  wxOK | wxCENTRE | wxRESIZE_BORDER |
264  wxICON_ERROR | wxSTAY_ON_TOP );
265 
266  if( !aExtraInfo.IsEmpty() )
267  {
268  dlg->ShowDetailedText( aExtraInfo );
269  }
270 
271  dlg->ShowModal();
272  dlg->Destroy();
273 }
274 
275 
276 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
277 {
278  wxRichMessageDialog* dlg;
279 
280  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
281  wxOK | wxCENTRE | wxRESIZE_BORDER |
282  wxICON_INFORMATION | wxSTAY_ON_TOP );
283 
284  if( !aExtraInfo.IsEmpty() )
285  {
286  dlg->ShowDetailedText( aExtraInfo );
287  }
288 
289  dlg->ShowModal();
290  dlg->Destroy();
291 }
292 
293 
294 bool IsOK( wxWindow* aParent, const wxString& aMessage )
295 {
296  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
297  wxYES_NO | wxCENTRE | wxICON_QUESTION | wxSTAY_ON_TOP );
298  dlg.SetEscapeId( wxID_NO );
299 
300  return dlg.ShowModal() == wxID_YES;
301 }
302 
303 
304 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
305  const wxString& aMessage, const wxArrayString& aOptions )
306 {
307  wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
308 
309  if( dlg.ShowModal() != wxID_OK )
310  return -1;
311 
312  return dlg.GetSelection();
313 }
314 
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)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:60
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:258
This file is part of the common library.
KD_TYPE
Dialog type. Selects appropriate icon and default dialog title
Definition: confirm.h:48
int OKOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel, bool *aApplyToAll)
Displays a warning dialog with aMessage and returns the user response.
Definition: confirm.cpp:221
static std::unordered_map< unsigned long, int > doNotShowAgainDlgs
Definition: confirm.cpp:41
static wxString getCaption(KD_TYPE aType, const wxString &aCaption)
Definition: confirm.cpp:120
Subclass of DIALOG_DISPLAY_HTML_TEXT_BASE, which is generated by wxFormBuilder.
bool Show(bool aShow=true) override
Definition: confirm.cpp:80
static long getStyle(KD_TYPE aType)
Definition: confirm.cpp:138
void ForceShowAgain()
Definition: confirm.cpp:74
int UnsavedChangesDialog(wxWindow *parent, wxString aMessage, bool *aApplyToAll)
A specialized version of HandleUnsavedChanges which handles an apply-to-all checkbox.
Definition: confirm.cpp:155
#define _(s)
bool DoNotShowAgain() const
Checks the 'do not show again' setting for the dialog
Definition: confirm.cpp:68
KIDIALOG(wxWindow *aParent, const wxString &aMessage, const wxString &aCaption, long aStyle=wxOK)
Definition: confirm.cpp:44
int ShowModal() override
Definition: confirm.cpp:102
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:304
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:276
unsigned long m_hash
Unique identifier of the dialog
Definition: confirm.h:67
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:243
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:294