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 #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  m_hash( 0 )
57 {
58 }
59 
60 
61 void KIDIALOG::DoNotShowCheckbox( wxString aUniqueId, int line )
62 {
63  ShowCheckBox( _( "Do not show again" ), false );
64 
65  m_hash = std::hash<wxString>{}( aUniqueId ) + line;
66 }
67 
68 
70 {
71  return doNotShowAgainDlgs.count( m_hash ) > 0;
72 }
73 
74 
76 {
77  doNotShowAgainDlgs.erase( m_hash );
78 }
79 
80 
81 bool KIDIALOG::Show( bool aShow )
82 {
83  // We should check the do-not-show-again setting only when the dialog is displayed
84  if( aShow )
85  {
86  // Check if this dialog should be shown to the user
87  auto it = doNotShowAgainDlgs.find( m_hash );
88 
89  if( it != doNotShowAgainDlgs.end() )
90  return it->second;
91  }
92 
93  bool ret = wxRichMessageDialog::Show( aShow );
94 
95  // Has the user asked not to show the dialog again
96  if( IsCheckBoxChecked() )
98 
99  return ret;
100 }
101 
102 
104 {
105  // Check if this dialog should be shown to the user
106  auto it = doNotShowAgainDlgs.find( m_hash );
107 
108  if( it != doNotShowAgainDlgs.end() )
109  return it->second;
110 
111  int ret = wxRichMessageDialog::ShowModal();
112 
113  // Has the user asked not to show the dialog again
114  if( IsCheckBoxChecked() )
115  doNotShowAgainDlgs[m_hash] = ret;
116 
117  return ret;
118 }
119 
120 
121 wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
122 {
123  if( !aCaption.IsEmpty() )
124  return aCaption;
125 
126  switch( aType )
127  {
128  case KD_NONE: /* fall through */
129  case KD_INFO: return _( "Message" );
130  case KD_QUESTION: return _( "Question" );
131  case KD_WARNING: return _( "Warning" );
132  case KD_ERROR: return _( "Error" );
133  }
134 
135  return wxEmptyString;
136 }
137 
138 
140 {
141  long style = wxOK | wxCENTRE | wxSTAY_ON_TOP;
142 
143  switch( aType )
144  {
145  case KD_NONE: break;
146  case KD_INFO: style |= wxICON_INFORMATION; break;
147  case KD_QUESTION: style |= wxICON_QUESTION; break;
148  case KD_WARNING: style |= wxICON_WARNING; break;
149  case KD_ERROR: style |= wxICON_ERROR; break;
150  }
151 
152  return style;
153 }
154 
155 
157 {
158 public:
159  DIALOG_EXIT( wxWindow *aParent, const wxString& aWarning, const wxString& aMessage,
160  const wxString& aOKLabel, const wxString& aCancelLabel ) :
161  DIALOG_EXIT_BASE( aParent )
162  {
163  m_bitmap->SetBitmap( KiBitmap( dialog_warning_xpm ) );
164  m_TextInfo->SetLabel( aWarning );
165  m_staticTextWarningMessage->SetLabel( aMessage );
166 
167  m_sdbSizerOK->SetLabel( aOKLabel );
168  m_sdbSizerCancel->SetLabel( aCancelLabel );
169  m_sdbSizerOK->SetFocus();
170  m_sdbSizerOK->SetDefault();
171 
173  };
174 
175 private:
176  void OnSave( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
177  void OnDiscard( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
178 };
179 
180 
181 int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
182 {
183  DIALOG_EXIT dlg( parent, aMessage,
184  _( "If you don't save, all your changes will be permanently lost." ),
185  _( "Save" ), _( "Cancel" ) );
186 
187  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
188 
189  int ret = dlg.ShowModal();
190 
191  if( aApplyToAll )
192  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
193 
194  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
195  return ret;
196 }
197 
198 
199 bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
200 {
201  DIALOG_EXIT dlg( parent, aMessage,
202  _( "Your current changes will be permanently lost." ),
203  _( "Revert" ), _( "Cancel" ) );
204 
205  dlg.m_ApplyToAllOpt->Show( false );
206  dlg.m_DiscardButton->Show( false );
207 
208  return dlg.ShowModal() == wxID_YES;
209 }
210 
211 
212 bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
213  const std::function<bool()>& aSaveFunction )
214 {
215  switch( UnsavedChangesDialog( aParent, aMessage, nullptr ) )
216  {
217  case wxID_YES: return aSaveFunction();
218  case wxID_NO: return true;
219  default:
220  case wxID_CANCEL: return false;
221  }
222 }
223 
224 
225 int YesOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
226  const wxString& aOKLabel, const wxString& aCancelLabel, bool* aApplyToAll )
227 {
228  DIALOG_EXIT dlg( aParent, aWarning, aMessage, aOKLabel, aCancelLabel );
229 
230  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
231  dlg.m_DiscardButton->Show( false );
232 
233  int ret = dlg.ShowModal();
234 
235  if( aApplyToAll )
236  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
237 
238  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
239  return ret;
240 }
241 
242 
243 // DisplayError should be deprecated, use DisplayErrorMessage instead
244 void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
245 {
246  wxMessageDialog* dialog;
247 
248  int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
249 
250  dialog = new wxMessageDialog( parent, text, _( "Warning" ),
251  wxOK | wxCENTRE | wxRESIZE_BORDER | 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, 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:212
void DoNotShowCheckbox(wxString file, int line)
Shows the 'do not show again' checkbox
Definition: confirm.cpp:61
bool ConfirmRevertDialog(wxWindow *parent, const wxString &aMessage)
Function ConfirmRevertDialog displays a confirmation for a revert action.
Definition: confirm.cpp:199
void OnDiscard(wxCommandEvent &event) override
Definition: confirm.cpp:177
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays 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
void FinishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
void OnSave(wxCommandEvent &event) override
Definition: confirm.cpp:176
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:225
wxButton * m_sdbSizerOK
wxStaticText * m_TextInfo
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:121
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:181
bool Show(bool aShow=true) override
Definition: confirm.cpp:81
static long getStyle(KD_TYPE aType)
Definition: confirm.cpp:139
wxCheckBox * m_ApplyToAllOpt
void ForceShowAgain()
Definition: confirm.cpp:75
wxStaticBitmap * m_bitmap
bool DoNotShowAgain() const
Checks the 'do not show again' setting for the dialog
Definition: confirm.cpp:69
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:103
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:276
wxButton * m_sdbSizerCancel
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:244
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:294
DIALOG_EXIT(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel)
Definition: confirm.cpp:159
wxStaticText * m_staticTextWarningMessage