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