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.

216 lines
6.6 KiB

11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2015 CERN
  5. * Copyright (C) 2021-2023 KiCad Developers, see AUTHORS.txt for contributors.
  6. * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * Transient mouse following popup window implementation.
  27. */
  28. #include <wx/settings.h>
  29. #include <math/vector2wx.h>
  30. #include <status_popup.h>
  31. #include <eda_draw_frame.h>
  32. #include <bitmaps.h>
  33. STATUS_POPUP::STATUS_POPUP( wxWindow* aParent ) :
  34. wxPopupWindow( aParent ),
  35. m_expireTimer( this )
  36. {
  37. SetDoubleBuffered( true );
  38. m_panel = new wxPanel( this, wxID_ANY );
  39. m_topSizer = new wxBoxSizer( wxHORIZONTAL );
  40. m_panel->SetSizer( m_topSizer );
  41. m_panel->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOW ) );
  42. Connect( wxEVT_TIMER, wxTimerEventHandler( STATUS_POPUP::onExpire ), nullptr, this );
  43. #ifdef __WXOSX_MAC__
  44. // Key events from popups don't get put through the wxWidgets event system on OSX,
  45. // so we have to fall back to the CHAR_HOOK to forward hotkeys from the popup to
  46. // the canvas / frame.
  47. Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( STATUS_POPUP::onCharHook ), nullptr, this );
  48. #endif
  49. }
  50. void STATUS_POPUP::onCharHook( wxKeyEvent& aEvent )
  51. {
  52. // Key events from the status popup don't get put through the wxWidgets event system on
  53. // OSX, so we have to fall back to the CHAR_HOOK to forward hotkeys from the popup to
  54. // the canvas / frame.
  55. aEvent.SetEventType( wxEVT_CHAR );
  56. EDA_DRAW_FRAME* frame = dynamic_cast<EDA_DRAW_FRAME*>( GetParent() );
  57. if( frame )
  58. frame->GetCanvas()->OnEvent( aEvent );
  59. else
  60. GetParent()->GetEventHandler()->ProcessEvent( aEvent );
  61. }
  62. void STATUS_POPUP::Popup( wxWindow* )
  63. {
  64. Show( true );
  65. Raise();
  66. }
  67. void STATUS_POPUP::PopupFor( int aMsecs )
  68. {
  69. Popup();
  70. Expire( aMsecs );
  71. }
  72. void STATUS_POPUP::Move( const VECTOR2I& aWhere )
  73. {
  74. SetPosition( ToWxPoint( aWhere ) );
  75. }
  76. void STATUS_POPUP::Move( const wxPoint& aWhere )
  77. {
  78. SetPosition( aWhere );
  79. }
  80. void STATUS_POPUP::Expire( int aMsecs )
  81. {
  82. m_expireTimer.StartOnce( aMsecs );
  83. }
  84. void STATUS_POPUP::updateSize()
  85. {
  86. m_topSizer->Fit( m_panel );
  87. SetClientSize( m_panel->GetSize() );
  88. }
  89. void STATUS_POPUP::onExpire( wxTimerEvent& aEvent )
  90. {
  91. Hide();
  92. }
  93. STATUS_TEXT_POPUP::STATUS_TEXT_POPUP( wxWindow* aParent ) :
  94. STATUS_POPUP( aParent )
  95. {
  96. SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
  97. m_panel->SetBackgroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE ) );
  98. m_panel->SetForegroundColour( wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT ) );
  99. m_statusLine = new wxStaticText( m_panel, wxID_ANY, wxEmptyString ) ;
  100. m_topSizer->Add( m_statusLine, 1, wxALL | wxEXPAND, 5 );
  101. }
  102. void STATUS_TEXT_POPUP::SetText( const wxString& aText )
  103. {
  104. m_statusLine->SetLabel( aText );
  105. updateSize();
  106. }
  107. void STATUS_TEXT_POPUP::SetTextColor( const wxColour& aColor )
  108. {
  109. m_statusLine->SetForegroundColour( aColor );
  110. }
  111. STATUS_MIN_MAX_POPUP::STATUS_MIN_MAX_POPUP( EDA_DRAW_FRAME* aFrame ) :
  112. STATUS_POPUP( aFrame ),
  113. m_frame( aFrame ),
  114. m_min( 0.0 ),
  115. m_max( 0.0 )
  116. {
  117. m_icon = new wxStaticBitmap( m_panel, wxID_ANY, KiBitmap( BITMAPS::checked_ok ),
  118. wxDefaultPosition, wxSize( 12, 12 ) );
  119. m_currentLabel = new wxStaticText( m_panel, wxID_ANY, _( "current" ) );
  120. wxStaticText* minLabel = new wxStaticText( m_panel, wxID_ANY, _( "min" ) );
  121. wxStaticText* maxLabel = new wxStaticText( m_panel, wxID_ANY, _( "max" ) );
  122. wxFont infoFont = KIUI::GetStatusFont( this );
  123. m_currentLabel->SetFont( infoFont );
  124. minLabel->SetFont( infoFont );
  125. maxLabel->SetFont( infoFont );
  126. m_currentText = new wxStaticText( m_panel, wxID_ANY, wxEmptyString );
  127. m_minText = new wxStaticText( m_panel, wxID_ANY, wxEmptyString );
  128. m_maxText = new wxStaticText( m_panel, wxID_ANY, wxEmptyString );
  129. wxBoxSizer* currentSizer = new wxBoxSizer( wxVERTICAL );
  130. currentSizer->Add( m_currentLabel, 0, 0, 5 );
  131. currentSizer->Add( m_currentText, 0, 0, 5 );
  132. wxBoxSizer* minSizer = new wxBoxSizer( wxVERTICAL );
  133. minSizer->Add( minLabel, 0, 0, 5 );
  134. minSizer->Add( m_minText, 0, 0, 5 );
  135. wxBoxSizer* maxSizer = new wxBoxSizer( wxVERTICAL );
  136. maxSizer->Add( maxLabel, 0, 0, 5 );
  137. maxSizer->Add( m_maxText, 0, 0, 5 );
  138. m_topSizer->Add( currentSizer, 0, wxLEFT | wxRIGHT, 3 );
  139. m_topSizer->Add( m_icon, 0, wxALL | wxALIGN_BOTTOM | wxRESERVE_SPACE_EVEN_IF_HIDDEN, 1 );
  140. m_topSizer->Add( minSizer, 0, wxLEFT | wxRIGHT, 3 );
  141. m_topSizer->Add( maxSizer, 0, wxLEFT | wxRIGHT, 3 );
  142. }
  143. void STATUS_MIN_MAX_POPUP::SetMinMax( double aMin, double aMax )
  144. {
  145. m_min = aMin;
  146. m_minText->SetLabel( m_frame->MessageTextFromValue( m_min, false ) );
  147. m_max = aMax;
  148. m_maxText->SetLabel( m_frame->MessageTextFromValue( m_max, false ) );
  149. }
  150. void STATUS_MIN_MAX_POPUP::SetCurrent( double aCurrent, const wxString& aLabel )
  151. {
  152. m_currentLabel->SetLabel( aLabel );
  153. m_currentText->SetLabel( m_frame->MessageTextFromValue( aCurrent ) );
  154. m_icon->Show( aCurrent >= m_min && aCurrent <= m_max );
  155. wxColour normal = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNTEXT );
  156. // Determine the background color first and choose a contrasting value
  157. COLOR4D bg = GetBackgroundColour();
  158. COLOR4D red;
  159. double bg_h, bg_s, bg_l;
  160. bg.ToHSL( bg_h, bg_s, bg_l );
  161. red.FromHSL( 0, 1.0, bg_l < 0.5 ? 0.7 : 0.3 );
  162. m_minText->SetForegroundColour( aCurrent < m_min ? red.ToColour() : normal );
  163. m_maxText->SetForegroundColour( aCurrent > m_max ? red.ToColour() : normal );
  164. m_topSizer->Layout();
  165. updateSize();
  166. Refresh();
  167. Update();
  168. }