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.

209 lines
4.9 KiB

11 years ago
11 years ago
6 years ago
  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 library_listbox.cpp
  25. * class to display used library and selecting it
  26. */
  27. #include <trace_helpers.h>
  28. #include <cvpcb_mainframe.h>
  29. #include <listboxes.h>
  30. #include <cvpcb_id.h>
  31. #include <wx/log.h>
  32. /***************************************/
  33. /* ListBox handling the library list */
  34. /***************************************/
  35. LIBRARY_LISTBOX::LIBRARY_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id ) :
  36. ITEMS_LISTBOX_BASE( parent, id, wxDefaultPosition, wxDefaultSize, wxLC_SINGLE_SEL )
  37. {
  38. }
  39. LIBRARY_LISTBOX::~LIBRARY_LISTBOX()
  40. {
  41. }
  42. int LIBRARY_LISTBOX::GetCount()
  43. {
  44. return m_libraryList.Count();
  45. }
  46. void LIBRARY_LISTBOX::SetString( unsigned linecount, const wxString& text )
  47. {
  48. unsigned count = m_libraryList.Count();
  49. if( count > 0 )
  50. {
  51. if( linecount >= count )
  52. linecount = count - 1;
  53. m_libraryList[linecount] = text;
  54. UpdateWidth( linecount );
  55. }
  56. }
  57. wxString LIBRARY_LISTBOX::GetSelectedLibrary()
  58. {
  59. wxString libraryName;
  60. int ii = GetFirstSelected();
  61. if( ii >= 0 )
  62. {
  63. libraryName = m_libraryList[ii];
  64. }
  65. return libraryName;
  66. }
  67. void LIBRARY_LISTBOX::AppendLine( const wxString& text )
  68. {
  69. m_libraryList.Add( text );
  70. int lines = m_libraryList.Count();
  71. SetItemCount( lines );
  72. UpdateWidth( lines - 1 );
  73. }
  74. wxString LIBRARY_LISTBOX::OnGetItemText( long item, long column ) const
  75. {
  76. return m_libraryList.Item( item );
  77. }
  78. void LIBRARY_LISTBOX::SetSelection( int index, bool State )
  79. {
  80. if( index >= GetCount() )
  81. index = GetCount() - 1;
  82. if( (index >= 0) && (GetCount() > 0) )
  83. {
  84. #ifndef __WXMAC__
  85. Select( index, State );
  86. #endif
  87. EnsureVisible( index );
  88. #ifdef __WXMAC__
  89. Refresh();
  90. #endif
  91. }
  92. }
  93. void LIBRARY_LISTBOX::SetLibraryList( const wxArrayString& aList )
  94. {
  95. int oldSelection = GetSelection();
  96. m_libraryList = aList;
  97. SetItemCount( m_libraryList.GetCount() );
  98. if( GetCount() == 0 || oldSelection < 0 || oldSelection >= GetCount() )
  99. SetSelection( 0, true );
  100. if( m_libraryList.Count() )
  101. {
  102. RefreshItems( 0L, m_libraryList.Count()-1 );
  103. UpdateWidth();
  104. }
  105. }
  106. BEGIN_EVENT_TABLE( LIBRARY_LISTBOX, ITEMS_LISTBOX_BASE )
  107. EVT_CHAR( LIBRARY_LISTBOX::OnChar )
  108. EVT_LIST_ITEM_SELECTED( ID_CVPCB_LIBRARY_LIST, LIBRARY_LISTBOX::OnSelectLibrary )
  109. END_EVENT_TABLE()
  110. void LIBRARY_LISTBOX::OnChar( wxKeyEvent& event )
  111. {
  112. wxLogTrace( kicadTraceKeyEvent, "LIBRARY_LISTBOX::OnChar %s", dump( event ) );
  113. int key = event.GetKeyCode();
  114. switch( key )
  115. {
  116. case WXK_HOME:
  117. case WXK_END:
  118. case WXK_UP:
  119. case WXK_DOWN:
  120. case WXK_PAGEUP:
  121. case WXK_PAGEDOWN:
  122. event.Skip();
  123. return;
  124. default:
  125. break;
  126. }
  127. // Search for an item name starting by the key code:
  128. key = toupper(key);
  129. for( unsigned ii = 0; ii < m_libraryList.GetCount(); ii++ )
  130. {
  131. wxString text = m_libraryList.Item( ii );
  132. // Search for the start char of the footprint name. Skip the line number.
  133. text.Trim( false ); // Remove leading spaces in line
  134. unsigned jj = 0;
  135. for( ; jj < text.Len(); jj++ )
  136. {
  137. // skip line number
  138. if( text[jj] == ' ' )
  139. break;
  140. }
  141. for( ; jj < text.Len(); jj++ )
  142. { // skip blanks
  143. if( text[jj] != ' ' )
  144. break;
  145. }
  146. int start_char = toupper( text[jj] );
  147. if( key == start_char )
  148. {
  149. SetSelection( ii, true ); // Ensure visible
  150. break;
  151. }
  152. }
  153. event.Skip();
  154. }
  155. void LIBRARY_LISTBOX::OnSelectLibrary( wxListEvent& event )
  156. {
  157. // Apply the filter
  158. GetParent()->SetFootprintFilter(
  159. FOOTPRINTS_LISTBOX::FILTERING_BY_LIBRARY, CVPCB_MAINFRAME::FILTER_ENABLE );
  160. SetFocus();
  161. GetParent()->OnSelectComponent( event );
  162. }