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.

299 lines
7.8 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. Select( index, State );
  93. EnsureVisible( index );
  94. Refresh();
  95. }
  96. }
  97. void FOOTPRINTS_LISTBOX::SetSelectedFootprint( const LIB_ID& aFPID )
  98. {
  99. wxString id = wxString::Format( "%s:%s",
  100. GetChars( aFPID.GetLibNickname() ),
  101. GetChars( aFPID.GetLibItemName() ) );
  102. for( int i = 0; i < GetCount(); ++i )
  103. {
  104. wxString candidate = m_footprintList.Item( i ).substr( 4 );
  105. if( candidate.CmpNoCase( id ) == 0 )
  106. {
  107. SetSelection( i, true );
  108. return;
  109. }
  110. }
  111. }
  112. void FOOTPRINTS_LISTBOX::SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName,
  113. COMPONENT* aComponent,
  114. const wxString &aFootPrintFilterPattern,
  115. int aFilterType )
  116. {
  117. wxArrayString newList;
  118. wxString msg;
  119. wxString oldSelection;
  120. FOOTPRINT_FILTER filter( aList );
  121. if( aFilterType & FILTERING_BY_COMPONENT_KEYWORD && aComponent )
  122. filter.FilterByFootprintFilters( aComponent->GetFootprintFilters() );
  123. if( aFilterType & FILTERING_BY_PIN_COUNT && aComponent )
  124. filter.FilterByPinCount( aComponent->GetPinCount() );
  125. if( aFilterType & FILTERING_BY_LIBRARY )
  126. filter.FilterByLibrary( aLibName );
  127. if( aFilterType & FILTERING_BY_NAME )
  128. filter.FilterByPattern( aFootPrintFilterPattern );
  129. if( GetSelection() >= 0 && GetSelection() < (int)m_footprintList.GetCount() )
  130. oldSelection = m_footprintList[ GetSelection() ];
  131. for( auto& i: filter )
  132. {
  133. msg.Printf( "%3d %s:%s", int( newList.GetCount() + 1 ),
  134. GetChars( i.GetLibNickname() ),
  135. GetChars( i.GetFootprintName() ) );
  136. newList.Add( msg );
  137. }
  138. if( newList == m_footprintList )
  139. return;
  140. m_footprintList = newList;
  141. int selection = m_footprintList.Index( oldSelection );
  142. if( selection == wxNOT_FOUND )
  143. selection = 0;
  144. wxWindowUpdateLocker freeze( this );
  145. DeleteAllItems();
  146. if( m_footprintList.GetCount() )
  147. {
  148. SetItemCount( m_footprintList.GetCount() );
  149. SetSelection( selection, true );
  150. RefreshItems( 0L, m_footprintList.GetCount()-1 );
  151. UpdateWidth();
  152. }
  153. }
  154. BEGIN_EVENT_TABLE( FOOTPRINTS_LISTBOX, ITEMS_LISTBOX_BASE )
  155. EVT_CHAR( FOOTPRINTS_LISTBOX::OnChar )
  156. EVT_LIST_ITEM_SELECTED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftClick )
  157. EVT_LIST_ITEM_ACTIVATED( ID_CVPCB_FOOTPRINT_LIST, FOOTPRINTS_LISTBOX::OnLeftDClick )
  158. END_EVENT_TABLE()
  159. void FOOTPRINTS_LISTBOX::OnLeftClick( wxListEvent& event )
  160. {
  161. if( m_footprintList.IsEmpty() )
  162. return;
  163. // On some plateforms (OSX) the focus is lost when the viewers (fp and 3D viewers)
  164. // are opened and refreshed when a new footprint is selected.
  165. // If the listbox has the focus before selecting a new footprint, it will be forced
  166. // after selection.
  167. bool hasFocus = HasFocus();
  168. // If the footprint view window is displayed, update the footprint.
  169. if( GetParent()->GetFootprintViewerFrame() )
  170. GetParent()->CreateScreenCmp();
  171. GetParent()->DisplayStatus();
  172. if( hasFocus )
  173. SetFocus();
  174. }
  175. void FOOTPRINTS_LISTBOX::OnLeftDClick( wxListEvent& event )
  176. {
  177. wxString footprintName = GetSelectedFootprint();
  178. GetParent()->SetNewPkg( footprintName );
  179. }
  180. void FOOTPRINTS_LISTBOX::OnChar( wxKeyEvent& event )
  181. {
  182. int key = event.GetKeyCode();
  183. switch( key )
  184. {
  185. case WXK_TAB:
  186. case WXK_RIGHT:
  187. case WXK_NUMPAD_RIGHT:
  188. GetParent()->ChangeFocus( true );
  189. return;
  190. case WXK_LEFT:
  191. case WXK_NUMPAD_LEFT:
  192. GetParent()->ChangeFocus( false );
  193. return;
  194. case WXK_HOME:
  195. case WXK_END:
  196. case WXK_UP:
  197. case WXK_DOWN:
  198. case WXK_PAGEUP:
  199. case WXK_PAGEDOWN:
  200. event.Skip();
  201. return;
  202. default:
  203. break;
  204. }
  205. // Search for an item name starting by the key code:
  206. key = toupper( key );
  207. for( unsigned ii = 0; ii < m_footprintList.GetCount(); ii++ )
  208. {
  209. wxString text = m_footprintList.Item( ii );
  210. // Search for the start char of the footprint name. Skip the line number.
  211. text.Trim( false ); // Remove leading spaces in line
  212. unsigned jj = 0;
  213. for( ; jj < text.Len(); jj++ )
  214. {
  215. // skip line number
  216. if( text[jj] == ' ' )
  217. break;
  218. }
  219. for( ; jj < text.Len(); jj++ )
  220. { // skip blanks
  221. if( text[jj] != ' ' )
  222. break;
  223. }
  224. int start_char = toupper( text[jj] );
  225. if( key == start_char )
  226. {
  227. SetSelection( ii, true ); // Ensure visible
  228. break;
  229. }
  230. }
  231. event.Skip();
  232. }