KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_executecommand_job_settings.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) 2024 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
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
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
24#include <scintilla_tricks.h>
25#include <grid_tricks.h>
26#include <project.h>
27#include <env_vars.h>
28#include <common.h>
29#include <widgets/ui_common.h>
30#include <wx/regex.h>
31
32
34 PROJECT* aProject ) :
36 m_job( aJob ),
37 m_project( aProject ),
38 m_lastFocusedInput( nullptr ),
39 m_scintillaTricks( nullptr )
40{
41 wxString builtinHelp = _( "Built-in variables:" );
42
43 for( const auto& [name, text] : GetExecuteBuiltinVariableHelp() )
44 builtinHelp += wxS( "\n ${" ) + name + wxS( "} " ) + text;
45
46 m_stBuiltinHelp->SetLabel( builtinHelp );
47
48 m_textCtrlCommand->SetWrapMode( wxSTC_WRAP_CHAR );
49
51
52 m_scintillaTricks = new SCINTILLA_TRICKS( m_textCtrlCommand, wxT( "{}" ), false,
53 // onAcceptFn
54 [this]( wxKeyEvent& aEvent )
55 {
56 wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
57 },
58
59 // onCharFn
60 [this]( wxStyledTextEvent& aEvent )
61 {
62 m_scintillaTricks->DoTextVarAutocomplete(
63 // getTokensFn
64 [this]( const wxString& xRef, wxArrayString* tokens )
65 {
67 tokens->Add( OUTPUT_TMP_PATH_VAR_NAME );
68
69 for( const auto& [name, text] : GetExecuteBuiltinVariableHelp() )
70 tokens->Add( name );
71
72 if( m_project )
73 {
74 for( const auto& [varName, varValue] : m_project->GetTextVars() )
75 {
76 if( tokens->Index( varName ) == wxNOT_FOUND )
77 tokens->Add( varName );
78 }
79 }
80 } );
81 } );
82
83 // add Cut, Copy, and Paste to wxGrids
84 m_path_subs_grid->PushEventHandler( new GRID_TRICKS( m_path_subs_grid ) );
85
87
89
90 m_textCtrlCommand->Bind( wxEVT_SET_FOCUS,
91 [this]( wxFocusEvent& aEvent )
92 {
94 aEvent.Skip();
95 } );
96
97 m_textCtrlOutputPath->Bind( wxEVT_SET_FOCUS,
98 [this]( wxFocusEvent& aEvent )
99 {
101 aEvent.Skip();
102 } );
103
104 m_path_subs_grid->Bind( wxEVT_GRID_CELL_LEFT_DCLICK, &DIALOG_EXECUTECOMMAND_JOB_SETTINGS::onVarGridDClick, this );
105
107
108 // keep the grid at 10 rows, it scrolls for more
109 m_path_subs_grid->SetMinSize( wxSize( -1, m_path_subs_grid->GetDefaultRowSize() * 10 ) );
110
111 m_path_subs_grid->SetColLabelValue( 0, _( "Name" ) );
112 m_path_subs_grid->SetColLabelValue( 1, _( "Value" ) );
113
115
117}
118
119
121{
122 delete m_scintillaTricks;
123 m_scintillaTricks = nullptr;
124
125 // Delete the GRID_TRICKS.
126 m_path_subs_grid->PopEventHandler( true );
127}
128
129
131{
132 m_job->m_command = m_textCtrlCommand->GetValue();
133 m_job->m_ignoreExitcode = m_cbIgnoreExitCode->GetValue();
134 m_job->m_recordOutput = m_cbRecordOutput->GetValue();
135 m_job->SetConfiguredOutputPath( m_textCtrlOutputPath->GetValue() );
136
137 return true;
138}
139
140
142{
143 m_textCtrlCommand->SetValue( m_job->m_command );
144 m_cbIgnoreExitCode->SetValue( m_job->m_ignoreExitcode );
145 m_cbRecordOutput->SetValue( m_job->m_recordOutput );
146
147 m_textCtrlOutputPath->SetValue( m_job->GetConfiguredOutputPath() );
148 m_textCtrlOutputPath->Enable( m_cbRecordOutput->GetValue() );
149
152
153 return true;
154}
155
156
158{
159 m_textCtrlOutputPath->Enable( m_cbRecordOutput->GetValue() );
160}
161
162
164{
167 aEvent.Skip();
168}
169
170
175
176
178{
179 int row = aEvent.GetRow();
180
181 if( row < 0 || row >= m_path_subs_grid->GetNumberRows() )
182 return;
183
184 wxString token = m_path_subs_grid->GetCellValue( row, 0 );
185
187 m_textCtrlOutputPath->WriteText( token );
188 else
189 m_textCtrlCommand->ReplaceSelection( token );
190}
191
192
194{
195 wxRegEx re( ".*?(\\$\\{(.+?)\\})|(\\$\\((.+?)\\)).*?", wxRE_ADVANCED );
196 wxASSERT( re.IsValid() ); // wxRE_ADVANCED is required.
197
198 std::set<wxString> unique;
199 wxString src = m_textCtrlCommand->GetValue();
200
201 // clear the table
202 m_path_subs_grid->ClearRows();
203
204 // all configured path and environment variables
205 wxArrayString envTokens;
207
208 for( const wxString& token : envTokens )
209 unique.insert( token );
210
211 // project text variables
212 if( m_project )
213 {
214 for( const auto& [varName, varValue] : m_project->GetTextVars() )
215 unique.insert( varName );
216 }
217
218 // variables referenced in the command, including undefined ones
219 while( re.Matches( src ) )
220 {
221 wxString envvar = re.GetMatch( src, 2 );
222
223 // if not ${...} form then must be $(...)
224 if( envvar.IsEmpty() )
225 envvar = re.GetMatch( src, 4 );
226
227 // ignore duplicates
228 unique.insert( envvar );
229
230 // delete the last match and search again
231 src.Replace( re.GetMatch( src, 0 ), wxEmptyString );
232 }
233
234 // Make sure these variables shows up even if not used yet.
235 unique.insert( OUTPUT_TMP_PATH_VAR_NAME );
236 unique.insert( PROJECT_VAR_NAME );
237
238 for( const auto& [name, text] : GetExecuteBuiltinVariableHelp() )
239 unique.insert( name );
240
241 for( const wxString& evName : unique )
242 {
243 int row = m_path_subs_grid->GetNumberRows();
244 m_path_subs_grid->AppendRows( 1 );
245
246 m_path_subs_grid->SetCellValue( row, 0, wxT( "${" ) + evName + wxT( "}" ) );
247 m_path_subs_grid->SetCellEditor( row, 0, new GRID_CELL_READONLY_TEXT_EDITOR() );
248
249 wxString evValue;
250
251 if( evName.IsSameAs( OUTPUT_TMP_PATH_VAR_NAME ) )
252 {
253 evValue = _( "<value set at runtime>" );
254 m_path_subs_grid->SetCellFont( row, 1, m_path_subs_grid->GetCellFont( row, 1 ).Italic() );
255 }
256 else
257 {
258 // resolve the same way the job runner will
259 wxString token = wxT( "${" ) + evName + wxT( "}" );
260 evValue = ExpandExecuteCommand( token, m_project );
261
262 if( evValue == token )
263 evValue = wxEmptyString;
264 }
265
266 m_path_subs_grid->SetCellValue( row, 1, evValue );
267 m_path_subs_grid->SetCellEditor( row, 1, new GRID_CELL_READONLY_TEXT_EDITOR() );
268 }
269
270 adjustPathSubsGridColumns( m_path_subs_grid->GetRect().GetWidth() );
271}
272
273
275{
276 // Account for scroll bars
277 aWidth -= ( m_path_subs_grid->GetSize().x - m_path_subs_grid->GetClientSize().x );
278
279 m_path_subs_grid->AutoSizeColumn( 0 );
280 m_path_subs_grid->SetColSize( 0, std::max( 200, m_path_subs_grid->GetColSize( 0 ) ) );
281 m_path_subs_grid->SetColSize( 1, std::max( 300, aWidth - m_path_subs_grid->GetColSize( 0 ) ) );
282}
283
284
286{
287 adjustPathSubsGridColumns( event.GetSize().GetX() );
288
289 event.Skip();
290}
291
292
const char * name
DIALOG_EXECUTECOMMAND_JOB_SETTINGS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Execute Command Job Settings"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER)
DIALOG_EXECUTECOMMAND_JOB_SETTINGS(wxWindow *aParent, JOB_SPECIAL_EXECUTE *aJob, PROJECT *aProject)
void populateEnvironReadOnlyTable()
Fill the variable table with the available variables and any referenced in the command,...
void updateExpandedPreview()
Show the command with variables expanded as the job runner will run it.
void OnRecordOutputClicked(wxCommandEvent &event) override
void SetupStandardButtons(std::map< int, wxString > aLabels={})
void finishDialogSettings()
In all dialogs, we must call the same functions to fix minimal dlg size, the default position and per...
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition grid_tricks.h:61
Container for project specific data.
Definition project.h:63
Add cut/copy/paste, dark theme, autocomplete and brace highlighting to a wxStyleTextCtrl instance.
#define _(s)
Functions related to environment variables, including help functions.
std::map< wxString, wxString > GetExecuteBuiltinVariableHelp()
Describe each variable returned by GetExecuteBuiltinVariables() for the user.
wxString ExpandExecuteCommand(const wxString &aCommand, const PROJECT *aProject)
Expand an Execute Command string, including the built-in variables.
#define OUTPUT_TMP_PATH_VAR_NAME
KICOMMON_API void GetEnvVarAutocompleteTokens(wxArrayString *aVars)
Return autocomplete tokens for environment variables for Scintilla.
Definition env_vars.cpp:69
KICOMMON_API wxFont GetMonospacedUIFont(int aRelativeSize=0)
Definition ui_common.cpp:95
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition project.h:37
Functions to provide common constants and other functions to assist in making a consistent UI.