KiCad PCB EDA Suite
class_components_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-2016 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 
28 #include <fctsys.h>
29 #include <wxstruct.h>
30 
31 #include <cvpcb.h>
32 #include <cvpcb_mainframe.h>
33 #include <listview_classes.h>
34 #include <cvpcb_id.h>
35 
36 
38  const wxPoint& loc, const wxSize& size ) :
39  ITEMS_LISTBOX_BASE( parent, id, loc, size, 0 )
40 {
41 }
42 
43 
45 {
46 }
47 
48 
49 BEGIN_EVENT_TABLE( COMPONENTS_LISTBOX, ITEMS_LISTBOX_BASE )
50  EVT_CHAR( COMPONENTS_LISTBOX::OnChar )
51  EVT_LIST_ITEM_SELECTED( ID_CVPCB_COMPONENT_LIST, COMPONENTS_LISTBOX::OnSelectComponent )
52 END_EVENT_TABLE()
53 
54 
55 void COMPONENTS_LISTBOX::Clear()
56 {
57  m_ComponentList.Clear();
58  SetItemCount( 0 );
59 }
60 
61 
63 {
64  return m_ComponentList.Count();
65 }
66 
67 
68 void COMPONENTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
69 {
70  if( linecount >= m_ComponentList.Count() )
71  linecount = m_ComponentList.Count() - 1;
72 
73  if( m_ComponentList.Count() > 0 )
74  {
75  m_ComponentList[linecount] = text;
76  UpdateWidth( linecount );
77  }
78 }
79 
80 
81 void COMPONENTS_LISTBOX::AppendLine( const wxString& text )
82 {
83  m_ComponentList.Add( text );
84  int lines = m_ComponentList.Count();
85  SetItemCount( lines );
86  UpdateWidth( lines - 1 );
87 }
88 
89 
90 wxString COMPONENTS_LISTBOX::OnGetItemText( long item, long column ) const
91 {
92  return m_ComponentList.Item( item );
93 }
94 
95 
96 void COMPONENTS_LISTBOX::SetSelection( int index, bool State )
97 {
98  if( index >= GetCount() )
99  index = GetCount() - 1;
100 
101  if( (index >= 0) && (GetCount() > 0) )
102  {
103  Select( index, State );
104  EnsureVisible( index );
105 
106 #ifdef __WXMAC__
107  Update();
108 #endif
109  }
110 }
111 
112 
113 void COMPONENTS_LISTBOX::OnChar( wxKeyEvent& event )
114 {
115  int key = event.GetKeyCode();
116 
117  switch( key )
118  {
119  case WXK_TAB:
120  case WXK_RIGHT:
121  case WXK_NUMPAD_RIGHT:
122  GetParent()->ChangeFocus( true );
123  return;
124 
125  case WXK_LEFT:
126  case WXK_NUMPAD_LEFT:
127  GetParent()->ChangeFocus( false );
128  return;
129 
130  case WXK_HOME:
131  case WXK_END:
132  case WXK_UP:
133  case WXK_DOWN:
134  case WXK_PAGEUP:
135  case WXK_PAGEDOWN:
136  event.Skip();
137  return;
138 
139 
140  default:
141  break;
142  }
143 
144  // Search for an item name starting by the key code:
145  key = toupper( key );
146 
147  for( unsigned ii = 0; ii < m_ComponentList.GetCount(); ii++ )
148  {
149  wxString text = m_ComponentList.Item( ii );
150 
151  // Search for the start char of the footprint name. Skip the line number.
152  text.Trim( false ); // Remove leading spaces in line
153  unsigned jj = 0;
154 
155  for( ; jj < text.Len(); jj++ )
156  { // skip line number
157  if( text[jj] == ' ' )
158  break;
159  }
160 
161  for( ; jj < text.Len(); jj++ )
162  { // skip blanks
163  if( text[jj] != ' ' )
164  break;
165  }
166 
167  int start_char = toupper( text[jj] );
168 
169  if( key == start_char )
170  {
171  SetSelection( (int) ii, true ); // Ensure visible
172  break;
173  }
174  }
175 
176  event.Skip();
177 }
178 
179 
180 void COMPONENTS_LISTBOX::OnSelectComponent( wxListEvent& event )
181 {
182  SetFocus();
183  GetParent()->OnSelectComponent( event );
184 }
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 component that the name starts by the letter.
void SetSelection(int index, bool State=true)
wxString OnGetItemText(long item, long column) const override
Function OnGetItemText this overloaded function MUST be provided for the wxLC_VIRTUAL mode because re...
void ChangeFocus(bool aMoveRight)
void AppendLine(const wxString &text)
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
Base window classes and related definitions.
COMPONENTS_LISTBOX(CVPCB_MAINFRAME *parent, wxWindowID id, const wxPoint &loc, const wxSize &size)
wxArrayString m_ComponentList
void SetString(unsigned linecount, const wxString &text)
virtual CVPCB_MAINFRAME * GetParent() const
Definition: listboxes.cpp:119
void OnSelectComponent(wxListEvent &event)
The CvPcb application main window.