KiCad PCB EDA Suite
FOOTPRINT_CHOICE Class Reference

Customized combo box for footprint selection. More...

#include <footprint_choice.h>

Inheritance diagram for FOOTPRINT_CHOICE:

Public Member Functions

 FOOTPRINT_CHOICE (wxWindow *aParent, int aId)
 
virtual ~FOOTPRINT_CHOICE ()
 

Protected Member Functions

virtual void DoSetPopupControl (wxComboPopup *aPopup) override
 
virtual void OnDrawItem (wxDC &aDC, wxRect const &aRect, int aItem, int aFlags) const override
 
virtual wxCoord OnMeasureItem (size_t aItem) const override
 
virtual wxCoord OnMeasureItemWidth (size_t aItem) const override
 
void TryVetoMouse (wxMouseEvent &aEvent)
 Veto a mouseover event if in the separator. More...
 
void TryVetoSelect (wxCommandEvent &aEvent, bool aInner)
 Veto a select event for the separator. More...
 
wxString SafeGetString (int aItem) const
 Safely get a string for an item, returning wxEmptyString if the item doesn't exist. More...
 
int GetSelectionEither (bool aInner) const
 Get selection from either the outer (combo box) or inner (popup) list. More...
 
void SetSelectionEither (bool aInner, int aSel)
 Safely set selection for either the outer (combo box) or inner (popup) list, doing nothing for invalid selections. More...
 

Static Protected Member Functions

static wxCoord DrawTextFragment (wxDC &aDC, wxCoord x, wxCoord y, wxString const &aText)
 Draw a fragment of text, then return the next x coordinate to continue drawing. More...
 

Static Protected Attributes

static wxColour m_grey
 

Private Attributes

int m_last_selection
 

Detailed Description

Customized combo box for footprint selection.

This provides the following features:

  • library name is greyed out for readability when lib:footprint format is found in the item text
  • empty items are displayed as nonselectable separators

Multiple separators in a row is undefined behavior; it is likely to result in errors such as the ability to select separators. Separators ARE valid at the top and bottom.

For any items containing footprints, the "lib:footprint" name should be attached to the item as a wxStringClientData.

Definition at line 38 of file footprint_choice.h.

Constructor & Destructor Documentation

◆ FOOTPRINT_CHOICE()

FOOTPRINT_CHOICE::FOOTPRINT_CHOICE ( wxWindow *  aParent,
int  aId 
)

Definition at line 28 of file footprint_choice.cpp.

28  :
29  wxOwnerDrawnComboBox( aParent, aId, wxEmptyString, wxDefaultPosition, wxDefaultSize,
30  /* n */ 0, /* choices */ nullptr, wxCB_READONLY ),
31  m_last_selection( 0 )
32 {
33 }

◆ ~FOOTPRINT_CHOICE()

FOOTPRINT_CHOICE::~FOOTPRINT_CHOICE ( )
virtual

Definition at line 36 of file footprint_choice.cpp.

37 {
38 }

Member Function Documentation

◆ DoSetPopupControl()

void FOOTPRINT_CHOICE::DoSetPopupControl ( wxComboPopup *  aPopup)
overrideprotectedvirtual

Definition at line 41 of file footprint_choice.cpp.

42 {
43  using namespace std::placeholders;
44  wxOwnerDrawnComboBox::DoSetPopupControl( aPopup );
45 
46  // Bind events to intercept selections, so the separator can be made nonselectable.
47 
48  GetVListBoxComboPopup()->Bind( wxEVT_MOTION, &FOOTPRINT_CHOICE::TryVetoMouse, this );
49  GetVListBoxComboPopup()->Bind( wxEVT_LEFT_DOWN, &FOOTPRINT_CHOICE::TryVetoMouse, this );
50  GetVListBoxComboPopup()->Bind( wxEVT_LEFT_UP, &FOOTPRINT_CHOICE::TryVetoMouse, this );
51  GetVListBoxComboPopup()->Bind( wxEVT_LEFT_DCLICK, &FOOTPRINT_CHOICE::TryVetoMouse, this );
52  GetVListBoxComboPopup()->Bind( wxEVT_LISTBOX, std::bind( &FOOTPRINT_CHOICE::TryVetoSelect, this, _1, true ) );
53  Bind( wxEVT_COMBOBOX, std::bind( &FOOTPRINT_CHOICE::TryVetoSelect, this, _1, false ) );
54 }
void TryVetoMouse(wxMouseEvent &aEvent)
Veto a mouseover event if in the separator.
void TryVetoSelect(wxCommandEvent &aEvent, bool aInner)
Veto a select event for the separator.

