You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
12 KiB

3 years ago
3 years ago
3 years ago
12 months ago
12 months ago
12 months ago
12 months ago
4 years ago
4 years ago
3 years ago
3 years ago
  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 <maciej.suminski@cern.ch>
  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, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. // kicad_curl_easy.h must be included before wxWidgets because on Windows (msys2), there are
  26. // collision with wx headers and curl.h defs
  27. #include <kicad_curl/kicad_curl_easy.h>
  28. #include <bitmaps.h>
  29. #include <build_version.h>
  30. #include <common.h> // for SearchHelpFileFullPath
  31. #include <pgm_base.h>
  32. #include <tool/actions.h>
  33. #include <tool/tool_manager.h>
  34. #include <eda_draw_frame.h>
  35. #include <view/view.h>
  36. #include <gal/graphics_abstraction_layer.h>
  37. #include <base_screen.h>
  38. #include <tool/common_control.h>
  39. #include <id.h>
  40. #include <kiface_base.h>
  41. #include <dialogs/dialog_configure_paths.h>
  42. #include <eda_doc.h>
  43. #include <wx/msgdlg.h>
  44. #define URL_GET_INVOLVED wxS( "https://go.kicad.org/contribute/" )
  45. #define URL_DONATE wxS( "https://go.kicad.org/app-donate" )
  46. #define URL_DOCUMENTATION wxS( "https://go.kicad.org/docs/" )
  47. /// URL to launch a new issue with pre-populated description
  48. wxString COMMON_CONTROL::m_bugReportUrl =
  49. wxS( "https://gitlab.com/kicad/code/kicad/-/issues/new?issuable_template=bare&issue"
  50. "[description]=%s" );
  51. /// Issue template to use for reporting bugs (this should not be translated)
  52. wxString COMMON_CONTROL::m_bugReportTemplate = wxS(
  53. "```\n"
  54. "%s\n"
  55. "```" );
  56. void COMMON_CONTROL::Reset( RESET_REASON aReason )
  57. {
  58. m_frame = getEditFrame<EDA_BASE_FRAME>();
  59. }
  60. int COMMON_CONTROL::OpenPreferences( const TOOL_EVENT& aEvent )
  61. {
  62. m_frame->ShowPreferences( wxEmptyString, wxEmptyString );
  63. return 0;
  64. }
  65. int COMMON_CONTROL::ConfigurePaths( const TOOL_EVENT& aEvent )
  66. {
  67. // If _pcbnew.kiface is running have it put up the dialog so the 3D paths can also
  68. // be edited
  69. if( KIFACE* pcbnew = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB, false ) )
  70. {
  71. try
  72. {
  73. pcbnew->CreateKiWindow( m_frame, DIALOG_CONFIGUREPATHS, &m_frame->Kiway() );
  74. }
  75. catch( ... )
  76. {
  77. // Do nothing here.
  78. // A error message is displayed after trying to load _pcbnew.kiface.
  79. }
  80. }
  81. else
  82. {
  83. DIALOG_CONFIGURE_PATHS dlg( m_frame );
  84. if( dlg.ShowModal() == wxID_OK )
  85. m_frame->Kiway().CommonSettingsChanged( ENVVARS_CHANGED );
  86. }
  87. return 0;
  88. }
  89. int COMMON_CONTROL::ShowLibraryTable( const TOOL_EVENT& aEvent )
  90. {
  91. if( aEvent.IsAction( &ACTIONS::showSymbolLibTable ) )
  92. {
  93. try // Sch frame was not available, try to start it
  94. {
  95. if( KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH ) )
  96. kiface->CreateKiWindow( m_frame, DIALOG_SCH_LIBRARY_TABLE, &m_frame->Kiway() );
  97. }
  98. catch( ... )
  99. {
  100. // _eeschema.kiface is not available: it contains the library table dialog.
  101. // Do nothing here.
  102. // A error message is displayed after trying to load _eeschema.kiface.
  103. }
  104. }
  105. else if( aEvent.IsAction( &ACTIONS::showFootprintLibTable ) )
  106. {
  107. try // Pcb frame was not available, try to start it
  108. {
  109. if( KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB ) )
  110. kiface->CreateKiWindow( m_frame, DIALOG_PCB_LIBRARY_TABLE, &m_frame->Kiway() );
  111. }
  112. catch( ... )
  113. {
  114. // _pcbnew.kiface is not available: it contains the library table dialog.
  115. // Do nothing here.
  116. // A error message is displayed after trying to load _pcbnew.kiface.
  117. }
  118. }
  119. else if( aEvent.IsAction( &ACTIONS::showDesignBlockLibTable ) )
  120. {
  121. try // Kicad frame was not available, try to start it
  122. {
  123. if( KIFACE* kiface = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH ) )
  124. kiface->CreateKiWindow( m_frame, DIALOG_DESIGN_BLOCK_LIBRARY_TABLE,
  125. &m_frame->Kiway() );
  126. }
  127. catch( ... )
  128. {
  129. // _eeschema.kiface is not available: it contains the library table dialog.
  130. // Do nothing here.
  131. // A error message is displayed after trying to load _eeschema.kiface.
  132. }
  133. }
  134. return 0;
  135. }
  136. void showFrame( EDA_BASE_FRAME* aFrame )
  137. {
  138. // Needed on Windows, other platforms do not use it, but it creates no issue
  139. if( aFrame->IsIconized() )
  140. aFrame->Iconize( false );
  141. aFrame->Raise();
  142. // Raising the window does not set the focus on Linux. This should work on
  143. // any platform.
  144. if( wxWindow::FindFocus() != aFrame )
  145. aFrame->SetFocus();
  146. // If the player is currently blocked, focus the user attention on the correct window
  147. if( wxWindow* blocking_win = aFrame->Kiway().GetBlockingDialog() )
  148. {
  149. blocking_win->Raise();
  150. blocking_win->SetFocus();
  151. }
  152. }
  153. int COMMON_CONTROL::ShowPlayer( const TOOL_EVENT& aEvent )
  154. {
  155. FRAME_T playerType = aEvent.Parameter<FRAME_T>();
  156. KIWAY_PLAYER* editor = m_frame->Kiway().Player( playerType, true );
  157. // editor can be null if Player() fails:
  158. wxCHECK_MSG( editor != nullptr, 0, wxT( "Cannot open/create the editor frame" ) );
  159. showFrame( editor );
  160. return 0;
  161. }
  162. int COMMON_CONTROL::ShowProjectManager( const TOOL_EVENT& aEvent )
  163. {
  164. // Note: dynamic_cast doesn't work over the Kiway() on MacOS. We have to use static_cast
  165. // here.
  166. EDA_BASE_FRAME* top = static_cast<EDA_BASE_FRAME*>( m_frame->Kiway().GetTop() );
  167. if( top && top->GetFrameType() == KICAD_MAIN_FRAME_T )
  168. {
  169. showFrame( top );
  170. }
  171. else
  172. {
  173. wxMessageDialog( m_frame, _( "Can not switch to project manager in stand-alone mode." ) );
  174. }
  175. return 0;
  176. }
  177. int COMMON_CONTROL::ShowHelp( const TOOL_EVENT& aEvent )
  178. {
  179. wxString helpFile;
  180. wxString msg;
  181. // the URL of help files is "https://go.kicad.org/docs/<version>/<language>/<name>/"
  182. const wxString baseUrl = URL_DOCUMENTATION + GetMajorMinorVersion() + wxT( "/" )
  183. + Pgm().GetLocale()->GetName().BeforeLast( '_' ) + wxT( "/" );
  184. /* We have to get document for beginners,
  185. * or the full specific doc
  186. * if event id is wxID_INDEX, we want the document for beginners.
  187. * else the specific doc file (its name is in Kiface().GetHelpFileName())
  188. * The document for beginners is the same for all KiCad utilities
  189. */
  190. if( aEvent.IsAction( &ACTIONS::gettingStarted ) )
  191. {
  192. // List of possible names for Getting Started in KiCad
  193. const wxChar* names[2] = {
  194. wxT( "getting_started_in_kicad" ),
  195. wxT( "Getting_Started_in_KiCad" )
  196. };
  197. // Search for "getting_started_in_kicad.html" or "getting_started_in_kicad.pdf"
  198. // or "Getting_Started_in_KiCad.html" or "Getting_Started_in_KiCad.pdf"
  199. for( auto& name : names )
  200. {
  201. helpFile = SearchHelpFileFullPath( name );
  202. if( !helpFile.IsEmpty() )
  203. break;
  204. }
  205. if( !helpFile )
  206. {
  207. msg = wxString::Format( _( "Help file '%s' or\n'%s' could not be found.\n"
  208. "Do you want to access the KiCad online help?" ),
  209. names[0], names[1] );
  210. wxMessageDialog dlg( nullptr, msg, _( "File Not Found" ),
  211. wxYES_NO | wxNO_DEFAULT | wxCANCEL );
  212. if( dlg.ShowModal() != wxID_YES )
  213. return -1;
  214. helpFile = baseUrl + names[0] + wxS( "/" );
  215. }
  216. }
  217. else
  218. {
  219. wxString base_name = m_frame->help_name();
  220. helpFile = SearchHelpFileFullPath( base_name );
  221. if( !helpFile )
  222. {
  223. msg = wxString::Format( _( "Help file '%s' could not be found.\n"
  224. "Do you want to access the KiCad online help?" ),
  225. base_name );
  226. wxMessageDialog dlg( nullptr, msg, _( "File Not Found" ),
  227. wxYES_NO | wxNO_DEFAULT | wxCANCEL );
  228. if( dlg.ShowModal() != wxID_YES )
  229. return -1;
  230. helpFile = baseUrl + base_name + wxS( "/" );
  231. }
  232. }
  233. GetAssociatedDocument( m_frame, helpFile, &m_frame->Prj() );
  234. return 0;
  235. }
  236. int COMMON_CONTROL::About( const TOOL_EVENT& aEvent )
  237. {
  238. void ShowAboutDialog( EDA_BASE_FRAME * aParent ); // See AboutDialog_main.cpp
  239. ShowAboutDialog( m_frame );
  240. return 0;
  241. }
  242. int COMMON_CONTROL::ListHotKeys( const TOOL_EVENT& aEvent )
  243. {
  244. DisplayHotkeyList( m_frame );
  245. return 0;
  246. }
  247. int COMMON_CONTROL::GetInvolved( const TOOL_EVENT& aEvent )
  248. {
  249. if( !wxLaunchDefaultBrowser( URL_GET_INVOLVED ) )
  250. {
  251. wxString msg;
  252. msg.Printf( _( "Could not launch the default browser.\n"
  253. "For information on how to help the KiCad project, visit %s" ),
  254. URL_GET_INVOLVED );
  255. wxMessageBox( msg, _( "Get involved with KiCad" ), wxOK, m_frame );
  256. }
  257. return 0;
  258. }
  259. int COMMON_CONTROL::Donate( const TOOL_EVENT& aEvent )
  260. {
  261. if( !wxLaunchDefaultBrowser( URL_DONATE ) )
  262. {
  263. wxString msg;
  264. msg.Printf( _( "Could not launch the default browser.\n"
  265. "To donate to the KiCad project, visit %s" ),
  266. URL_DONATE );
  267. wxMessageBox( msg, _( "Donate to KiCad" ), wxOK, m_frame );
  268. }
  269. return 0;
  270. }
  271. int COMMON_CONTROL::ReportBug( const TOOL_EVENT& aEvent )
  272. {
  273. if( WarnUserIfOperatingSystemUnsupported() )
  274. return 0;
  275. wxString version = GetVersionInfoData( m_frame->GetUntranslatedAboutTitle(), false, true );
  276. wxString message;
  277. message.Printf( m_bugReportTemplate, version );
  278. KICAD_CURL_EASY kcurl;
  279. wxString url_string;
  280. url_string.Printf( m_bugReportUrl, kcurl.Escape( std::string( message.utf8_str() ) ) );
  281. wxLaunchDefaultBrowser( url_string );
  282. return 0;
  283. }
  284. void COMMON_CONTROL::setTransitions()
  285. {
  286. Go( &COMMON_CONTROL::OpenPreferences, ACTIONS::openPreferences.MakeEvent() );
  287. Go( &COMMON_CONTROL::ConfigurePaths, ACTIONS::configurePaths.MakeEvent() );
  288. Go( &COMMON_CONTROL::ShowLibraryTable, ACTIONS::showSymbolLibTable.MakeEvent() );
  289. Go( &COMMON_CONTROL::ShowLibraryTable, ACTIONS::showFootprintLibTable.MakeEvent() );
  290. Go( &COMMON_CONTROL::ShowLibraryTable, ACTIONS::showDesignBlockLibTable.MakeEvent() );
  291. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showSymbolBrowser.MakeEvent() );
  292. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showSymbolEditor.MakeEvent() );
  293. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showFootprintBrowser.MakeEvent() );
  294. Go( &COMMON_CONTROL::ShowPlayer, ACTIONS::showFootprintEditor.MakeEvent() );
  295. Go( &COMMON_CONTROL::ShowProjectManager, ACTIONS::showProjectManager.MakeEvent() );
  296. Go( &COMMON_CONTROL::ShowHelp, ACTIONS::gettingStarted.MakeEvent() );
  297. Go( &COMMON_CONTROL::ShowHelp, ACTIONS::help.MakeEvent() );
  298. Go( &COMMON_CONTROL::ListHotKeys, ACTIONS::listHotKeys.MakeEvent() );
  299. Go( &COMMON_CONTROL::GetInvolved, ACTIONS::getInvolved.MakeEvent() );
  300. Go( &COMMON_CONTROL::Donate, ACTIONS::donate.MakeEvent() );
  301. Go( &COMMON_CONTROL::ReportBug, ACTIONS::reportBug.MakeEvent() );
  302. Go( &COMMON_CONTROL::About, ACTIONS::about.MakeEvent() );
  303. }