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 <trace_helpers.h>
29 
30 #include <cvpcb_mainframe.h>
31 #include <listboxes.h>
32 #include <cvpcb_id.h>
33 
34 
36  ITEMS_LISTBOX_BASE( parent, id )
37 {
38 }
39 
40 
42 {
43 }
44 
45 
46 BEGIN_EVENT_TABLE( COMPONENTS_LISTBOX, ITEMS_LISTBOX_BASE )
47  EVT_CHAR( COMPONENTS_LISTBOX::OnChar )
49 END_EVENT_TABLE()
50 
51 
52 void COMPONENTS_LISTBOX::Clear()
53 {
54  m_ComponentList.Clear();
55  SetItemCount( 0 );
56 }
57 
58 
60 {
61  return m_ComponentList.Count();
62 }
63 
64 
65 void COMPONENTS_LISTBOX::SetString( unsigned linecount, const wxString& text )
66 {
67  if( linecount >= m_ComponentList.Count() )
68  linecount = m_ComponentList.Count() - 1;
69 
70  if( m_ComponentList.Count() > 0 )
71  {
72  m_ComponentList[linecount] = text;
73  UpdateWidth( linecount );
74  }
75 }
76 
77 
78 void COMPONENTS_LISTBOX::AppendLine( const wxString& text )
79 {
80  m_ComponentList.Add( text );
81  int lines = m_ComponentList.Count();
82  SetItemCount( lines );
83  UpdateWidth( lines - 1 );
84 }
85 
86 
87 wxString COMPONENTS_LISTBOX::OnGetItemText( long item, long column ) const
88 {
89  return m_ComponentList.Item( item );
90 }
91 
92 
93 void COMPONENTS_LISTBOX::SetSelection( int index, bool State )
94 {
95  if( index >= GetCount() )
96  index = GetCount() - 1;
97 
98  if( (index >= 0) && (GetCount() > 0) )
99  {
100  Select( index, State );
101  EnsureVisible( index );
102 
103 #ifdef __WXMAC__
104  Update();
105 #endif
106  }
107 }
108 
109 
110 void COMPONENTS_LISTBOX::OnChar( wxKeyEvent& event )
111 {
112  wxLogTrace( kicadTraceKeyEvent, "COMPONENTS_LISTBOX::OnChar %s", dump( event ) );
113 
114  int key = event.GetKeyCode();
115 
116  switch( key )
117  {
118  case WXK_HOME:
119  case WXK_END:
120  case WXK_UP:
121  case WXK_DOWN:
122  case WXK_PAGEUP:
123  case WXK_PAGEDOWN:
124  event.Skip();
125  return;
126 
127 
128  default:
129  break;
130  }
131 
132  // Search for an item name starting by the key code:
133  key = toupper( key );
134 
135  for( unsigned ii = 0; ii < m_ComponentList.GetCount(); ii++ )
136  {
137  wxString text = m_ComponentList.Item( ii );
138 
139  // Search for the start char of the footprint name. Skip the line number.
140  text.Trim( false ); // Remove leading spaces in line
141  unsigned jj = 0;
142 
143  for( ; jj < text.Len(); jj++ )
144  { // skip line number
145  if( text[jj] == ' ' )
146  break;
147  }
148 
149  for( ; jj < text.Len(); jj++ )
150  { // skip blanks
151  if( text[jj] != ' ' )
152  break;
153  }
154 
155  int start_char = toupper( text[jj] );
156 
157  if( key == start_char )
158  {
159  SetSelection( (int) ii, true ); // Ensure visible
160  break;
161  }
162  }
163 
164  event.Skip();
165 }
166 
167 
168 void COMPONENTS_LISTBOX::OnSelectComponent( wxListEvent& event )
169 {
170  SetFocus();
171  GetParent()->OnSelectComponent( event );
172 }
void OnChar(wxKeyEvent &event)
Function OnChar called on a key pressed Call default handler for some special keys,...
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 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)
wxString dump(const wxArrayString &aArray)
Debug helper for printing wxArrayString contents.
wxLogTrace helper definitions.
wxArrayString m_ComponentList
Definition: listboxes.h:194
void SetString(unsigned linecount, const wxString &text)
const wxChar *const kicadTraceKeyEvent
Flag to enable wxKeyEvent debug tracing.
void OnSelectComponent(wxListEvent &event)
The CvPcb application main window.
virtual CVPCB_MAINFRAME * GetParent() const
COMPONENTS_LISTBOX(CVPCB_MAINFRAME *parent, wxWindowID id)