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.

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