KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common_control.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) 2014-2016 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 * @author Maciej Suminski <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21// kicad_curl_easy.h must be included before wxWidgets because on Windows (msys2), there are
22// collision with wx headers and curl.h defs
24
25#include <bitmaps.h>
26#include <build_version.h>
27#include <common.h> // for SearchHelpFileFullPath
28#include <pgm_base.h>
29#include <tool/actions.h>
30#include <tool/tool_manager.h>
31#include <eda_draw_frame.h>
32#include <view/view.h>
34#include <base_screen.h>
35#include <tool/common_control.h>
36
38#include <id.h>
39#include <kiface_base.h>
41#include <eda_doc.h>
42#include <launch_ext.h>
43#include <paths.h>
44#include <wx/filename.h>
45#include <wx/msgdlg.h>
46#include <executable_names.h>
47#include <gestfich.h>
49#include <confirm.h>
50#include <reporter.h>
51#include <widgets/kistatusbar.h>
52
53#define URL_GET_INVOLVED wxS( "https://go.kicad.org/contribute/" )
54#define URL_DONATE wxS( "https://go.kicad.org/app-donate" )
55#define URL_DOCUMENTATION wxS( "https://go.kicad.org/docs/" )
56
57
60 wxS( "https://gitlab.com/kicad/code/kicad/-/issues/new?issuable_template=bare&issue"
61 "[description]=%s" );
62
63
66 "```\n"
67 "%s\n"
68 "```" );
69
70
75
76
78{
79 m_frame->ShowPreferences( wxEmptyString, wxEmptyString );
80 return 0;
81}
82
83
85{
86 // If _pcbnew.kiface is running have it put up the dialog so the 3D paths can also
87 // be edited
88 if( KIFACE* pcbnew = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB, false ) )
89 {
90 try
91 {
92 pcbnew->CreateKiWindow( m_frame, DIALOG_CONFIGUREPATHS, &m_frame->Kiway() );
93 }
94 catch( ... )
95 {
96 // Do nothing here.
97 // A error message is displayed after trying to load _pcbnew.kiface.
98 }
99 }
100 else
101 {
103
104 if( dlg.ShowModal() == wxID_OK )
105 m_frame->Kiway().CommonSettingsChanged( ENVVARS_CHANGED );
106 }
107
108 return 0;
109}
110
111
113{
115 {
116 try // Sch frame was not available, try to start it
117 {
118 if( KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH ) )
119 kiface->CreateKiWindow( m_frame, DIALOG_SCH_LIBRARY_TABLE, &m_frame->Kiway() );
120 }
121 catch( ... )
122 {
123 // _eeschema.kiface is not available: it contains the library table dialog.
124 // Do nothing here.
125 // A error message is displayed after trying to load _eeschema.kiface.
126 }
127 }
128 else if( aEvent.IsAction( &ACTIONS::showFootprintLibTable ) )
129 {
130 try // Pcb frame was not available, try to start it
131 {
132 if( KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB ) )
133 kiface->CreateKiWindow( m_frame, DIALOG_PCB_LIBRARY_TABLE, &m_frame->Kiway() );
134 }
135 catch( ... )
136 {
137 // _pcbnew.kiface is not available: it contains the library table dialog.
138 // Do nothing here.
139 // A error message is displayed after trying to load _pcbnew.kiface.
140 }
141 }
142 else if( aEvent.IsAction( &ACTIONS::showDesignBlockLibTable ) )
143 {
144 try // Kicad frame was not available, try to start it
145 {
146 if( KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH ) )
147 kiface->CreateKiWindow( m_frame, DIALOG_DESIGN_BLOCK_LIBRARY_TABLE, &m_frame->Kiway() );
148 }
149 catch( ... )
150 {
151 // _eeschema.kiface is not available: it contains the library table dialog.
152 // Do nothing here.
153 // A error message is displayed after trying to load _eeschema.kiface.
154 }
155 }
156
157 return 0;
158}
159
160
162{
163 // Needed on Windows, other platforms do not use it, but it creates no issue
164 if( aFrame->IsIconized() )
165 aFrame->Iconize( false );
166
167 aFrame->Raise();
168
169 // Raising the window does not set the focus on Linux. This should work on
170 // any platform.
171 if( wxWindow::FindFocus() != aFrame )
172 aFrame->SetFocus();
173
174 // If the player is currently blocked, focus the user attention on the correct window
175 if( wxWindow* blocking_win = aFrame->Kiway().GetBlockingDialog() )
176 {
177 blocking_win->Raise();
178 blocking_win->SetFocus();
179 }
180}
181
182
184{
185 FRAME_T playerType = aEvent.Parameter<FRAME_T>();
186 KIWAY_PLAYER* editor = m_frame->Kiway().Player( playerType, true );
187
188 // editor can be null if Player() fails:
189 wxCHECK_MSG( editor != nullptr, 0, wxT( "Cannot open/create the editor frame" ) );
190
191 showFrame( editor );
192
193 return 0;
194}
195
196
198{
199 m_frame->CallAfter(
200 [this]()
201 {
202 m_frame->Kiway().OnKiCadExit();
203 } );
204
205 return 0;
206}
207
208
209class TERMINATE_HANDLER : public wxProcess
210{
211public:
212 TERMINATE_HANDLER( const wxString& appName )
213 { }
214
215 void OnTerminate( int pid, int status ) override
216 {
217 delete this;
218 }
219};
220
221
222int COMMON_CONTROL::Execute( const wxString& aExecutible, const wxString& aParam )
223{
224 TERMINATE_HANDLER* callback = new TERMINATE_HANDLER( aExecutible );
225
226 bool isKicadBinary = false;
227
228 if( aExecutible == GERBVIEW_EXE || aExecutible == PCB_CALCULATOR_EXE || aExecutible == PL_EDITOR_EXE
229 || aExecutible == EESCHEMA_EXE || aExecutible == PCBNEW_EXE )
230 {
231 isKicadBinary = true;
232 }
233
234 long pid = ExecuteFile( aExecutible, aParam, callback, isKicadBinary );
235
236 if( pid > 0 )
237 {
238#ifdef __WXMAC__
239 wxString script;
240
241 script.Printf( wxS( "tell application \"System Events\" to tell application process \"%s\"\n"
242 " set frontmost to true\n"
243 "end tell" ),
244 aExecutible );
245
246 // This non-parameterized use of wxExecute is fine because script is not derived
247 // from user input.
248 wxExecute( wxString::Format( "osascript -e '%s'", script ) );
249#endif
250 }
251 else
252 {
253 delete callback;
254 }
255
256 return 0;
257}
258
259
261{
262 wxString execFile;
263 wxString param;
264
266 execFile = PCB_CALCULATOR_EXE;
267 else
268 wxFAIL_MSG( "Execute(): unexpected request" );
269
270 if( execFile.IsEmpty() )
271 return 0;
272
273 return Execute( execFile, param );
274}
275
276
278{
279 // Note: dynamic_cast doesn't work over the Kiway() on MacOS. We have to use static_cast
280 // here.
281 EDA_BASE_FRAME* top = static_cast<EDA_BASE_FRAME*>( m_frame->Kiway().GetTop() );
282
283 if( top && top->GetFrameType() == KICAD_MAIN_FRAME_T )
284 showFrame( top );
285 else
286 KICAD_MESSAGE_DIALOG( m_frame, _( "Can not switch to project manager in stand-alone mode." ) );
287
288 return 0;
289}
290
291
293{
294 wxString helpFile;
295 wxString msg;
296
297 // the URL of help files is "https://go.kicad.org/docs/<version>/<language>/<name>/"
298 const wxString baseUrl = URL_DOCUMENTATION + GetMajorMinorVersion() + wxT( "/" )
299 + Pgm().GetLocale()->GetName().BeforeLast( '_' ) + wxT( "/" );
300
301 /* We have to get document for beginners,
302 * or the full specific doc
303 * if event id is wxID_INDEX, we want the document for beginners.
304 * else the specific doc file (its name is in Kiface().GetHelpFileName())
305 * The document for beginners is the same for all KiCad utilities
306 */
307 if( aEvent.IsAction( &ACTIONS::gettingStarted ) )
308 {
309 // List of possible names for Getting Started in KiCad
310 const wxChar* names[2] = {
311 wxT( "getting_started_in_kicad" ),
312 wxT( "Getting_Started_in_KiCad" )
313 };
314
315 // Search for "getting_started_in_kicad.html" or "getting_started_in_kicad.pdf"
316 // or "Getting_Started_in_KiCad.html" or "Getting_Started_in_KiCad.pdf"
317 for( auto& name : names )
318 {
319 helpFile = SearchHelpFileFullPath( name );
320
321 if( !helpFile.IsEmpty() )
322 break;
323 }
324
325 if( !helpFile )
326 {
327 msg = wxString::Format( _( "Help file '%s' or\n'%s' could not be found.\n"
328 "Do you want to access the KiCad online help?" ),
329 names[0], names[1] );
330 KICAD_MESSAGE_DIALOG dlg( nullptr, msg, _( "File Not Found" ),
331 wxYES_NO | wxNO_DEFAULT | wxCANCEL );
332
333 if( dlg.ShowModal() != wxID_YES )
334 return -1;
335
336 helpFile = baseUrl + names[0] + wxS( "/" );
337 }
338 }
339 else
340 {
341 wxString base_name = m_frame->help_name();
342
343 helpFile = SearchHelpFileFullPath( base_name );
344
345 if( !helpFile )
346 {
347 msg = wxString::Format( _( "Help file '%s' could not be found.\n"
348 "Do you want to access the KiCad online help?" ),
349 base_name );
350 KICAD_MESSAGE_DIALOG dlg( nullptr, msg, _( "File Not Found" ),
351 wxYES_NO | wxNO_DEFAULT | wxCANCEL );
352
353 if( dlg.ShowModal() != wxID_YES )
354 return -1;
355
356 helpFile = baseUrl + base_name + wxS( "/" );
357 }
358 }
359
360 GetAssociatedDocument( m_frame, helpFile, &m_frame->Prj() );
361 return 0;
362}
363
364
366{
367 void ShowAboutDialog( EDA_BASE_FRAME * aParent ); // See AboutDialog_main.cpp
369 return 0;
370}
371
372
374{
376 return 0;
377}
378
379
381{
382 if( !wxLaunchDefaultBrowser( URL_GET_INVOLVED ) )
383 {
384 wxString msg;
385 msg.Printf( _( "Could not launch the default browser.\n"
386 "For information on how to help the KiCad project, visit %s" ),
388 wxMessageBox( msg, _( "Get involved with KiCad" ), wxOK, m_frame );
389 }
390
391 return 0;
392}
393
394
396{
397 if( !wxLaunchDefaultBrowser( URL_DONATE ) )
398 {
399 wxString msg;
400 msg.Printf( _( "Could not launch the default browser.\n"
401 "To donate to the KiCad project, visit %s" ),
402 URL_DONATE );
403 wxMessageBox( msg, _( "Donate to KiCad" ), wxOK, m_frame );
404 }
405
406 return 0;
407}
408
409
411{
413 return 0;
414
415 wxString version = GetVersionInfoData( m_frame->GetUntranslatedAboutTitle(), false, true );
416
417 wxString message;
418 message.Printf( m_bugReportTemplate, version );
419
420 KICAD_CURL_EASY kcurl;
421 wxString url_string;
422 url_string.Printf( m_bugReportUrl, kcurl.Escape( std::string( message.utf8_str() ) ) );
423
424 wxLaunchDefaultBrowser( url_string );
425
426 return 0;
427}
428
429
431{
432 if( Pgm().GetCommonSettings()->m_Api.enable_server )
433 {
434 std::shared_ptr<REPORTER> reporter;
435
436 if( KISTATUSBAR* statusBar = dynamic_cast<KISTATUSBAR*>( m_frame->GetStatusBar() ) )
437 reporter = std::make_shared<STATUSBAR_WARNING_REPORTER>( statusBar, wxS( "plugin" ) );
438
439 Pgm().GetPluginManager().ReloadPlugins( std::nullopt, reporter );
440 }
441 return 0;
442}
443
444
446{
447 wxString dir( PATHS::GetUserPluginsPath() );
448
449 if( !wxFileName::DirExists( dir ) )
450 {
451 if( !PATHS::EnsurePathExists( dir ) )
452 {
454 wxString::Format( _( "Unable to create plugin directory '%s'." ),
455 dir ) );
456 return 0;
457 }
458 }
459
460 if( !LaunchExternal( dir ) )
462 wxString::Format( _( "Unable to open plugin directory '%s'." ),
463 dir ) );
464
465 return 0;
466}
467
468
470{
471 Go( &COMMON_CONTROL::Quit, ACTIONS::quit.MakeEvent() );
472
484
491 Go( &COMMON_CONTROL::About, ACTIONS::about.MakeEvent() );
494}
495
496
void ShowAboutDialog(EDA_BASE_FRAME *aParent)
const char * name
BASE_SCREEN class implementation.
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
wxString GetVersionInfoData(const wxString &aTitle, bool aHtml, bool aBrief)
Create a version info string for bug reports and the about dialog.
static TOOL_ACTION showCalculatorTools
Definition actions.h:259
static TOOL_ACTION about
Definition actions.h:287
static TOOL_ACTION reportBug
Definition actions.h:291
static TOOL_ACTION pluginsReload
Definition actions.h:294
static TOOL_ACTION showSymbolLibTable
Definition actions.h:282
static TOOL_ACTION showSymbolEditor
Definition actions.h:256
static TOOL_ACTION showSymbolBrowser
Definition actions.h:255
static TOOL_ACTION pluginsShowFolder
Definition actions.h:295
static TOOL_ACTION donate
Definition actions.h:289
static TOOL_ACTION listHotKeys
Definition actions.h:288
static TOOL_ACTION openPreferences
Definition actions.h:280
static TOOL_ACTION getInvolved
Definition actions.h:290
static TOOL_ACTION showFootprintLibTable
Definition actions.h:283
static TOOL_ACTION quit
Definition actions.h:62
static TOOL_ACTION showDesignBlockLibTable
Definition actions.h:284
static TOOL_ACTION showFootprintEditor
Definition actions.h:258
static TOOL_ACTION configurePaths
Definition actions.h:281
static TOOL_ACTION showProjectManager
Definition actions.h:253
static TOOL_ACTION gettingStarted
Definition actions.h:285
static TOOL_ACTION showFootprintBrowser
Definition actions.h:257
static TOOL_ACTION help
Definition actions.h:286
void ReloadPlugins(std::optional< wxString > aDirectoryToScan=std::nullopt, std::shared_ptr< REPORTER > aReporter=nullptr)
Clears the loaded plugins and actions and re-scans the filesystem to register new ones.
int ConfigurePaths(const TOOL_EVENT &aEvent)
int ShowHelp(const TOOL_EVENT &aEvent)
EDA_BASE_FRAME * m_frame
Pointer to the currently used edit frame.
int ShowLibraryTable(const TOOL_EVENT &aEvent)
int Quit(const TOOL_EVENT &aEvent)
int ShowPluginFolder(const TOOL_EVENT &aEvent)
int ReloadPlugins(const TOOL_EVENT &aEvent)
int About(const TOOL_EVENT &aEvent)
int ListHotKeys(const TOOL_EVENT &aEvent)
static wxString m_bugReportUrl
URL to launch a new issue with pre-populated description.
int GetInvolved(const TOOL_EVENT &aEvent)
int ShowProjectManager(const TOOL_EVENT &aEvent)
int Execute(const TOOL_EVENT &aEvent)
int ReportBug(const TOOL_EVENT &aEvent)
int ShowPlayer(const TOOL_EVENT &aEvent)
int Donate(const TOOL_EVENT &aEvent)
void Reset(RESET_REASON aReason) override
Bring the tool to a known, initial state.
int OpenPreferences(const TOOL_EVENT &aEvent)
static wxString m_bugReportTemplate
Issue template to use for reporting bugs (this should not be translated)
void setTransitions() override
Sets up handlers for various events.
int ShowModal() override
The base frame for deriving all KiCad main window classes.
std::string Escape(const std::string &aUrl)
Escapes a string for use as a URL.
KISTATUSBAR is a wxStatusBar suitable for Kicad manager.
Definition kistatusbar.h:50
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
A wxFrame capable of the OpenProjectFiles function, meaning it can load a portion of a KiCad project.
wxWindow * GetBlockingDialog()
Gets the window pointer to the blocking dialog (to send it signals)
Definition kiway.cpp:693
@ FACE_SCH
eeschema DSO
Definition kiway.h:355
@ FACE_PCB
pcbnew DSO
Definition kiway.h:356
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition paths.cpp:49
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
Definition paths.cpp:470
virtual API_PLUGIN_MANAGER & GetPluginManager() const
Definition pgm_base.h:139
virtual wxLocale * GetLocale()
Definition pgm_base.h:174
void OnTerminate(int pid, int status) override
TERMINATE_HANDLER(const wxString &appName)
T * getEditFrame() const
Return the application window object, casted to requested user type.
Definition tool_base.h:182
RESET_REASON
Determine the reason of reset for a tool.
Definition tool_base.h:74
Generic, UI-independent tool event.
Definition tool_event.h:167
bool IsAction(const TOOL_ACTION *aAction) const
Test if the event contains an action issued upon activation of the given TOOL_ACTION.
T Parameter() const
Return a parameter assigned to the event.
Definition tool_event.h:484
void Go(int(T::*aStateFunc)(const TOOL_EVENT &), const TOOL_EVENT_LIST &aConditions=TOOL_EVENT(TC_ANY, TA_ANY))
Define which state (aStateFunc) to go when a certain event arrives (aConditions).
bool WarnUserIfOperatingSystemUnsupported()
Checks if the operating system is explicitly unsupported and displays a disclaimer message box.
Definition common.cpp:1033
#define URL_DONATE
#define URL_DOCUMENTATION
void showFrame(EDA_BASE_FRAME *aFrame)
#define URL_GET_INVOLVED
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
#define KICAD_MESSAGE_DIALOG
Definition confirm.h:48
#define _(s)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths, std::vector< EMBEDDED_FILES * > aFilesStack)
Open a document (file) with the suitable browser.
Definition eda_doc.cpp:59
This file is part of the common library.
KiCad executable names.
const wxString EESCHEMA_EXE
const wxString GERBVIEW_EXE
const wxString PL_EDITOR_EXE
const wxString PCBNEW_EXE
const wxString PCB_CALCULATOR_EXE
FRAME_T
The set of EDA_BASE_FRAME derivatives, typically stored in EDA_BASE_FRAME::m_Ident.
Definition frame_type.h:29
@ DIALOG_PCB_LIBRARY_TABLE
Definition frame_type.h:131
@ DIALOG_SCH_LIBRARY_TABLE
Definition frame_type.h:130
@ DIALOG_CONFIGUREPATHS
Definition frame_type.h:128
@ DIALOG_DESIGN_BLOCK_LIBRARY_TABLE
Definition frame_type.h:129
@ KICAD_MAIN_FRAME_T
Definition frame_type.h:67
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
Definition gestfich.cpp:166
void DisplayHotkeyList(EDA_BASE_FRAME *aParent)
Display the current hotkey list.
std::string message
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
wxString SearchHelpFileFullPath(const wxString &aBaseName)
Return the help file's full path.
Implement a participant in the KIWAY alchemy.
Definition kiway.h:153
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
IbisParser parser & reporter
KIBIS top(path, &reporter)
#define ENVVARS_CHANGED