Dear developers,

Attached is a complete patch that, under windows,

1. configure.py generates "auto" as viewers.

2. when no user specified viewer exists, the default viewer through
windows ShellExecute will be used.

Windows users will never again need to worry about viewers.

Will be applied to 1.5.x if no objection is heard. Someone may want to
change configure.m4 so that this can be applied to 1.4.x as well.

Bo
Index: src/format.C
===================================================================
--- src/format.C	(revision 13715)
+++ src/format.C	(working copy)
@@ -35,6 +35,7 @@
 using lyx::support::quoteName;
 using lyx::support::subst;
 using lyx::support::Systemcall;
+using lyx::support::autoViewFile;
 
 using std::string;
 using std::distance;
@@ -223,13 +224,15 @@
 	if (format && format->viewer().empty() &&
 	    format->isChildFormat())
 		format = getFormat(format->parentFormat());
-	if (!format || format->viewer().empty()) {
-// I believe this is the wrong place to show alerts, it should be done by
-// the caller (this should be "utility" code)
-		Alert::error(_("Cannot view file"),
-			bformat(_("No information for viewing %1$s"),
-				prettyName(format_name)));
-		return false;
+
+	// auto viewer is the default setting under windows
+	if (!format || format->viewer().empty() || format->viewer() == "auto") {
+		if (!autoViewFile(filename, "open")) {
+			Alert::error(_("Cannot view file"),
+			     _("No default viewer for viewing ") + filename);
+			return false;
+		}
+		return true;
 	}
 
 	string command = libScriptSearch(format->viewer());
@@ -279,13 +282,15 @@
 	if (format && format->editor().empty() &&
 	    format->isChildFormat())
 		format = getFormat(format->parentFormat());
-	if (!format || format->editor().empty()) {
-// I believe this is the wrong place to show alerts, it should be done by
-// the caller (this should be "utility" code)
-		Alert::error(_("Cannot edit file"),
-			bformat(_("No information for editing %1$s"),
-				prettyName(format_name)));
-		return false;
+
+	// auto editor is the default setting under windows
+	if (!format || format->editor().empty() || format->editor() == "auto") {
+		if (!autoViewFile(filename, "edit")) {
+			Alert::error(_("Cannot edit file"),
+				_("No default editor for editing " + filename));
+			return false;
+		}
+		return true;
 	}
 
 	string command = format->editor();
Index: src/support/filetools.C
===================================================================
--- src/support/filetools.C	(revision 13715)
+++ src/support/filetools.C	(working copy)
@@ -32,6 +32,11 @@
 #include "support/path.h"
 #include "support/systemcall.h"
 
+#if defined(_WIN32) || defined(__CYGWIN__)
+# include <windef.h>
+# include <shellapi.h>	
+#endif
+
 // FIXME Interface violation
 #include "gettext.h"
 #include "debug.h"
@@ -1225,5 +1230,24 @@
 	return cmp;
 }
 
+
+bool autoViewFile(string const & filename, string const & mode)
+{
+#if defined(_WIN32) || defined(__CYGWIN__)
+	lyxerr[Debug::FILES] << mode << " file: " << filename << std::endl;
+
+	// reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc
+	//                 /platform/shell/reference/functions/shellexecute.asp
+	int const res = reinterpret_cast<int>(ShellExecute(NULL, mode.c_str(), 
+		filename.c_str(), NULL, NULL, 1));
+	return res > 32;
+#else
+	// currently, no default viewer is tried for non-windows system
+	// support for KDE/Gnome/Macintosh may be added later
+	return false;
+#endif
+}
+
+
 } //namespace support
 } // namespace lyx
Index: src/support/filetools.h
===================================================================
--- src/support/filetools.h	(revision 13715)
+++ src/support/filetools.h	(working copy)
@@ -266,6 +266,13 @@
 
 cmd_ret const runCommand(std::string const & cmd);
 
+/** view a file, with given command and parameter.
+ *  \param filename
+ *  \param mode  "open" or "edit"
+ *  \returns whether or not the file is viewed (or edited) successfully.
+ */
+bool autoViewFile(std::string const & filename, std::string const & mode="open");
+
 } // namespace support
 } // namespace lyx
 
