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.

343 lines
11 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 (C) 1992-2021 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 <wx/stockitem.h>
  25. #include <wx/richmsgdlg.h>
  26. #include <wx/choicdlg.h>
  27. #include <confirm.h>
  28. #include <dialogs/html_message_box.h>
  29. #include <functional>
  30. #include <unordered_map>
  31. // Set of dialogs that have been chosen not to be shown again
  32. static std::unordered_map<unsigned long, int> doNotShowAgainDlgs;
  33. KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage, const wxString& aCaption,
  34. long aStyle )
  35. : wxRichMessageDialog( aParent, aMessage, aCaption, aStyle | wxCENTRE | wxSTAY_ON_TOP ),
  36. m_hash( 0 ),
  37. m_cancelMeansCancel( true )
  38. {
  39. }
  40. KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage, KD_TYPE aType,
  41. const wxString& aCaption )
  42. : wxRichMessageDialog( aParent, aMessage, getCaption( aType, aCaption ), getStyle( aType ) ),
  43. m_hash( 0 ),
  44. m_cancelMeansCancel( true )
  45. {
  46. }
  47. void KIDIALOG::DoNotShowCheckbox( wxString aUniqueId, int line )
  48. {
  49. ShowCheckBox( _( "Do not show again" ), false );
  50. m_hash = std::hash<wxString>{}( aUniqueId ) + line;
  51. }
  52. bool KIDIALOG::DoNotShowAgain() const
  53. {
  54. return doNotShowAgainDlgs.count( m_hash ) > 0;
  55. }
  56. void KIDIALOG::ForceShowAgain()
  57. {
  58. doNotShowAgainDlgs.erase( m_hash );
  59. }
  60. bool KIDIALOG::Show( bool aShow )
  61. {
  62. // We should check the do-not-show-again setting only when the dialog is displayed
  63. if( aShow )
  64. {
  65. // Check if this dialog should be shown to the user
  66. auto it = doNotShowAgainDlgs.find( m_hash );
  67. if( it != doNotShowAgainDlgs.end() )
  68. return it->second;
  69. }
  70. int ret = wxRichMessageDialog::Show( aShow );
  71. // Has the user asked not to show the dialog again?
  72. // Note that we don't save a Cancel value unless the Cancel button is being used for some
  73. // other function (which is actually more common than it being used for Cancel).
  74. if( IsCheckBoxChecked() && (!m_cancelMeansCancel || ret != wxID_CANCEL ) )
  75. doNotShowAgainDlgs[m_hash] = ret;
  76. return ret;
  77. }
  78. int KIDIALOG::ShowModal()
  79. {
  80. // Check if this dialog should be shown to the user
  81. auto it = doNotShowAgainDlgs.find( m_hash );
  82. if( it != doNotShowAgainDlgs.end() )
  83. return it->second;
  84. int ret = wxRichMessageDialog::ShowModal();
  85. // Has the user asked not to show the dialog again?
  86. // Note that we don't save a Cancel value unless the Cancel button is being used for some
  87. // other function (which is actually more common than it being used for Cancel).
  88. if( IsCheckBoxChecked() && (!m_cancelMeansCancel || ret != wxID_CANCEL ) )
  89. doNotShowAgainDlgs[m_hash] = ret;
  90. return ret;
  91. }
  92. wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
  93. {
  94. if( !aCaption.IsEmpty() )
  95. return aCaption;
  96. switch( aType )
  97. {
  98. case KD_NONE: /* fall through */
  99. case KD_INFO: return _( "Message" );
  100. case KD_QUESTION: return _( "Question" );
  101. case KD_WARNING: return _( "Warning" );
  102. case KD_ERROR: return _( "Error" );
  103. }
  104. return wxEmptyString;
  105. }
  106. long KIDIALOG::getStyle( KD_TYPE aType )
  107. {
  108. long style = wxOK | wxCENTRE | wxSTAY_ON_TOP;
  109. switch( aType )
  110. {
  111. case KD_NONE: break;
  112. case KD_INFO: style |= wxICON_INFORMATION; break;
  113. case KD_QUESTION: style |= wxICON_QUESTION; break;
  114. case KD_WARNING: style |= wxICON_WARNING; break;
  115. case KD_ERROR: style |= wxICON_ERROR; break;
  116. }
  117. return style;
  118. }
  119. bool OverrideLock( wxWindow* aParent, const wxString& aMessage )
  120. {
  121. #ifdef __APPLE__
  122. // wxMessageDialog gets the button spacing wrong on Mac so we have to use wxRichMessageDialog.
  123. // Note that its warning icon is more like wxMessageDialog's error icon, so we use it instead
  124. // of wxICON_ERROR.
  125. wxRichMessageDialog dlg( aParent, aMessage, _( "File Open Error" ),
  126. wxYES_NO | wxICON_WARNING | wxCENTER );
  127. dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." )
  128. + wxS( "\n" ) );
  129. dlg.SetYesNoLabels( _( "OK" ), _( "Open Anyway" ) );
  130. #else
  131. wxMessageDialog dlg( aParent, aMessage, _( "File Open Error" ),
  132. wxYES_NO | wxICON_ERROR | wxCENTER );
  133. dlg.SetExtendedMessage( _( "Interleaved saves may produce very unexpected results." ) );
  134. dlg.SetYesNoLabels( _( "OK" ), _( "Open Anyway" ) );
  135. #endif
  136. return dlg.ShowModal() == wxID_NO;
  137. }
  138. int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage, bool* aApplyToAll )
  139. {
  140. static bool s_apply_to_all = false;
  141. wxRichMessageDialog dlg( parent, aMessage, _( "Save Changes?" ),
  142. wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
  143. dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." )
  144. + wxS( "\n" ) );
  145. dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
  146. if( aApplyToAll )
  147. dlg.ShowCheckBox( _( "Apply to all" ), s_apply_to_all );
  148. int ret = dlg.ShowModal();
  149. if( aApplyToAll )
  150. {
  151. *aApplyToAll = dlg.IsCheckBoxChecked();
  152. s_apply_to_all = dlg.IsCheckBoxChecked();
  153. }
  154. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  155. return ret;
  156. }
  157. int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage )
  158. {
  159. #ifdef __APPLE__
  160. // wxMessageDialog gets the button order (and spacing) wrong on Mac so we have to use
  161. // wxRichMessageDialog.
  162. return UnsavedChangesDialog( parent, aMessage, nullptr );
  163. #else
  164. #ifdef _WIN32
  165. // wxMessageDialog on windows invokes TaskDialogIndirect which is a native function for a dialog
  166. // As a result it skips wxWidgets for modal management...and we don't parent frames properly
  167. // among other things for Windows to do the right thing by default
  168. // Disable all the windows manually to avoid being able to hit this dialog from the tool frame and kicad frame at the same time
  169. wxWindowDisabler disable( true );
  170. #endif
  171. wxMessageDialog dlg( parent, aMessage, _( "Save Changes?" ),
  172. wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
  173. dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
  174. dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
  175. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  176. return dlg.ShowModal();
  177. #endif
  178. }
  179. bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
  180. {
  181. wxMessageDialog dlg( parent, aMessage, wxEmptyString,
  182. wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
  183. dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
  184. dlg.SetOKCancelLabels( _( "Revert" ), _( "Cancel" ) );
  185. return dlg.ShowModal() == wxID_OK;
  186. }
  187. bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
  188. const std::function<bool()>& aSaveFunction )
  189. {
  190. switch( UnsavedChangesDialog( aParent, aMessage ) )
  191. {
  192. case wxID_YES: return aSaveFunction();
  193. case wxID_NO: return true;
  194. default:
  195. case wxID_CANCEL: return false;
  196. }
  197. }
  198. int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
  199. const wxString& aDetailedMessage, const wxString& aOKLabel,
  200. const wxString& aCancelLabel, bool* aApplyToAll )
  201. {
  202. wxRichMessageDialog dlg( aParent, aMessage, aWarning,
  203. wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
  204. dlg.SetOKCancelLabels( ( aOKLabel.IsEmpty() ) ? _( "OK" ) : aOKLabel,
  205. ( aCancelLabel.IsEmpty() ) ? _( "Cancel" ) : aCancelLabel );
  206. if( !aDetailedMessage.IsEmpty() )
  207. dlg.SetExtendedMessage( aDetailedMessage );
  208. if( aApplyToAll )
  209. dlg.ShowCheckBox( _( "Apply to all" ), true );
  210. int ret = dlg.ShowModal();
  211. if( aApplyToAll )
  212. *aApplyToAll = dlg.IsCheckBoxChecked();
  213. // Returns wxID_OK or wxID_CANCEL
  214. return ret;
  215. }
  216. // DisplayError should be deprecated, use DisplayErrorMessage instead
  217. void DisplayError( wxWindow* aParent, const wxString& aText, int aDisplayTime )
  218. {
  219. wxMessageDialog* dlg;
  220. int icon = aDisplayTime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
  221. dlg = new wxMessageDialog( aParent, aText, _( "Warning" ),
  222. wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
  223. dlg->ShowModal();
  224. dlg->Destroy();
  225. }
  226. void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
  227. {
  228. wxMessageDialog* dlg;
  229. dlg = new wxMessageDialog( aParent, aText, _( "Error" ),
  230. wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR | wxSTAY_ON_TOP );
  231. if( !aExtraInfo.IsEmpty() )
  232. dlg->SetExtendedMessage( aExtraInfo );
  233. dlg->ShowModal();
  234. dlg->Destroy();
  235. }
  236. void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
  237. {
  238. wxMessageDialog* dlg;
  239. int icon = wxICON_INFORMATION;
  240. dlg = new wxMessageDialog( aParent, aMessage, _( "Information" ),
  241. wxOK | wxCENTRE | wxRESIZE_BORDER | icon | wxSTAY_ON_TOP );
  242. if( !aExtraInfo.IsEmpty() )
  243. dlg->SetExtendedMessage( aExtraInfo );
  244. dlg->ShowModal();
  245. dlg->Destroy();
  246. }
  247. bool IsOK( wxWindow* aParent, const wxString& aMessage )
  248. {
  249. wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
  250. wxYES_NO | wxCENTRE | wxICON_QUESTION | wxSTAY_ON_TOP );
  251. dlg.SetEscapeId( wxID_NO );
  252. return dlg.ShowModal() == wxID_YES;
  253. }
  254. int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
  255. const wxString& aMessage, const wxArrayString& aOptions )
  256. {
  257. wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
  258. if( dlg.ShowModal() != wxID_OK )
  259. return -1;
  260. return dlg.GetSelection();
  261. }