KiCad PCB EDA Suite
TWO_COLUMN_TREE_LIST Class Reference

Modified wxTreeListCtrl designed for use with two columns, with better column resizing. More...

#include <two_column_tree_list.h>

Inheritance diagram for TWO_COLUMN_TREE_LIST:
WIDGET_HOTKEY_LIST

Public Member Functions

 TWO_COLUMN_TREE_LIST (wxWindow *aParent, wxWindowID aID, const wxPoint &aPos=wxDefaultPosition, const wxSize &aSize=wxDefaultSize, long aStyle=wxTL_DEFAULT_STYLE, const wxString &aName=wxTreeListCtrlNameStr)
 Create a TWO_COLUMN_TREE_LIST. More...
 
void SetRubberBandColumn (int aRubberBandColumn)
 Set the column number that will "rubber-band" (expand with available space). More...
 
void SetClampedMinWidth (int aClampedMinWidth)
 Set the minimum width of the non-rubber-band column. More...
 
void AutosizeColumns ()
 Recompute column sizes. More...
 
void OnSize (wxSizeEvent &aEvent)
 Override buggy wxTreeListCtrl size handler. More...
 

Protected Member Functions

int MemoWidthFor (const wxString &aStr)
 Memoized version of wx WidthFor(), which returns the width in pixels required to display a string. More...
 

Protected Attributes

int m_rubber_band_column
 
int m_clamped_min_width
 

Static Protected Attributes

static std::map< wxString, int > m_width_cache
 

Detailed Description

Modified wxTreeListCtrl designed for use with two columns, with better column resizing.

wxTreeListCtrl accumulates error as columns resize, leading to columns with a totally wrong size.

Definition at line 40 of file two_column_tree_list.h.

Constructor & Destructor Documentation

TWO_COLUMN_TREE_LIST::TWO_COLUMN_TREE_LIST ( wxWindow *  aParent,
wxWindowID  aID,
const wxPoint aPos = wxDefaultPosition,
const wxSize &  aSize = wxDefaultSize,
long  aStyle = wxTL_DEFAULT_STYLE,
const wxString &  aName = wxTreeListCtrlNameStr 
)

Create a TWO_COLUMN_TREE_LIST.

Definition at line 38 of file two_column_tree_list.cpp.

References OnSize().

40  : wxTreeListCtrl( aParent, aID, aPos, aSize, aStyle, aName ),
43 {
44  Bind( wxEVT_SIZE, &TWO_COLUMN_TREE_LIST::OnSize, this );
45  GetDataView()->SetIndent( 10 );
46 }
void OnSize(wxSizeEvent &aEvent)
Override buggy wxTreeListCtrl size handler.

Member Function Documentation

void TWO_COLUMN_TREE_LIST::AutosizeColumns ( )

Recompute column sizes.

This should be called after adding columns. There is no need to call this in an OnSize handler - this CALLS the OnSize handler.

Definition at line 49 of file two_column_tree_list.cpp.

References dummy(), and OnSize().

50 {
51  wxSizeEvent dummy;
52  OnSize( dummy );
53 }
static LIB_PART * dummy()
Used when a LIB_PART is not found in library to draw a dummy shape This component is a 400 mils squar...
void OnSize(wxSizeEvent &aEvent)
Override buggy wxTreeListCtrl size handler.
int TWO_COLUMN_TREE_LIST::MemoWidthFor ( const wxString &  aStr)
protected

Memoized version of wx WidthFor(), which returns the width in pixels required to display a string.

This function is REALLY SLOW on macOS.

Definition at line 123 of file two_column_tree_list.cpp.

References m_width_cache.

Referenced by OnSize().

124 {
125  int width;
126  auto found = m_width_cache.find( aStr );
127 
128  if( found == m_width_cache.end() )
129  {
130  width = WidthFor( aStr );
131  m_width_cache[aStr] = width;
132  }
133  else
134  {
135  width = found->second;
136  }
137 
138  return width;
139 }
static std::map< wxString, int > m_width_cache
void TWO_COLUMN_TREE_LIST::OnSize ( wxSizeEvent &  aEvent)

Override buggy wxTreeListCtrl size handler.

Definition at line 56 of file two_column_tree_list.cpp.

