2013-10-18 13:32:22 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Jean-Pierre Charras, jp.charras at wanadoo.fr
|
2024-05-23 11:59:45 +00:00
|
|
|
* Copyright (C) 1992-2023, 2024 KiCad Developers, see AUTHORS.txt for contributors.
|
2013-10-18 13:32:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
2024-04-27 19:57:24 +00:00
|
|
|
#include <confirm.h>
|
|
|
|
|
|
|
|
#include <functional>
|
2022-06-21 02:22:52 +00:00
|
|
|
#include <wx/app.h>
|
2013-10-18 13:32:22 +00:00
|
|
|
#include <wx/stockitem.h>
|
2017-07-20 13:03:33 +00:00
|
|
|
#include <wx/richmsgdlg.h>
|
2024-04-27 19:57:24 +00:00
|
|
|
#include <wx/msgdlg.h>
|
2020-10-18 11:31:07 +00:00
|
|
|
#include <wx/choicdlg.h>
|
2022-12-08 04:55:47 +00:00
|
|
|
#include <wx/crt.h>
|
2017-11-06 08:35:05 +00:00
|
|
|
|
2007-12-05 20:54:11 +00:00
|
|
|
|
2024-05-23 11:59:45 +00:00
|
|
|
/**
|
|
|
|
* Flag to enable confirmation dialog debugging output.
|
|
|
|
*
|
|
|
|
* @ingroup trace_env_vars
|
|
|
|
*/
|
|
|
|
static const wxChar traceConfirm[] = wxT( "KICAD_CONFIRM" );
|
|
|
|
|
|
|
|
|
2023-07-21 22:40:19 +00:00
|
|
|
bool AskOverrideLock( wxWindow* aParent, const wxString& aMessage )
|
2021-08-31 14:03:40 +00:00
|
|
|
{
|
2021-08-31 21:38:45 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// wxMessageDialog gets the button spacing wrong on Mac so we have to use wxRichMessageDialog.
|
|
|
|
// Note that its warning icon is more like wxMessageDialog's error icon, so we use it instead
|
|
|
|
// of wxICON_ERROR.
|
2022-07-24 07:46:19 +00:00
|
|
|
wxRichMessageDialog dlg( aParent, aMessage, _( "File Open Warning" ),
|
2021-08-31 21:38:45 +00:00
|
|
|
wxYES_NO | wxICON_WARNING | wxCENTER );
|
|
|
|
dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." )
|
|
|
|
+ wxS( "\n" ) );
|
2022-07-24 07:46:19 +00:00
|
|
|
dlg.SetYesNoLabels( _( "Cancel" ), _( "Open Anyway" ) );
|
2021-08-31 21:38:45 +00:00
|
|
|
#else
|
2022-07-24 07:46:19 +00:00
|
|
|
wxMessageDialog dlg( aParent, aMessage, _( "File Open Warning" ),
|
2021-08-31 21:38:45 +00:00
|
|
|
wxYES_NO | wxICON_ERROR | wxCENTER );
|
|
|
|
dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." ) );
|
2022-07-24 07:46:19 +00:00
|
|
|
dlg.SetYesNoLabels( _( "Cancel" ), _( "Open Anyway" ) );
|
2021-08-31 21:38:45 +00:00
|
|
|
#endif
|
2021-08-31 14:03:40 +00:00
|
|
|
|
|
|
|
return dlg.ShowModal() == wxID_NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
|
2012-03-08 17:47:23 +00:00
|
|
|
{
|
2019-06-17 15:59:39 +00:00
|
|
|
static bool s_apply_to_all = false;
|
|
|
|
|
2021-06-17 17:23:58 +00:00
|
|
|
wxRichMessageDialog dlg( parent, aMessage, _( "Save Changes?" ),
|
2019-06-15 16:05:33 +00:00
|
|
|
wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
|
2021-08-31 21:38:45 +00:00
|
|
|
dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." )
|
|
|
|
+ wxS( "\n" ) );
|
2019-06-17 15:59:39 +00:00
|
|
|
dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
|
2018-08-01 23:06:12 +00:00
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
if( aApplyToAll )
|
2019-06-17 15:59:39 +00:00
|
|
|
dlg.ShowCheckBox( _( "Apply to all" ), s_apply_to_all );
|
2018-08-01 23:06:12 +00:00
|
|
|
|
|
|
|
int ret = dlg.ShowModal();
|
|
|
|
|
|
|
|
if( aApplyToAll )
|
2019-06-17 15:59:39 +00:00
|
|
|
{
|
2019-06-15 16:05:33 +00:00
|
|
|
*aApplyToAll = dlg.IsCheckBoxChecked();
|
2019-06-17 15:59:39 +00:00
|
|
|
s_apply_to_all = dlg.IsCheckBoxChecked();
|
|
|
|
}
|
2018-08-01 23:06:12 +00:00
|
|
|
|
|
|
|
// Returns wxID_YES, wxID_NO, or wxID_CANCEL
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage )
|
2018-10-03 21:44:17 +00:00
|
|
|
{
|
2019-06-17 15:59:39 +00:00
|
|
|
#ifdef __APPLE__
|
2021-08-31 21:38:45 +00:00
|
|
|
// wxMessageDialog gets the button order (and spacing) wrong on Mac so we have to use
|
|
|
|
// wxRichMessageDialog.
|
2019-06-17 15:59:39 +00:00
|
|
|
return UnsavedChangesDialog( parent, aMessage, nullptr );
|
|
|
|
#else
|
2021-09-30 03:00:08 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
// wxMessageDialog on windows invokes TaskDialogIndirect which is a native function for a dialog
|
|
|
|
// As a result it skips wxWidgets for modal management...and we don't parent frames properly
|
|
|
|
// among other things for Windows to do the right thing by default
|
2024-05-23 11:59:45 +00:00
|
|
|
// Disable all the windows manually to avoid being able to hit this dialog from the tool frame
|
|
|
|
// and kicad frame at the same time.
|
2021-09-30 03:00:08 +00:00
|
|
|
wxWindowDisabler disable( true );
|
|
|
|
#endif
|
|
|
|
|
2021-06-17 17:23:58 +00:00
|
|
|
wxMessageDialog dlg( parent, aMessage, _( "Save Changes?" ),
|
2019-06-15 16:05:33 +00:00
|
|
|
wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
|
|
|
|
dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
|
2019-06-17 15:59:39 +00:00
|
|
|
dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
|
2018-10-03 21:44:17 +00:00
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
// Returns wxID_YES, wxID_NO, or wxID_CANCEL
|
|
|
|
return dlg.ShowModal();
|
2019-06-17 15:59:39 +00:00
|
|
|
#endif
|
2019-06-15 16:05:33 +00:00
|
|
|
}
|
2018-10-03 21:44:17 +00:00
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
|
|
|
|
bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
|
|
|
|
{
|
|
|
|
wxMessageDialog dlg( parent, aMessage, wxEmptyString,
|
|
|
|
wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
|
|
|
|
dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
|
2019-06-17 15:59:39 +00:00
|
|
|
dlg.SetOKCancelLabels( _( "Revert" ), _( "Cancel" ) );
|
2019-06-15 16:05:33 +00:00
|
|
|
|
|
|
|
return dlg.ShowModal() == wxID_OK;
|
2018-10-03 21:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-11 20:46:03 +00:00
|
|
|
bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
|
|
|
|
const std::function<bool()>& aSaveFunction )
|
|
|
|
{
|
2019-06-15 16:05:33 +00:00
|
|
|
switch( UnsavedChangesDialog( aParent, aMessage ) )
|
2018-08-11 20:46:03 +00:00
|
|
|
{
|
|
|
|
case wxID_YES: return aSaveFunction();
|
|
|
|
case wxID_NO: return true;
|
|
|
|
default:
|
|
|
|
case wxID_CANCEL: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
|
2021-07-27 12:22:27 +00:00
|
|
|
const wxString& aDetailedMessage, const wxString& aOKLabel,
|
|
|
|
const wxString& aCancelLabel, bool* aApplyToAll )
|
2018-08-01 23:06:12 +00:00
|
|
|
{
|
2020-02-14 19:07:27 +00:00
|
|
|
wxRichMessageDialog dlg( aParent, aMessage, aWarning,
|
2019-06-15 16:05:33 +00:00
|
|
|
wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
|
2020-12-11 22:44:10 +00:00
|
|
|
|
2021-07-27 12:22:27 +00:00
|
|
|
dlg.SetOKCancelLabels( ( aOKLabel.IsEmpty() ) ? _( "OK" ) : aOKLabel,
|
|
|
|
( aCancelLabel.IsEmpty() ) ? _( "Cancel" ) : aCancelLabel );
|
2012-03-08 17:47:23 +00:00
|
|
|
|
2020-02-14 19:07:27 +00:00
|
|
|
if( !aDetailedMessage.IsEmpty() )
|
2020-12-11 22:44:10 +00:00
|
|
|
dlg.SetExtendedMessage( aDetailedMessage );
|
2020-02-14 19:07:27 +00:00
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
if( aApplyToAll )
|
|
|
|
dlg.ShowCheckBox( _( "Apply to all" ), true );
|
2018-08-01 13:36:42 +00:00
|
|
|
|
2012-03-08 17:47:23 +00:00
|
|
|
int ret = dlg.ShowModal();
|
2016-05-06 15:15:30 +00:00
|
|
|
|
2018-08-01 13:36:42 +00:00
|
|
|
if( aApplyToAll )
|
2019-06-15 16:05:33 +00:00
|
|
|
*aApplyToAll = dlg.IsCheckBoxChecked();
|
2018-08-01 13:36:42 +00:00
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
// Returns wxID_OK or wxID_CANCEL
|
2012-03-08 17:47:23 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-11-08 16:37:25 +00:00
|
|
|
|
2013-10-18 13:32:22 +00:00
|
|
|
|
2017-07-20 14:06:41 +00:00
|
|
|
// DisplayError should be deprecated, use DisplayErrorMessage instead
|
2019-08-20 17:22:30 +00:00
|
|
|
void DisplayError( wxWindow* aParent, const wxString& aText, int aDisplayTime )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
|
|
|
|
{
|
2022-08-09 18:57:49 +00:00
|
|
|
wxLogError( "%s", aText );
|
2022-06-21 02:22:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 14:19:15 +00:00
|
|
|
if( !wxTheApp->IsGUI() )
|
2022-12-08 04:55:47 +00:00
|
|
|
{
|
|
|
|
wxFprintf( stderr, aText );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-20 17:22:30 +00:00
|
|
|
wxMessageDialog* dlg;
|
|
|
|
int icon = aDisplayTime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
|
2017-07-20 13:03:33 +00:00
|
|
|
|
2019-08-20 17:22:30 +00:00
|
|
|
dlg = new wxMessageDialog( aParent, aText, _( "Warning" ),
|
|
|
|
wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
|
2007-12-05 20:54:11 +00:00
|
|
|
|
2019-08-20 17:22:30 +00:00
|
|
|
dlg->ShowModal();
|
|
|
|
dlg->Destroy();
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 20:54:11 +00:00
|
|
|
|
2017-09-23 09:20:10 +00:00
|
|
|
void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
|
2007-05-06 16:03:28 +00:00
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
|
|
|
|
{
|
2022-08-09 18:57:49 +00:00
|
|
|
wxLogError( "%s %s", aText, aExtraInfo );
|
2022-06-21 02:22:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 14:19:15 +00:00
|
|
|
if( !wxTheApp->IsGUI() )
|
2022-12-08 04:55:47 +00:00
|
|
|
{
|
|
|
|
wxFprintf( stderr, aText );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-14 17:29:38 +00:00
|
|
|
wxMessageDialog* dlg;
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2019-12-14 17:29:38 +00:00
|
|
|
dlg = new wxMessageDialog( aParent, aText, _( "Error" ),
|
|
|
|
wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR | wxSTAY_ON_TOP );
|
2007-05-06 16:03:28 +00:00
|
|
|
|
2017-07-20 13:03:33 +00:00
|
|
|
if( !aExtraInfo.IsEmpty() )
|
2019-12-14 17:29:38 +00:00
|
|
|
dlg->SetExtendedMessage( aExtraInfo );
|
2017-07-20 13:03:33 +00:00
|
|
|
|
|
|
|
dlg->ShowModal();
|
|
|
|
dlg->Destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-23 09:20:10 +00:00
|
|
|
void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
|
2017-07-20 13:03:33 +00:00
|
|
|
{
|
2022-06-21 02:22:52 +00:00
|
|
|
if( !wxTheApp || !wxTheApp->GetTopWindow() )
|
|
|
|
{
|
2024-05-23 11:59:45 +00:00
|
|
|
wxLogTrace( traceConfirm, wxS( "%s %s" ), aMessage, aExtraInfo );
|
2022-06-21 02:22:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-23 14:19:15 +00:00
|
|
|
if( !wxTheApp->IsGUI() )
|
2022-12-08 04:55:47 +00:00
|
|
|
{
|
|
|
|
wxFprintf( stdout, "%s %s", aMessage, aExtraInfo );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-31 18:41:40 +00:00
|
|
|
wxMessageDialog* dlg;
|
|
|
|
int icon = wxICON_INFORMATION;
|
2017-07-20 13:03:33 +00:00
|
|
|
|
2021-08-31 18:41:40 +00:00
|
|
|
dlg = new wxMessageDialog( aParent, aMessage, _( "Information" ),
|
|
|
|
wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
|
2017-07-20 13:03:33 +00:00
|
|
|
|
|
|
|
if( !aExtraInfo.IsEmpty() )
|
2021-08-31 18:41:40 +00:00
|
|
|
dlg->SetExtendedMessage( aExtraInfo );
|
2017-07-20 13:03:33 +00:00
|
|
|
|
|
|
|
dlg->ShowModal();
|
|
|
|
dlg->Destroy();
|
2007-05-06 16:03:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-18 13:32:22 +00:00
|
|
|
bool IsOK( wxWindow* aParent, const wxString& aMessage )
|
|
|
|
{
|
2022-09-11 11:20:39 +00:00
|
|
|
// wxMessageDialog no longer responds correctly to the <ESC> key (on at least OSX and MSW)
|
|
|
|
// so we're now using wxRichMessageDialog.
|
|
|
|
//
|
|
|
|
// Note also that we have to repurpose an OK/Cancel version of it because otherwise wxWidgets
|
|
|
|
// uses "destructive" spacing for the "No" button.
|
2013-10-18 13:32:22 +00:00
|
|
|
|
2022-09-11 11:20:39 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
// Why is wxICON_QUESTION a light-bulb on Mac? That has more of a hint or info connotation.
|
|
|
|
int icon = wxICON_WARNING;
|
|
|
|
#else
|
|
|
|
int icon = wxICON_QUESTION;
|
|
|
|
#endif
|
|
|
|
|
2023-01-19 13:28:48 +00:00
|
|
|
#if !defined( __WXGTK__ )
|
2022-09-11 11:20:39 +00:00
|
|
|
wxRichMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
|
|
|
|
wxOK | wxCANCEL | wxOK_DEFAULT | wxCENTRE | icon | wxSTAY_ON_TOP );
|
2023-01-19 13:28:48 +00:00
|
|
|
#else
|
|
|
|
wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
|
|
|
|
wxOK | wxCANCEL | wxOK_DEFAULT | wxCENTRE | icon | wxSTAY_ON_TOP );
|
|
|
|
#endif
|
2022-09-11 11:20:39 +00:00
|
|
|
|
|
|
|
dlg.SetOKCancelLabels( _( "Yes" ), _( "No" ) );
|
|
|
|
|
|
|
|
return dlg.ShowModal() == wxID_OK;
|
2013-10-18 13:32:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-15 16:05:33 +00:00
|
|
|
int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
|
|
|
|
const wxString& aMessage, const wxArrayString& aOptions )
|
2017-11-06 08:35:05 +00:00
|
|
|
{
|
2018-03-02 16:30:10 +00:00
|
|
|
wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
|
2017-11-06 08:35:05 +00:00
|
|
|
|
2018-03-02 16:30:10 +00:00
|
|
|
if( dlg.ShowModal() != wxID_OK )
|
|
|
|
return -1;
|
2017-11-06 08:35:05 +00:00
|
|
|
|
2018-03-02 16:30:10 +00:00
|
|
|
return dlg.GetSelection();
|
2017-11-06 08:35:05 +00:00
|
|
|
}
|
|
|
|
|