References TryVetoMouse(), and TryVetoSelect().

◆ DrawTextFragment()

wxCoord FOOTPRINT_CHOICE::DrawTextFragment ( wxDC &  aDC,
wxCoord  x,
wxCoord  y,
wxString const &  aText 
)
staticprotected

Draw a fragment of text, then return the next x coordinate to continue drawing.

Definition at line 142 of file footprint_choice.cpp.

143 {
144  aDC.DrawText( aText, x, y );
145  return x + aDC.GetTextExtent( aText ).GetWidth();
146 }

Referenced by OnDrawItem().

◆ GetSelectionEither()

int FOOTPRINT_CHOICE::GetSelectionEither ( bool  aInner) const
protected

Get selection from either the outer (combo box) or inner (popup) list.

Definition at line 188 of file footprint_choice.cpp.

189 {
190  if( aInner )
191  return GetVListBoxComboPopup()->wxVListBox::GetSelection();
192  else
193  return GetSelection();
194 }

Referenced by TryVetoSelect().

◆ OnDrawItem()

void FOOTPRINT_CHOICE::OnDrawItem ( wxDC &  aDC,
wxRect const &  aRect,
int  aItem,
int  aFlags 
) const
overrideprotectedvirtual

Definition at line 57 of file footprint_choice.cpp.

58 {
59  wxString text = SafeGetString( aItem );
60 
61  if( text == wxEmptyString )
62  {
63  wxPen pen( m_grey, 1, wxPENSTYLE_SOLID );
64 
65  aDC.SetPen( pen );
66  aDC.DrawLine( aRect.x, aRect.y + aRect.height / 2, aRect.x + aRect.width,
67  aRect.y + aRect.height / 2 );
68  }
69  else
70  {
71  wxCoord x, y;
72 
73  if( aFlags & wxODCB_PAINTING_CONTROL )
74  {
75  x = aRect.x + GetMargins().x;
76  y = ( aRect.height - aDC.GetCharHeight() ) / 2 + aRect.y;
77  }
78  else
79  {
80  x = aRect.x + 2;
81  y = aRect.y;
82  }
83 
84  // If this item has a footprint and that footprint has a ":" delimiter, find the
85  // library component, then find that in the display string and grey it out.
86 
87  size_t start_grey = 0;
88  size_t end_grey = 0;
89 
90  wxString lib = static_cast<wxStringClientData*>( GetClientObject( aItem ) )->GetData();
91  size_t colon_index = lib.rfind( ':' );
92 
93  if( colon_index != wxString::npos )
94  {
95  wxString library_part = lib.SubString( 0, colon_index );
96  size_t library_index = text.rfind( library_part );
97 
98  if( library_index != wxString::npos )
99  {
100  start_grey = library_index;
101  end_grey = start_grey + library_part.Length();
102  }
103  }
104 
105  if( start_grey != end_grey && !( aFlags & wxODCB_PAINTING_SELECTED ) )
106  {
107  x = DrawTextFragment( aDC, x, y, text.SubString( 0, start_grey - 1 ) );
108 
109  wxColour standard_color = aDC.GetTextForeground();
110 
111  aDC.SetTextForeground( m_grey );
112  x = DrawTextFragment( aDC, x, y, text.SubString( start_grey, end_grey - 1 ) );
113 
114  aDC.SetTextForeground( standard_color );
115  x = DrawTextFragment( aDC, x, y, text.SubString( end_grey, text.Length() - 1 ) );
116  }
117  else
118  {
119  aDC.DrawText( text, x, y );
120  }
121  }
122 }
wxString SafeGetString(int aItem) const
Safely get a string for an item, returning wxEmptyString if the item doesn't exist.
static wxCoord DrawTextFragment(wxDC &aDC, wxCoord x, wxCoord y, wxString const &aText)
Draw a fragment of text, then return the next x coordinate to continue drawing.
static wxColour m_grey

References DrawTextFragment(), m_grey, SafeGetString(), and text.

◆ OnMeasureItem()

wxCoord FOOTPRINT_CHOICE::OnMeasureItem ( size_t  aItem) const
overrideprotectedvirtual

Definition at line 124 of file footprint_choice.cpp.