References GetNextItem(), HORIZ_MARGIN, m_clamped_min_width, m_rubber_band_column, and MemoWidthFor().

Referenced by AutosizeColumns(), WIDGET_HOTKEY_LIST::EditItem(), and TWO_COLUMN_TREE_LIST().

57 {
58  wxDataViewCtrl* view = GetDataView();
59 
60  if( !view )
61  return;
62 
63  wxRect rect = GetClientRect();
64  view->SetSize( rect );
65 
66 #ifdef wxHAS_GENERIC_DATAVIEWCTRL
67  {
68  wxWindow* win_view = GetView();
69  win_view->Refresh();
70  win_view->Update();
71  }
72 #endif
73 
74  // Find the maximum width of both columns
75  int clamped_column = ( m_rubber_band_column == 0 ) ? 1 : 0;
76  int clamped_column_width = 0;
77  int rubber_max_width = 0;
78 
79  for( wxTreeListItem item = GetFirstItem(); item.IsOk(); item = GetNextItem( item ) )
80  {
81  const wxString& text = GetItemText( item, clamped_column );
82  int width = WidthFor( text );
83 
84  if( clamped_column == 0 )
85  {
86  width += 4 * view->GetIndent();
87  }
88 
89  if( width > clamped_column_width )
90  clamped_column_width = width;
91 
92  width = MemoWidthFor( GetItemText( item, m_rubber_band_column ) );
93  if( width > rubber_max_width )
94  rubber_max_width = width;
95  }
96 
97  if( clamped_column_width < m_clamped_min_width )
98  clamped_column_width = m_clamped_min_width;
99 
100  // Rubber column width is only limited if the rubber column is on the LEFT.
101  // If on the right, let the horiz scrollbar show.
102 
103  int rubber_width = 0;
104 
105  if( m_rubber_band_column == 0 )
106  rubber_width = rect.width - clamped_column_width - HORIZ_MARGIN;
107  else
108  rubber_width = rubber_max_width;
109 
110  if( rubber_width <= 0 )
111  rubber_width = 1;
112 
113  wxASSERT( m_rubber_band_column == 0 || m_rubber_band_column == 1 );
114 
115  if( GetColumnCount() >= 2 )
116  {
117  SetColumnWidth( m_rubber_band_column, rubber_width );
118  SetColumnWidth( clamped_column, clamped_column_width );
119  }
120 }
static wxDataViewItem GetNextItem(const wxDataViewCtrl &ctrl, const wxDataViewItem &item)
int MemoWidthFor(const wxString &aStr)
Memoized version of wx WidthFor(), which returns the width in pixels required to display a string...
static const int HORIZ_MARGIN
Extra margin to compensate for vertical scrollbar.
void TWO_COLUMN_TREE_LIST::SetClampedMinWidth ( int  aClampedMinWidth)
inline

Set the minimum width of the non-rubber-band column.

Definition at line 65 of file two_column_tree_list.h.

References m_clamped_min_width.

Referenced by WIDGET_HOTKEY_LIST::WIDGET_HOTKEY_LIST().

66  {
67  m_clamped_min_width = aClampedMinWidth;
68  }
void TWO_COLUMN_TREE_LIST::SetRubberBandColumn ( int  aRubberBandColumn)
inline

Set the column number that will "rubber-band" (expand with available space).

As this is a TWO column tree list, this must be zero or one.

Definition at line 57 of file two_column_tree_list.h.

References m_rubber_band_column.

Referenced by WIDGET_HOTKEY_LIST::WIDGET_HOTKEY_LIST().

58  {
59  m_rubber_band_column = aRubberBandColumn;
60  }

Member Data Documentation

int TWO_COLUMN_TREE_LIST::m_clamped_min_width
protected

Definition at line 91 of file two_column_tree_list.h.

Referenced by OnSize(), and SetClampedMinWidth().

int TWO_COLUMN_TREE_LIST::m_rubber_band_column
protected

Definition at line 90 of file two_column_tree_list.h.

Referenced by OnSize(), and SetRubberBandColumn().

std::map< wxString, int > TWO_COLUMN_TREE_LIST::m_width_cache
staticprotected

Definition at line 93 of file two_column_tree_list.h.

Referenced by MemoWidthFor().


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