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.

288 lines
8.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2013 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  6. * Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. #include <eda_list_dialog.h>
  26. #include <eda_draw_frame.h>
  27. #include <string_utils.h>
  28. #include <macros.h>
  29. #include "lib_tree_model_adapter.h"
  30. // wxWidgets spends *far* too long calculating column widths (most of it, believe it or
  31. // not, in repeatedly creating/destroying a wxDC to do the measurement in).
  32. // Use default column widths instead.
  33. static int DEFAULT_SINGLE_COL_WIDTH = 260;
  34. static int DEFAULT_COL_WIDTHS[] = { 200, 300 };
  35. EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle,
  36. const wxArrayString& aItemHeaders,
  37. const std::vector<wxArrayString>& aItemList,
  38. const wxString& aPreselectText, bool aSortList ) :
  39. EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
  40. m_sortList( aSortList )
  41. {
  42. m_filterBox->SetHint( _( "Filter" ) );
  43. initDialog( aItemHeaders, aItemList, aPreselectText );
  44. // DIALOG_SHIM needs a unique hash_key because classname is not sufficient
  45. // because so many dialogs share this same class, with different numbers of
  46. // columns, different column names, and column widths.
  47. m_hash_key = TO_UTF8( aTitle );
  48. SetupStandardButtons();
  49. Layout();
  50. GetSizer()->Fit( this );
  51. Centre();
  52. }
  53. EDA_LIST_DIALOG::EDA_LIST_DIALOG( wxWindow* aParent, const wxString& aTitle, bool aSortList ) :
  54. EDA_LIST_DIALOG_BASE( aParent, wxID_ANY, aTitle ),
  55. m_sortList( aSortList )
  56. {
  57. m_filterBox->SetHint( _( "Filter" ) );
  58. }
  59. bool EDA_LIST_DIALOG::Show( bool show )
  60. {
  61. bool retVal = DIALOG_SHIM::Show( show );
  62. if( show )
  63. {
  64. wxSizeEvent dummy;
  65. onSize( dummy );
  66. }
  67. return retVal;
  68. }
  69. void EDA_LIST_DIALOG::initDialog( const wxArrayString& aItemHeaders,
  70. const std::vector<wxArrayString>& aItemList,
  71. const wxString& aPreselectText )
  72. {
  73. if( aItemHeaders.Count() == 1 )
  74. {
  75. m_listBox->InsertColumn( 0, aItemHeaders.Item( 0 ), wxLIST_FORMAT_LEFT,
  76. DEFAULT_SINGLE_COL_WIDTH );
  77. m_listBox->SetMinClientSize( wxSize( DEFAULT_SINGLE_COL_WIDTH, 200 ) );
  78. SetMinClientSize( wxSize( DEFAULT_COL_WIDTHS[0], 220 ) );
  79. }
  80. else if( aItemHeaders.Count() == 2 )
  81. {
  82. for( unsigned i = 0; i < aItemHeaders.Count(); i++ )
  83. {
  84. m_listBox->InsertColumn( i, aItemHeaders.Item( i ), wxLIST_FORMAT_LEFT,
  85. DEFAULT_COL_WIDTHS[ i ] );
  86. }
  87. m_listBox->SetMinClientSize( wxSize( DEFAULT_COL_WIDTHS[0] * 3, 200 ) );
  88. SetMinClientSize( wxSize( DEFAULT_COL_WIDTHS[0] * 2, 220 ) );
  89. }
  90. m_itemsList = aItemList;
  91. InsertItems( m_itemsList, 0 );
  92. if( !aPreselectText.IsEmpty() )
  93. {
  94. long sel = m_listBox->FindItem( -1, aPreselectText );
  95. if( sel != wxNOT_FOUND )
  96. {
  97. m_listBox->SetItemState( sel, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
  98. // Set to a small size so EnsureVisible() won't be foiled by later additions.
  99. // ListBox will expand to fit later.
  100. m_listBox->SetSize( m_listBox->GetSize().GetX(), 100 );
  101. m_listBox->EnsureVisible( sel );
  102. }
  103. }
  104. }
  105. void EDA_LIST_DIALOG::SetListLabel( const wxString& aLabel )
  106. {
  107. m_listLabel->SetLabel( aLabel );
  108. m_listBox->SetSingleStyle( wxLC_NO_HEADER, true );
  109. }
  110. void EDA_LIST_DIALOG::SetOKLabel( const wxString& aLabel )
  111. {
  112. m_sdbSizerOK->SetLabel( aLabel );
  113. }
  114. void EDA_LIST_DIALOG::HideFilter()
  115. {
  116. m_filterBox->Hide();
  117. }
  118. void EDA_LIST_DIALOG::textChangeInFilterBox( wxCommandEvent& event )
  119. {
  120. wxString filter;
  121. wxString itemName;
  122. filter = wxT( "*" ) + m_filterBox->GetLineText( 0 ).MakeLower() + wxT( "*" );
  123. m_listBox->DeleteAllItems();
  124. for( const wxArrayString& row : m_itemsList )
  125. {
  126. itemName = row.Item( 0 );
  127. if( itemName.MakeLower().Matches( filter ) )
  128. Append( row );
  129. }
  130. sortList();
  131. }
  132. long EDA_LIST_DIALOG::GetSelection()
  133. {
  134. return m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
  135. }
  136. wxString EDA_LIST_DIALOG::GetTextSelection( int aColumn )
  137. {
  138. wxCHECK_MSG( unsigned( aColumn ) < unsigned( m_listBox->GetColumnCount() ), wxEmptyString,
  139. wxT( "Invalid list control column." ) );
  140. long item = m_listBox->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
  141. wxString text;
  142. if( item >= 0 ) // if something is selected.
  143. {
  144. wxListItem info;
  145. info.m_mask = wxLIST_MASK_TEXT;
  146. info.m_itemId = item;
  147. info.m_col = aColumn;
  148. if( m_listBox->GetItem( info ) )
  149. text = info.m_text;
  150. if( text.StartsWith( LIB_TREE_MODEL_ADAPTER::GetPinningSymbol() ) )
  151. text = text.substr( LIB_TREE_MODEL_ADAPTER::GetPinningSymbol().length() );
  152. }
  153. return text;
  154. }
  155. void EDA_LIST_DIALOG::Append( const wxArrayString& itemList )
  156. {
  157. long itemIndex = m_listBox->InsertItem( m_listBox->GetItemCount(), itemList[0] );
  158. m_listBox->SetItemPtrData( itemIndex, wxUIntPtr( &itemList[0] ) );
  159. // Adding the next columns content
  160. for( unsigned i = 1; i < itemList.size(); i++ )
  161. m_listBox->SetItem( itemIndex, i, itemList[i] );
  162. }
  163. void EDA_LIST_DIALOG::InsertItems( const std::vector< wxArrayString >& itemList, int position )
  164. {
  165. for( unsigned row = 0; row < itemList.size(); row++ )
  166. {
  167. wxASSERT( (int) itemList[row].GetCount() == m_listBox->GetColumnCount() );
  168. for( unsigned col = 0; col < itemList[row].GetCount(); col++ )
  169. {
  170. wxListItem info;
  171. info.m_itemId = row + position;
  172. info.m_col = col;
  173. info.m_text = itemList[row].Item( col );
  174. info.m_width = DEFAULT_COL_WIDTHS[ col ];
  175. info.m_mask = wxLIST_MASK_TEXT | wxLIST_MASK_WIDTH;
  176. if( col == 0 )
  177. {
  178. info.m_data = wxUIntPtr( &itemList[row].Item( col ) );
  179. info.m_mask |= wxLIST_MASK_DATA;
  180. m_listBox->InsertItem( info );
  181. }
  182. else
  183. {
  184. m_listBox->SetItem( info );
  185. }
  186. }
  187. }
  188. sortList();
  189. }
  190. void EDA_LIST_DIALOG::onListItemActivated( wxListEvent& event )
  191. {
  192. wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
  193. }
  194. void EDA_LIST_DIALOG::onSize( wxSizeEvent& event )
  195. {
  196. if( m_listBox->GetColumnCount() == 1 )
  197. {
  198. m_listBox->SetColumnWidth( 0, m_listBox->GetClientSize().x );
  199. }
  200. else if( m_listBox->GetColumnCount() == 2 )
  201. {
  202. int first = KiROUND( m_listBox->GetClientSize().x * 0.42 );
  203. m_listBox->SetColumnWidth( 0, first );
  204. m_listBox->SetColumnWidth( 1, m_listBox->GetClientSize().x - first );
  205. }
  206. event.Skip();
  207. }
  208. /*
  209. * Sort alphabetically, case insensitive.
  210. */
  211. static int wxCALLBACK myCompareFunction( wxIntPtr aItem1, wxIntPtr aItem2,
  212. wxIntPtr WXUNUSED( aSortData ) )
  213. {
  214. wxString* component1Name = (wxString*) aItem1;
  215. wxString* component2Name = (wxString*) aItem2;
  216. return StrNumCmp( *component1Name, *component2Name, true );
  217. }
  218. void EDA_LIST_DIALOG::sortList()
  219. {
  220. if( m_sortList )
  221. m_listBox->SortItems( myCompareFunction, 0 );
  222. }