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.

286 lines
7.3 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) 2016 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 1992-2017 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 footprints_listbox.cpp
  26. * class to display the list of available footprints
  27. */
  28. #include <fctsys.h>
  29. #include <wx/wupdlock.h>
  30. #include <cvpcb.h>
  31. #include <cvpcb_mainframe.h>
  32. #include <listboxes.h>
  33. #include <cvpcb_id.h>
  34. #include <eda_pattern_match.h>
  35. #include <footprint_filter.h>
  36. FOOTPRINTS_LISTBOX::FOOTPRINTS_LISTBOX( CVPCB_MAINFRAME* parent,
  37. wxWindowID id, const wxPoint& loc,
  38. const wxSize& size ) :
  39. ITEMS_LISTBOX_BASE( parent, id, loc, size, wxLC_SINGLE_SEL | wxNO_BORDER )
  40. {
  41. }
  42. FOOTPRINTS_LISTBOX::~FOOTPRINTS_LISTBOX()
  43. {
  44. }
  45. int FOOTPRINTS_LISTBOX::GetCount()
  46. {
  47. return m_footprintList.Count();
  48. }
  49. void FOOTPRINTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
  50. {
  51. unsigned count = m_footprintList.Count();
  52. if( count > 0 )
  53. {
  54. if( linecount >= count )
  55. linecount = count - 1;
  56. m_footprintList[linecount] = text;
  57. }
  58. UpdateWidth( linecount );
  59. }
  60. wxString FOOTPRINTS_LISTBOX::GetSelectedFootprint()
  61. {
  62. wxString footprintName;
  63. int ii = GetFirstSelected();
  64. if( ii >= 0 )
  65. {
  66. wxString msg = m_footprintList[ii];
  67. msg.Trim( true );
  68. msg.Trim( false );
  69. footprintName = msg.AfterFirst( wxChar( ' ' ) );
  70. }
  71. return footprintName;
  72. }
  73. void FOOTPRINTS_LISTBOX::AppendLine( const wxString& text )
  74. {
  75. m_footprintList.Add( text );
  76. int lines = m_footprintList.Count();
  77. SetItemCount( lines );
  78. UpdateWidth( lines - 1 );
  79. }
  80. wxString FOOTPRINTS_LISTBOX::OnGetItemText( long item, long column ) const
  81. {
  82. if( item < 0 || item >= (long)m_footprintList.GetCount() )
  83. return wxEmptyString;
  84. return m_footprintList.Item( item );
  85. }
  86. void FOOTPRINTS_LISTBOX::SetSelection( int index, bool State )
  87. {
  88. if( index >= GetCount() )
  89. index = GetCount() - 1;
  90. if( (index >= 0) && (GetCount() > 0) )
  91. {
  92. #ifndef __WXMAC__
  93. Select( index, State );
  94. #endif
  95. EnsureVisible( index );
  96. #ifdef __WXMAC__
  97. Refresh();
  98. #endif
  99. }
  100. }
  101. void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
  102. COMPONENT* aComponent,
  103. const wxString &aFootPrintFilterPattern,
  104. int aFilterType )
  105. {
  106. wxArrayString newList;
  107. wxString msg;
  108. wxString oldSelection;
  109. FOOTPRINT_FILTER filter( aList );
  110. if( aFilterType & FILTERING_BY_COMPONENT_KEYWORD && aComponent )
  111. filter.FilterByFootprintFilters( aComponent->GetFootprintFilters() );
  112. if( aFilterType & FILTERING_BY_PIN_COUNT && aComponent )
  113. filter.FilterByPinCount( aComponent->GetPinCount() );
  114. if( aFilterType & FILTERING_BY_LIBRARY )
  115. filter.FilterByLibrary( aLibName );
  116. if( aFilterType & FILTERING_BY_NAME )
  117. filter.FilterByPattern( aFootPrintFilterPattern );
  118. if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
  119. oldSelection = m_footprintList[ GetSelection() ];
  120. for( auto& i: filter )
  121. {
  122. msg.Printf( "%3d %s:%s", int( newList.GetCount() + 1 ),
  123. GetChars( i.GetLibNickname() ),
  124. GetChars( i.GetFootprintName() ) );
  125. newList.Add( msg );
  126. }
  127. if( newList == m_footprintList )
  128. return;
  129. m_footprintList = newList;
  130. int selection = m_footprintList.Index( oldSelection );
  131. if( selection == wxNOT_FOUND )
  132. selection = 0;
  133. wxWindowUpdateLocker freeze( this );
  134. DeleteAllItems();
  135. if( m_footprintList.GetCount() )
  136. {
  137. SetItemCount( m_footprintList.GetCount() );
  138. SetSelection( selection, true );
  139. RefreshItems( 0L, m_footprintList.GetCount()-1 );
  140. UpdateWidth();
  141. }
  142. }
  143. BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
  144. EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
  145. EVT_LIST_ITEM_SELECTED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftClick )
  146. EVT_LIST_ITEM_ACTIVATED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftDClick )
  147. END_EVENT_TABLE()
  148. void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
  149. {
  150. if( m_footprintList.IsEmpty() )
  151. return;
  152. // On some plateforms (OSX) the focus is lost when the viewers (fp and 3D viewers)
  153. // are opened and refreshed when a new footprint is selected.
  154. // If the listbox has the focus before selecting a new footprint, it will be forced
  155. // after selection.
  156. bool hasFocus = HasFocus();
  157. // If the footprint view window is displayed, update the footprint.
  158. if( GetParent()->GetFootprintViewerFrame() )
  159. GetParent()->CreateScreenCmp();
  160. GetParent()->DisplayStatus();
  161. if( hasFocus )
  162. SetFocus();
  163. }
  164. void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
  165. {
  166. wxString footprintName = GetSelectedFootprint();
  167. GetParent()->SetNewPkg( footprintName );
  168. }
  169. void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
  170. {
  171. int key = event.GetKeyCode();
  172. switch( key )
  173. {
  174. case WXK_TAB:
  175. case WXK_RIGHT:
  176. case WXK_NUMPAD_RIGHT:
  177. GetParent()->ChangeFocus( true );
  178. return;
  179. case WXK_LEFT:
  180. case WXK_NUMPAD_LEFT:
  181. GetParent()->ChangeFocus( false );
  182. return;
  183. case WXK_HOME:
  184. case WXK_END:
  185. case WXK_UP:
  186. case WXK_DOWN:
  187. case WXK_PAGEUP:
  188. case WXK_PAGEDOWN:
  189. event.Skip();
  190. return;
  191. default:
  192. break;
  193. }
  194. // Search for an item name starting by the key code:
  195. key = toupper( key );
  196. for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
  197. {
  198. wxString text = m_footprintList.Item( ii );
  199. // Search for the start char of the footprint name. Skip the line number.
  200. text.Trim( false ); // Remove leading spaces in line
  201. unsigned jj = 0;
  202. for( ; jj < text.Len(); jj++ )
  203. {
  204. // skip line number
  205. if( text[jj] == ' ' )
  206. break;
  207. }
  208. for( ; jj < text.Len(); jj++ )
  209. { // skip blanks
  210. if( text[jj] != ' ' )
  211. break;
  212. }
  213. int start_char = toupper( text[jj] );
  214. if( key == start_char )
  215. {
  216. SetSelection( ii, true ); // Ensure visible
  217. break;
  218. }
  219. }
  220. event.Skip();
  221. }