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 <fctsys.h>
30 #include <macros.h>
31 
32 #include <cvpcb.h>
33 #include <cvpcb_mainframe.h>
34 #include <listboxes.h>
35 #include <cvpcb_id.h>
36 
37 
38 /***************************************/
39 /* ListBox handling the library list */
40 /***************************************/
41 
43  const wxPoint& loc, const wxSize& size ) :
44  ITEMS_LISTBOX_BASE( parent, id, loc, size, wxLC_SINGLE_SEL )
45 {
46 }
47 
48 
50 {
51 }
52 
53 
55 {
56  return m_libraryList.Count();
57 }
58 
59 
60 void LIBRARY_LISTBOX::SetString( unsigned linecount, const wxString& text )
61 {
62  unsigned count = m_libraryList.Count();
63  if( count > 0 )
64  {
65  if( linecount >= count )
66  linecount = count - 1;
67  m_libraryList[linecount] = text;
68  UpdateWidth( linecount );
69  }
70 }
71 
72 
74 {
75  wxString libraryName;
76  int ii = GetFirstSelected();
77 
78  if( ii >= 0 )
79  {
80  libraryName = m_libraryList[ii];
81  }
82 
83  return libraryName;
84 }
85 
86 
87 void LIBRARY_LISTBOX::AppendLine( const wxString& text )
88 {
89  m_libraryList.Add( text );
90  int lines = m_libraryList.Count();
91  SetItemCount( lines );
92  UpdateWidth( lines - 1 );
93 }
94 
95 
96 wxString LIBRARY_LISTBOX::OnGetItemText( long item, long column ) const
97 {
98  return m_libraryList.Item( item );
99 }
100 
101 
102 void LIBRARY_LISTBOX::SetSelection( int index, bool State )
103 {
104  if( index >= GetCount() )
105  index = GetCount() - 1;
106 
107  if( (index >= 0) && (GetCount() > 0) )
108  {
109 #ifndef __WXMAC__
110  Select( index, State );
111 #endif
112  EnsureVisible( index );
113 #ifdef __WXMAC__
114  Refresh();
115 #endif
116  }
117 }
118 
119 
120 void LIBRARY_LISTBOX::SetLibraryList( const wxArrayString& aList )
121 {
122  int oldSelection = GetSelection();
123 
124  m_libraryList = aList;
125 
126  SetItemCount( m_libraryList.GetCount() );
127 
128  if( GetCount() == 0 || oldSelection < 0 || oldSelection >= GetCount() )
129  SetSelection( 0, true );
130 
131  if( m_libraryList.Count() )
132  {
133  RefreshItems( 0L, m_libraryList.Count()-1 );
134  UpdateWidth();
135  }
136 }
137 
138 
139 BEGIN_EVENT_TABLE( LIBRARY_LISTBOX, ITEMS_LISTBOX_BASE )
140  EVT_CHAR( LIBRARY_LISTBOX::OnChar )
142 END_EVENT_TABLE()
143 
144 
145 void LIBRARY_LISTBOX::OnChar( wxKeyEvent& event )
146 {
147  int key = event.GetKeyCode();
148 
149  switch( key )
150  {
151  case WXK_TAB:
152  case WXK_RIGHT:
153  case WXK_NUMPAD_RIGHT:
154  GetParent()->ChangeFocus( true );
155  return;
156 
157  case WXK_LEFT:
158  case WXK_NUMPAD_LEFT:
159  GetParent()->ChangeFocus( false );
160  return;
161 
162  case WXK_HOME:
163  case WXK_END:
164  case WXK_UP:
165  case WXK_DOWN:
166  case WXK_PAGEUP:
167  case WXK_PAGEDOWN:
168  event.Skip();
169  return;
170 
171  default:
172  break;
173  }
174 
175  // Search for an item name starting by the key code:
176  key = toupper(key);
177 
178  for( unsigned ii = 0; ii < m_libraryList.GetCount(); ii++ )
179  {
180  wxString text = m_libraryList.Item( ii );
181 
182  // Search for the start char of the footprint name. Skip the line number.
183  text.Trim( false ); // Remove leading spaces in line
184  unsigned jj = 0;
185 
186  for( ; jj < text.Len(); jj++ )
187  {
188  // skip line number
189  if( text[jj] == ' ' )
190  break;
191  }
192 
193  for( ; jj < text.Len(); jj++ )
194  { // skip blanks
195  if( text[jj] != ' ' )
196  break;
197  }
198 
199  int start_char = toupper( text[jj] );
200 
201  if( key == start_char )
202  {
203  SetSelection( ii, true ); // Ensure visible
204  break;
205  }
206  }
207 
208  event.Skip();
209 }
210 
211 
212 void LIBRARY_LISTBOX::OnSelectLibrary( wxListEvent& event )
213 {
214  wxCommandEvent setLibraryFilterEvent;
215  setLibraryFilterEvent.SetId( ID_CVPCB_FOOTPRINT_DISPLAY_BY_LIBRARY_LIST );
216  setLibraryFilterEvent.SetInt( 1 );
217  GetParent()->OnSelectFilteringFootprint( setLibraryFilterEvent );
218 
219  SetFocus();
220  GetParent()->OnSelectComponent( event );
221 }
void SetLibraryList(const wxArrayString &aList)
void OnSelectFilteringFootprint(wxCommandEvent &event)
Function OnSelectFilteringFootprint is the command event handler for enabling and disabling footprint...
void AppendLine(const wxString &text)
void OnChar(wxKeyEvent &event)
Function OnChar called on a key pressed Call default handler for some special keys, and for "ascii" keys, select the first footprint that the name starts by the letter.
void ChangeFocus(bool aMoveRight)
wxString OnGetItemText(long item, long column) const override
This file contains miscellaneous commonly used macros and functions.
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)
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()
wxArrayString m_libraryList
Definition: listboxes.h:155
void SetSelection(int index, bool State=true)
virtual CVPCB_MAINFRAME * GetParent() const
The CvPcb application main window.