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.

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