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
129bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
130 const std::function<bool()>& aSaveFunction )
131{
132 switch( UnsavedChangesDialog( aParent, aMessage ) )
133 {
134 case wxID_YES: return aSaveFunction();
135 case wxID_NO: return true;
136 default:
137 case wxID_CANCEL: return false;
138 }
139}
140
141
142int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
143 const wxString& aDetailedMessage, const wxString& aOKLabel,
144 const wxString& aCancelLabel, bool* aApplyToAll )
145{
146 KICAD_RICH_MESSAGE_DIALOG_BASE dlg( aParent, aMessage, aWarning,
147 wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
148
149 dlg.SetOKCancelLabels( ( aOKLabel.IsEmpty() ) ? _( "&OK" ) : aOKLabel,
150 ( aCancelLabel.IsEmpty() ) ? _( "&Cancel" ) : aCancelLabel );
151
152 if( !aDetailedMessage.IsEmpty() )
153 dlg.SetExtendedMessage( aDetailedMessage );
154
155 if( aApplyToAll )
156 dlg.ShowCheckBox( _( "&Apply to all" ), true );
157
158 int ret = dlg.ShowModal();
159
160 if( aApplyToAll )
161 *aApplyToAll = dlg.IsCheckBoxChecked();
162
163 // Returns wxID_OK or wxID_CANCEL
164 return ret;
165}
166
167
168// DisplayError should be deprecated, use DisplayErrorMessage instead
169void DisplayError( wxWindow* aParent, const wxString& aText )
170{
171 if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
172 {
173 wxLogError( "%s", aText );
174 return;
175 }
176
177 if( !wxTheApp->IsGUI() )
178 {
179 wxFprintf( stderr, aText );
180 return;
181 }
182
184
185 dlg = new KICAD_MESSAGE_DIALOG_BASE( aParent, aText, _( "Error" ),
186 wxOK | wxCENTRE | wxRESIZE_BORDER |
187 wxICON_ERROR | wxSTAY_ON_TOP );
188
189 dlg->ShowModal();
190 dlg->Destroy();
191}
192
193
194void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
195{
196 if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
197 {
198 wxLogError( "%s %s", aText, aExtraInfo );
199 return;
200 }
201
202 if( !wxTheApp->IsGUI() )
203 {
204 wxFprintf( stderr, aText );
205 return;
206 }
207
209
210 dlg = new KICAD_MESSAGE_DIALOG_BASE( aParent, aText, _( "Error" ),
211 wxOK | wxCENTRE | wxRESIZE_BORDER |
212 wxICON_ERROR | wxSTAY_ON_TOP );
213
214 if( !aExtraInfo.IsEmpty() )
215 dlg->SetExtendedMessage( aExtraInfo );
216
217 dlg->ShowModal();
218 dlg->Destroy();
219}
220
221
222void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
223{
224 if( !wxTheApp || !wxTheApp->GetTopWindow() )
225 {
226 wxLogTrace( traceConfirm, wxS( "%s %s" ), aMessage, aExtraInfo );
227 return;
228 }
229
230 if( !wxTheApp->IsGUI() )
231 {
232 wxFprintf( stdout, "%s %s", aMessage, aExtraInfo );
233 return;
234 }
235
237 int icon = wxICON_INFORMATION;
238
239 dlg = new KICAD_MESSAGE_DIALOG_BASE( aParent, aMessage, _( "Information" ),
240 wxOK | wxCENTRE | wxRESIZE_BORDER |
241 icon | wxSTAY_ON_TOP );
242
243 if( !aExtraInfo.IsEmpty() )
244 dlg->SetExtendedMessage( aExtraInfo );
245
246 dlg->ShowModal();
247 dlg->Destroy();
248}
249
250
251bool IsOK( wxWindow* aParent, const wxString& aMessage )
252{
253 // wxMessageDialog no longer responds correctly to the <ESC> key (on at least OSX and MSW)
254 // so we're now using wxRichMessageDialog.
255 //
256 // Note also that we have to repurpose an OK/Cancel version of it because otherwise wxWidgets
257 // uses "destructive" spacing for the "No" button.
258
259#ifdef __APPLE__
260 // Why is wxICON_QUESTION a light-bulb on Mac? That has more of a hint or info connotation.
261 int icon = wxICON_WARNING;
262#else
263 int icon = wxICON_QUESTION;
264#endif
265
266#if !defined( __WXGTK__ )
267 KICAD_RICH_MESSAGE_DIALOG_BASE dlg( aParent, aMessage, _( "Confirmation" ),
268 wxOK | wxCANCEL | wxOK_DEFAULT |
269 wxCENTRE | icon | wxSTAY_ON_TOP );
270#else
271 wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
272 wxOK | wxCANCEL | wxOK_DEFAULT | wxCENTRE | icon | wxSTAY_ON_TOP );
273#endif
274
275 dlg.SetOKCancelLabels( _( "&Yes" ), _( "&No" ) );
276
277 return dlg.ShowModal() == wxID_OK;
278}
279
280
281int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
282 const wxString& aMessage, const wxArrayString& aOptions )
283{
284 wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
285
286 if( dlg.ShowModal() != wxID_OK )
287 return -1;
288
289 return dlg.GetSelection();
290}
291
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:281
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:142
bool IsOK(wxWindow *aParent, const wxString &aMessage)
Display a yes/no dialog with aMessage and returns the user response.
Definition: confirm.cpp:251
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:222
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:129
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:194
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
void DisplayError(wxWindow *aParent, const wxString &aText)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:169
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