Index: lib/configure.py
===================================================================
--- lib/configure.py	(revision 13713)
+++ lib/configure.py	(working copy)
@@ -262,6 +262,55 @@
 ''')
 
 
+def autoFormatEntries():  
+  ''' Use auto for windows system (\Format entries) '''
+  addToRC(r'''\Format tgif       obj     Tgif                   "" "auto"	"auto"
+\Format fig        fig     FIG                    "" "auto"	"auto"
+\Format agr        agr     Grace                  "" "auto"	"auto"
+\Format fen        fen     FEN                    "" "auto"	"auto"
+\Format bmp        bmp     BMP                    "" "auto"	"auto"
+\Format gif        gif     GIF                    "" "auto"	"auto"
+\Format jpg        jpg     JPEG                   "" "auto"	"auto"
+\Format pbm        pbm     PBM                    "" "auto"	"auto"
+\Format pgm        pgm     PGM                    "" "auto"	"auto"
+\Format png        png     PNG                    "" "auto"	"auto"
+\Format ppm        ppm     PPM                    "" "auto"	"auto"
+\Format tiff       tif     TIFF                   "" "auto"	"auto"
+\Format xbm        xbm     XBM                    "" "auto"	"auto"
+\Format xpm        xpm     XPM                    "" "auto"	"auto"
+\Format asciichess asc    "Plain text (chess output)"  "" ""	"auto"
+\Format asciiimage asc    "Plain text (image)"         "" ""	"auto"
+\Format asciixfig  asc    "Plain text (Xfig output)"   "" ""	"auto"
+\Format dateout    tmp    "date (output)"         "" ""	"auto"
+\Format docbook    sgml    DocBook                B  ""	"auto"
+\Format docbook-xml xml   "Docbook (XML)"         "" ""	"auto"
+\Format literate   nw      NoWeb                  N  ""	"auto"
+\Format latex      tex    "TeX (latex)"           l  ""	"auto"
+\Format linuxdoc   sgml    LinuxDoc               x  ""	"auto"
+\Format pdflatex   tex    "TeX (pdflatex)"        "" ""	"auto"
+\Format text       txt    "Plain text"            a  ""	"auto"
+\Format textparagraph txt "Plain text (paragraphs)"    "" "auto"	"auto"
+\Format eps        eps     EPS                    "" "auto"	""
+\Format ps         ps      Postscript             t  "auto"	""
+\Format pdf        pdf    "PDF (ps2pdf)"          P  "auto"	"auto"
+\Format pdf2       pdf    "PDF (pdflatex)"        F  "auto"	"auto"
+\Format pdf3       pdf    "PDF (dvipdfm)"         m  "auto"	"auto"
+\Format dvi        dvi     DVI                    D  "auto"	""
+\Format html       html    HTML                   H  "auto"	"auto"
+\Format date       ""     "date command"          "" ""	""
+\Format fax        ""      Fax                    "" ""	""
+\Format lyx        lyx     LyX                    "" ""	""
+\Format lyx13x     lyx13  "LyX 1.3.x"             "" ""	""
+\Format lyxpreview lyxpreview "LyX Preview"       "" ""	""
+\Format pdftex     pdftex_t PDFTEX                "" ""	""
+\Format program    ""      Program                "" ""	""
+\Format pstex      pstex_t PSTEX                  "" ""	""
+\Format sxw        sxw    "OpenOffice.Org Writer" O  ""	""
+\Format word       doc    "MS Word"               W  "auto"	"auto"
+\Format wordhtml   html   "MS Word (HTML)"        "" "auto" "auto"
+''')
+
+
 def checkConverterEntries():
   ''' Check all converters (\converter entries) '''
   checkProg('the pdflatex program', ['pdflatex $$i'],
@@ -696,7 +745,10 @@
 ''')
   # check latex
   LATEX = checkLatex()
-  checkFormatEntries()
+  if os.name == 'nt' or sys.platform == 'cygwin':
+    autoFormatEntries()
+  else:
+    checkFormatEntries()
   checkConverterEntries()
   (chk_linuxdoc, bool_linuxdoc, linuxdoc_cmd) = checkLinuxDoc()
   (chk_docbook, bool_docbook, docbook_cmd) = checkDocBook()






Reply via email to