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.

314 lines
8.9 KiB

18 years ago
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-2019 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. * @file confirm.cpp
  26. * @brief utilities to display some error, warning and info short messges
  27. */
  28. #include <wx/stockitem.h>
  29. #include <wx/richmsgdlg.h>
  30. #include <confirm.h>
  31. #include <bitmaps.h>
  32. #include <html_messagebox.h>
  33. #include <functional>
  34. #include <unordered_map>
  35. // Set of dialogs that have been chosen not to be shown again
  36. static std::unordered_map<unsigned long, int> doNotShowAgainDlgs;
  37. KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage,
  38. const wxString& aCaption, long aStyle )
  39. : wxRichMessageDialog( aParent, aMessage, aCaption, aStyle | wxCENTRE | wxSTAY_ON_TOP ),
  40. m_hash( 0 )
  41. {
  42. }
  43. KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage,
  44. KD_TYPE aType, const wxString& aCaption )
  45. : wxRichMessageDialog( aParent, aMessage, getCaption( aType, aCaption ), getStyle( aType ) ),
  46. m_hash( 0 )
  47. {
  48. }
  49. void KIDIALOG::DoNotShowCheckbox( wxString aUniqueId, int line )
  50. {
  51. ShowCheckBox( _( "Do not show again" ), false );
  52. m_hash = std::hash<wxString>{}( aUniqueId ) + line;
  53. }
  54. bool KIDIALOG::DoNotShowAgain() const
  55. {
  56. return doNotShowAgainDlgs.count( m_hash ) > 0;
  57. }
  58. void KIDIALOG::ForceShowAgain()
  59. {
  60. doNotShowAgainDlgs.erase( m_hash );
  61. }
  62. bool KIDIALOG::Show( bool aShow )
  63. {
  64. // We should check the do-not-show-again setting only when the dialog is displayed
  65. if( aShow )
  66. {
  67. // Check if this dialog should be shown to the user
  68. auto it = doNotShowAgainDlgs.find( m_hash );
  69. if( it != doNotShowAgainDlgs.end() )
  70. return it->second;
  71. }
  72. bool ret = wxRichMessageDialog::Show( aShow );
  73. // Has the user asked not to show the dialog again
  74. if( IsCheckBoxChecked() )
  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. if( IsCheckBoxChecked() )
  87. doNotShowAgainDlgs[m_hash] = ret;
  88. return ret;
  89. }
  90. wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
  91. {
  92. if( !aCaption.IsEmpty() )
  93. return aCaption;
  94. switch( aType )
  95. {
  96. case KD_NONE: /* fall through */
  97. case KD_INFO: return _( "Message" );
  98. case KD_QUESTION: return _( "Question" );
  99. case KD_WARNING: return _( "Warning" );
  100. case KD_ERROR: return _( "Error" );
  101. }
  102. return wxEmptyString;
  103. }
  104. long KIDIALOG::getStyle( KD_TYPE aType )
  105. {
  106. long style = wxOK | wxCENTRE | wxSTAY_ON_TOP;
  107. switch( aType )
  108. {
  109. case KD_NONE: break;
  110. case KD_INFO: style |= wxICON_INFORMATION; break;
  111. case KD_QUESTION: style |= wxICON_QUESTION; break;
  112. case KD_WARNING: style |= wxICON_WARNING; break;
  113. case KD_ERROR: style |= wxICON_ERROR; break;
  114. }
  115. return style;
  116. }
  117. int UnsavedChangesDialog( wxWindow* parent, wxString aMessage, bool* aApplyToAll )
  118. {
  119. static bool s_apply_to_all = false;
  120. wxRichMessageDialog dlg( parent, aMessage, wxEmptyString,
  121. wxYES_NO | wxCANCEL | wxYES_DEFAULT | wxICON_WARNING | wxCENTER );
  122. dlg.SetExtendedMessage( _( "If you don't save, all your changes will be permanently lost." ) );
  123. dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
  124. if( aApplyToAll )
  125. dlg.ShowCheckBox( _( "Apply to all" ), s_apply_to_all );
  126. int ret = dlg.ShowModal();
  127. if( aApplyToAll )
  128. {
  129. *aApplyToAll = dlg.IsCheckBoxChecked();
  130. s_apply_to_all = dlg.IsCheckBoxChecked();
  131. }
  132. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  133. return ret;
  134. }
  135. int UnsavedChangesDialog( wxWindow* parent, const wxString& aMessage )
  136. {
  137. #ifdef __APPLE__
  138. // wxWidgets gets the button order wrong on Mac so use the other dialog.
  139. return UnsavedChangesDialog( parent, aMessage, nullptr );
  140. #else
  141. wxMessageDialog dlg( parent, aMessage, wxEmptyString,
  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. dlg.SetYesNoLabels( _( "Save" ), _( "Discard Changes" ) );
  145. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  146. return dlg.ShowModal();
  147. #endif
  148. }
  149. bool ConfirmRevertDialog( wxWindow* parent, const wxString& aMessage )
  150. {
  151. wxMessageDialog dlg( parent, aMessage, wxEmptyString,
  152. wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
  153. dlg.SetExtendedMessage( _( "Your current changes will be permanently lost." ) );
  154. dlg.SetOKCancelLabels( _( "Revert" ), _( "Cancel" ) );
  155. return dlg.ShowModal() == wxID_OK;
  156. }
  157. bool HandleUnsavedChanges( wxWindow* aParent, const wxString& aMessage,
  158. const std::function<bool()>& aSaveFunction )
  159. {
  160. switch( UnsavedChangesDialog( aParent, aMessage ) )
  161. {
  162. case wxID_YES: return aSaveFunction();
  163. case wxID_NO: return true;
  164. default:
  165. case wxID_CANCEL: return false;
  166. }
  167. }
  168. int OKOrCancelDialog( wxWindow* aParent, const wxString& aWarning, const wxString& aMessage,
  169. const wxString& aOKLabel, const wxString& aCancelLabel, bool* aApplyToAll )
  170. {
  171. wxRichMessageDialog dlg( aParent, aMessage, wxEmptyString,
  172. wxOK | wxCANCEL | wxOK_DEFAULT | wxICON_WARNING | wxCENTER );
  173. dlg.ShowDetailedText( _( "If you don't save, all your changes will be permanently lost." ) );
  174. dlg.SetOKCancelLabels( aOKLabel, aCancelLabel );
  175. if( aApplyToAll )
  176. dlg.ShowCheckBox( _( "Apply to all" ), true );
  177. int ret = dlg.ShowModal();
  178. if( aApplyToAll )
  179. *aApplyToAll = dlg.IsCheckBoxChecked();
  180. // Returns wxID_OK or wxID_CANCEL
  181. return ret;
  182. }
  183. // DisplayError should be deprecated, use DisplayErrorMessage instead
  184. void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
  185. {
  186. wxMessageDialog* dialog;
  187. int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
  188. dialog = new wxMessageDialog( parent, text, _( "Warning" ),
  189. wxOK | wxCENTRE | wxRESIZE_BORDER |
  190. icon | wxSTAY_ON_TOP );
  191. dialog->ShowModal();
  192. dialog->Destroy();
  193. }
  194. void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
  195. {
  196. wxRichMessageDialog* dlg;
  197. dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
  198. wxOK | wxCENTRE | wxRESIZE_BORDER |
  199. wxICON_ERROR | wxSTAY_ON_TOP );
  200. if( !aExtraInfo.IsEmpty() )
  201. {
  202. dlg->ShowDetailedText( aExtraInfo );
  203. }
  204. dlg->ShowModal();
  205. dlg->Destroy();
  206. }
  207. void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
  208. {
  209. wxRichMessageDialog* dlg;
  210. dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
  211. wxOK | wxCENTRE | wxRESIZE_BORDER |
  212. wxICON_INFORMATION | wxSTAY_ON_TOP );
  213. if( !aExtraInfo.IsEmpty() )
  214. {
  215. dlg->ShowDetailedText( aExtraInfo );
  216. }
  217. dlg->ShowModal();
  218. dlg->Destroy();
  219. }
  220. bool IsOK( wxWindow* aParent, const wxString& aMessage )
  221. {
  222. wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
  223. wxYES_NO | wxCENTRE | wxICON_QUESTION | wxSTAY_ON_TOP );
  224. dlg.SetEscapeId( wxID_NO );
  225. return dlg.ShowModal() == wxID_YES;
  226. }
  227. int SelectSingleOption( wxWindow* aParent, const wxString& aTitle,
  228. const wxString& aMessage, const wxArrayString& aOptions )
  229. {
  230. wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
  231. if( dlg.ShowModal() != wxID_OK )
  232. return -1;
  233. return dlg.GetSelection();
  234. }