]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetgraphics.C
prevent crash when inserting minipage in table cell,
[lyx.git] / src / insets / insetgraphics.C
index 782b954033aed0a16c9bd0b28c11a6ab36996587..c2b4a355f7e43ec987f3d1cdb60b42c8b5e72877 100644 (file)
@@ -82,11 +82,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 {
@@ -141,7 +143,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 +153,9 @@ InsetGraphics::InsetGraphics(InsetGraphics const & ig)
 }
 
 
-Inset * InsetGraphics::clone() const
+auto_ptr<InsetBase> InsetGraphics::clone() const
 {
-       return new InsetGraphics(*this);
+       return auto_ptr<InsetBase>(new InsetGraphics(*this));
 }
 
 
@@ -169,14 +171,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 +197,7 @@ dispatch_result InsetGraphics::localDispatch(FuncRequest const & cmd)
                return DISPATCHED;
 
        default:
-               return Inset::localDispatch(cmd);
+               return InsetOld::localDispatch(cmd);
        }
 }
 
@@ -202,6 +205,7 @@ dispatch_result InsetGraphics::localDispatch(FuncRequest const & cmd)
 void InsetGraphics::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        graphic_->metrics(mi, dim);
+       dim_ = dim;
 }
 
 
@@ -211,7 +215,7 @@ void InsetGraphics::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-Inset::EDITABLE InsetGraphics::editable() const
+InsetOld::EDITABLE InsetGraphics::editable() const
 {
        return IS_EDITABLE;
 }
@@ -284,8 +288,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 +303,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];
@@ -327,8 +331,8 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
 {
        // 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]
@@ -361,8 +365,8 @@ string const InsetGraphics::prepareFile(Buffer const * buf,
                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 +409,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
@@ -462,14 +466,15 @@ int InsetGraphics::latex(Buffer const * buf, ostream & os,
        // 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
@@ -539,14 +544,20 @@ int InsetGraphics::ascii(Buffer const *, ostream & os, int) const
        // 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;
 }
 
@@ -571,7 +582,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 +601,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 +622,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 +650,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());
 }