]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetinclude.C
The speed patch: redraw only rows that have changed
[lyx.git] / src / insets / insetinclude.C
index 2f3a0648960099a7de612cd212bffa631eadf870..5afd3f382273c667b96711b17879f2b54c472540 100644 (file)
@@ -20,7 +20,9 @@
 #include "cursor.h"
 #include "debug.h"
 #include "dispatchresult.h"
+#include "exporter.h"
 #include "funcrequest.h"
+#include "FuncStatus.h"
 #include "gettext.h"
 #include "LaTeXFeatures.h"
 #include "lyx_main.h"
 
 #include "insets/render_preview.h"
 
-#include "support/FileInfo.h"
 #include "support/filename.h"
 #include "support/filetools.h"
 #include "support/lstrings.h" // contains
 #include "support/lyxlib.h"
-#include "support/tostr.h"
+#include "support/convert.h"
 
 #include <boost/bind.hpp>
+#include <boost/filesystem/operations.hpp>
 
 #include "support/std_ostream.h"
-#include "support/std_sstream.h"
+
+#include <sstream>
 
 using lyx::support::AddName;
 using lyx::support::AbsolutePath;
@@ -56,11 +59,11 @@ using lyx::support::bformat;
 using lyx::support::ChangeExtension;
 using lyx::support::contains;
 using lyx::support::copy;
-using lyx::support::FileInfo;
 using lyx::support::FileName;
 using lyx::support::GetFileContents;
 using lyx::support::IsFileReadable;
 using lyx::support::IsLyXFilename;
+using lyx::support::latex_path;
 using lyx::support::MakeAbsPath;
 using lyx::support::MakeDisplayPath;
 using lyx::support::MakeRelPath;
@@ -76,6 +79,7 @@ using std::istringstream;
 using std::ostream;
 using std::ostringstream;
 
+namespace fs = boost::filesystem;
 
 extern BufferList bufferlist;
 
@@ -85,7 +89,7 @@ namespace {
 string const uniqueID()
 {
        static unsigned int seed = 1000;
-       return "file" + tostr(++seed);
+       return "file" + convert<string>(++seed);
 }
 
 } // namespace anon
@@ -105,7 +109,7 @@ InsetInclude::InsetInclude(InsetInclude const & other)
          params_(other.params_),
          include_label(other.include_label),
          preview_(new RenderMonitoredPreview(this)),
-         set_label_(other.set_label_)
+         set_label_(false)
 {
        preview_->fileChanged(boost::bind(&InsetInclude::fileChanged, this));
 }
@@ -117,17 +121,17 @@ InsetInclude::~InsetInclude()
 }
 
 
-void InsetInclude::priv_dispatch(LCursor & cur, FuncRequest & cmd)
+void InsetInclude::doDispatch(LCursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
                InsetCommandParams p;
                InsetIncludeMailer::string2params(cmd.argument, p);
-               if (!p.getCmdName().empty()) {
+               if (!p.getCmdName().empty())
                        set(p, cur.buffer());
-                       cur.bv().update();
-               }
+               else
+                       cur.noUpdate();
                break;
        }
 
@@ -136,21 +140,34 @@ void InsetInclude::priv_dispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_MOUSE_RELEASE:
-               if (button_.box().contains(cmd.x, cmd.y))
-                       InsetIncludeMailer(*this).showDialog(&cur.bv());
-               break;
-
        case LFUN_INSET_DIALOG_SHOW:
                InsetIncludeMailer(*this).showDialog(&cur.bv());
                break;
 
        default:
-               InsetOld::priv_dispatch(cur, cmd);
+               InsetBase::doDispatch(cur, cmd);
                break;
        }
 }
 
 
+bool InsetInclude::getStatus(LCursor & cur, FuncRequest const & cmd,
+               FuncStatus & flag) const
+{
+       switch (cmd.action) {
+
+       case LFUN_INSET_MODIFY:
+       case LFUN_INSET_DIALOG_UPDATE:
+       case LFUN_INSET_DIALOG_SHOW:
+               flag.enabled(true);
+               return true;
+
+       default:
+               return InsetBase::getStatus(cur, cmd, flag);
+       }
+}
+
+
 InsetCommandParams const & InsetInclude::params() const
 {
        return params_;
@@ -228,7 +245,7 @@ void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
 }
 
 
-auto_ptr<InsetBase> InsetInclude::clone() const
+auto_ptr<InsetBase> InsetInclude::doClone() const
 {
        return auto_ptr<InsetBase>(new InsetInclude(*this));
 }
