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 )
48 {
49  setHash();
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  setHash();
58 }
59 
60 
62 {
63  return doNotShowAgainDlgs.count( m_hash ) > 0;
64 }
65 
66 
68 {
69  doNotShowAgainDlgs.erase( m_hash );
70 }
71 
72 
73 bool KIDIALOG::Show( bool aShow )
74 {
75  // We should check the do-not-show-again setting only when the dialog is displayed
76  if( aShow )
77  {
78  // Check if this dialog should be shown to the user
79  auto it = doNotShowAgainDlgs.find( m_hash );
80 
81  if( it != doNotShowAgainDlgs.end() )
82  return it->second;
83  }
84 
85  bool ret = wxRichMessageDialog::Show( aShow );
86 
87  // Has the user asked not to show the dialog again
88  if( IsCheckBoxChecked() )
90 
91  return ret;
92 }
93 
94 
96 {
97  // Check if this dialog should be shown to the user
98  auto it = doNotShowAgainDlgs.find( m_hash );
99 
100  if( it != doNotShowAgainDlgs.end() )
101  return it->second;
102 
103  int ret = wxRichMessageDialog::ShowModal();
104 
105  // Has the user asked not to show the dialog again
106  if( IsCheckBoxChecked() )
107  doNotShowAgainDlgs[m_hash] = ret;
108 
109  return ret;
110 }
111 
112 
114 {
115  std::size_t h1 = std::hash<wxString>{}( GetMessage() );
116  std::size_t h2 = std::hash<wxString>{}( GetTitle() );
117  m_hash = h1 ^ ( h2 << 1 );
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;
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_staticText2->SetLabel( aMessage );
166 
167  m_sdbSizer1OK->SetLabel( aOKLabel );
168  m_sdbSizer1Cancel->SetLabel( aCancelLabel );
169  m_sdbSizer1OK->SetDefault();
170 
171  m_sdbSizer1->Layout();
172  m_buttonSizer->Layout();
173 
174  FinishDialogSettings();
175  };
176 
177 private:
178  void OnSave( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
179  void OnDiscard( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
180 };
181 
182 
183 int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
184 {
185  DIALOG_EXIT dlg( parent, aMessage,
186  _( "If you don't save, all your changes will be permanently lost." ),
187  _( "Save" ), _( "Cancel" ) );
188 
189  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
190 
191  int ret = dlg.ShowModal();
192 
193  if( aApplyToAll )
194  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
195 
196  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
197  return ret;
198 }
199 
200 
201 bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
202  const std::function<bool()>& aSaveFunction )
203 {
204  switch( UnsavedChangesDialog( aParent, aMessage, nullptr ) )
205  {
206  case wxID_YES: return aSaveFunction();
207  case wxID_NO: return true;
208  default:
209  case wxID_CANCEL: return false;
210  }
211 }
212 
213 
214 int YesOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
215  const wxString& aOKLabel, const wxString& aCancelLabel, bool* aApplyToAll )
216 {
217  DIALOG_EXIT dlg( aParent, aWarning, aMessage, aOKLabel, aCancelLabel );
218 
219  dlg.m_ApplyToAllOpt->Show( aApplyToAll != nullptr );
220  dlg.m_DiscardButton->Show( false );
221 
222  int ret = dlg.ShowModal();
223 
224  if( aApplyToAll )
225  *aApplyToAll = dlg.m_ApplyToAllOpt->GetValue();
226 
227  // Returns wxID_YES, wxID_NO, or wxID_CANCEL
228  return ret;
229 }
230 
231 
232 // DisplayError should be deprecated, use DisplayErrorMessage instead
233 void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
234 {
235  wxMessageDialog* dialog;
236 
237  int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
238 
239  dialog = new wxMessageDialog( parent, text, _( "Warning" ),
240  wxOK | wxCENTRE | wxRESIZE_BORDER | icon );
241 
242  dialog->ShowModal();
243  dialog->Destroy();
244 }
245 
246 
247 void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
248 {
249  wxRichMessageDialog* dlg;
250 
251  dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
252  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR );
253 
254  if( !aExtraInfo.IsEmpty() )
255  {
256  dlg->ShowDetailedText( aExtraInfo );
257  }
258 
259  dlg->ShowModal();
260  dlg->Destroy();
261 }
262 
263 
264 void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
265 {
266  wxRichMessageDialog* dlg;
267 
268  dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
269  wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_INFORMATION );
270 
271  if( !aExtraInfo.IsEmpty() )
272  {
273  dlg->ShowDetailedText( aExtraInfo );
274  }
275 
276  dlg->ShowModal();
277  dlg->Destroy();
278 }
279 
280 
281 bool IsOK( wxWindow* aParent, const wxString& aMessage )
282 {
283  wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
284  wxYES_NO | wxCENTRE | wxICON_QUESTION );
285 
286  return dlg.ShowModal() == wxID_YES;
287 }
288 
289 
290 int SelectSingleOption( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage, const wxArrayString& aOptions )
291 {
292  wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
293 
294  if( dlg.ShowModal() != wxID_OK )
295  return -1;
296 
297  return dlg.GetSelection();
298 }
299 
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:201
void OnDiscard(wxCommandEvent &event) override
Definition: confirm.cpp:179
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Function DisplayErrorMessage displays an error message with aMessage.
Definition: confirm.cpp:247
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:178
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:214
void setHash()
Sets the dialog hash value
Definition: confirm.cpp:113
Class DIALOG_EXIT_BASE.
wxBitmap KiBitmap(BITMAP_DEF aBitmap)
Function KiBitmap constructs a wxBitmap from a memory record, held in a BITMAP_DEF.
Definition: bitmap.cpp:78
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:183
bool DoNotShowAgain() const
Checks the &#39;do not show again&#39; setting for the dialog
Definition: confirm.cpp:61
bool Show(bool aShow=true) override
Definition: confirm.cpp:73
static long getStyle(KD_TYPE aType)
Definition: confirm.cpp:139
wxCheckBox * m_ApplyToAllOpt
void ForceShowAgain()
Definition: confirm.cpp:67
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:95
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:290
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Function DisplayInfoMessage displays an informational message box with aMessage.
Definition: confirm.cpp:264
unsigned long m_hash
Unique identifier of the dialog
Definition: confirm.h:71
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:233
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Function IsOK displays a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:281
DIALOG_EXIT(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aOKLabel, const wxString &aCancelLabel)
Definition: confirm.cpp:159