]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetinclude.C
Enable convertDefault.sh to run even if its executable bit is not set.
[lyx.git] / src / insets / insetinclude.C
index 90ed386307fe34a2871ed650d723084021360a02..7e1af317bb7dacbda3fa09b1a6df5150e1f5c601 100644 (file)
@@ -5,21 +5,26 @@
  *
  * \author Lars Gullik Bjønnes
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
-#include <config.h>
 
+#include <config.h>
 
 #include "insetinclude.h"
 #include "buffer.h"
+#include "buffer_funcs.h"
 #include "bufferlist.h"
 #include "BufferView.h"
 #include "debug.h"
 #include "funcrequest.h"
 #include "gettext.h"
 #include "LaTeXFeatures.h"
+#include "latexrunparams.h"
+#include "Lsstream.h"
 #include "lyxlex.h"
 #include "lyxrc.h"
+#include "metricsinfo.h"
+#include "dimension.h"
 
 #include "frontends/Dialogs.h"
 #include "frontends/LyXView.h"
@@ -28,7 +33,8 @@
 #include "support/filetools.h"
 #include "support/FileInfo.h"
 #include "support/FileMonitor.h"
-#include "support/lstrings.h"
+#include "support/lstrings.h" // contains
+#include "support/tostr.h"
 
 #include "graphics/PreviewedInset.h"
 #include "graphics/PreviewImage.h"
 
 #include <cstdlib>
 
+using namespace lyx::support;
 
 using std::ostream;
 using std::endl;
 using std::vector;
 using std::pair;
+using std::auto_ptr;
+
 
 extern BufferList bufferlist;
 
 