@@ -243,7 +260,7 @@ void InsetInclude::write(Buffer const &, ostream & os) const
 void InsetInclude::write(ostream & os) const
 {
        os << "Include " << params_.getCommand() << '\n'
-          << "preview " << tostr(params_.preview()) << '\n';
+          << "preview " << convert<string>(params_.preview()) << '\n';
 }
 
 
@@ -296,8 +313,7 @@ bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
        Buffer * buf = bufferlist.getBuffer(included_file);
        if (!buf) {
                // the readonly flag can/will be wrong, not anymore I think.
-               FileInfo finfo(included_file);
-               if (!finfo.isOK())
+               if (!fs::exists(included_file))
                        return false;
                buf = bufferlist.newBuffer(included_file);
                if (!loadLyXFile(buf, included_file))
@@ -332,13 +348,15 @@ int InsetInclude::latex(Buffer const & buffer, ostream & os,
        }
 
        // write it to a file (so far the complete file)
-       string writefile = ChangeExtension(included_file, ".tex");
+       string const exportfile = ChangeExtension(incfile, ".tex");
+       string const mangled = FileName(ChangeExtension(included_file,
+                                                       ".tex")).mangledFilename();
+       string const writefile = MakeAbsPath(mangled, m_buffer->temppath());
 
-       if (!runparams.nice) {
-               incfile = FileName(writefile).mangledFilename();
-               writefile = MakeAbsPath(incfile, m_buffer->temppath());
-       }
+       if (!runparams.nice)
+               incfile = mangled;
        lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+       lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
        lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
 
        if (loadIfNeeded(buffer, params_)) {
@@ -366,7 +384,7 @@ int InsetInclude::latex(Buffer const & buffer, ostream & os,
                tmp->makeLaTeXFile(writefile,
                                   OnlyPath(masterFilename(buffer)),
                                   runparams, false);
-       } else if (!runparams.nice) {
+       } else {
                // Copy the file to the temp dir, so that .aux files etc.
                // are not created in the original dir. Files included by
                // this file will be found via input@path, see ../buffer.C.
@@ -386,21 +404,33 @@ int InsetInclude::latex(Buffer const & buffer, ostream & os,
        }
 
        if (isVerbatim(params_)) {
+               incfile = latex_path(incfile);
                os << '\\' << params_.getCmdName() << '{' << incfile << '}';
        } else if (type(params_) == INPUT) {
+               runparams.exportdata->addExternalFile("latex", writefile,
+                                                     exportfile);
+
                // \input wants file with extension (default is .tex)
                if (!IsLyXFilename(included_file)) {
+                       incfile = latex_path(incfile);
                        os << '\\' << params_.getCmdName() << '{' << incfile << '}';
                } else {
+               incfile = ChangeExtension(incfile, ".tex");
+               incfile = latex_path(incfile);
                        os << '\\' << params_.getCmdName() << '{'
-                          << ChangeExtension(incfile, ".tex")
+                          << incfile
                           <<  '}';
                }
        } else {
+               runparams.exportdata->addExternalFile("latex", writefile,
+                                                     exportfile);
+
                // \include don't want extension and demands that the
                // file really have .tex
+               incfile = ChangeExtension(incfile, string());
+               incfile = latex_path(incfile);
                os << '\\' << params_.getCmdName() << '{'
-                  << ChangeExtension(incfile, string())
+                  << incfile
                   << '}';
        }
 
@@ -428,31 +458,34 @@ int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os,
 
        string const included_file = includedFilename(buffer, params_);
 
+       // write it to a file (so far the complete file)
+       string const exportfile = ChangeExtension(incfile, ".sgml");
+       string writefile = ChangeExtension(included_file, ".sgml");
+
        if (loadIfNeeded(buffer, params_)) {
                Buffer * tmp = bufferlist.getBuffer(included_file);
 
-               // write it to a file (so far the complete file)
-               string writefile = ChangeExtension(included_file, ".sgml");
-
-               if (!runparams.nice) {
-                       incfile = FileName(writefile).mangledFilename();
-                       writefile = MakeAbsPath(incfile,
-                                               buffer.getMasterBuffer()->temppath());
-               }
+               writefile = MakeAbsPath(FileName(writefile).mangledFilename(),
+                                       buffer.getMasterBuffer()->temppath());
+               if (!runparams.nice)
+                       incfile = writefile;
 
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+               lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
 
-               OutputParams runp = runparams;
-               tmp->makeLinuxDocFile(writefile, runp, true);
+               tmp->makeLinuxDocFile(writefile, runparams, true);
        }
 
        if (isVerbatim(params_)) {
                os << "<![CDATA["
                   << GetFileContents(included_file)
                   << "]]>";
-       } else
+       } else {
+               runparams.exportdata->addExternalFile("linuxdoc", writefile,
+                                                     exportfile);
                os << '&' << include_label << ';';
+       }
 
        return 0;
 }
@@ -469,25 +502,31 @@ int InsetInclude::docbook(Buffer const & buffer, ostream & os,
 
        string const included_file = includedFilename(buffer, params_);
 
+       // write it to a file (so far the complete file)
+       string const exportfile = ChangeExtension(incfile, ".sgml");
+       string writefile = ChangeExtension(included_file, ".sgml");
+
        if (loadIfNeeded(buffer, params_)) {
                Buffer * tmp = bufferlist.getBuffer(included_file);
 
-               // write it to a file (so far the complete file)
-               string writefile = ChangeExtension(included_file, ".sgml");
-
-               if (!runparams.nice) {
-                       incfile = FileName(writefile).mangledFilename();
-                       writefile = MakeAbsPath(incfile,
-                                               buffer.getMasterBuffer()->temppath());
-               }
+               string const mangled = FileName(writefile).mangledFilename();
+               writefile = MakeAbsPath(mangled,
+                                       buffer.getMasterBuffer()->temppath());
+               if (!runparams.nice)
+                       incfile = mangled;
 
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
+               lyxerr[Debug::LATEX] << "exportfile:" << exportfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
 
-               OutputParams runp = runparams;
-               tmp->makeDocBookFile(writefile, runp, true);
+               tmp->makeDocBookFile(writefile, runparams, true);
        }
 
+       runparams.exportdata->addExternalFile("docbook", writefile,
+                                             exportfile);
+       runparams.exportdata->addExternalFile("docbook-xml", writefile,
+                                             exportfile);
+
        if (isVerbatim(params_)) {
                os << "<inlinegraphic fileref=\""
                   << '&' << include_label << ';'
@@ -585,16 +624,14 @@ void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
                if (!set_label_) {
                        set_label_ = true;
                        button_.update(getScreenLabel(*mi.base.bv->buffer()),
-                                      editable() != NOT_EDITABLE);
+                                      true);
                }
                button_.metrics(mi, dim);
        }
-       int center_indent = type(params_) == INPUT ?
-               0 : (mi.base.textwidth - dim.wid) / 2;
-       Box b(center_indent, center_indent + dim.wid, -dim.asc, dim.des);
+
+       Box b(0, dim.wid, -dim.asc, dim.des);
        button_.setBox(b);
 
-       dim.wid = mi.base.textwidth;
        dim_ = dim;
 }
 
