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