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.

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