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.

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