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  const wxPoint& loc, const wxSize& size ) :
42  ITEMS_LISTBOX_BASE( parent, id, loc, size, wxLC_SINGLE_SEL )
43 {
44 }
45 
46 
48 {
49 }
50 
51 
53 {
54  return m_libraryList.Count();
55 }
56 
57 
58 void LIBRARY_LISTBOX::SetString( unsigned linecount, const wxString& text )
59 {
60  unsigned count = m_libraryList.Count();
61  if( count > 0 )
62  {
63  if( linecount >= count )
64  linecount = count - 1;
65  m_libraryList[linecount] = text;
66  UpdateWidth( linecount );
67  }
68 }
69 
70 
72 {
73  wxString libraryName;
74  int ii = GetFirstSelected();
75 
76  if( ii >= 0 )
77  {
78  libraryName = m_libraryList[ii];
79  }
80 
81  return libraryName;
82 }
83 
84 
85 void LIBRARY_LISTBOX::AppendLine( const wxString& text )
86 {
87  m_libraryList.Add( text );
88  int lines = m_libraryList.Count();
89  SetItemCount( lines );
90  UpdateWidth( lines - 1 );
91 }
92 
93 
94 wxString LIBRARY_LISTBOX::OnGetItemText( long item, long column ) const
95 {
96  return m_libraryList.Item( item );
97 }
98 
99 
100 void LIBRARY_LISTBOX::SetSelection( int index, bool State )
101 {
102  if( index >= GetCount() )
103  index = GetCount() - 1;
104 
105  if( (index >= 0) && (GetCount() > 0) )
106  {
107 #ifndef __WXMAC__
108  Select( index, State );
109 #endif
110  EnsureVisible( index );
111 #ifdef __WXMAC__
112  Refresh();
113 #endif
114  }
115 }
116 
117 
118 void LIBRARY_LISTBOX::SetLibraryList( const wxArrayString& aList )
119 {
120  int oldSelection = GetSelection();
121 
122  m_libraryList = aList;
123 
124  SetItemCount( m_libraryList.GetCount() );
125 
126  if( GetCount() == 0 || oldSelection < 0 || oldSelection >= GetCount() )
127  SetSelection( 0, true );
128 
129  if( m_libraryList.Count() )
130  {
131  RefreshItems( 0L, m_libraryList.Count()-1 );
132  UpdateWidth();
133  }
134 }
135 
136 
137 BEGIN_EVENT_TABLE( LIBRARY_LISTBOX, ITEMS_LISTBOX_BASE )
138  EVT_CHAR( LIBRARY_LISTBOX::OnChar )
140 END_EVENT_TABLE()
141 
142 
143 void LIBRARY_LISTBOX::OnChar( wxKeyEvent& event )
144 {
145  wxLogTrace( kicadTraceKeyEvent, "LIBRARY_LISTBOX::OnChar %s", dump( event ) );
146 
147  int key = event.GetKeyCode();
148 
149  switch( key )
150  {
151  case WXK_HOME:
152  case WXK_END:
153  case WXK_UP:
154  case WXK_DOWN:
155  case WXK_PAGEUP:
156  case WXK_PAGEDOWN:
157  event.Skip();
158  return;
159 
160  default:
161  break;
162  }
163 
164  // Search for an item name starting by the key code:
165  key = toupper(key);
166 
167  for( unsigned ii = 0; ii < m_libraryList.GetCount(); ii++ )
168  {
169  wxString text = m_libraryList.Item( ii );
170 
171  // Search for the start char of the footprint name. Skip the line number.
172  text.Trim( false ); // Remove leading spaces in line
173  unsigned jj = 0;
174 
175  for( ; jj < text.Len(); jj++ )
176  {
177  // skip line number
178  if( text[jj] == ' ' )
179  break;
180  }
181 
182  for( ; jj < text.Len(); jj++ )
183  { // skip blanks
184  if( text[jj] != ' ' )
185  break;
186  }
187 
188  int start_char = toupper( text[jj] );
189 
190  if( key == start_char )
191  {
192  SetSelection( ii, true ); // Ensure visible
193  break;
194  }
195  }
196 
197  event.Skip();
198 }
199 
200 
201 void LIBRARY_LISTBOX::OnSelectLibrary( wxListEvent& event )
202 {
203  // Apply the filter
206 
207  SetFocus();
208  GetParent()->OnSelectComponent( event );
209 }
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...
LIBRARY_LISTBOX(CVPCB_MAINFRAME *parent, wxWindowID id, const wxPoint &loc, const wxSize &size)
void SetString(unsigned linecount, const wxString &text)
wxString GetSelectedLibrary()
wxLogTrace helper definitions.
wxArrayString m_libraryList
Definition: listboxes.h:155
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