@@ -606,18 +643,24 @@ void InsetInclude::draw(PainterInfo & pi, int x, int y) const
        BOOST_ASSERT(pi.base.bv && pi.base.bv->buffer());
 
        bool use_preview = false;
-       if (RenderPreview::status() == LyXRC::PREVIEW_OFF) {
+       if (RenderPreview::status() != LyXRC::PREVIEW_OFF) {
                lyx::graphics::PreviewImage const * pimage =
                        preview_->getPreviewImage(*pi.base.bv->buffer());
                use_preview = pimage && pimage->image();
        }
 
        if (use_preview)
-               preview_->draw(pi, x + button_.box().x1, y);
+               preview_->draw(pi, x, y);
        else
-               button_.draw(pi, x + button_.box().x1, y);
+               button_.draw(pi, x, y);
 }
 
+bool InsetInclude::display() const
+{
+       return type(params_) != INPUT;
+}
+
+
 
 //
 // preview stuff
@@ -662,7 +705,7 @@ void add_preview(RenderMonitoredPreview & renderer, InsetInclude const & inset,
                 Buffer const & buffer)
 {
        InsetCommandParams const & params = inset.params();
-       if (RenderPreview::status() == LyXRC::PREVIEW_OFF &&
+       if (RenderPreview::status() != LyXRC::PREVIEW_OFF &&
            preview_wanted(params, buffer)) {
                renderer.setAbsFile(includedFilename(buffer, params));
                string const snippet = latex_string(inset, buffer);
@@ -718,7 +761,7 @@ void InsetIncludeMailer::string2params(string const & in,
        string id;
        lex >> id;
        if (!lex || id != "Include")
-               return print_mailer_error("InsetBoxMailer", in, 2, "Include");
+               return print_mailer_error("InsetIncludeMailer", in, 2, "Include");
 
        InsetInclude inset(params);
        inset.read(lex);