KiCad PCB EDA Suite
library_listbox.cpp
Go to the documentation of this file.
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 
29 #include <trace_helpers.h>
30 
31 #include <cvpcb_mainframe.h>
32 #include <listboxes.h>
33 #include <cvpcb_id.h>
34 
35 
36 /***************************************/
37 /* ListBox handling the library list */
38 /***************************************/
39 
41  ITEMS_LISTBOX_BASE( parent, id, wxDefaultPosition, wxDefaultSize, wxLC_SINGLE_SEL )
42 {
43 }
44 
45 
47 {
48 }
49 
50 
52 {
53  return m_libraryList.Count();
54 }
55 
56 
57 void LIBRARY_LISTBOX::SetString( unsigned linecount, const wxString& text )
58 {
59  unsigned count = m_libraryList.Count();
60  if( count > 0 )
61  {
62  if( linecount >= count )
63  linecount = count - 1;
64  m_libraryList[linecount] = text;
65  UpdateWidth( linecount );
66  }
67 }
68 
69 
71 {
72  wxString libraryName;
73  int ii = GetFirstSelected();
74 
75  if( ii >= 0 )
76  {
77  libraryName = m_libraryList[ii];
78  }
79 
80  return libraryName;
81 }
82 
83 
84 void LIBRARY_LISTBOX::AppendLine( const wxString& text )
85 {
86  m_libraryList.Add( text );
87  int lines = m_libraryList.Count();
88  SetItemCount( lines );
89  UpdateWidth( lines - 1 );
90 }
91 
92 
93 wxString LIBRARY_LISTBOX::OnGetItemText( long item, long column ) const
94 {
95  return m_libraryList.Item( item );
96 }
97 
98 
99 void LIBRARY_LISTBOX::SetSelection( int index, bool State )
100 {
101  if( index >= GetCount() )
102  index = GetCount() - 1;
103 
104  if( (index >= 0) && (GetCount() > 0) )
105  {
106 #ifndef __WXMAC__
107  Select( index, State );
108 #endif
109  EnsureVisible( index );
110 #ifdef __WXMAC__
111  Refresh();
112 #endif
113  }
114 }
115 
116 
117 void LIBRARY_LISTBOX::SetLibraryList( const wxArrayString& aList )
118 {
119  int oldSelection = GetSelection();
120 
121  m_libraryList = aList;
122 
123  SetItemCount( m_libraryList.GetCount() );
124 
125  if( GetCount() == 0 || oldSelection < 0 || oldSelection >= GetCount() )
126  SetSelection( 0, true );
127 
128  if( m_libraryList.Count() )
129  {
130  RefreshItems( 0L, m_libraryList.Count()-1 );
131  UpdateWidth();
132  }
133 }
134 
135 
136 BEGIN_EVENT_TABLE( LIBRARY_LISTBOX, ITEMS_LISTBOX_BASE )
137  EVT_CHAR( LIBRARY_LISTBOX::OnChar )
139 END_EVENT_TABLE()
140 
141 
142 void LIBRARY_LISTBOX::OnChar( wxKeyEvent& event )
143 {
144  wxLogTrace( kicadTraceKeyEvent, "LIBRARY_LISTBOX::OnChar %s", dump( event ) );
145 
146  int key = event.GetKeyCode();
147 
148  switch( key )
149  {
150  case WXK_HOME:
151  case WXK_END:
152  case WXK_UP:
153  case WXK_DOWN:
154  case WXK_PAGEUP:
155  case WXK_PAGEDOWN:
156  event.Skip();
157  return;
158 
159  default:
160  break;
161  }
162 
163  // Search for an item name starting by the key code:
164  key = toupper(key);
165 
166  for( unsigned ii = 0; ii < m_libraryList.GetCount(); ii++ )
167  {
168  wxString text = m_libraryList.Item( ii );
169 
170  // Search for the start char of the footprint name. Skip the line number.
171  text.Trim( false ); // Remove leading spaces in line
172  unsigned jj = 0;
173 
174  for( ; jj < text.Len(); jj++ )
175  {
176  // skip line number
177  if( text[jj] == ' ' )
178  break;
179  }
180 
181  for( ; jj < text.Len(); jj++ )
182  { // skip blanks
183  if( text[jj] != ' ' )
184  break;
185  }
186 
187  int start_char = toupper( text[jj] );
188 
189  if( key == start_char )
190  {
191  SetSelection( ii, true ); // Ensure visible
192  break;
193  }
194  }
195 
196  event.Skip();
197 }
198 
199 
200 void LIBRARY_LISTBOX::OnSelectLibrary( wxListEvent& event )
201 {
202  // Apply the filter
205 
206  SetFocus();
207  GetParent()->OnSelectComponent( event );
208 }
void SetLibraryList(const wxArrayString &aList)
void AppendLine(const wxString &text)
void OnChar(wxKeyEvent &event)
Function OnChar called on a key pressed Call default handler for some special keys,...
wxString OnGetItemText(long item, long column) const override
void OnSelectLibrary(wxListEvent &event)
void OnSelectComponent(wxListEvent &event)
Function OnSelectComponent Called when clicking on a component in component list window.
void UpdateWidth(int aLine=-1)
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
void Refresh()
Update the board display after modifying it by a python script (note: it is automatically called by a...
void SetString(unsigned linecount, const wxString &text)
wxString GetSelectedLibrary()
wxLogTrace helper definitions.
LIBRARY_LISTBOX(CVPCB_MAINFRAME *parent, wxWindowID id)
wxArrayString m_libraryList
Definition: listboxes.h:150
void SetSelection(int index, bool State=true)
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
void SetFootprintFilter(FOOTPRINTS_LISTBOX::FP_FILTER_T aFilter, CVPCB_MAINFRAME::CVPCB_FILTER_ACTION aAction)
Function SetFootprintFilter Set a filter criteria to either on/off or toggle the criteria.
The CvPcb application main window.
virtual CVPCB_MAINFRAME * GetParent() const