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.

209 lines
6.6 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
  5. * Copyright (C) 2014-2021 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, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <pgm_base.h>
  25. #include <common.h>
  26. #include <confirm.h>
  27. #include <embedded_files.h>
  28. #include <gestfich.h>
  29. #include <settings/common_settings.h>
  30. #include <wx/filedlg.h>
  31. #include <wx/filename.h>
  32. #include <wx/log.h>
  33. #include <wx/mimetype.h>
  34. #include <wx/uri.h>
  35. // Mime type extensions (PDF files are not considered here)
  36. static wxMimeTypesManager* mimeDatabase;
  37. static const wxFileTypeInfo EDAfallbacks[] =
  38. {
  39. wxFileTypeInfo( wxT( "text/html" ),
  40. wxT( "wxhtml %s" ),
  41. wxT( "wxhtml %s" ),
  42. wxT( "html document (from KiCad)" ),
  43. wxT( "htm" ),
  44. wxT( "html" ),wxNullPtr ),
  45. wxFileTypeInfo( wxT( "application/sch" ),
  46. wxT( "eeschema %s" ),
  47. wxT( "eeschema -p %s" ),
  48. wxT( "sch document (from KiCad)" ),
  49. wxT( "sch" ),
  50. wxT( "SCH" ), wxNullPtr ),
  51. // must terminate the table with this!
  52. wxFileTypeInfo()
  53. };
  54. bool GetAssociatedDocument( wxWindow* aParent, const wxString& aDocName, PROJECT* aProject,
  55. SEARCH_STACK* aPaths, EMBEDDED_FILES* aFiles )
  56. {
  57. wxString docname;
  58. wxString fullfilename;
  59. wxString msg;
  60. wxString command;
  61. bool success = false;
  62. // Replace before resolving as we might have a URL in a variable
  63. docname = ResolveUriByEnvVars( aDocName, aProject );
  64. // We don't want the wx error message about not being able to open the URI
  65. {
  66. wxURI uri( docname );
  67. wxLogNull logNo; // Disable log messages
  68. if( uri.HasScheme() )
  69. {
  70. wxString scheme = uri.GetScheme().Lower();
  71. if( scheme != FILEEXT::KiCadUriPrefix )
  72. {
  73. if( wxLaunchDefaultBrowser( docname ) )
  74. return true;
  75. }
  76. else
  77. {
  78. if( !aFiles )
  79. {
  80. wxLogTrace( wxT( "KICAD_EMBED" ),
  81. wxT( "No EMBEDDED_FILES object provided for kicad_embed URI" ) );
  82. return false;
  83. }
  84. if( !docname.starts_with( FILEEXT::KiCadUriPrefix + "://" ) )
  85. {
  86. wxLogTrace( wxT( "KICAD_EMBED" ),
  87. wxT( "Invalid kicad_embed URI '%s'" ), docname );
  88. return false;
  89. }
  90. docname = docname.Mid( 14 );
  91. wxFileName temp_file = aFiles->GetTemporaryFileName( docname );
  92. if( !temp_file.IsOk() )
  93. {
  94. wxLogTrace( wxT( "KICAD_EMBED" ),
  95. wxT( "Failed to get temp file '%s' for kicad_embed URI" ), docname );
  96. return false;
  97. }
  98. wxLogTrace( wxT( "KICAD_EMBED" ),
  99. wxT( "Opening embedded file '%s' as '%s'" ),
  100. docname, temp_file.GetFullPath() );
  101. docname = temp_file.GetFullPath();
  102. }
  103. }
  104. }
  105. #ifdef __WINDOWS__
  106. docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
  107. #else
  108. docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
  109. #endif
  110. /* Compute the full file name */
  111. if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
  112. fullfilename = docname;
  113. /* If the file exists, this is a trivial case: return the filename "as this". the name can
  114. * be an absolute path, or a relative path like ./filename or ../<filename>.
  115. */
  116. else if( wxFileName::FileExists( docname ) )
  117. fullfilename = docname;
  118. else
  119. fullfilename = aPaths->FindValidPath( docname );
  120. wxString extension;
  121. #ifdef __WINDOWS__
  122. extension = wxT( ".*" );
  123. #endif
  124. if( wxIsWild( fullfilename ) )
  125. {
  126. fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
  127. fullfilename, extension, wxFileSelectorDefaultWildcardStr,
  128. wxFD_OPEN, aParent );
  129. if( fullfilename.IsEmpty() )
  130. return false;
  131. }
  132. if( !wxFileExists( fullfilename ) )
  133. {
  134. msg.Printf( _( "Documentation file '%s' not found." ), docname );
  135. DisplayError( aParent, msg );
  136. return false;
  137. }
  138. wxFileName currentFileName( fullfilename );
  139. // Use wxWidgets to resolve any "." and ".." in the path
  140. fullfilename = currentFileName.GetAbsolutePath();
  141. wxString file_ext = currentFileName.GetExt();
  142. if( file_ext.Lower() == wxT( "pdf" ) )
  143. {
  144. success = OpenPDF( fullfilename );
  145. return success;
  146. }
  147. /* Try to launch some browser (useful under linux) */
  148. wxFileType* filetype;
  149. wxString type;
  150. filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
  151. if( !filetype ) // 2nd attempt.
  152. {
  153. mimeDatabase = new wxMimeTypesManager;
  154. mimeDatabase->AddFallbacks( EDAfallbacks );
  155. filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
  156. delete mimeDatabase;
  157. mimeDatabase = nullptr;
  158. }
  159. if( filetype )
  160. {
  161. wxFileType::MessageParameters params( fullfilename, type );
  162. success = filetype->GetOpenCommand( &command, params );
  163. delete filetype;
  164. if( success )
  165. success = wxExecute( command );
  166. }
  167. if( !success )
  168. {
  169. msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
  170. DisplayError( aParent, msg );
  171. }
  172. return success;
  173. }