KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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
25#include <confirm.h>
26
27#include <functional>
28#include <wx/app.h>
29#include <wx/stockitem.h>
30#include <wx/richmsgdlg.h>
31#include <wx/msgdlg.h>
32#include <wx/choicdlg.h>
33#include <wx/crt.h>
39static const wxChar traceConfirm[] = wxT( "KICAD_CONFIRM" );
40
41
42bool AskOverrideLock( wxWindow* aParent, const wxString& aMessage )
43{
44#ifdef __APPLE__
45 // wxMessageDialog gets the button spacing wrong on Mac so we have to use wxRichMessageDialog.
46 // Note that its warning icon is more like wxMessageDialog's error icon, so we use it instead
47 // of wxICON_ERROR.
48 wxRichMessageDialog dlg( aParent, aMessage, _( "File Open Warning" ),
49 wxYES_NO | wxICON_WARNING | wxCENTER );
50 dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." )
51 + wxS( "\n" ) );
52 dlg.SetYesNoLabels( _( "&Cancel" ), _( "&Open Anyway" ) );
53#else
54 KICAD_MESSAGE_DIALOG_BASE dlg( aParent, aMessage, _( "File Open Warning" ),
55 wxYES_NO | wxICON_ERROR | wxCENTER );
56 dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." ) );
57 dlg.SetYesNoLabels( _( "&Cancel" ), _( "&Open Anyway" ) );
58#endif
59
60 return dlg.ShowModal() == wxID_NO;
61}
62
63
64int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
65{
66 static bool s_apply_to_all = false;
67
68 KICAD_RICH_MESSAGE_DIALOG_BASE dlg( parent, aMessage, _( "Save Changes?" ),
69 wxYES_NO | wxCANCEL |
70 wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
71 dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." )
72 + wxS( "\n" ) );
73 dlg.SetYesNoLabels( _( "&Save" ), _( "&Discard Changes" ) );
74
75 if( aApplyToAll )
76 dlg.ShowCheckBox( _( "&Apply to all" ), s_apply_to_all );
77
78 int ret = dlg.ShowModal();
79
80 if( aApplyToAll )
81 {
82 *aApplyToAll = dlg.IsCheckBoxChecked();
83 s_apply_to_all = dlg.IsCheckBoxChecked();
84 }
85
86 // Returns wxID_YES, wxID_NO, or wxID_CANCEL
87 return ret;
88}
89
90
91int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage )
92{
93#ifdef __APPLE__
94 // wxMessageDialog gets the button order (and spacing) wrong on Mac so we have to use
95 // wxRichMessageDialog.
96 return UnsavedChangesDialog( parent, aMessage, nullptr );
97#else
98 #ifdef _WIN32
99 // wxMessageDialog on windows invokes TaskDialogIndirect which is a native function for a dialog
100 // As a result it skips wxWidgets for modal management...and we don't parent frames properly
101 // among other things for Windows to do the right thing by default
102 // Disable all the windows manually to avoid being able to hit this dialog from the tool frame
103 // and kicad frame at the same time.
104 wxWindowDisabler disable( true );
105 #endif
106
107 KICAD_MESSAGE_DIALOG_BASE dlg( parent, aMessage, _( "Save Changes?" ),
108 wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
109 dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
110 dlg.SetYesNoLabels( _( "&Save" ), _( "&Discard Changes" ) );
111
112 // Returns wxID_YES, wxID_NO, or wxID_CANCEL
113 return dlg.ShowModal();
114#endif
115}
116
117
118bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
119{
120 KICAD_MESSAGE_DIALOG_BASE dlg( parent, aMessage, wxEmptyString,
121 wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
122 dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
123 dlg.SetOKCancelLabels( _( "&Revert" ), _( "&Cancel" ) );
124
125 return dlg.ShowModal() == wxID_OK;
126}
127
128
130
131bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
132 const std::function<bool()>& aSaveFunction )
133{
136 {
137 case wxID_YES: return aSaveFunction();
138 case wxID_NO: return true; // proceed without saving
139 default:
140 case wxID_CANCEL: return false;
141 }
142}
143
148
149
150int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
151 const wxString& aDetailedMessage, const wxString& aOKLabel,
152 const wxString& aCancelLabel, bool* aApplyToAll )
153{
154 KICAD_RICH_MESSAGE_DIALOG_BASE dlg( aParent, aMessage, aWarning,
155 wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
156
157 dlg.SetOKCancelLabels( ( aOKLabel.IsEmpty() ) ? _( "&OK" ) : aOKLabel,
158 ( aCancelLabel.IsEmpty() ) ? _( "&Cancel" ) : aCancelLabel );
159
160 if( !aDetailedMessage.IsEmpty() )
161 dlg.SetExtendedMessage( aDetailedMessage );
162
163 if( aApplyToAll )
164 dlg.ShowCheckBox( _( "&Apply to all" ), true );
165
166 int ret = dlg.ShowModal();
167
168 if( aApplyToAll )
169 *aApplyToAll = dlg.IsCheckBoxChecked();
170
171 // Returns wxID_OK or wxID_CANCEL
172 return ret;
173}
174
175
176// DisplayError should be deprecated, use DisplayErrorMessage instead
177void DisplayError( wxWindow* aParent, const wxString& aText )
178{
179 if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
180 {
181 wxLogError( "%s", aText );
182 return;
183 }
184
185 if( !wxTheApp->IsGUI() )
186 {
187 wxFprintf( stderr, aText );
188 return;
189 }
190
192
193 dlg = new KICAD_MESSAGE_DIALOG_BASE( aParent, aText, _( "Error" ),
194 wxOK | wxCENTRE | wxRESIZE_BORDER |
195 wxICON_ERROR | wxSTAY_ON_TOP );
196
197 dlg->ShowModal();
198 dlg->Destroy();
199}
200
201
202void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
203{
204 if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
205 {
206 wxLogError( "%s %s", aText, aExtraInfo );
207 return;
208 }
209
210 if( !wxTheApp->IsGUI() )
211 {
212 wxFprintf( stderr, aText );
213 return;
214 }
215
217
218 dlg = new KICAD_MESSAGE_DIALOG_BASE( aParent, aText, _( "Error" ),
219 wxOK | wxCENTRE | wxRESIZE_BORDER |
220 wxICON_ERROR | wxSTAY_ON_TOP );
221
222 if( !aExtraInfo.IsEmpty() )
223 dlg->SetExtendedMessage( aExtraInfo );
224
225 dlg->ShowModal();
226 dlg->Destroy();
227}
228
229
230void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
231{
232 if( !wxTheApp || !wxTheApp->GetTopWindow() )
233 {
234 wxLogTrace( traceConfirm, wxS( "%s %s" ), aMessage, aExtraInfo );
235 return;
236 }
237
238 if( !wxTheApp->IsGUI() )
239 {
240 wxFprintf( stdout, "%s %s", aMessage, aExtraInfo );
241 return;
242 }
243
245 int icon = wxICON_INFORMATION;
246
247 dlg = new KICAD_MESSAGE_DIALOG_BASE( aParent, aMessage, _( "Information" ),
248 wxOK | wxCENTRE | wxRESIZE_BORDER |
249 icon | wxSTAY_ON_TOP );
250
251 if( !aExtraInfo.IsEmpty() )
252 dlg->SetExtendedMessage( aExtraInfo );
253
254 dlg->ShowModal();
255 dlg->Destroy();
256}
257
258
259bool IsOK( wxWindow* aParent, const wxString& aMessage )
260{
261 // wxMessageDialog no longer responds correctly to the <ESC> key (on at least OSX and MSW)
262 // so we're now using wxRichMessageDialog.
263 //
264 // Note also that we have to repurpose an OK/Cancel version of it because otherwise wxWidgets
265 // uses "destructive" spacing for the "No" button.
266
267#ifdef __APPLE__
268 // Why is wxICON_QUESTION a light-bulb on Mac? That has more of a hint or info connotation.
269 int icon = wxICON_WARNING;
270#else
271 int icon = wxICON_QUESTION;
272#endif
273
274#if !defined( __WXGTK__ )
275 KICAD_RICH_MESSAGE_DIALOG_BASE dlg( aParent, aMessage, _( "Confirmation" ),
276 wxOK | wxCANCEL | wxOK_DEFAULT |
277 wxCENTRE | icon | wxSTAY_ON_TOP );
278#else
279 wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
280 wxOK | wxCANCEL | wxOK_DEFAULT | wxCENTRE | icon | wxSTAY_ON_TOP );
281#endif
282
283 dlg.SetOKCancelLabels( _( "&Yes" ), _( "&No" ) );
284
285 return dlg.ShowModal() == wxID_OK;
286}
287
288
289int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
290 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 AskOverrideLock(wxWindow *aParent, const wxString &aMessage)
Display a dialog indicating the file is already open, with an option to reset the lock.
Definition confirm.cpp:42
int SelectSingleOption(wxWindow *aParent, const wxString &aTitle, const wxString &aMessage, const wxArrayString &aOptions)
Display a dialog with radioboxes asking the user to select an option.
Definition confirm.cpp:289
int OKOrCancelDialog(wxWindow *aParent, const wxString &aWarning, const wxString &aMessage, const wxString &aDetailedMessage, const wxString &aOKLabel, const wxString &aCancelLabel, bool *aApplyToAll)
Display a warning dialog with aMessage and returns the user response.
Definition confirm.cpp:150
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition confirm.cpp:259
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:230
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:131
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
int UnsavedChangesDialog(wxWindow *parent, const wxString &aMessage, bool *aApplyToAll)
A specialized version of HandleUnsavedChanges which handles an apply-to-all checkbox.
Definition confirm.cpp:64
bool ConfirmRevertDialog(wxWindow *parent, const wxString &aMessage)
Display a confirmation dialog for a revert action.
Definition confirm.cpp:118
static int g_lastUnsavedChangesResult
Definition confirm.cpp:129
int GetLastUnsavedChangesResponse()
Return the result code from the last call to HandleUnsavedChanges(): wxID_YES, wxID_NO or wxID_CANCEL...
Definition confirm.cpp:144
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition confirm.cpp:177
This file is part of the common library.
#define KICAD_RICH_MESSAGE_DIALOG_BASE
Definition confirm.h:47
#define KICAD_MESSAGE_DIALOG_BASE
Definition confirm.h:46
#define _(s)
static const wxChar traceConfirm[]
Flag to enable confirmation dialog debugging output.
Definition confirm.cpp:39