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.

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