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.

184 lines
4.3 KiB

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