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 | wxSTAY_ON_TOP ),
48  m_hash( 0 )
49 {
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 }
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 
156 {
157 public:
158  DIALOG_EXIT( wxWindow *aParent, const wxString& aWarning, const wxString& aMessage,
159  const wxString& aOKLabel, const wxString& aCancelLabel ) :
160  DIALOG_EXIT_BASE( aParent )
161  {
162  m_bitmap->SetBitmap( KiBitmap( dialog_warning_xpm ) );
163  m_TextInfo->SetLabel( aWarning );
164  m_staticText2->SetLabel( aMessage );
165 
166  m_sdbSizer1OK->SetLabel( aOKLabel );
167  m_sdbSizer1Cancel->SetLabel( aCancelLabel );
168  m_sdbSizer1OK->SetDefault();
169 
170  m_sdbSizer1->Layout();
171  m_buttonSizer->Layout();
172 
173  FinishDialogSettings();
174  };
175 
176 private:
177  void OnSave( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
178  void OnDiscard( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
179 };
180 
181 
182 int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
183 {
184  DIALOG_EXIT dlg( parent, aMessage,
185  _( "If you don't save, all your changes will be permanently lost." ),
186  _( "Save" ), _( "Cancel" ) );
187 
188  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
189 
190  int ret = dlg.ShowModal();
191 
192  if( aApplyToAll )
193  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
194 
195  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
196  return ret;
197 }
198 
199 
200 bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
201 {
202  DIALOG_EXIT dlg( parent, aMessage,
203  _( "Your current changes will be permanently lost." ),
204  _( "Revert" ), _( "Cancel" ) );
205 
206  dlg.m_ApplyToAllOpt->Show( false );
207  dlg.m_DiscardButton->Show( false );
208 
209  return dlg.ShowModal() == wxID_YES;
210 }
211 
212 
213 bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
214  const std::function<bool()>& aSaveFunction )
215 {
216  switch( UnsavedChangesDialog( aParent, aMessage, nullptr ) )
217  {
218  case wxID_YES: return aSaveFunction();
219  case wxID_NO: return true;
220  default:
221  case wxID_CANCEL: return false;
222  }
223 }
224 
225 
226 int YesOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
227  const wxString& aOKLabel, const wxString& aCancelLabel, bool* aApplyToAll )
228 {
229  DIALOG_EXIT dlg( aParent, aWarning, aMessage, aOKLabel, aCancelLabel );
230 
231  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
232  dlg.m_DiscardButton->Show( false );
233 
234  int ret = dlg.ShowModal();
235 
236  if( aApplyToAll )
237  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
238 
239  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
240  return ret;
241 }
242 
243 
244 // DisplayError should be deprecated, use DisplayErrorMessage instead
245 void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
246 {
247  wxMessageDialog* dialog;
248 
249  int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
250 
251  dialog = new wxMessageDialog( parent, text, _( "Warning" ),
252  wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
253 
254  dialog->ShowModal();
255  dialog->Destroy();
256 }
257 
258 
259 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
260 {
261  wxRichMessageDialog* dlg;
262 
263  dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
264  wxOK | wxCENTRE | wxRESIZE_BORDER |
265  wxICON_ERROR | wxSTAY_ON_TOP );
266 
267  if( !aExtraInfo.IsEmpty() )
268  {
269  dlg->ShowDetailedText( aExtraInfo );
270  }
271 
272  dlg->ShowModal();
273  dlg->Destroy();
274 }
275 
276 
277 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
278 {
279  wxRichMessageDialog* dlg;
280 
281  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
282  wxOK | wxCENTRE | wxRESIZE_BORDER |
283  wxICON_INFORMATION | wxSTAY_ON_TOP );
284 
285  if( !aExtraInfo.IsEmpty() )
286  {
287  dlg->ShowDetailedText( aExtraInfo );
288  }
289 
290  dlg->ShowModal();
291  dlg->Destroy();
292 }
293 
294 
295 bool IsOK( wxWindow* aParent, const wxString& aMessage )
296 {
297  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
298  wxYES_NO | wxCENTRE | wxICON_QUESTION | wxSTAY_ON_TOP );
299 
300  return dlg.ShowModal() == wxID_YES;
301 }
302 
303 
304 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage, const wxArrayString& aOptions )
305 {
306  wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
307 
308  if( dlg.ShowModal() != wxID_OK )
309  return -1;
310 
311  return dlg.GetSelection();
312 }
313 
bool HandleUnsavedChanges(wxWindow *aParent, const wxString &aMessage, const std::function< bool()> &aSaveFunction)
Function HandleUnsavedChanges displays a dialog with Save, Cancel and Discard Changes buttons...
Definition: confirm.cpp:213
void DoNotShowCheckbox(wxString file, int line)
Shows the &#39;do not show again&#39; checkbox
Definition: confirm.cpp:60
bool ConfirmRevertDialog(wxWindow *parent, const wxString &aMessage)
Function ConfirmRevertDialog displays a confirmation for a revert action.
Definition: confirm.cpp:200
void OnDiscard(wxCommandEvent &event) override
Definition: confirm.cpp:178
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:259
This file is part of the common library.
KD_TYPE
Dialog type. Selects appropriate icon and default dialog title
Definition: confirm.h:48
void OnSave(wxCommandEvent &event) override
Definition: confirm.cpp:177
int YesOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel, bool *aApplyToAll)
Function YesOrCancelDialog displays a warning dialog with aMessage and returns the user response...
Definition: confirm.cpp:226
Class DIALOG_EXIT_BASE.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Construct a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:79
static std::unordered_map< unsigned long, int > doNotShowAgainDlgs
Definition: confirm.cpp:42
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.
int UnsavedChangesDialog(wxWindow *parent, const wxString &aMessage, bool *aApplyToAll)
Function UnsavedChangesDialog a specialized version of HandleUnsavedChanges which handles an apply-to...
Definition: confirm.cpp:182
bool DoNotShowAgain() const
Checks the &#39;do not show again&#39; setting for the dialog
Definition: confirm.cpp:68
bool Show(bool aShow=true) override
Definition: confirm.cpp:80
static long getStyle(KD_TYPE aType)
Definition: confirm.cpp:138
wxCheckBox * m_ApplyToAllOpt
void ForceShowAgain()
Definition: confirm.cpp:74
wxButton * m_DiscardButton
KIDIALOG(wxWindow *aParent, const wxString &aMessage, const wxString &aCaption, long aStyle=wxOK)
Definition: confirm.cpp:45
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)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:277
unsigned long m_hash
Unique identifier of the dialog
Definition: confirm.h:65
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:245
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:295
DIALOG_EXIT(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel)
Definition: confirm.cpp:158