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.

111 lines
3.6 KiB

5 years ago
5 years ago
5 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 CERN
  5. * Copyright (C) 2022 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <wx/evtloop.h>
  27. #include <thread>
  28. #include <widgets/wx_progress_reporters.h>
  29. WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle,
  30. int aNumPhases, bool aCanAbort,
  31. bool aReserveSpaceForMessage ) :
  32. PROGRESS_REPORTER_BASE( aNumPhases ),
  33. wxProgressDialog( aTitle, ( aReserveSpaceForMessage ? wxT( " " ) : wxT( "" ) ), 1, aParent,
  34. // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
  35. // quasi-modal dialog
  36. wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
  37. // up another event loop on completion which
  38. // causes all sorts of grief
  39. ( aCanAbort ? wxPD_CAN_ABORT : 0 ) | wxPD_ELAPSED_TIME ),
  40. #if wxCHECK_VERSION( 3, 1, 0 )
  41. m_appProgressIndicator( aParent ),
  42. #endif
  43. m_messageWidth( 0 )
  44. {
  45. #if wxCHECK_VERSION( 3, 1, 0 )
  46. // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
  47. // with multithreaded setting of those values making a mess
  48. // the cop out is just to set the progress to "indeterminate"
  49. m_appProgressIndicator.Pulse();
  50. #endif
  51. }
  52. WX_PROGRESS_REPORTER::~WX_PROGRESS_REPORTER()
  53. {
  54. }
  55. bool WX_PROGRESS_REPORTER::updateUI()
  56. {
  57. int cur = currentProgress();
  58. if( cur < 0 || cur > 1000 )
  59. cur = 0;
  60. SetRange( 1000 );
  61. wxString message;
  62. {
  63. std::lock_guard<std::mutex> guard( m_mutex );
  64. message = m_rptMessage;
  65. }
  66. int newWidth = GetTextExtent( m_rptMessage ).x;
  67. if( newWidth > m_messageWidth )
  68. {
  69. m_messageWidth = newWidth;
  70. Fit();
  71. }
  72. bool diag = wxProgressDialog::Update( cur, message );
  73. return diag;
  74. }
  75. GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
  76. PROGRESS_REPORTER_BASE( aNumPhases ),
  77. wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL,
  78. wxDefaultValidator, wxGaugeNameStr )
  79. {
  80. }
  81. bool GAUGE_PROGRESS_REPORTER::updateUI()
  82. {
  83. int cur = currentProgress();
  84. if( cur < 0 || cur > 1000 )
  85. cur = 0;
  86. wxGauge::SetValue( cur );
  87. wxEventLoopBase::GetActive()->YieldFor( wxEVT_CATEGORY_UI );
  88. return true; // No cancel button on a wxGauge
  89. }