KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_tree.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) 2004-2012 Jean-Pierre Charras
5 * Copyright The 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 2
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, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <bitmaps.h>
24#include <wx/settings.h>
25#include <wx/dcmemory.h>
26#include <wx/image.h>
27
28#include <cstring>
29
30#include "project_tree_item.h"
31#include "project_tree_pane.h"
32#include "project_tree.h"
33#include "kicad_id.h"
34
35
36IMPLEMENT_ABSTRACT_CLASS( PROJECT_TREE, wxTreeCtrl )
37
38
39// A wxBitmap made from a size alone holds uninitialized memory, which draws as a solid block.
40static wxBitmap blankStatusBitmap( int aSize )
41{
42 wxImage image( aSize, aSize );
43 image.InitAlpha();
44 memset( image.GetAlpha(), wxIMAGE_ALPHA_TRANSPARENT, aSize * aSize );
45
46 return wxBitmap( image );
47}
48
49
50#ifdef __WXMSW__
51#define PLATFORM_STYLE wxTR_LINES_AT_ROOT
52#else
53#define PLATFORM_STYLE wxTR_NO_LINES
54#endif
55
57 wxTreeCtrl( parent, ID_PROJECT_TREE, wxDefaultPosition, wxDefaultSize,
58 PLATFORM_STYLE | wxTR_HAS_BUTTONS | wxTR_MULTIPLE, wxDefaultValidator,
59 wxT( "EDATreeCtrl" ) ),
60 m_statusImageList( nullptr )
61{
62 m_projectTreePane = parent;
63
64 // Make sure the GUI font scales properly on GTK
65 SetFont( KIUI::GetControlFont( this ) );
66
67 LoadIcons();
68}
69
70
72{
73 // We pass ownership of these to wxWidgets in SetImageList() and SetStateImageList()
74 //delete m_statusImageList;
75}
76
77
79{
80#ifdef __WXMAC__
81 const int c_fileDefSize = 18;
82 const int c_gitDefSize = 16;
83#else
84 const int c_fileDefSize = 22;
85 const int c_gitDefSize = 16;
86#endif
87
88 auto getBundle = [&]( BITMAPS aBmp, int aDefSize )
89 {
90#ifdef __WXMSW__
91 // Add padding to bitmaps because MSW is the only platform that doesn't get it automatically
92 const int c_padding = 1;
93 wxVector<wxBitmap> bmps;
94
95 for( double scale : { 1.0, 1.25, 1.5, 1.75, 2.0, 3.0 } )
96 {
97 int size = aDefSize * scale;
98 int paddedSize = size + c_padding * 2 * scale;
99 wxBitmapBundle scaled = KiBitmapBundleDef( aBmp, size );
100 wxBitmap bmp = scaled.GetBitmap( wxDefaultSize );
101 wxBitmap padded( paddedSize, paddedSize, 32 );
102
103 {
104 padded.UseAlpha();
105 wxMemoryDC dc( padded );
106 dc.DrawBitmap( bmp, c_padding * scale, c_padding * scale );
107 }
108
109 bmps.push_back( padded );
110 }
111
112 return wxBitmapBundle::FromBitmaps( bmps );
113#else
114 return KiBitmapBundleDef( aBmp, aDefSize );
115#endif
116 };
117
118 wxVector<wxBitmapBundle> images;
119 images.push_back( getBundle( BITMAPS::project, c_fileDefSize ) ); // TREE_LEGACY_PROJECT
120 images.push_back( getBundle( BITMAPS::project_kicad, c_fileDefSize ) ); // TREE_JSON_PROJECT
121 images.push_back( getBundle( BITMAPS::icon_eeschema_24, c_fileDefSize ) ); // TREE_LEGACY_SCHEMATIC
122 images.push_back( getBundle( BITMAPS::icon_eeschema_24, c_fileDefSize ) ); // TREE_SEXPR_SCHEMATIC
123 images.push_back( getBundle( BITMAPS::icon_pcbnew_24, c_fileDefSize ) ); // TREE_LEGACY_PCB
124 images.push_back( getBundle( BITMAPS::icon_pcbnew_24, c_fileDefSize ) ); // TREE_SEXPR_PCB
125 images.push_back( getBundle( BITMAPS::icon_gerbview_24, c_fileDefSize ) ); // TREE_GERBER
126 images.push_back( getBundle( BITMAPS::file_gerber_job, c_fileDefSize ) ); // TREE_GERBER_JOB_FILE (.gbrjob)
127 images.push_back( getBundle( BITMAPS::file_html, c_fileDefSize ) ); // TREE_HTML
128 images.push_back( getBundle( BITMAPS::file_pdf, c_fileDefSize ) ); // TREE_PDF
129 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // TREE_TXT
130 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // TREE_MD
131 images.push_back( getBundle( BITMAPS::netlist, c_fileDefSize ) ); // TREE_NET
132 images.push_back( getBundle( BITMAPS::file_cir, c_fileDefSize ) ); // TREE_NET_SPICE
133 images.push_back( getBundle( BITMAPS::unknown, c_fileDefSize ) ); // TREE_UNKNOWN
134 images.push_back( getBundle( BITMAPS::directory, c_fileDefSize ) ); // TREE_DIRECTORY
135 images.push_back( getBundle( BITMAPS::icon_cvpcb_24, c_fileDefSize ) ); // TREE_CMP_LINK
136 images.push_back( getBundle( BITMAPS::tools, c_fileDefSize ) ); // TREE_REPORT
137 images.push_back( getBundle( BITMAPS::file_pos, c_fileDefSize ) ); // TREE_POS
138 images.push_back( getBundle( BITMAPS::file_drl, c_fileDefSize ) ); // TREE_DRILL
139 images.push_back( getBundle( BITMAPS::file_drl, c_fileDefSize ) ); // TREE_DRILL_NC (similar TREE_DRILL)
140 images.push_back( getBundle( BITMAPS::file_drl, c_fileDefSize ) ); // TREE_DRILL_XNC (similar TREE_DRILL)
141 images.push_back( getBundle( BITMAPS::file_svg, c_fileDefSize ) ); // TREE_SVG
142 images.push_back( getBundle( BITMAPS::file_csv, c_fileDefSize ) ); // TREE_CSV
143 images.push_back( getBundle( BITMAPS::icon_pagelayout_editor_24, c_fileDefSize ) ); // TREE_PAGE_LAYOUT_DESCR
144 images.push_back( getBundle( BITMAPS::module, c_fileDefSize ) ); // TREE_FOOTPRINT_FILE
145 images.push_back( getBundle( BITMAPS::library, c_fileDefSize ) ); // TREE_SCHEMATIC_LIBFILE
146 images.push_back( getBundle( BITMAPS::library, c_fileDefSize ) ); // TREE_SEXPR_SYMBOL_LIB_FILE
147 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // DESIGN_RULES
148 images.push_back( getBundle( BITMAPS::zip, c_fileDefSize ) ); // ZIP_ARCHIVE
149 images.push_back( getBundle( BITMAPS::editor, c_fileDefSize ) ); // JOBSET_FILE
150
151 SetImages( images );
152
153 // KiCad for macOS currently has backported SetStateImages for this control
154 // that is otherwise available since wxWidgets 3.3 on other platforms.
155#if wxCHECK_VERSION( 3, 3, 0 ) || defined( __WXMAC__ )
156 wxVector<wxBitmapBundle> stateImages;
157 stateImages.push_back( wxBitmapBundle( blankStatusBitmap( c_gitDefSize ) ) ); // GIT_STATUS_UNTRACKED
158 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_good_check, c_gitDefSize ) ); // GIT_STATUS_CURRENT
159 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_modified, c_gitDefSize ) ); // GIT_STATUS_MODIFIED
160 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_add, c_gitDefSize ) ); // GIT_STATUS_ADDED
161 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_delete, c_gitDefSize ) ); // GIT_STATUS_DELETED
162 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_out_of_date, c_gitDefSize ) ); // GIT_STATUS_BEHIND
163 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_changed_ahead, c_gitDefSize ) ); // GIT_STATUS_AHEAD
164 stateImages.push_back( KiBitmapBundleDef( BITMAPS::git_conflict, c_gitDefSize ) ); // GIT_STATUS_CONFLICTED
165 stateImages.push_back( wxBitmapBundle( blankStatusBitmap( c_gitDefSize ) ) ); // GIT_STATUS_IGNORED
166
167 SetStateImages( stateImages );
168#else
169 // Make an image list containing small icons
170 wxBitmap blank_bitmap = blankStatusBitmap( c_gitDefSize );
171
172 delete m_statusImageList;
173 m_statusImageList = new wxImageList( c_gitDefSize, c_gitDefSize, true,
174 static_cast<int>( KIGIT_COMMON::GIT_STATUS::GIT_STATUS_LAST ) );
175
176 m_statusImageList->Add( blank_bitmap ); // GIT_STATUS_UNTRACKED
177 m_statusImageList->Add( KiBitmap( BITMAPS::git_good_check, c_gitDefSize ) ); // GIT_STATUS_CURRENT
178 m_statusImageList->Add( KiBitmap( BITMAPS::git_modified, c_gitDefSize ) ); // GIT_STATUS_MODIFIED
179 m_statusImageList->Add( KiBitmap( BITMAPS::git_add, c_gitDefSize ) ); // GIT_STATUS_ADDED
180 m_statusImageList->Add( KiBitmap( BITMAPS::git_delete, c_gitDefSize ) ); // GIT_STATUS_DELETED
181 m_statusImageList->Add( KiBitmap( BITMAPS::git_out_of_date, c_gitDefSize ) ); // GIT_STATUS_BEHIND
182 m_statusImageList->Add( KiBitmap( BITMAPS::git_changed_ahead, c_gitDefSize ) ); // GIT_STATUS_AHEAD
183 m_statusImageList->Add( KiBitmap( BITMAPS::git_conflict, c_gitDefSize ) ); // GIT_STATUS_CONFLICTED
184 m_statusImageList->Add( blank_bitmap ); // GIT_STATUS_IGNORED
185
186 SetStateImageList( m_statusImageList );
187#endif
188
189}
190
191
192void PROJECT_TREE::GetItemsRecursively( const wxTreeItemId& aParentId, std::vector<wxTreeItemId>& aItems )
193{
194 wxTreeItemIdValue cookie;
195 wxTreeItemId child = GetFirstChild( aParentId, cookie );
196
197 while( child.IsOk() )
198 {
199 aItems.push_back( child );
200 GetItemsRecursively( child, aItems );
201 child = GetNextChild( aParentId, cookie );
202 }
203}
204
205
206int PROJECT_TREE::OnCompareItems( const wxTreeItemId& item1, const wxTreeItemId& item2 )
207{
208 PROJECT_TREE_ITEM* myitem1 = (PROJECT_TREE_ITEM*) GetItemData( item1 );
209 PROJECT_TREE_ITEM* myitem2 = (PROJECT_TREE_ITEM*) GetItemData( item2 );
210
211 if( !myitem1 || !myitem2 )
212 return 0;
213
214 if( myitem1->GetType() == TREE_FILE_TYPE::DIRECTORY
215 && myitem2->GetType() != TREE_FILE_TYPE::DIRECTORY )
216 return -1;
217
218 if( myitem2->GetType() == TREE_FILE_TYPE::DIRECTORY
219 && myitem1->GetType() != TREE_FILE_TYPE::DIRECTORY )
220 return 1;
221
222 if( myitem1->IsRootFile() && !myitem2->IsRootFile() )
223 return -1;
224
225 if( myitem2->IsRootFile() && !myitem1->IsRootFile() )
226 return 1;
227
228 return myitem1->GetFileName().CmpNoCase( myitem2->GetFileName() );
229}
wxBitmapBundle KiBitmapBundleDef(BITMAPS aBitmap, int aDefHeight)
Construct and return a bitmap bundle for the given icon ID, with the default bitmap size being aDefHe...
Definition bitmap.cpp:112
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition bitmap.cpp:100
BITMAPS
A list of all bitmap identifiers.
@ git_out_of_date
@ icon_eeschema_24
@ icon_pagelayout_editor_24
@ file_gerber_job
@ git_changed_ahead
@ icon_gerbview_24
Handle one item (a file or a directory name) for the tree file.
const wxString & GetFileName() const
TREE_FILE_TYPE GetType() const
bool IsRootFile() const
Window to display the tree files.
PROJECT_TREE This is the class to show (as a tree) the files in the project directory.
PROJECT_TREE_PANE * m_projectTreePane
void GetItemsRecursively(const wxTreeItemId &aParentId, std::vector< wxTreeItemId > &aItems)
int OnCompareItems(const wxTreeItemId &item1, const wxTreeItemId &item2) override
wxImageList * m_statusImageList
PROJECT_TREE(PROJECT_TREE_PANE *parent)
@ ID_PROJECT_TREE
Definition kicad_id.h:32
KICOMMON_API wxFont GetControlFont(wxWindow *aWindow)
static wxBitmap blankStatusBitmap(int aSize)
#define PLATFORM_STYLE
const int scale
DATABASE_CONNECTION dc(odbcBackend(cs))