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.

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