You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
9.2 KiB

18 years ago
18 years ago
18 years ago
  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. #include <confirm.h>
  25. #include <functional>
  26. #include <wx/app.h>
  27. #include <wx/stockitem.h>
  28. #include <wx/richmsgdlg.h>
  29. #include <wx/msgdlg.h>
  30. #include <wx/choicdlg.h>
  31. #include <wx/crt.h>
  32. /**
  33. * Flag to enable confirmation dialog debugging output.
  34. *
  35. * @ingroup trace_env_vars
  36. */
  37. static const wxChar traceConfirm[] = wxT( "KICAD_CONFIRM" );
  38. bool AskOverrideLock( wxWindow* aParent, const wxString& aMessage )
  39. {
  40. #ifdef __APPLE__
  41. // wxMessageDialog gets the button spacing wrong on Mac so we have to use wxRichMessageDialog.
  42. // Note that its warning icon is more like wxMessageDialog's error icon, so we use it instead
  43. // of wxICON_ERROR.
  44. wxRichMessageDialog dlg( aParent, aMessage, _( "File Open Warning" ),
  45. wxYES_NO | wxICON_WARNING | wxCENTER );
  46. dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." )
  47. + wxS( "\n" ) );
  48. dlg.SetYesNoLabels( _( "&Cancel" ), _( "&Open Anyway" ) );
  49. #else
  50. wxMessageDialog dlg( aParent, aMessage, _( "File Open Warning" ),
  51. wxYES_NO | wxICON_ERROR | wxCENTER );
  52. dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." ) );
  53. dlg.SetYesNoLabels( _( "&Cancel" ), _( "&Open Anyway" ) );
  54. #endif
  55. return dlg.ShowModal() == wxID_NO;
  56. }
  57. int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
  58. {
  59. static bool s_apply_to_all = false;
  60. wxRichMessageDialog dlg( parent, aMessage, _( "Save Changes?" ),
  61. wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
  62. dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." )
  63. + wxS( "\n" ) );
  64. dlg.SetYesNoLabels( _( "&Save" ), _( "&Discard Changes" ) );
  65. if( aApplyToAll )
  66. dlg.ShowCheckBox( _( "&Apply to all" ), s_apply_to_all );
  67. int ret = dlg.ShowModal();
  68. if( aApplyToAll )
  69. {
  70. *aApplyToAll = dlg.IsCheckBoxChecked();
  71. s_apply_to_all = dlg.IsCheckBoxChecked();
  72. }
  73. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  74. return ret;
  75. }
  76. int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage )
  77. {
  78. #ifdef __APPLE__
  79. // wxMessageDialog gets the button order (and spacing) wrong on Mac so we have to use
  80. // wxRichMessageDialog.
  81. return UnsavedChangesDialog( parent, aMessage, nullptr );
  82. #else
  83. #ifdef _WIN32
  84. // wxMessageDialog on windows invokes TaskDialogIndirect which is a native function for a dialog
  85. // As a result it skips wxWidgets for modal management...and we don't parent frames properly
  86. // among other things for Windows to do the right thing by default
  87. // Disable all the windows manually to avoid being able to hit this dialog from the tool frame
  88. // and kicad frame at the same time.
  89. wxWindowDisabler disable( true );
  90. #endif
  91. wxMessageDialog dlg( parent, aMessage, _( "Save Changes?" ),
  92. wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
  93. dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
  94. dlg.SetYesNoLabels( _( "&Save" ), _( "&Discard Changes" ) );
  95. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  96. return dlg.ShowModal();
  97. #endif
  98. }
  99. bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
  100. {
  101. wxMessageDialog dlg( parent, aMessage, wxEmptyString,
  102. wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
  103. dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
  104. dlg.SetOKCancelLabels( _( "&Revert" ), _( "&Cancel" ) );
  105. return dlg.ShowModal() == wxID_OK;
  106. }
  107. bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
  108. const std::function<bool()>& aSaveFunction )
  109. {
  110. switch( UnsavedChangesDialog( aParent, aMessage ) )
  111. {
  112. case wxID_YES: return aSaveFunction();
  113. case wxID_NO: return true;
  114. default:
  115. case wxID_CANCEL: return false;
  116. }
  117. }
  118. int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
  119. const wxString& aDetailedMessage, const wxString& aOKLabel,
  120. const wxString& aCancelLabel, bool* aApplyToAll )
  121. {
  122. wxRichMessageDialog dlg( aParent, aMessage, aWarning,
  123. wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
  124. dlg.SetOKCancelLabels( ( aOKLabel.IsEmpty() ) ? _( "&OK" ) : aOKLabel,
  125. ( aCancelLabel.IsEmpty() ) ? _( "&Cancel" ) : aCancelLabel );
  126. if( !aDetailedMessage.IsEmpty() )
  127. dlg.SetExtendedMessage( aDetailedMessage );
  128. if( aApplyToAll )
  129. dlg.ShowCheckBox( _( "&Apply to all" ), true );
  130. int ret = dlg.ShowModal();
  131. if( aApplyToAll )
  132. *aApplyToAll = dlg.IsCheckBoxChecked();
  133. // Returns wxID_OK or wxID_CANCEL
  134. return ret;
  135. }
  136. // DisplayError should be deprecated, use DisplayErrorMessage instead
  137. void DisplayError( wxWindow* aParent, const wxString& aText )
  138. {
  139. if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
  140. {
  141. wxLogError( "%s", aText );
  142. return;
  143. }
  144. if( !wxTheApp->IsGUI() )
  145. {
  146. wxFprintf( stderr, aText );
  147. return;
  148. }
  149. wxMessageDialog* dlg;
  150. dlg = new wxMessageDialog( aParent, aText, _( "Error" ),
  151. wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR | wxSTAY_ON_TOP );
  152. dlg->ShowModal();
  153. dlg->Destroy();
  154. }
  155. void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
  156. {
  157. if( !wxTheApp || !wxTheApp->IsMainLoopRunning() )
  158. {
  159. wxLogError( "%s %s", aText, aExtraInfo );
  160. return;
  161. }
  162. if( !wxTheApp->IsGUI() )
  163. {
  164. wxFprintf( stderr, aText );
  165. return;
  166. }
  167. wxMessageDialog* dlg;
  168. dlg = new wxMessageDialog( aParent, aText, _( "Error" ),
  169. wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR | wxSTAY_ON_TOP );
  170. if( !aExtraInfo.IsEmpty() )
  171. dlg->SetExtendedMessage( aExtraInfo );
  172. dlg->ShowModal();
  173. dlg->Destroy();
  174. }
  175. void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
  176. {
  177. if( !wxTheApp || !wxTheApp->GetTopWindow() )
  178. {
  179. wxLogTrace( traceConfirm, wxS( "%s %s" ), aMessage, aExtraInfo );
  180. return;
  181. }
  182. if( !wxTheApp->IsGUI() )
  183. {
  184. wxFprintf( stdout, "%s %s", aMessage, aExtraInfo );
  185. return;
  186. }
  187. wxMessageDialog* dlg;
  188. int icon = wxICON_INFORMATION;
  189. dlg = new wxMessageDialog( aParent, aMessage, _( "Information" ),
  190. wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
  191. if( !aExtraInfo.IsEmpty() )
  192. dlg->SetExtendedMessage( aExtraInfo );
  193. dlg->ShowModal();
  194. dlg->Destroy();
  195. }
  196. bool IsOK( wxWindow* aParent, const wxString& aMessage )
  197. {
  198. // wxMessageDialog no longer responds correctly to the <ESC> key (on at least OSX and MSW)
  199. // so we're now using wxRichMessageDialog.
  200. //
  201. // Note also that we have to repurpose an OK/Cancel version of it because otherwise wxWidgets
  202. // uses "destructive" spacing for the "No" button.
  203. #ifdef __APPLE__
  204. // Why is wxICON_QUESTION a light-bulb on Mac? That has more of a hint or info connotation.
  205. int icon = wxICON_WARNING;
  206. #else
  207. int icon = wxICON_QUESTION;
  208. #endif
  209. #if !defined( __WXGTK__ )
  210. wxRichMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
  211. wxOK | wxCANCEL | wxOK_DEFAULT | wxCENTRE | icon | wxSTAY_ON_TOP );
  212. #else
  213. wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
  214. wxOK | wxCANCEL | wxOK_DEFAULT | wxCENTRE | icon | wxSTAY_ON_TOP );
  215. #endif
  216. dlg.SetOKCancelLabels( _( "&Yes" ), _( "&No" ) );
  217. return dlg.ShowModal() == wxID_OK;
  218. }
  219. int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
  220. const wxString& aMessage, const wxArrayString& aOptions )
  221. {
  222. wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
  223. if( dlg.ShowModal() != wxID_OK )
  224. return -1;
  225. return dlg.GetSelection();
  226. }