You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.8 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The 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. #ifndef LISTBOXES_H
  24. #define LISTBOXES_H
  25. #include <wx/listctrl.h>
  26. #include <footprint_filter.h>
  27. #include <memory>
  28. /* Forward declarations of all top-level window classes. */
  29. class CVPCB_MAINFRAME;
  30. class COMPONENT;
  31. class FOOTPRINT_LIST;
  32. #define LISTBOX_STYLE ( wxBORDER_NONE | wxLC_NO_HEADER | wxLC_REPORT | wxLC_VIRTUAL | \
  33. wxVSCROLL | wxHSCROLL )
  34. /**
  35. * Base class to display symbol and footprint lists.
  36. */
  37. class ITEMS_LISTBOX_BASE : public wxListView
  38. {
  39. public:
  40. ITEMS_LISTBOX_BASE( CVPCB_MAINFRAME* aParent, wxWindowID aId,
  41. const wxPoint& aLocation = wxDefaultPosition,
  42. const wxSize& aSize = wxDefaultSize, long aStyle = 0 );
  43. virtual ~ITEMS_LISTBOX_BASE() = default;
  44. /**
  45. * @return the index of the selected item in lists allowing only one item selected
  46. * and the index of the first selected item in lists allowing many selection
  47. */
  48. int GetSelection();
  49. /**
  50. * Remove all selection in lists which can have more than one item selected.
  51. */
  52. void DeselectAll();
  53. virtual CVPCB_MAINFRAME* GetParent() const;
  54. /**
  55. * Update the width of the column based on its contents.
  56. *
  57. * @param aLine is the line to calculate the width from. If positive, the
  58. * width will only be increased if needed. If negative, we start from
  59. * scratch and all lines are considered, i.e., the column may be shrunk.
  60. */
  61. void UpdateWidth( int aLine = -1 );
  62. void Shutdown() { m_isClosing = true; }
  63. private:
  64. /**
  65. * Calculate the width of the given line, and increase the column width
  66. * if needed. This is effectively the wxListCtrl code for autosizing.
  67. * NB. it relies on the caller checking the given line number is valid.
  68. */
  69. void UpdateLineWidth( unsigned aLine, wxClientDC& dc );
  70. protected:
  71. bool m_isClosing;
  72. private:
  73. int m_columnWidth;
  74. };
  75. class FOOTPRINTS_LISTBOX : public ITEMS_LISTBOX_BASE
  76. {
  77. public:
  78. /**
  79. * Filter setting constants. The filter type is a bitwise OR of these flags,
  80. * and only footprints matching all selected filter types are shown.
  81. */
  82. enum FP_FILTER_T: int
  83. {
  84. UNFILTERED_FP_LIST = 0,
  85. FILTERING_BY_COMPONENT_FP_FILTERS = 0x0001,
  86. FILTERING_BY_PIN_COUNT = 0x0002,
  87. FILTERING_BY_LIBRARY = 0x0004
  88. };
  89. FOOTPRINTS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id );
  90. virtual ~FOOTPRINTS_LISTBOX() = default;
  91. int GetCount();
  92. void SetSelection( int aIndex, bool aState = true );
  93. void SetSelectedFootprint( const LIB_ID& aFPID );
  94. void SetString( unsigned linecount, const wxString& text );
  95. /**
  96. * Populate the wxListCtrl with the footprints from \a aList that meet the filter
  97. * criteria defined by \a aFilterType.
  98. *
  99. * @param aList is a #FOOTPRINT_LIST item containing the footprints.
  100. * @param aLibName is wxString containing the name of the selected library. Can be
  101. * wxEmptyString.
  102. * @param aComponent is the #COMPONENT used by the filtering criteria. Can be NULL.
  103. * @param aFootPrintFilterPattern is the filter used to filter list by names.
  104. * @param aFilterType defines the criteria to filter \a aList.
  105. */
  106. void SetFootprints( FOOTPRINT_LIST& aList, const wxString& aLibName, COMPONENT* aComponent,
  107. const wxString& aFootPrintFilterPattern, int aFilterType );
  108. wxString GetSelectedFootprint();
  109. /**
  110. * This overloaded function MUST be provided for the wxLC_VIRTUAL mode
  111. * because real data is not handled by ITEMS_LISTBOX_BASE.
  112. */
  113. wxString OnGetItemText( long item, long column ) const override;
  114. // Events functions:
  115. void OnLeftClick( wxListEvent& event );
  116. void OnLeftDClick( wxListEvent& event );
  117. void OnChar( wxKeyEvent& event );
  118. DECLARE_EVENT_TABLE();
  119. private:
  120. wxArrayString m_footprintList;
  121. };
  122. class LIBRARY_LISTBOX : public ITEMS_LISTBOX_BASE
  123. {
  124. public:
  125. LIBRARY_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id );
  126. virtual ~LIBRARY_LISTBOX() = default;
  127. int GetCount();
  128. void SetSelection( int index, bool State = true );
  129. void SetString( unsigned linecount, const wxString& text );
  130. void AppendLine( const wxString& text );
  131. void Finish();
  132. void ClearList();
  133. wxString GetSelectedLibrary();
  134. wxString OnGetItemText( long item, long column ) const override;
  135. void OnSelectLibrary( wxListEvent& event );
  136. /**
  137. * Called on a key press.
  138. *
  139. * Call default handler for some special keys, and for "ASCII" keys, select the first
  140. * footprint that the name starts by the letter.
  141. *
  142. * This is the default behavior of a listbox, but because we use virtual lists, the
  143. * listbox does not know anything to what is displayed, we must handle this behavior
  144. * here. Furthermore the footprint name is not at the beginning of displayed lines
  145. * (the first word is the line number).
  146. */
  147. void OnChar( wxKeyEvent& event );
  148. DECLARE_EVENT_TABLE();
  149. private:
  150. wxArrayString m_libraryList;
  151. };
  152. class SYMBOLS_LISTBOX : public ITEMS_LISTBOX_BASE
  153. {
  154. public:
  155. SYMBOLS_LISTBOX( CVPCB_MAINFRAME* parent, wxWindowID id );
  156. virtual ~SYMBOLS_LISTBOX() = default;
  157. void Clear();
  158. int GetCount();
  159. /**
  160. * This overloaded function MUST be provided for the wxLC_VIRTUAL mode
  161. * because real data is not handled by #ITEMS_LISTBOX_BASE.
  162. */
  163. wxString OnGetItemText( long item, long column ) const override;
  164. wxListItemAttr* OnGetItemAttr( long item) const override;
  165. /*
  166. * Enable or disable an item
  167. */
  168. void SetSelection( int index, bool State = true );
  169. void SetString( unsigned linecount, const wxString& text );
  170. void AppendLine( const wxString& text );
  171. void AppendWarning( int index );
  172. void RemoveWarning( int index );
  173. // Events functions:
  174. /**
  175. * Called on a key press.
  176. *
  177. * Call default handler for some special keys, and for "ASCII" keys, select the first
  178. * component that the name starts by the letter.
  179. *
  180. * This is the default behavior of a listbox, but because we use virtual lists, the
  181. * listbox does not know anything to what is displayed, we must handle this behavior
  182. * here. Furthermore the reference of components is not at the beginning of displayed
  183. * lines (the first word is the line number).
  184. */
  185. void OnChar( wxKeyEvent& event );
  186. void OnSelectComponent( wxListEvent& event );
  187. DECLARE_EVENT_TABLE();
  188. public:
  189. wxArrayString m_SymbolList;
  190. private:
  191. std::vector<long> m_symbolWarning;
  192. std::unique_ptr<wxListItemAttr> m_warningAttr;
  193. };
  194. #endif //#ifndef LISTBOXES_H