]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphics.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / insets / insetgraphics.C
index 23bc48e92e5e5e7e50e87da6f9723c6af16a76d5..59b811390ddeb4c4230b658e5d65e5825d741504 100644 (file)
@@ -6,7 +6,7 @@
  * \author Baruch Even
  * \author Herbert Voss
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 /*
@@ -68,6 +68,8 @@ TODO
 #include "Lsstream.h"
 #include "lyxlex.h"
 #include "lyxrc.h"
+#include "paragraph_funcs.h"
+#include "lyxtext.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -82,11 +84,13 @@ TODO
 
 #include <algorithm> // For the std::max
 
-extern string system_tempdir;
 // set by Exporters
 
+using namespace lyx::support;
+
 using std::ostream;
 using std::endl;
+using std::auto_ptr;
 
 
 namespace {
@@ -115,7 +119,7 @@ string findTargetFormat(string const & suffix, LatexRunParams const & runparams)
 {
        // Are we using latex or pdflatex).
        if (runparams.flavor == LatexRunParams::PDFLATEX) {
-               lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode\n";
+               lyxerr[Debug::GRAPHICS] << "findTargetFormat: PDF mode" << endl;
                if (contains(suffix, "ps") || suffix == "pdf")
                        return "pdf";
                if (suffix == "jpg")    // pdflatex can use jpeg
@@ -123,7 +127,7 @@ string findTargetFormat(string const & suffix, LatexRunParams const & runparams)
                return "png";         // and also png
        }
        // If it's postscript, we always do eps.
-       lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode\n";
+       lyxerr[Debug::GRAPHICS] << "findTargetFormat: PostScript mode" << endl;
        if (suffix != "ps")     // any other than ps
                return "eps";         // is changed to eps
        return suffix;          // let ps untouched
@@ -141,7 +145,7 @@ InsetGraphics::InsetGraphics()
 
 
 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
-       : Inset(ig),
+       : InsetOld(ig),
          boost::signals::trackable(),
          graphic_label(uniqueID()),
          graphic_(new GraphicRenderer(*ig.graphic_))
@@ -151,9 +155,9 @@ InsetGraphics::InsetGraphics(InsetGraphics const & ig)
 }
 
 
-InsetBase * InsetGraphics::clone() const
+auto_ptr<InsetBase> InsetGraphics::clone() const
 {
-       return new InsetGraphics(*this);
+       return auto_ptr<InsetBase>(new InsetGraphics(*this));
 }
 
 
@@ -169,14 +173,15 @@ void InsetGraphics::statusChanged()
        if (bv)
                bv->updateInset(this);
 }
-       
+
 
 dispatch_result InsetGraphics::localDispatch(FuncRequest const & cmd)
 {
        switch (cmd.action) {
        case LFUN_INSET_MODIFY: {
+               Buffer const & buffer = *cmd.view()->buffer();
                InsetGraphicsParams p;
-               InsetGraphicsMailer::string2params(cmd.argument, p);
+               InsetGraphicsMailer::string2params(cmd.argument, buffer, p);
                if (!p.filename.empty()) {
                        setParams(p);
                        cmd.view()->updateInset(this);
@@ -194,7 +199,7 @@ dispatch_result InsetGraphics::localDispatch(FuncRequest const & cmd)
                return DISPATCHED;
 
        default:
-               return Inset::localDispatch(cmd);
+               return InsetOld::localDispatch(cmd);
        }
 }
 
@@ -202,6 +207,7 @@ dispatch_result InsetGraphics::localDispatch(FuncRequest const & cmd)
 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        graphic_->metrics(mi, dim);
+       dim_ = dim;
 }
 
 
@@ -211,27 +217,27 @@ void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-Inset::EDITABLE InsetGraphics::editable() const
+InsetOld::EDITABLE InsetGraphics::editable() const
 {
        return IS_EDITABLE;
 }
 
 
-void InsetGraphics::write(Buffer const * buf, ostream & os) const
+void InsetGraphics::write(Buffer const & buf, ostream & os) const
 {
        os << "Graphics\n";
-       params().Write(os, buf->filePath());
+       params().Write(os, buf.filePath());
 }
 
 
-void InsetGraphics::read(Buffer const * buf, LyXLex & lex)
+void InsetGraphics::read(Buffer const & buf, LyXLex & lex)
 {
        string const token = lex.getString();
 
        if (token == "Graphics")
-               readInsetGraphics(lex, buf->filePath());
+               readInsetGraphics(lex, buf.filePath());
        else
-               lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!\n";
+               lyxerr[Debug::GRAPHICS] << "Not a Graphics inset!" << endl;
 
        graphic_->update(params().as_grfxParams());
 }
@@ -246,7 +252,7 @@ void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
 
                string const token = lex.getString();
                lyxerr[Debug::GRAPHICS] << "Token: '" << token << '\''
-                                   << std::endl;
+                                   << endl;
 
                if (token.empty()) {
                        continue;
@@ -260,7 +266,7 @@ void InsetGraphics::readInsetGraphics(LyXLex & lex, string const & bufpath)
                                << "This document was created with a newer Graphics widget"
                                ", You should use a newer version of LyX to read this"
                                " file."
-                               << std::endl;
+                               << endl;
                        // TODO: Possibly open up a dialog?
                }
                else {
@@ -284,8 +290,8 @@ string const InsetGraphics::createLatexOptions() const
            options << "  draft,\n";
        if (params().clip)
            options << "  clip,\n";
-       if (!lyx::float_equal(params().scale, 0.0, 0.05)) {
-               if (!lyx::float_equal(params().scale, 100.0, 0.05))
+       if (!float_equal(params().scale, 0.0, 0.05)) {
+               if (!float_equal(params().scale, 100.0, 0.05))
                        options << "  scale=" << params().scale / 100.0
                                << ",\n";
        } else {
@@ -299,7 +305,7 @@ string const InsetGraphics::createLatexOptions() const
 
        // Make sure rotation angle is not very close to zero;
        // a float can be effectively zero but not exactly zero.
-       if (!lyx::float_equal(params().rotateAngle, 0, 0.001)) {
+       if (!float_equal(params().rotateAngle, 0, 0.001)) {
            options << "  angle=" << params().rotateAngle << ",\n";
            if (!params().rotateOrigin.empty()) {
                options << "  origin=" << params().rotateOrigin[0];
@@ -322,13 +328,13 @@ string const InsetGraphics::createLatexOptions() const
 }
 
 
-string const InsetGraphics::prepareFile(Buffer const * buf,
+string const InsetGraphics::prepareFile(Buffer const & buf,
                                        LatexRunParams const & runparams) const
 {
        // LaTeX can cope if the graphics file doesn't exist, so just return the
        // filename.
-       string orig_file = params().filename;
-       string const rel_file = MakeRelPath(orig_file, buf->filePath());
+       string orig_file = params().filename.absFilename();
+       string const rel_file = params().filename.relFilename(buf.filePath());
 
        if (!IsFileReadable(orig_file)) {
                lyxerr[Debug::GRAPHICS]
@@ -358,11 +364,11 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
                // Uncompress the file if necessary.
                // If it has been uncompressed in a previous call to
                // prepareFile, do nothing.
-               temp_file = MakeAbsPath(OnlyFilename(temp_file), buf->tmppath);
+               temp_file = MakeAbsPath(OnlyFilename(temp_file), buf.tmppath);
                lyxerr[Debug::GRAPHICS]
                        << "\ttemp_file: " << temp_file << endl;
-               if (graphic_->hasFileChanged() || !IsFileReadable(temp_file)) {
-                       bool const success = lyx::copy(orig_file, temp_file);
+               if (graphic_->hasFileChanged() || !IsFileReadable(temp_file)) {
+                       bool const success = copy(orig_file, temp_file);
                        lyxerr[Debug::GRAPHICS]
                                << "\tCopying zipped file from "
                                << orig_file << " to " << temp_file
@@ -405,14 +411,14 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
                << "\tthe orig file is: " << orig_file << endl;
 
        if (lyxrc.use_tempdir) {
-               temp_file = copyFileToDir(buf->tmppath, orig_file);
+               temp_file = copyFileToDir(buf.tmppath, orig_file);
                if (temp_file.empty()) {
                        string str = bformat(_("Could not copy the file\n%1$s\n"
                                               "into the temporary directory."),
                                             orig_file);
                        Alert::error(_("Graphics display failed"), str);
                        return orig_file;
-               }
+               }
 
                if (from == to) {
                        // No conversion is needed. LaTeX can handle the
@@ -433,9 +439,9 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
 
        // if no special converter defined, than we take the default one
        // from ImageMagic: convert from:inname.from to:outname.to
-       if (!converters.convert(buf, temp_file, outfile_base, from, to)) {
+       if (!converters.convert(&buf, temp_file, outfile_base, from, to)) {
                string const command =
-                       LibFileSearch("scripts", "convertDefault.sh") +
+                       "sh " + LibFileSearch("scripts", "convertDefault.sh") +
                                ' ' + from + ':' + temp_file + ' ' +
                                to + ':' + outfile_base + '.' + to;
                lyxerr[Debug::GRAPHICS]
@@ -455,21 +461,22 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
 }
 
 
-int InsetGraphics::latex(Buffer const * buf, ostream & os,
+int InsetGraphics::latex(Buffer const & buf, ostream & os,
                         LatexRunParams const & runparams) const
 {
        // If there is no file specified or not existing,
        // just output a message about it in the latex output.
        lyxerr[Debug::GRAPHICS]
                << "insetgraphics::latex: Filename = "
-               << params().filename << endl;
+               << params().filename.absFilename() << endl;
 
-       string const relative_file = MakeRelPath(params().filename, buf->filePath());
+       string const relative_file =
+               params().filename.relFilename(buf.filePath());
 
        // A missing (e)ps-extension is no problem for LaTeX, so
        // we have to test three different cases
 #warning uh, but can our cache handle it ? no.
-       string const file_ = params().filename;
+       string const file_ = params().filename.absFilename();
        bool const file_exists =
                !file_.empty() &&
                (IsFileReadable(file_) ||               // original
@@ -532,21 +539,27 @@ int InsetGraphics::latex(Buffer const * buf, ostream & os,
 }
 
 
-int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
+int InsetGraphics::ascii(Buffer const &, ostream & os, int) const
 {
        // No graphics in ascii output. Possible to use gifscii to convert
        // images to ascii approximation.
        // 1. Convert file to ascii using gifscii
        // 2. Read ascii output file and add it to the output stream.
        // at least we send the filename
-       os << '<' << bformat(_("Graphics file: %1$s"), params().filename) << ">\n";
+       os << '<' << bformat(_("Graphics file: %1$s"),
+                            params().filename.absFilename()) << ">\n";
        return 0;
 }
 
 
-int InsetGraphics::linuxdoc(Buffer const *, ostream &) const
+int InsetGraphics::linuxdoc(Buffer const & buf, ostream & os) const
 {
-       // No graphics in LinuxDoc output. Should check how/what to add.
+       string const file_name = buf.niceFile ?
+                               params().filename.relFilename(buf.filePath()):
+                               params().filename.absFilename();
+
+       os << "<eps file=\"" << file_name << "\">\n";
+       os << "<img src=\"" << file_name << "\">";
        return 0;
 }
 
@@ -554,7 +567,7 @@ int InsetGraphics::linuxdoc(Buffer const *, ostream &) const
 // For explanation on inserting graphics into DocBook checkout:
 // http://en.tldp.org/LDP/LDP-Author-Guide/inserting-pictures.html
 // See also the docbook guide at http://www.docbook.org/
-int InsetGraphics::docbook(Buffer const *, ostream & os,
+int InsetGraphics::docbook(Buffer const &, ostream & os,
                           bool /*mixcont*/) const
 {
        // In DocBook v5.0, the graphic tag will be eliminated from DocBook, will
@@ -571,7 +584,8 @@ void InsetGraphics::validate(LaTeXFeatures & features) const
        if (params().filename.empty())
                return;
 
-       features.includeFile(graphic_label, RemoveExtension(params().filename));
+       features.includeFile(graphic_label,
+                            RemoveExtension(params().filename.absFilename()));
 
        features.require("graphicx");
 
@@ -589,8 +603,8 @@ bool InsetGraphics::setParams(InsetGraphicsParams const & p)
        // Copy the new parameters.
        params_ = p;
 
-       // Update the display using the new parameters.
-       graphic_->update(params().as_grfxParams());
+       // Update the display using the new parameters.
+       graphic_->update(params().as_grfxParams());
 
        // We have changed data, report it.
        return true;
@@ -610,13 +624,14 @@ InsetGraphicsMailer::InsetGraphicsMailer(InsetGraphics & inset)
 {}
 
 
-string const InsetGraphicsMailer::inset2string() const
+string const InsetGraphicsMailer::inset2string(Buffer const & buffer) const
 {
-       return params2string(inset_.params());
+       return params2string(inset_.params(), buffer);
 }
 
 
 void InsetGraphicsMailer::string2params(string const & in,
+                                       Buffer const & buffer,
                                        InsetGraphicsParams & params)
 {
        params = InsetGraphicsParams();
@@ -637,20 +652,19 @@ void InsetGraphicsMailer::string2params(string const & in,
 
        if (lex.isOK()) {
                InsetGraphics inset;
-#warning FIXME not setting bufpath is dubious
-               inset.readInsetGraphics(lex, string());
+               inset.readInsetGraphics(lex, buffer.filePath());
                params = inset.params();
        }
 }
 
 
 string const
-InsetGraphicsMailer::params2string(InsetGraphicsParams const & params)
+InsetGraphicsMailer::params2string(InsetGraphicsParams const & params,
+                                  Buffer const & buffer)
 {
        ostringstream data;
        data << name_ << ' ';
-#warning FIXME not setting bufpath is dubious
-       params.Write(data, string());
+       params.Write(data, buffer.filePath());
        data << "\\end_inset\n";
        return STRCONV(data.str());
 }