KiCad PCB EDA Suite
dialog_file_dir_picker.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 CERN
3  * Copyright (C) 2018 KiCad Developers, see AUTHORS.txt for contributors.
4  *
5  * Author: Maciej Suminski <maciej.suminski@cern.ch>
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 // inspired by David Hart's FileDirDlg widget (4Pane project)
22 
23 #include "dialog_file_dir_picker.h"
24 #include <wx/filename.h>
25 
26 
27 DIALOG_FILE_DIR_PICKER::DIALOG_FILE_DIR_PICKER( wxWindow* parent, const wxString& title,
28  const wxString& defaultPath, const wxString& wildcard, int style )
29  : DIALOG_SHIM( parent, wxID_ANY, title, wxDefaultPosition,
30  wxSize( 500, 600 ), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
31 {
32  m_showHidden = nullptr;
33 
34  wxString path = defaultPath.IsEmpty() ? wxGetCwd() : defaultPath;
36 
37  int m_GDC_style = wxDIRCTRL_3D_INTERNAL | wxDIRCTRL_EDIT_LABELS;
38 
39  if( !wildcard.IsEmpty() )
40  m_GDC_style |= wxDIRCTRL_SHOW_FILTERS;
41 
42  if( style & FD_MULTIPLE )
43  m_GDC_style |= wxDIRCTRL_MULTIPLE;
44 
45 
46  SetSizeHints( wxDefaultSize, wxDefaultSize );
47 
48  wxBoxSizer* mainSizer;
49  mainSizer = new wxBoxSizer( wxVERTICAL );
50 
51  m_GDC = new wxGenericDirCtrl( this, wxID_ANY, wxEmptyString,
52  wxDefaultPosition, wxDefaultSize, m_GDC_style );
53 
54  m_GDC->ShowHidden( style & FD_SHOW_HIDDEN );
55 
56  // TODO filter is not applied until it is reselected in the drop-down list, why?
57  if( !wildcard.IsEmpty() )
58  m_GDC->SetFilter( wildcard );
59 
60  mainSizer->Add( m_GDC, 1, wxEXPAND | wxALL, 5 );
61 
62  // TODO commented out due to string freeze, uncomment in v6
63  //m_showHidden = new wxCheckBox( this, wxID_ANY, _( "Show Hidden" ), wxDefaultPosition, wxDefaultSize, 0 );
64  //m_showHidden->SetValue( style & FD_SHOW_HIDDEN );
65  //mainSizer->Add( m_showHidden, 0, wxALL, 5 );
66 
67  auto sdbSizer = new wxStdDialogButtonSizer();
68  sdbSizer->AddButton( new wxButton( this, wxID_OK ) );
69  sdbSizer->AddButton( new wxButton( this, wxID_CANCEL ) );
70  sdbSizer->Realize();
71  mainSizer->Add( sdbSizer, 0, wxEXPAND, 5 );
72 
73  SetSizer( mainSizer );
74  Layout();
75  Centre( wxBOTH );
76 
77  // Use Connect() here instead of an event table entry, as otherwise an
78  // event is fired before the dialog is created and m_GDC and Text initialised
79  Connect( wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED,
80  (wxObjectEventFunction) &DIALOG_FILE_DIR_PICKER::onHidden );
81 
82  // Call SetDirectory() to make the path visible
83  SetDirectory( path );
84 }
85 
86 
87 void DIALOG_FILE_DIR_PICKER::SetDirectory( const wxString& aDirectory ) const
88 {
89  // Make the requested path visible. Without scrolling to the bottom, the requested path
90  // is just below the window.
91  wxArrayTreeItemIds selections;
92  auto treeCtrl = m_GDC->GetTreeCtrl();
93 
94  treeCtrl->UnselectAll();
95  m_GDC->SetPath( aDirectory );
96  m_GDC->SetDefaultPath( aDirectory );
97  treeCtrl->GetSelections( selections );
98 
99  if( !selections.IsEmpty() && selections[0].IsOk() )
100  {
101  auto lastChild = treeCtrl->GetLastChild( treeCtrl->GetRootItem() );
102 
103  if( lastChild.IsOk() )
104  treeCtrl->ScrollTo( lastChild );
105 
106  treeCtrl->ScrollTo( selections[0] );
107  }
108 }
109 
110 
112 {
113  wxFileName fileName( m_GDC->GetPath() );
114 
115  // Strip the file name, if it is included in the path
116  return fileName.FileExists() ? fileName.GetPath() : fileName.GetFullPath();
117 }
118 
119 
120 size_t DIALOG_FILE_DIR_PICKER::GetFilenames( wxArrayString& aFilePaths )
121 {
122  wxArrayTreeItemIds selectedIds;
123  size_t count = m_GDC->GetTreeCtrl()->GetSelections( selectedIds );
124 
125  for( size_t c = 0; c < count; ++c )
126  {
127  wxDirItemData* data = (wxDirItemData*) m_GDC->GetTreeCtrl()->GetItemData( selectedIds[c] );
128 
129  if( m_filesOnly && wxDirExists( data->m_path ) )
130  continue; // If we only want files, skip dirs
131 
132  aFilePaths.Add( data->m_path );
133  }
134 
135  return aFilePaths.GetCount();
136 }
137 
138 
139 void DIALOG_FILE_DIR_PICKER::onHidden( wxCommandEvent& event )
140 {
141  m_GDC->ShowHidden( event.IsChecked() );
142 }
DIALOG_FILE_DIR_PICKER(wxWindow *parent, const wxString &message, const wxString &defaultPath, const wxString &wildcard, int style=FD_MULTIPLE|FD_SHOW_HIDDEN)
Class DIALOG_SHIM may sit in the inheritance tree between wxDialog and any class written by wxFormBui...
Definition: dialog_shim.h:82
size_t GetFilenames(wxArrayString &aFilepaths)
void SetDirectory(const wxString &aDirectory) const
void onHidden(wxCommandEvent &event)