KiCad PCB EDA Suite
two_column_tree_list.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) 2017 Chris Pavlina <pavlina.chris@gmail.com>
5  * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include <wx/dataview.h>
31 
35 static const int HORIZ_MARGIN = 30;
36 
37 
38 TWO_COLUMN_TREE_LIST::TWO_COLUMN_TREE_LIST( wxWindow* aParent, wxWindowID aID,
39  const wxPoint & aPos, const wxSize & aSize, long aStyle, const wxString & aName )
40  : wxTreeListCtrl( aParent, aID, aPos, aSize, aStyle, aName ),
41  m_rubber_band_column( 0 ),
42  m_clamped_min_width( 50 )
43 {
44  Bind( wxEVT_SIZE, &TWO_COLUMN_TREE_LIST::OnSize, this );
45  GetDataView()->SetIndent( 10 );
46 }
47 
48 
50 {
51  wxSizeEvent dummy;
52  OnSize( dummy );
53 }
54 
55 
56 void TWO_COLUMN_TREE_LIST::OnSize( wxSizeEvent& aEvent )
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 }
121 
122 
123 int TWO_COLUMN_TREE_LIST::MemoWidthFor( const wxString& aStr )
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 }
140 
141 
142 std::map<wxString, int> TWO_COLUMN_TREE_LIST::m_width_cache;
void AutosizeColumns()
Recompute column sizes.
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.
static wxDataViewItem GetNextItem(const wxDataViewCtrl &ctrl, const wxDataViewItem &item)
static std::map< wxString, int > m_width_cache
int MemoWidthFor(const wxString &aStr)
Memoized version of wx WidthFor(), which returns the width in pixels required to display a string...
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.
static const int HORIZ_MARGIN
Extra margin to compensate for vertical scrollbar.