125 {
126  if( SafeGetString( aItem ) == "" )
127  return 11;
128  else
129  return wxOwnerDrawnComboBox::OnMeasureItem( aItem );
130 }
wxString SafeGetString(int aItem) const
Safely get a string for an item, returning wxEmptyString if the item doesn't exist.

References SafeGetString().

◆ OnMeasureItemWidth()

wxCoord FOOTPRINT_CHOICE::OnMeasureItemWidth ( size_t  aItem) const
overrideprotectedvirtual

Definition at line 133 of file footprint_choice.cpp.

134 {
135  if( SafeGetString( aItem ) == "" )
136  return GetTextRect().GetWidth() - 2;
137  else
138  return wxOwnerDrawnComboBox::OnMeasureItemWidth( aItem );
139 }
wxString SafeGetString(int aItem) const
Safely get a string for an item, returning wxEmptyString if the item doesn't exist.

References SafeGetString().

◆ SafeGetString()

wxString FOOTPRINT_CHOICE::SafeGetString ( int  aItem) const
protected

Safely get a string for an item, returning wxEmptyString if the item doesn't exist.

Definition at line 179 of file footprint_choice.cpp.

180 {
181  if( aItem >= 0 && aItem < (int) GetCount() )
182  return GetVListBoxComboPopup()->GetString( aItem );
183  else
184  return wxEmptyString;
185 }

Referenced by OnDrawItem(), OnMeasureItem(), OnMeasureItemWidth(), TryVetoMouse(), and TryVetoSelect().

◆ SetSelectionEither()

void FOOTPRINT_CHOICE::SetSelectionEither ( bool  aInner,
int  aSel 
)
protected

Safely set selection for either the outer (combo box) or inner (popup) list, doing nothing for invalid selections.

Definition at line 197 of file footprint_choice.cpp.

198 {
199  if( aSel >= 0 && aSel < (int) GetCount() )
200  {
201  if( aInner )
202  return GetVListBoxComboPopup()->wxVListBox::SetSelection( aSel );
203  else
204  return SetSelection( aSel );
205  }
206 }

Referenced by TryVetoSelect().

◆ TryVetoMouse()

void FOOTPRINT_CHOICE::TryVetoMouse ( wxMouseEvent &  aEvent)
protected

Veto a mouseover event if in the separator.

Definition at line 149 of file footprint_choice.cpp.

150 {
151  int item = GetVListBoxComboPopup()->VirtualHitTest( aEvent.GetPosition().y );
152 
153  if( SafeGetString( item ) != "" )
154  aEvent.Skip();
155 }
wxString SafeGetString(int aItem) const
Safely get a string for an item, returning wxEmptyString if the item doesn't exist.

References SafeGetString().

Referenced by DoSetPopupControl().

◆ TryVetoSelect()

void FOOTPRINT_CHOICE::TryVetoSelect ( wxCommandEvent &  aEvent,
bool  aInner 
)
protected

Veto a select event for the separator.

Parameters
aEvent- the wxCommandEvent caller
aInner- true if event was called for the inner list (ie the popup)

Definition at line 158 of file footprint_choice.cpp.

159 {
160  int sel = GetSelectionEither( aInner );
161 
162  if( sel >= 0 && sel < (int) GetCount() )
163  {
164  wxString text = SafeGetString( sel );
165 
166  if( text == "" )
167  {
169  }
170  else
171  {
172  m_last_selection = sel;
173  aEvent.Skip();
174  }
175  }
176 }
wxString SafeGetString(int aItem) const
Safely get a string for an item, returning wxEmptyString if the item doesn't exist.
int GetSelectionEither(bool aInner) const
Get selection from either the outer (combo box) or inner (popup) list.
void SetSelectionEither(bool aInner, int aSel)
Safely set selection for either the outer (combo box) or inner (popup) list, doing nothing for invali...

References GetSelectionEither(), m_last_selection, SafeGetString(), SetSelectionEither(), and text.

Referenced by DoSetPopupControl().

Member Data Documentation

◆ m_grey

wxColour FOOTPRINT_CHOICE::m_grey
staticprotected

Definition at line 83 of file footprint_choice.h.

Referenced by OnDrawItem().

◆ m_last_selection

int FOOTPRINT_CHOICE::m_last_selection
private

Definition at line 86 of file footprint_choice.h.

Referenced by TryVetoSelect().


The documentation for this class was generated from the following files: