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.

141 lines
3.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2014-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/dcmemory.h>
  25. #include <wx/odcombo.h>
  26. #include <wx/menuitem.h>
  27. #include <wx/settings.h>
  28. #include <dpi_scaling_common.h>
  29. #include <layer_ids.h>
  30. #include <widgets/layer_box_selector.h>
  31. #include "kiplatform/ui.h"
  32. LAYER_SELECTOR::LAYER_SELECTOR()
  33. {
  34. m_layerhotkeys = true;
  35. }
  36. bool LAYER_SELECTOR::SetLayersHotkeys( bool value )
  37. {
  38. m_layerhotkeys = value;
  39. return m_layerhotkeys;
  40. }
  41. LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos,
  42. const wxSize& size, int n, const wxString choices[] ) :
  43. wxBitmapComboBox( parent, id, wxEmptyString, pos, size, n, choices, wxCB_READONLY ),
  44. LAYER_SELECTOR()
  45. {
  46. #ifdef __WXMAC__
  47. GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
  48. nullptr, this );
  49. #endif
  50. }
  51. LAYER_BOX_SELECTOR::~LAYER_BOX_SELECTOR()
  52. {
  53. #ifdef __WXMAC__
  54. GetParent()->Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
  55. nullptr, this );
  56. #endif
  57. }
  58. int LAYER_BOX_SELECTOR::GetLayerSelection() const
  59. {
  60. if( GetSelection() < 0 )
  61. return UNDEFINED_LAYER;
  62. return (int)(intptr_t) GetClientData( GetSelection() );
  63. }
  64. int LAYER_BOX_SELECTOR::SetLayerSelection( int layer )
  65. {
  66. for( int i = 0; i < (int) GetCount(); i++ )
  67. {
  68. if( GetClientData( (unsigned) i ) == (void*)(intptr_t) layer )
  69. {
  70. if( GetSelection() != i ) // Element (i) is not selected
  71. {
  72. SetSelection( i );
  73. return i;
  74. }
  75. else
  76. {
  77. return i; // If element already selected; do nothing
  78. }
  79. }
  80. }
  81. // Not Found
  82. SetSelection( -1 );
  83. return -1;
  84. }
  85. #ifdef __WXMAC__
  86. void LAYER_BOX_SELECTOR::onKeyDown( wxKeyEvent& aEvent )
  87. {
  88. if( aEvent.GetKeyCode() == WXK_ESCAPE && IsPopupShown() )
  89. {
  90. Dismiss();
  91. return;
  92. }
  93. aEvent.Skip();
  94. }
  95. void LAYER_BOX_SELECTOR::OnDrawBackground( wxDC& dc, const wxRect& rect, int item, int flags) const
  96. {
  97. if( ( flags & wxODCB_PAINTING_CONTROL ) && !IsEnabled() )
  98. {
  99. wxColour fgCol = wxSystemSettings::GetColour( wxSYS_COLOUR_GRAYTEXT );
  100. wxColour bgCol = wxSystemSettings::GetColour( wxSYS_COLOUR_BTNFACE );
  101. if( KIPLATFORM::UI::IsDarkTheme() )
  102. bgCol = bgCol.ChangeLightness( 106 );
  103. else
  104. bgCol = bgCol.ChangeLightness( 160 );
  105. dc.SetTextForeground( fgCol );
  106. dc.SetBrush( bgCol );
  107. dc.SetPen( bgCol );
  108. dc.DrawRectangle( rect.Inflate( 1, 1 ) );
  109. dc.SetClippingRegion( rect );
  110. return;
  111. }
  112. wxBitmapComboBox::OnDrawBackground( dc, rect, item, flags );
  113. }
  114. #endif