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.

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