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