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.

217 lines
4.9 KiB

12 years ago
12 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 class_library_listbox.cpp
  25. * class to display used library and selecting it
  26. */
  27. #include <fctsys.h>
  28. #include <wxstruct.h>
  29. #include <macros.h>
  30. #include <cvpcb.h>
  31. #include <cvpcb_mainframe.h>
  32. #include <listview_classes.h>
  33. #include <cvpcb_id.h>
  34. /***************************************/
  35. /* ListBox handling the library list */
  36. /***************************************/
  37. LIBRARY_LISTBOX::LIBRARY_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id,
  38. const wxPoint& loc, const wxSize& size ) :
  39. ITEMS_LISTBOX_BASE( parent, id, loc, size, wxLC_SINGLE_SEL )
  40. {
  41. }
  42. LIBRARY_LISTBOX::~LIBRARY_LISTBOX()
  43. {
  44. }
  45. int LIBRARY_LISTBOX::GetCount()
  46. {
  47. return m_libraryList.Count();
  48. }
  49. void LIBRARY_LISTBOX::SetString( unsigned linecount, const wxString& text )
  50. {
  51. unsigned count = m_libraryList.Count();
  52. if( count > 0 )
  53. {
  54. if( linecount >= count )
  55. linecount = count - 1;
  56. m_libraryList[linecount] = text;
  57. UpdateWidth( linecount );
  58. }
  59. }
  60. wxString LIBRARY_LISTBOX::GetSelectedLibrary()
  61. {
  62. wxString libraryName;
  63. int ii = GetFirstSelected();
  64. if( ii >= 0 )
  65. {
  66. libraryName = m_libraryList[ii];
  67. }
  68. return libraryName;
  69. }
  70. void LIBRARY_LISTBOX::AppendLine( const wxString& text )
  71. {
  72. m_libraryList.Add( text );
  73. int lines = m_libraryList.Count();
  74. SetItemCount( lines );
  75. UpdateWidth( lines - 1 );
  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::SetLibraryList( const wxArrayString& aList )
  97. {
  98. int oldSelection = GetSelection();
  99. m_libraryList = aList;
  100. SetItemCount( m_libraryList.GetCount() );
  101. if( GetCount() == 0 || oldSelection < 0 || oldSelection >= GetCount() )
  102. SetSelection( 0, true );
  103. if( m_libraryList.Count() )
  104. {
  105. RefreshItems( 0L, m_libraryList.Count()-1 );
  106. UpdateWidth();
  107. }
  108. }
  109. BEGIN_EVENT_TABLE( LIBRARY_LISTBOX, ITEMS_LISTBOX_BASE )
  110. EVT_CHAR( LIBRARY_LISTBOX::OnChar )
  111. EVT_LIST_ITEM_SELECTED( ID_CVPCB_LIBRARY_LIST, LIBRARY_LISTBOX::OnSelectLibrary )
  112. END_EVENT_TABLE()
  113. void LIBRARY_LISTBOX::OnChar( wxKeyEvent& event )
  114. {
  115. int key = event.GetKeyCode();
  116. switch( key )
  117. {
  118. case WXK_TAB:
  119. case WXK_RIGHT:
  120. case WXK_NUMPAD_RIGHT:
  121. GetParent()->ChangeFocus( true );
  122. return;
  123. case WXK_LEFT:
  124. case WXK_NUMPAD_LEFT:
  125. GetParent()->ChangeFocus( false );
  126. return;
  127. case WXK_HOME:
  128. case WXK_END:
  129. case WXK_UP:
  130. case WXK_DOWN:
  131. case WXK_PAGEUP:
  132. case WXK_PAGEDOWN:
  133. event.Skip();
  134. return;
  135. default:
  136. break;
  137. }
  138. // Search for an item name starting by the key code:
  139. key = toupper(key);
  140. for( unsigned ii = 0; ii < m_libraryList.GetCount(); ii++ )
  141. {
  142. wxString text = m_libraryList.Item( ii );
  143. // Search for the start char of the footprint name. Skip the line number.
  144. text.Trim( false ); // Remove leading spaces in line
  145. unsigned jj = 0;
  146. for( ; jj < text.Len(); jj++ )
  147. {
  148. // skip line number
  149. if( text[jj] == ' ' )
  150. break;
  151. }
  152. for( ; jj < text.Len(); jj++ )
  153. { // skip blanks
  154. if( text[jj] != ' ' )
  155. break;
  156. }
  157. int start_char = toupper( text[jj] );
  158. if( key == start_char )
  159. {
  160. SetSelection( ii, true ); // Ensure visible
  161. break;
  162. }
  163. }
  164. event.Skip();
  165. }
  166. void LIBRARY_LISTBOX::OnSelectLibrary( wxListEvent& event )
  167. {
  168. SetFocus();
  169. GetParent()->OnSelectComponent( event );
  170. }