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.

206 lines
4.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. /**
  24. * @file symbols_listbox.cpp
  25. */
  26. #include <trace_helpers.h>
  27. #include <kiplatform/ui.h>
  28. #include <cvpcb_mainframe.h>
  29. #include <listboxes.h>
  30. #include <cvpcb_id.h>
  31. #include <wx/log.h>
  32. SYMBOLS_LISTBOX::SYMBOLS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id ) :
  33. ITEMS_LISTBOX_BASE( parent, id ),
  34. m_warningAttr( std::make_unique<wxListItemAttr>() )
  35. {
  36. m_warningAttr->SetBackgroundColour( KIPLATFORM::UI::IsDarkTheme() ? wxColour( 112, 96, 32 )
  37. : wxColour( 255, 248, 212 ) );
  38. }
  39. SYMBOLS_LISTBOX::~SYMBOLS_LISTBOX()
  40. {
  41. }
  42. ;
  43. BEGIN_EVENT_TABLE( SYMBOLS_LISTBOX, ITEMS_LISTBOX_BASE )
  44. EVT_CHAR( SYMBOLS_LISTBOX::OnChar )
  45. EVT_LIST_ITEM_SELECTED( ID_CVPCB_COMPONENT_LIST, SYMBOLS_LISTBOX::OnSelectComponent )
  46. END_EVENT_TABLE()
  47. void SYMBOLS_LISTBOX::Clear()
  48. {
  49. m_SymbolList.Clear();
  50. SetItemCount( 0 );
  51. }
  52. int SYMBOLS_LISTBOX::GetCount()
  53. {
  54. return m_SymbolList.Count();
  55. }
  56. void SYMBOLS_LISTBOX::SetString( unsigned linecount, const wxString& text )
  57. {
  58. if( linecount >= m_SymbolList.Count() )
  59. linecount = m_SymbolList.Count() - 1;
  60. if( m_SymbolList.Count() > 0 )
  61. {
  62. m_SymbolList[linecount] = text;
  63. UpdateWidth( linecount );
  64. }
  65. }
  66. void SYMBOLS_LISTBOX::AppendLine( const wxString& text )
  67. {
  68. m_SymbolList.Add( text );
  69. int lines = m_SymbolList.Count();
  70. SetItemCount( lines );
  71. UpdateWidth( lines - 1 );
  72. }
  73. void SYMBOLS_LISTBOX::AppendWarning( int index )
  74. {
  75. if( !std::count( m_symbolWarning.begin(), m_symbolWarning.end(), index ) )
  76. {
  77. m_symbolWarning.emplace_back( index );
  78. }
  79. }
  80. void SYMBOLS_LISTBOX::RemoveWarning( int index )
  81. {
  82. if( auto const found{ std::find( m_symbolWarning.begin(), m_symbolWarning.end(), index ) };
  83. found != m_symbolWarning.end() )
  84. {
  85. m_symbolWarning.erase( found );
  86. }
  87. }
  88. wxString SYMBOLS_LISTBOX::OnGetItemText( long item, long column ) const
  89. {
  90. return m_SymbolList.Item( item );
  91. }
  92. wxListItemAttr* SYMBOLS_LISTBOX::OnGetItemAttr( long item ) const
  93. {
  94. if( std::count( m_symbolWarning.begin(), m_symbolWarning.end(), item ) )
  95. {
  96. return m_warningAttr.get();
  97. }
  98. return nullptr;
  99. }
  100. void SYMBOLS_LISTBOX::SetSelection( int index, bool State )
  101. {
  102. if( index >= GetCount() )
  103. index = GetCount() - 1;
  104. if( (index >= 0) && (GetCount() > 0) )
  105. {
  106. Select( index, State );
  107. EnsureVisible( index );
  108. #ifdef __WXMAC__
  109. Update();
  110. #endif
  111. }
  112. }
  113. void SYMBOLS_LISTBOX::OnChar( wxKeyEvent& event )
  114. {
  115. wxLogTrace( kicadTraceKeyEvent, wxS( "SYMBOLS_LISTBOX::OnChar %s" ), dump( event ) );
  116. int key = event.GetKeyCode();
  117. switch( key )
  118. {
  119. case WXK_HOME:
  120. case WXK_END:
  121. case WXK_UP:
  122. case WXK_DOWN:
  123. case WXK_PAGEUP:
  124. case WXK_PAGEDOWN:
  125. event.Skip();
  126. return;
  127. default:
  128. break;
  129. }
  130. // Search for an item name starting by the key code:
  131. key = toupper( key );
  132. for( unsigned ii = 0; ii < m_SymbolList.GetCount(); ii++ )
  133. {
  134. wxString text = m_SymbolList.Item( ii );
  135. // Search for the start char of the footprint name. Skip the line number.
  136. text.Trim( false ); // Remove leading spaces in line
  137. unsigned jj = 0;
  138. for( ; jj < text.Len(); jj++ )
  139. { // skip line number
  140. if( text[jj] == ' ' )
  141. break;
  142. }
  143. for( ; jj < text.Len(); jj++ )
  144. { // skip blanks
  145. if( text[jj] != ' ' )
  146. break;
  147. }
  148. int start_char = toupper( text[jj] );
  149. if( key == start_char )
  150. {
  151. SetSelection( (int) ii, true ); // Ensure visible
  152. break;
  153. }
  154. }
  155. event.Skip();
  156. }
  157. void SYMBOLS_LISTBOX::OnSelectComponent( wxListEvent& event )
  158. {
  159. SetFocus();
  160. GetParent()->OnSelectComponent( event );
  161. }