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->SetDefault();
170 
172  };
173 
174 private:
175  void OnSave( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
176  void OnDiscard( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
177 };
178 
179 
180 int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
181 {
182  DIALOG_EXIT dlg( parent, aMessage,
183  _( "If you don't save, all your changes will be permanently lost." ),
184  _( "Save" ), _( "Cancel" ) );
185 
186  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
187 
188  int ret = dlg.ShowModal();
189 
190  if( aApplyToAll )
191  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
192 
193  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
194  return ret;
195 }
196 
197 
198 bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
199 {
200  DIALOG_EXIT dlg( parent, aMessage,
201  _( "Your current changes will be permanently lost." ),
202  _( "Revert" ), _( "Cancel" ) );
203 
204  dlg.m_ApplyToAllOpt->Show( false );
205  dlg.m_DiscardButton->Show( false );
206 
207  return dlg.ShowModal() == wxID_YES;
208 }
209 
210 
211 bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
212  const std::function<bool()>& aSaveFunction )
213 {
214  switch( UnsavedChangesDialog( aParent, aMessage, nullptr ) )
215  {
216  case wxID_YES: return aSaveFunction();
217  case wxID_NO: return true;
218  default:
219  case wxID_CANCEL: return false;
220  }
221 }
222 
223 
224 int YesOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
225  const wxString& aOKLabel, const wxString& aCancelLabel, bool* aApplyToAll )
226 {
227  DIALOG_EXIT dlg( aParent, aWarning, aMessage, aOKLabel, aCancelLabel );
228 
229  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
230  dlg.m_DiscardButton->Show( false );
231 
232  int ret = dlg.ShowModal();
233 
234  if( aApplyToAll )
235  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
236 
237  // Returns wxID_YES, wxID_NO, 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 | icon | wxSTAY_ON_TOP );
251 
252  dialog->ShowModal();
253  dialog->Destroy();
254 }
255 
256 
257 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
258 {
259  wxRichMessageDialog* dlg;
260 
261  dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
262  wxOK | wxCENTRE | wxRESIZE_BORDER |
263  wxICON_ERROR | wxSTAY_ON_TOP );
264 
265  if( !aExtraInfo.IsEmpty() )
266  {
267  dlg->ShowDetailedText( aExtraInfo );
268  }
269 
270  dlg->ShowModal();
271  dlg->Destroy();
272 }
273 
274 
275 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
276 {
277  wxRichMessageDialog* dlg;
278 
279  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
280  wxOK | wxCENTRE | wxRESIZE_BORDER |
281  wxICON_INFORMATION | wxSTAY_ON_TOP );
282 
283  if( !aExtraInfo.IsEmpty() )
284  {
285  dlg->ShowDetailedText( aExtraInfo );
286  }
287 
288  dlg->ShowModal();
289  dlg->Destroy();
290 }
291 
292 
293 bool IsOK( wxWindow* aParent, const wxString& aMessage )
294 {
295  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
296  wxYES_NO | wxCENTRE | wxICON_QUESTION | wxSTAY_ON_TOP );
297  dlg.SetEscapeId( wxID_NO );
298 
299  return dlg.ShowModal() == wxID_YES;
300 }
301 
302 
303 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage, const wxArrayString& aOptions )
304 {
305  wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
306 
307  if( dlg.ShowModal() != wxID_OK )
308  return -1;
309 
310  return dlg.GetSelection();
311 }
312 
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:211
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:198
void OnDiscard(wxCommandEvent &event) override
Definition: confirm.cpp:176
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:257
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:175
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:224
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:180
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:303
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:275
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:243
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:293
DIALOG_EXIT(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel)
Definition: confirm.cpp:159
wxStaticText * m_staticTextWarningMessage