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.

269 lines
6.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file class_footprints_listbox.cpp
  26. * class to display the list of available footprints
  27. */
  28. #include <fctsys.h>
  29. #include <wxstruct.h>
  30. #include <macros.h>
  31. #include <appl_wxstruct.h>
  32. #include <wildcards_and_files_ext.h>
  33. #include <cvpcb.h>
  34. #include <cvpcb_mainframe.h>
  35. #include <cvstruct.h>
  36. #include <cvpcb_id.h>
  37. FOOTPRINTS_LISTBOX::FOOTPRINTS_LISTBOX( CVPCB_MAINFRAME* parent,
  38. wxWindowID id, const wxPoint& loc,
  39. const wxSize& size ) :
  40. ITEMS_LISTBOX_BASE( parent, id, loc, size )
  41. {
  42. }
  43. FOOTPRINTS_LISTBOX::~FOOTPRINTS_LISTBOX()
  44. {
  45. }
  46. int FOOTPRINTS_LISTBOX::GetCount()
  47. {
  48. return m_footprintList.Count();
  49. }
  50. void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
  51. {
  52. if( linecount >= m_footprintList.Count() )
  53. linecount = m_footprintList.Count() - 1;
  54. if( linecount >= 0 )
  55. m_footprintList[linecount] = text;
  56. }
  57. wxString FOOTPRINTS_LISTBOX::GetSelectedFootprint()
  58. {
  59. wxString footprintName;
  60. int ii = GetFirstSelected();
  61. if( ii >= 0 )
  62. {
  63. wxString msg = m_footprintList[ii];
  64. msg.Trim( true );
  65. msg.Trim( false );
  66. footprintName = msg.AfterFirst( wxChar( ' ' ) );
  67. }
  68. return footprintName;
  69. }
  70. void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text )
  71. {
  72. m_footprintList.Add( text );
  73. SetItemCount( m_footprintList.Count() );
  74. }
  75. wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
  76. {
  77. if( item < 0 || item >= (long)m_footprintList.GetCount() )
  78. return wxEmptyString;
  79. return m_footprintList.Item( item );
  80. }
  81. void FOOTPRINTS_LISTBOX::SetSelection( unsigned index, bool State )
  82. {
  83. if( (int) 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 FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
  97. COMPONENT* aComponent, int aFilterType )
  98. {
  99. wxArrayString newList;
  100. wxString msg;
  101. wxString oldSelection;
  102. if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
  103. oldSelection = m_footprintList[ GetSelection() ];
  104. for( unsigned ii = 0; ii < aList.GetCount(); ii++ )
  105. {
  106. if( aFilterType == UNFILTERED )
  107. {
  108. msg.Printf( wxT( "%3zu %s" ), newList.GetCount() + 1,
  109. GetChars( aList.GetItem( ii ).m_Module ) );
  110. newList.Add( msg );
  111. continue;
  112. }
  113. if( (aFilterType & BY_LIBRARY) && !aLibName.IsEmpty()
  114. && !aList.GetItem( ii ).InLibrary( aLibName ) )
  115. continue;
  116. if( (aFilterType & BY_COMPONENT) && (aComponent != NULL)
  117. && !aComponent->MatchesFootprintFilters( aList.GetItem( ii ).m_Module ) )
  118. continue;
  119. if( (aFilterType & BY_PIN_COUNT) && (aComponent!= NULL)
  120. && (aComponent->GetNetCount() != aList.GetItem( ii ).m_padCount) )
  121. continue;
  122. msg.Printf( wxT( "%3zu %s" ), newList.GetCount() + 1,
  123. aList.GetItem( ii ).m_Module.GetData() );
  124. newList.Add( msg );
  125. }
  126. if( newList == m_footprintList )
  127. return;
  128. m_footprintList = newList;
  129. int selection = m_footprintList.Index( oldSelection );
  130. if( selection == wxNOT_FOUND )
  131. selection = 0;
  132. DeleteAllItems();
  133. SetItemCount( m_footprintList.GetCount() );
  134. SetSelection( selection, true );
  135. Refresh();
  136. }
  137. BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
  138. EVT_SIZE( ITEMS_LISTBOX_BASE::OnSize )
  139. EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
  140. EVT_LIST_ITEM_SELECTED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftClick )
  141. EVT_LIST_ITEM_ACTIVATED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftDClick )
  142. END_EVENT_TABLE()
  143. void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
  144. {
  145. if( m_footprintList.IsEmpty() )
  146. return;
  147. // If the footprint view window is displayed, update the footprint.
  148. if( GetParent()->m_DisplayFootprintFrame )
  149. GetParent()->CreateScreenCmp();
  150. GetParent()->DisplayStatus();
  151. }
  152. void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
  153. {
  154. wxString footprintName = GetSelectedFootprint();
  155. GetParent()->SetNewPkg( footprintName );
  156. }
  157. void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
  158. {
  159. int key = event.GetKeyCode();
  160. switch( key )
  161. {
  162. case WXK_TAB:
  163. case WXK_RIGHT:
  164. case WXK_NUMPAD_RIGHT:
  165. GetParent()->ChangeFocus( true );
  166. return;
  167. case WXK_LEFT:
  168. case WXK_NUMPAD_LEFT:
  169. GetParent()->ChangeFocus( false );
  170. return;
  171. case WXK_HOME:
  172. case WXK_END:
  173. case WXK_UP:
  174. case WXK_DOWN:
  175. case WXK_PAGEUP:
  176. case WXK_PAGEDOWN:
  177. event.Skip();
  178. return;
  179. default:
  180. break;
  181. }
  182. // Search for an item name starting by the key code:
  183. key = toupper( key );
  184. for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
  185. {
  186. wxString text = m_footprintList.Item( ii );
  187. // Search for the start char of the footprint name. Skip the line number.
  188. text.Trim( false ); // Remove leading spaces in line
  189. unsigned jj = 0;
  190. for( ; jj < text.Len(); jj++ )
  191. {
  192. // skip line number
  193. if( text[jj] == ' ' )
  194. break;
  195. }
  196. for( ; jj < text.Len(); jj++ )
  197. { // skip blanks
  198. if( text[jj] != ' ' )
  199. break;
  200. }
  201. int start_char = toupper( text[jj] );
  202. if( key == start_char )
  203. {
  204. SetSelection( ii, true ); // Ensure visible
  205. break;
  206. }
  207. }
  208. }