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.

375 lines
11 KiB

18 years ago
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-2017 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 )
  41. {
  42. setHash();
  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. {
  48. setHash();
  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. // Check if this dialog should be shown to the user
  61. auto it = doNotShowAgainDlgs.find( m_hash );
  62. if( it != doNotShowAgainDlgs.end() )
  63. return it->second;
  64. bool ret = wxRichMessageDialog::Show();
  65. // Has the user asked not to show the dialog again
  66. if( IsCheckBoxChecked() )
  67. doNotShowAgainDlgs[m_hash] = ret;
  68. return ret;
  69. }
  70. int KIDIALOG::ShowModal()
  71. {
  72. // Check if this dialog should be shown to the user
  73. auto it = doNotShowAgainDlgs.find( m_hash );
  74. if( it != doNotShowAgainDlgs.end() )
  75. return it->second;
  76. int ret = wxRichMessageDialog::ShowModal();
  77. // Has the user asked not to show the dialog again
  78. if( IsCheckBoxChecked() )
  79. doNotShowAgainDlgs[m_hash] = ret;
  80. return ret;
  81. }
  82. void KIDIALOG::setHash()
  83. {
  84. std::size_t h1 = std::hash<wxString>{}( GetMessage() );
  85. std::size_t h2 = std::hash<wxString>{}( GetTitle() );
  86. m_hash = h1 ^ ( h2 << 1 );
  87. }
  88. wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
  89. {
  90. if( !aCaption.IsEmpty() )
  91. return aCaption;
  92. switch( aType )
  93. {
  94. case KD_NONE: /* fall through */
  95. case KD_INFO: return _( "Message" );
  96. case KD_QUESTION: return _( "Question" );
  97. case KD_WARNING: return _( "Warning" );
  98. case KD_ERROR: return _( "Error" );
  99. }
  100. return wxEmptyString;
  101. }
  102. long KIDIALOG::getStyle( KD_TYPE aType )
  103. {
  104. long style = wxOK | wxCENTRE;
  105. switch( aType )
  106. {
  107. case KD_NONE: break;
  108. case KD_INFO: style |= wxICON_INFORMATION; break;
  109. case KD_QUESTION: style |= wxICON_QUESTION; break;
  110. case KD_WARNING: style |= wxICON_WARNING; break;
  111. case KD_ERROR: style |= wxICON_ERROR; break;
  112. }
  113. return style;
  114. }
  115. class DIALOG_EXIT: public DIALOG_EXIT_BASE
  116. {
  117. public:
  118. DIALOG_EXIT( wxWindow *aParent, const wxString& aMessage ) :
  119. DIALOG_EXIT_BASE( aParent )
  120. {
  121. m_bitmap->SetBitmap( KiBitmap( dialog_warning_xpm ) );
  122. if( !aMessage.IsEmpty() )
  123. m_TextInfo->SetLabel( aMessage );
  124. GetSizer()->Fit( this );
  125. GetSizer()->SetSizeHints( this );
  126. };
  127. private:
  128. void OnSaveAndExit( wxCommandEvent& event ) override { EndModal( wxID_YES ); }
  129. void OnExitNoSave( wxCommandEvent& event ) override { EndModal( wxID_NO ); }
  130. };
  131. int DisplayExitDialog( wxWindow* parent, const wxString& aMessage )
  132. {
  133. DIALOG_EXIT dlg( parent, aMessage );
  134. int ret = dlg.ShowModal();
  135. // Returns wxID_YES, wxID_NO, or wxID_CANCEL
  136. return ret;
  137. }
  138. // DisplayError should be deprecated, use DisplayErrorMessage instead
  139. void DisplayError( wxWindow* parent, const wxString& text, int displaytime )
  140. {
  141. wxMessageDialog* dialog;
  142. int icon = displaytime > 0 ? wxICON_INFORMATION : wxICON_ERROR;
  143. dialog = new wxMessageDialog( parent, text, _( "Warning" ),
  144. wxOK | wxCENTRE | wxRESIZE_BORDER | icon );
  145. dialog->ShowModal();
  146. dialog->Destroy();
  147. }
  148. void DisplayErrorMessage( wxWindow* aParent, const wxString& aText, const wxString& aExtraInfo )
  149. {
  150. wxRichMessageDialog* dlg;
  151. dlg = new wxRichMessageDialog( aParent, aText, _( "Error" ),
  152. wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_ERROR );
  153. if( !aExtraInfo.IsEmpty() )
  154. {
  155. dlg->ShowDetailedText( aExtraInfo );
  156. }
  157. dlg->ShowModal();
  158. dlg->Destroy();
  159. }
  160. void DisplayInfoMessage( wxWindow* aParent, const wxString& aMessage, const wxString& aExtraInfo )
  161. {
  162. wxRichMessageDialog* dlg;
  163. dlg = new wxRichMessageDialog( aParent, aMessage, _( "Info" ),
  164. wxOK | wxCENTRE | wxRESIZE_BORDER | wxICON_INFORMATION );
  165. if( !aExtraInfo.IsEmpty() )
  166. {
  167. dlg->ShowDetailedText( aExtraInfo );
  168. }
  169. dlg->ShowModal();
  170. dlg->Destroy();
  171. }
  172. bool IsOK( wxWindow* aParent, const wxString& aMessage )
  173. {
  174. wxMessageDialog dlg( aParent, aMessage, _( "Confirmation" ),
  175. wxYES_NO | wxCENTRE | wxICON_QUESTION );
  176. return dlg.ShowModal() == wxID_YES;
  177. }
  178. class DIALOG_YES_NO_CANCEL : public DIALOG_EXIT
  179. {
  180. public:
  181. DIALOG_YES_NO_CANCEL( wxWindow *aParent,
  182. const wxString& aPrimaryMessage,
  183. const wxString& aSecondaryMessage = wxEmptyString,
  184. const wxString& aYesButtonText = wxEmptyString,
  185. const wxString& aNoButtonText = wxEmptyString,
  186. const wxString& aCancelButtonText = wxEmptyString ) :
  187. DIALOG_EXIT( aParent, aSecondaryMessage )
  188. {
  189. m_TextInfo->SetLabel( aPrimaryMessage );
  190. if( aSecondaryMessage.IsEmpty() )
  191. m_staticText2->Hide();
  192. m_buttonSaveAndExit->SetLabel( aYesButtonText.IsEmpty() ? wxGetStockLabel( wxID_YES ) :
  193. aYesButtonText );
  194. m_buttonExitNoSave->SetLabel( aNoButtonText.IsEmpty() ? wxGetStockLabel( wxID_NO ) :
  195. aNoButtonText );
  196. m_buttonCancel->SetLabel( aCancelButtonText.IsEmpty() ? wxGetStockLabel( wxID_CANCEL ) :
  197. aCancelButtonText );
  198. GetSizer()->Fit( this );
  199. GetSizer()->SetSizeHints( this );
  200. };
  201. };
  202. int YesNoCancelDialog( wxWindow* aParent,
  203. const wxString& aPrimaryMessage,
  204. const wxString& aSecondaryMessage,
  205. const wxString& aYesButtonText,
  206. const wxString& aNoButtonText,
  207. const wxString& aCancelButtonText )
  208. {
  209. DIALOG_YES_NO_CANCEL dlg( aParent, aPrimaryMessage, aSecondaryMessage,
  210. aYesButtonText, aNoButtonText, aCancelButtonText );
  211. return dlg.ShowModal();
  212. }
  213. int SelectSingleOption( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage, const wxArrayString& aOptions )
  214. {
  215. wxSingleChoiceDialog dlg( aParent, aMessage, aTitle, aOptions );
  216. if( dlg.ShowModal() != wxID_OK )
  217. return -1;
  218. return dlg.GetSelection();
  219. }
  220. class DIALOG_MULTI_OPTIONS : public wxMultiChoiceDialog
  221. {
  222. public:
  223. DIALOG_MULTI_OPTIONS( wxWindow* aParent, const wxString& aTitle, const wxString& aMessage,
  224. const wxArrayString& aOptions )
  225. : wxMultiChoiceDialog( aParent, aMessage, aTitle, aOptions ),
  226. m_optionsCount( aOptions.GetCount() )
  227. {
  228. wxBoxSizer* btnSizer = new wxBoxSizer( wxHORIZONTAL );
  229. wxButton* selectAll = new wxButton( this, wxID_ANY, _( "Select All" ) );
  230. btnSizer->Add( selectAll, 1, wxEXPAND | wxALL, 5 );
  231. wxButton* unselectAll = new wxButton( this, wxID_ANY, _( "Unselect All" ) );
  232. btnSizer->Add( unselectAll, 1, wxEXPAND | wxALL, 5 );
  233. auto sizer = GetSizer();
  234. sizer->Insert( sizer->GetItemCount() - 1, btnSizer, 0, wxEXPAND | wxALL, 0 );
  235. Layout();
  236. sizer->Fit( this );
  237. sizer->SetSizeHints( this );
  238. Centre( wxBOTH );
  239. selectAll->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MULTI_OPTIONS::selectAll, this );
  240. unselectAll->Bind( wxEVT_COMMAND_BUTTON_CLICKED, &DIALOG_MULTI_OPTIONS::unselectAll, this );
  241. }
  242. void SetCheckboxes( bool aValue )
  243. {
  244. wxArrayInt selIdxs;
  245. if( aValue ) // select all indices
  246. {
  247. for( int i = 0; i < m_optionsCount; ++i )
  248. selIdxs.Add( i );
  249. }
  250. SetSelections( selIdxs );
  251. }
  252. protected:
  253. ///> Number of displayed options
  254. int m_optionsCount;
  255. void selectAll( wxCommandEvent& aEvent )
  256. {
  257. SetCheckboxes( true );
  258. }
  259. void unselectAll( wxCommandEvent& aEvent )
  260. {
  261. SetCheckboxes( false );
  262. }
  263. };
  264. std::pair<bool, wxArrayInt> SelectMultipleOptions( wxWindow* aParent, const wxString& aTitle,
  265. const wxString& aMessage, const wxArrayString& aOptions, bool aDefaultState )
  266. {
  267. DIALOG_MULTI_OPTIONS dlg( aParent, aTitle, aMessage, aOptions );
  268. dlg.Layout();
  269. dlg.SetCheckboxes( aDefaultState );
  270. wxArrayInt ret;
  271. bool clickedOk = ( dlg.ShowModal() == wxID_OK );
  272. if( clickedOk )
  273. ret = dlg.GetSelections();
  274. return std::make_pair( clickedOk, ret );
  275. }
  276. std::pair<bool, wxArrayInt> SelectMultipleOptions( wxWindow* aParent, const wxString& aTitle,
  277. const wxString& aMessage, const std::vector<std::string>& aOptions, bool aDefaultState )
  278. {
  279. wxArrayString array;
  280. for( const auto& option : aOptions )
  281. array.Add( option );
  282. return SelectMultipleOptions( aParent, aTitle, aMessage, array, aDefaultState );
  283. }