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.

216 lines
4.9 KiB

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