-class InsetInclude::PreviewImpl : public grfx::PreviewedInset {
+class InsetInclude::PreviewImpl : public lyx::graphics::PreviewedInset {
 public:
        ///
        PreviewImpl(InsetInclude & p) : PreviewedInset(p) {}
@@ -80,12 +89,7 @@ namespace {
 string const uniqueID()
 {
        static unsigned int seed = 1000;
-
-       ostringstream ost;
-       ost << "file" << ++seed;
-
-       // Needed if we use lyxstring.
-       return STRCONV(ost.str());
+       return "file" + tostr(++seed);
 }
 
 } // namespace anon
@@ -93,19 +97,30 @@ string const uniqueID()
 
 InsetInclude::InsetInclude(Params const & p)
        : params_(p), include_label(uniqueID()),
-         preview_(new PreviewImpl(*this))
+         preview_(new PreviewImpl(*this)),
+         set_label_(false)
 {}
 
 
 InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & b)
        : include_label(uniqueID()),
-         preview_(new PreviewImpl(*this))
+         preview_(new PreviewImpl(*this)),
+         set_label_(false)
 {
        params_.cparams = p;
        params_.masterFilename_ = b.fileName();
 }
 
 
+InsetInclude::InsetInclude(InsetInclude const & other)
+       : InsetOld(other),
+         params_(other.params_),
+         include_label(other.include_label),
+         preview_(new PreviewImpl(*this)),
+         set_label_(other.set_label_)
+{}
+
+
 InsetInclude::~InsetInclude()
 {
        InsetIncludeMailer mailer(*this);
@@ -115,38 +130,31 @@ InsetInclude::~InsetInclude()
 
 dispatch_result InsetInclude::localDispatch(FuncRequest const & cmd)
 {
-       dispatch_result result = UNDISPATCHED;
-
        switch (cmd.action) {
+
        case LFUN_INSET_MODIFY: {
                InsetInclude::Params p;
                InsetIncludeMailer::string2params(cmd.argument, p);
-               if (p.cparams.getCmdName().empty())
-                       break;
-
-               set(p);
-               params_.masterFilename_ = cmd.view()->buffer()->fileName();
-
-               cmd.view()->updateInset(this);
-               result = DISPATCHED;
+               if (!p.cparams.getCmdName().empty()) {
+                       set(p);
+                       params_.masterFilename_ = cmd.view()->buffer()->fileName();
+                       cmd.view()->updateInset(this);
+               }
+               return DISPATCHED;
        }
-       break;
 
-       case LFUN_INSET_DIALOG_UPDATE: {
-               InsetIncludeMailer mailer(*this);
-               mailer.updateDialog(cmd.view());
-       }
-       break;
+       case LFUN_INSET_DIALOG_UPDATE:
+               InsetIncludeMailer(*this).updateDialog(cmd.view());
+               return DISPATCHED;
 
        case LFUN_MOUSE_RELEASE:
-               edit(cmd.view(), cmd.x, cmd.y, cmd.button());
-               break;
+       case LFUN_INSET_EDIT:
+               InsetIncludeMailer(*this).showDialog(cmd.view());
+               return DISPATCHED;
 
        default:
-               break;
+               return InsetOld::localDispatch(cmd);
        }
-
-       return result;
 }
 
 
@@ -158,11 +166,8 @@ InsetInclude::Params const & InsetInclude::params() const
 
 bool InsetInclude::Params::operator==(Params const & o) const
 {
-       if (cparams == o.cparams && flag == o.flag &&
-           masterFilename_ == o.masterFilename_)
-               return true;
-
-       return false;
+       return cparams == o.cparams && flag == o.flag &&
+           masterFilename_ == o.masterFilename_;
 }
 
 
@@ -198,41 +203,41 @@ void InsetInclude::set(Params const & p)
        if (preview_->monitoring())
                preview_->stopMonitoring();
 
-       if (grfx::PreviewedInset::activated() && params_.flag == INPUT)
+       if (lyx::graphics::PreviewedInset::activated() && params_.flag == INPUT)
                preview_->generatePreview();
 }
 
 
-Inset * InsetInclude::clone(Buffer const & buffer, bool) const
+auto_ptr<InsetBase> InsetInclude::clone() const
 {
-       Params p(params_);
-       p.masterFilename_ = buffer.fileName();
+       //Params p(params_);
+       //p.masterFilename_ = buffer.fileName();
+#warning FIXME: broken cross-doc copy/paste - must fix
 
-       return new InsetInclude(p);
+       return auto_ptr<InsetBase>(new InsetInclude(params_));
 }
 
 
-void InsetInclude::edit(BufferView * bv, int, int, mouse_button::state)
+void InsetInclude::write(Buffer const &, ostream & os) const
 {
-       InsetIncludeMailer mailer(*this);
-       mailer.showDialog(bv);
+       write(os);
 }
 
 
-void InsetInclude::edit(BufferView * bv, bool)
+void InsetInclude::write(ostream & os) const
 {
-       edit(bv, 0, 0, mouse_button::none);
+       os << "Include " << params_.cparams.getCommand() << '\n'
+          << "preview " << tostr(params_.cparams.preview()) << '\n';
 }
 
 
-void InsetInclude::write(Buffer const *, ostream & os) const
+void InsetInclude::read(Buffer const &, LyXLex & lex)
 {
-       os << "Include " << params_.cparams.getCommand() << '\n'
-          << "preview " << tostr(params_.cparams.preview()) << '\n';
+       read(lex);
 }
 
 
-void InsetInclude::read(Buffer const *, LyXLex & lex)
+void InsetInclude::read(LyXLex & lex)
 {
        params_.cparams.read(lex);
 
@@ -249,13 +254,7 @@ void InsetInclude::read(Buffer const *, LyXLex & lex)
 }
 
 
-bool InsetInclude::display() const
-{
-       return !(params_.flag == INPUT);
-}
-
-
-string const InsetInclude::getScreenLabel(Buffer const *) const
+string const InsetInclude::getScreenLabel(Buffer const &) const
 {
        string temp;
 
@@ -305,13 +304,13 @@ bool InsetInclude::loadIfNeeded() const
        FileInfo finfo(getFileName());
        if (!finfo.isOK())
                return false;
-
-       return bufferlist.loadLyXFile(getFileName(), false) != 0;
+       return loadLyXFile(bufferlist.newBuffer(getFileName()),
+                          getFileName());
 }
 
 
-int InsetInclude::latex(Buffer const * buffer, ostream & os,
-                       bool /*fragile*/, bool /*fs*/) const
+int InsetInclude::latex(Buffer const & buffer, ostream & os,
+                       LatexRunParams const & runparams) const
 {
        string incfile(params_.cparams.getContents());
 
@@ -323,13 +322,13 @@ int InsetInclude::latex(Buffer const * buffer, ostream & os,
                Buffer * tmp = bufferlist.getBuffer(getFileName());
 
                // FIXME: this should be a GUI warning
-               if (tmp->params.textclass != buffer->params.textclass) {
+               if (tmp->params.textclass != buffer.params.textclass) {
                        lyxerr << "WARNING: Included file `"
                               << MakeDisplayPath(getFileName())
                               << "' has textclass `"
                               << tmp->params.getLyXTextClass().name()
                               << "' while parent file has textclass `"
-                              << buffer->params.getLyXTextClass().name()
+                              << buffer.params.getLyXTextClass().name()
                               << "'." << endl;
                        //return 0;
                }
@@ -337,24 +336,22 @@ int InsetInclude::latex(Buffer const * buffer, ostream & os,
                // write it to a file (so far the complete file)
                string writefile = ChangeExtension(getFileName(), ".tex");
 
-               if (!buffer->tmppath.empty()
-                   && !buffer->niceFile) {
+               if (!buffer.tmppath.empty() && !runparams.nice) {
                        incfile = subst(incfile, '/','@');
 #ifdef __EMX__
                        incfile = subst(incfile, ':', '$');
 #endif
-                       writefile = AddName(buffer->tmppath, incfile);
+                       writefile = AddName(buffer.tmppath, incfile);
                } else
                        writefile = getFileName();
                writefile = ChangeExtension(writefile, ".tex");
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
 
-               tmp->markDepClean(buffer->tmppath);
+               tmp->markDepClean(buffer.tmppath);
 
-               tmp->makeLaTeXFile(writefile,
-                                  OnlyPath(getMasterFilename()),
-                                  buffer->niceFile, true);
+               tmp->makeLaTeXFile(writefile, OnlyPath(getMasterFilename()),
+                                  runparams, false);
        }
 
        if (isVerbatim()) {
@@ -380,7 +377,7 @@ int InsetInclude::latex(Buffer const * buffer, ostream & os,
 }
 
 
-int InsetInclude::ascii(Buffer const *, ostream & os, int) const
+int InsetInclude::ascii(Buffer const &, ostream & os, int) const
 {
        if (isVerbatim())
                os << GetFileContents(getFileName());
@@ -388,7 +385,7 @@ int InsetInclude::ascii(Buffer const *, ostream & os, int) const
 }
 
 
-int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
+int InsetInclude::linuxdoc(Buffer const & buffer, ostream & os) const
 {
        string incfile(params_.cparams.getContents());
 
@@ -401,9 +398,9 @@ int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
 
                // write it to a file (so far the complete file)
                string writefile = ChangeExtension(getFileName(), ".sgml");
-               if (!buffer->tmppath.empty() && !buffer->niceFile) {
+               if (!buffer.tmppath.empty() && !buffer.niceFile) {
                        incfile = subst(incfile, '/','@');
-                       writefile = AddName(buffer->tmppath, incfile);
+                       writefile = AddName(buffer.tmppath, incfile);
                } else
                        writefile = getFileName();
 
@@ -413,7 +410,7 @@ int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
 
-               tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
+               tmp->makeLinuxDocFile(writefile, buffer.niceFile, true);
        }
 
        if (isVerbatim()) {
@@ -427,7 +424,7 @@ int InsetInclude::linuxdoc(Buffer const * buffer, ostream & os) const
 }
 
 
-int InsetInclude::docbook(Buffer const * buffer, ostream & os,
+int InsetInclude::docbook(Buffer const & buffer, ostream & os,
                          bool /*mixcont*/) const
 {
        string incfile(params_.cparams.getContents());
@@ -441,9 +438,9 @@ int InsetInclude::docbook(Buffer const * buffer, ostream & os,
 
                // write it to a file (so far the complete file)
                string writefile = ChangeExtension(getFileName(), ".sgml");
-               if (!buffer->tmppath.empty() && !buffer->niceFile) {
+               if (!buffer.tmppath.empty() && !buffer.niceFile) {
                        incfile = subst(incfile, '/','@');
-                       writefile = AddName(buffer->tmppath, incfile);
+                       writefile = AddName(buffer.tmppath, incfile);
                } else
                        writefile = getFileName();
                if (IsLyXFilename(getFileName()))
@@ -452,7 +449,7 @@ int InsetInclude::docbook(Buffer const * buffer, ostream & os,
                lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
                lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
 
-               tmp->makeDocBookFile(writefile, buffer->niceFile, true);
+               tmp->makeDocBookFile(writefile, buffer.niceFile, true);
        }
 
        if (isVerbatim()) {
@@ -472,11 +469,11 @@ void InsetInclude::validate(LaTeXFeatures & features) const
        string incfile(params_.cparams.getContents());
        string writefile;
 
-       Buffer const * const b = bufferlist.getBuffer(getMasterFilename());
+       Buffer const & b = *bufferlist.getBuffer(getMasterFilename());
 
-       if (b && !b->tmppath.empty() && !b->niceFile && !isVerbatim()) {
+       if (!b.tmppath.empty() && !b.niceFile && !isVerbatim()) {
                incfile = subst(incfile, '/','@');
-               writefile = AddName(b->tmppath, incfile);
+               writefile = AddName(b.tmppath, incfile);
        } else
                writefile = getFileName();
 
@@ -495,30 +492,25 @@ void InsetInclude::validate(LaTeXFeatures & features) const
                // a file got loaded
                Buffer * const tmp = bufferlist.getBuffer(getFileName());
                if (tmp) {
-                       if (b)
-                               tmp->niceFile = b->niceFile;
+                       tmp->niceFile = b.niceFile;
                        tmp->validate(features);
                }
        }
 }
 
 
-vector<string> const InsetInclude::getLabelList() const
+void InsetInclude::getLabelList(std::vector<string> & list) const
 {
-       vector<string> l;
-
        if (loadIfNeeded()) {
                Buffer * tmp = bufferlist.getBuffer(getFileName());
                tmp->setParentName("");
-               l = tmp->getLabelList();
+               tmp->getLabelList(list);
                tmp->setParentName(getMasterFilename());
        }
-
-       return l;
 }
 
 
-void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
+void InsetInclude::fillWithBibKeys(std::vector<std::pair<string,string> > & keys) const
 {
        if (loadIfNeeded()) {
                Buffer * tmp = bufferlist.getBuffer(getFileName());
@@ -529,49 +521,48 @@ void InsetInclude::fillWithBibKeys(vector<pair<string,string> > & keys) const
 }
 
 
-int InsetInclude::ascent(BufferView * bv, LyXFont const & font) const
-{
-       return preview_->previewReady() ?
-               preview_->pimage()->ascent() : InsetButton::ascent(bv, font);
-}
-
-
-int InsetInclude::descent(BufferView * bv, LyXFont const & font) const
-{
-       return preview_->previewReady() ?
-               preview_->pimage()->descent() : InsetButton::descent(bv, font);
-}
-
-
-int InsetInclude::width(BufferView * bv, LyXFont const & font) const
+void InsetInclude::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       return preview_->previewReady() ?
-               preview_->pimage()->width() : InsetButton::width(bv, font);
+       if (preview_->previewReady()) {
+               dim.asc = preview_->pimage()->ascent();
+               dim.des = preview_->pimage()->descent();
+               dim.wid = preview_->pimage()->width();
+       } else {
+               if (!set_label_) {
+                       set_label_ = true;
+                       button_.update(getScreenLabel(*mi.base.bv->buffer()),
+                                      editable() != NOT_EDITABLE);
+               }
+               button_.metrics(mi, dim);
+       }
+       if (params_.flag == INPUT)
+               center_indent_ = 0;
+       else
+               center_indent_ = (mi.base.textwidth - dim.wid) / 2;
+       dim.wid = mi.base.textwidth;
+       dim_ = dim;
 }
 
 
-void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
-                       float & xx) const
+void InsetInclude::draw(PainterInfo & pi, int x, int y) const
 {
-       cache(bv);
+       cache(pi.base.bv);
        if (!preview_->previewReady()) {
-               InsetButton::draw(bv, font, y, xx);
+               button_.draw(pi, x + center_indent_, y);
                return;
        }
 
        if (!preview_->monitoring())
                preview_->startMonitoring();
 
-       int const x = int(xx);
-       int const w = width(bv, font);
-       int const d = descent(bv, font);
-       int const a = ascent(bv, font);
-       int const h = a + d;
-
-       bv->painter().image(x, y - a, w, h,
+       pi.pain.image(x + center_indent_, y - dim_.asc, dim_.wid, dim_.height(),
                            *(preview_->pimage()->image()));
+}
+
 
-       xx += w;
+BufferView * InsetInclude::view() const
+{
+       return button_.view();
 }
 
 
@@ -579,7 +570,7 @@ void InsetInclude::draw(BufferView * bv, LyXFont const & font, int y,
 // preview stuff
 //
 
-void InsetInclude::addPreview(grfx::PreviewLoader & ploader) const
+void InsetInclude::addPreview(lyx::graphics::PreviewLoader & ploader) const
 {
        preview_->addPreview(ploader);
 }
@@ -599,7 +590,9 @@ string const InsetInclude::PreviewImpl::latexString() const
                return string();
 
        ostringstream os;
-       parent().latex(view()->buffer(), os, false, false);
+       LatexRunParams runparams;
+       runparams.flavor = LatexRunParams::LATEX;
+       parent().latex(*view()->buffer(), os, runparams);
 
        return STRCONV(os.str());
 }
@@ -630,7 +623,7 @@ InsetIncludeMailer::InsetIncludeMailer(InsetInclude & inset)
 {}
 
 
-string const InsetIncludeMailer::inset2string() const
+string const InsetIncludeMailer::inset2string(Buffer const &) const
 {
        return params2string(inset_.params());
 }
@@ -643,8 +636,8 @@ void InsetIncludeMailer::string2params(string const & in,
 
        if (in.empty())
                return;
-       
-       istringstream data(in);
+
+       istringstream data(STRCONV(in));
        LyXLex lex(0,0);
        lex.setStream(data);
 
@@ -666,7 +659,7 @@ void InsetIncludeMailer::string2params(string const & in,
 
        if (lex.isOK()) {
                InsetInclude inset(params);
-               inset.read(0, lex);
+               inset.read(lex);
                params = inset.params();
        }
 }
@@ -679,8 +672,7 @@ InsetIncludeMailer::params2string(InsetInclude::Params const & params)
        inset.set(params);
        ostringstream data;
        data << name_ << ' ';
-       inset.write(0, data);
+       inset.write(data);
        data << "\\end_inset\n";
-
-       return data.str();
+       return STRCONV(data.str());
 }