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.

166 lines
4.7 KiB

4 years ago
4 years ago
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-2021 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 <gal/dpi_scaling.h>
  28. #include <layer_ids.h>
  29. #include <widgets/layer_box_selector.h>
  30. LAYER_SELECTOR::LAYER_SELECTOR()
  31. {
  32. m_layerhotkeys = true;
  33. }
  34. bool LAYER_SELECTOR::SetLayersHotkeys( bool value )
  35. {
  36. m_layerhotkeys = value;
  37. return m_layerhotkeys;
  38. }
  39. void LAYER_SELECTOR::DrawColorSwatch( wxBitmap& aLayerbmp, const COLOR4D& aBackground,
  40. const COLOR4D& aColor )
  41. {
  42. wxMemoryDC bmpDC;
  43. wxBrush brush;
  44. // Prepare Bitmap
  45. bmpDC.SelectObject( aLayerbmp );
  46. brush.SetStyle( wxBRUSHSTYLE_SOLID );
  47. if( aBackground != COLOR4D::UNSPECIFIED )
  48. {
  49. brush.SetColour( aBackground.WithAlpha( 1.0 ).ToColour() );
  50. bmpDC.SetBrush( brush );
  51. bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
  52. }
  53. brush.SetColour( aColor.ToColour() );
  54. bmpDC.SetBrush( brush );
  55. bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
  56. bmpDC.SetBrush( *wxTRANSPARENT_BRUSH );
  57. bmpDC.SetPen( *wxBLACK_PEN );
  58. bmpDC.DrawRectangle( 0, 0, aLayerbmp.GetWidth(), aLayerbmp.GetHeight() );
  59. }
  60. LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos,
  61. const wxSize& size, int n, const wxString choices[] ) :
  62. wxBitmapComboBox( parent, id, wxEmptyString, pos, size, n, choices, wxCB_READONLY ),
  63. LAYER_SELECTOR()
  64. {
  65. if( choices != nullptr )
  66. ResyncBitmapOnly();
  67. GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
  68. nullptr, this );
  69. }
  70. LAYER_BOX_SELECTOR::LAYER_BOX_SELECTOR( wxWindow* parent, wxWindowID id, const wxPoint& pos,
  71. const wxSize& size, const wxArrayString& choices ) :
  72. wxBitmapComboBox( parent, id, wxEmptyString, pos, size, choices, wxCB_READONLY ),
  73. LAYER_SELECTOR()
  74. {
  75. if( !choices.IsEmpty() )
  76. ResyncBitmapOnly();
  77. GetParent()->Connect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
  78. nullptr, this );
  79. }
  80. LAYER_BOX_SELECTOR::~LAYER_BOX_SELECTOR()
  81. {
  82. GetParent()->Disconnect( wxEVT_CHAR_HOOK, wxKeyEventHandler( LAYER_BOX_SELECTOR::onKeyDown ),
  83. nullptr, this );
  84. }
  85. int LAYER_BOX_SELECTOR::GetLayerSelection() const
  86. {
  87. if( GetSelection() < 0 )
  88. return UNDEFINED_LAYER;
  89. return (int)(intptr_t) GetClientData( GetSelection() );
  90. }
  91. int LAYER_BOX_SELECTOR::SetLayerSelection( int layer )
  92. {
  93. for( int i = 0; i < (int) GetCount(); i++ )
  94. {
  95. if( GetClientData( (unsigned) i ) == (void*)(intptr_t) layer )
  96. {
  97. if( GetSelection() != i ) // Element (i) is not selected
  98. {
  99. SetSelection( i );
  100. return i;
  101. }
  102. else
  103. {
  104. return i; // If element already selected; do nothing
  105. }
  106. }
  107. }
  108. // Not Found
  109. SetSelection( -1 );
  110. return -1;
  111. }
  112. void LAYER_BOX_SELECTOR::ResyncBitmapOnly()
  113. {
  114. DPI_SCALING dpi( nullptr, this );
  115. int size = static_cast<int>( dpi.GetScaleFactor() * 14 );
  116. for( int i = 0; i < (int) GetCount(); ++i )
  117. {
  118. wxBitmap layerbmp( size, size );
  119. DrawColorSwatch( layerbmp, getLayerColor( LAYER_PCB_BACKGROUND ), getLayerColor( i ) );
  120. }
  121. }
  122. void LAYER_BOX_SELECTOR::onKeyDown( wxKeyEvent& aEvent )
  123. {
  124. #ifdef __WXOSX_MAC__
  125. if( aEvent.GetKeyCode() == WXK_ESCAPE && IsPopupShown() )
  126. {
  127. Dismiss();
  128. return;
  129. }
  130. #endif
  131. aEvent.Skip();
  132. }