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.

173 lines
4.0 KiB

  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 class_components_listbox.cpp
  25. */
  26. #include <trace_helpers.h>
  27. #include <cvpcb_mainframe.h>
  28. #include <listboxes.h>
  29. #include <cvpcb_id.h>
  30. #include <wx/log.h>
  31. COMPONENTS_LISTBOX::COMPONENTS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id ) :
  32. ITEMS_LISTBOX_BASE( parent, id )
  33. {
  34. }
  35. COMPONENTS_LISTBOX::~COMPONENTS_LISTBOX()
  36. {
  37. }
  38. BEGIN_EVENT_TABLE( COMPONENTS_LISTBOX, ITEMS_LISTBOX_BASE )
  39. EVT_CHAR( COMPONENTS_LISTBOX::OnChar )
  40. EVT_LIST_ITEM_SELECTED( ID_CVPCB_COMPONENT_LIST, COMPONENTS_LISTBOX::OnSelectComponent )
  41. END_EVENT_TABLE()
  42. void COMPONENTS_LISTBOX::Clear()
  43. {
  44. m_ComponentList.Clear();
  45. SetItemCount( 0 );
  46. }
  47. int COMPONENTS_LISTBOX::GetCount()
  48. {
  49. return m_ComponentList.Count();
  50. }
  51. void COMPONENTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
  52. {
  53. if( linecount >= m_ComponentList.Count() )
  54. linecount = m_ComponentList.Count() - 1;
  55. if( m_ComponentList.Count() > 0 )
  56. {
  57. m_ComponentList[linecount] = text;
  58. UpdateWidth( linecount );
  59. }
  60. }
  61. void COMPONENTS_LISTBOX::AppendLine( const wxString& text )
  62. {
  63. m_ComponentList.Add( text );
  64. int lines = m_ComponentList.Count();
  65. SetItemCount( lines );
  66. UpdateWidth( lines - 1 );
  67. }
  68. wxString COMPONENTS_LISTBOX::OnGetItemText( long item, long column ) const
  69. {
  70. return m_ComponentList.Item( item );
  71. }
  72. void COMPONENTS_LISTBOX::SetSelection( int index, bool State )
  73. {
  74. if( index >= GetCount() )
  75. index = GetCount() - 1;
  76. if( (index >= 0) && (GetCount() > 0) )
  77. {
  78. Select( index, State );
  79. EnsureVisible( index );
  80. #ifdef __WXMAC__
  81. Update();
  82. #endif
  83. }
  84. }
  85. void COMPONENTS_LISTBOX::OnChar( wxKeyEvent& event )
  86. {
  87. wxLogTrace( kicadTraceKeyEvent, "COMPONENTS_LISTBOX::OnChar %s", dump( event ) );
  88. int key = event.GetKeyCode();
  89. switch( key )
  90. {
  91. case WXK_HOME:
  92. case WXK_END:
  93. case WXK_UP:
  94. case WXK_DOWN:
  95. case WXK_PAGEUP:
  96. case WXK_PAGEDOWN:
  97. event.Skip();
  98. return;
  99. default:
  100. break;
  101. }
  102. // Search for an item name starting by the key code:
  103. key = toupper( key );
  104. for( unsigned ii = 0; ii < m_ComponentList.GetCount(); ii++ )
  105. {
  106. wxString text = m_ComponentList.Item( ii );
  107. // Search for the start char of the footprint name. Skip the line number.
  108. text.Trim( false ); // Remove leading spaces in line
  109. unsigned jj = 0;
  110. for( ; jj < text.Len(); jj++ )
  111. { // skip line number
  112. if( text[jj] == ' ' )
  113. break;
  114. }
  115. for( ; jj < text.Len(); jj++ )
  116. { // skip blanks
  117. if( text[jj] != ' ' )
  118. break;
  119. }
  120. int start_char = toupper( text[jj] );
  121. if( key == start_char )
  122. {
  123. SetSelection( (int) ii, true ); // Ensure visible
  124. break;
  125. }
  126. }
  127. event.Skip();
  128. }
  129. void COMPONENTS_LISTBOX::OnSelectComponent( wxListEvent& event )
  130. {
  131. SetFocus();
  132. GetParent()->OnSelectComponent( event );
  133. }