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.

148 lines
4.3 KiB

  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 The 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 <kidialog.h>
  25. #include <unordered_map>
  26. // Set of dialogs that have been chosen not to be shown again
  27. static std::unordered_map<unsigned long, int> doNotShowAgainDlgs;
  28. KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage, const wxString& aCaption,
  29. long aStyle )
  30. : wxRichMessageDialog( aParent, aMessage, aCaption, aStyle | wxCENTRE | wxSTAY_ON_TOP ),
  31. m_hash( 0 ),
  32. m_cancelMeansCancel( true )
  33. {
  34. }
  35. KIDIALOG::KIDIALOG( wxWindow* aParent, const wxString& aMessage, KD_TYPE aType,
  36. const wxString& aCaption )
  37. : wxRichMessageDialog( aParent, aMessage, getCaption( aType, aCaption ), getStyle( aType ) ),
  38. m_hash( 0 ),
  39. m_cancelMeansCancel( true )
  40. {
  41. }
  42. void KIDIALOG::DoNotShowCheckbox( wxString aUniqueId, int line )
  43. {
  44. ShowCheckBox( _( "Do not show again" ), false );
  45. m_hash = std::hash<wxString>{}( aUniqueId ) + line;
  46. }
  47. bool KIDIALOG::DoNotShowAgain() const
  48. {
  49. return doNotShowAgainDlgs.count( m_hash ) > 0;
  50. }
  51. void KIDIALOG::ForceShowAgain()
  52. {
  53. doNotShowAgainDlgs.erase( m_hash );
  54. }
  55. bool KIDIALOG::Show( bool aShow )
  56. {
  57. // We should check the do-not-show-again setting only when the dialog is displayed
  58. if( 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. }
  65. int ret = wxRichMessageDialog::Show( aShow );
  66. // Has the user asked not to show the dialog again?
  67. // Note that we don't save a Cancel value unless the Cancel button is being used for some
  68. // other function (which is actually more common than it being used for Cancel).
  69. if( IsCheckBoxChecked() && (!m_cancelMeansCancel || ret != wxID_CANCEL ) )
  70. doNotShowAgainDlgs[m_hash] = ret;
  71. return ret;
  72. }
  73. int KIDIALOG::ShowModal()
  74. {
  75. // Check if this dialog should be shown to the user
  76. auto it = doNotShowAgainDlgs.find( m_hash );
  77. if( it != doNotShowAgainDlgs.end() )
  78. return it->second;
  79. int ret = wxRichMessageDialog::ShowModal();
  80. // Has the user asked not to show the dialog again?
  81. // Note that we don't save a Cancel value unless the Cancel button is being used for some
  82. // other function (which is actually more common than it being used for Cancel).
  83. if( IsCheckBoxChecked() && (!m_cancelMeansCancel || ret != wxID_CANCEL ) )
  84. doNotShowAgainDlgs[m_hash] = ret;
  85. return ret;
  86. }
  87. wxString KIDIALOG::getCaption( KD_TYPE aType, const wxString& aCaption )
  88. {
  89. if( !aCaption.IsEmpty() )
  90. return aCaption;
  91. switch( aType )
  92. {
  93. case KD_NONE: /* fall through */
  94. case KD_INFO: return _( "Message" );
  95. case KD_QUESTION: return _( "Question" );
  96. case KD_WARNING: return _( "Warning" );
  97. case KD_ERROR: return _( "Error" );
  98. }
  99. return wxEmptyString;
  100. }
  101. long KIDIALOG::getStyle( KD_TYPE aType )
  102. {
  103. long style = wxOK | wxCENTRE | wxSTAY_ON_TOP;
  104. switch( aType )
  105. {
  106. case KD_NONE: break;
  107. case KD_INFO: style |= wxICON_INFORMATION; break;
  108. case KD_QUESTION: style |= wxICON_QUESTION; break;
  109. case KD_WARNING: style |= wxICON_WARNING; break;
  110. case KD_ERROR: style |= wxICON_ERROR; break;
  111. }
  112. return style;
  113. }