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.

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