]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetInclude.cpp
Remove color dependency of framed note, fix bug 3598
[lyx.git] / src / insets / InsetInclude.cpp
index 01ee1745d7fe76dee5c907a0f51bfe19db57bfc8..8ec571d6051cc82678a94c4c5c8168ca633ec07d 100644 (file)
@@ -17,7 +17,7 @@
 #include "BufferList.h"
 #include "BufferParams.h"
 #include "BufferView.h"
-#include "LCursor.h"
+#include "Cursor.h"
 #include "debug.h"
 #include "DispatchResult.h"
 #include "Exporter.h"
 #include "LaTeXFeatures.h"
 #include "LyX.h"
 #include "LyXRC.h"
-#include "LyXLex.h"
+#include "Lexer.h"
 #include "MetricsInfo.h"
 #include "OutputParams.h"
 #include "TocBackend.h"
 
-#include "frontends/Alert.h"
+#include "frontends/alert.h"
 #include "frontends/Painter.h"
 
 #include "graphics/PreviewImage.h"
 #include "graphics/PreviewLoader.h"
 
 #include "insets/RenderPreview.h"
+#include "insets/InsetListingsParams.h"
 
 #include "support/filetools.h"
 #include "support/lstrings.h" // contains
@@ -61,6 +62,7 @@ using support::copy;
 using support::DocFileName;
 using support::FileName;
 using support::getFileContents;
+using support::getVectorFromString;
 using support::isFileReadable;
 using support::isLyXFilename;
 using support::latex_path;
@@ -69,6 +71,7 @@ using support::makeDisplayPath;
 using support::makeRelPath;
 using support::onlyFilename;
 using support::onlyPath;
+using support::prefixIs;
 using support::subst;
 using support::sum;
 
@@ -78,6 +81,7 @@ using std::auto_ptr;
 using std::istringstream;
 using std::ostream;
 using std::ostringstream;
+using std::vector;
 
 namespace Alert = frontend::Alert;
 namespace fs = boost::filesystem;
@@ -91,6 +95,12 @@ docstring const uniqueID()
        return "file" + convert<docstring>(++seed);
 }
 
+
+bool isListings(InsetCommandParams const & params)
+{
+       return params.getCmdName() == "lstinputlisting";
+}
+
 } // namespace anon
 
 
@@ -104,7 +114,7 @@ InsetInclude::InsetInclude(InsetCommandParams const & p)
 
 
 InsetInclude::InsetInclude(InsetInclude const & other)
-       : InsetOld(other),
+       : Inset(other),
          params_(other.params_),
          include_label(other.include_label),
          preview_(new RenderMonitoredPreview(this)),
@@ -120,7 +130,7 @@ InsetInclude::~InsetInclude()
 }
 
 
-void InsetInclude::doDispatch(LCursor & cur, FuncRequest & cmd)
+void InsetInclude::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
 
@@ -128,6 +138,17 @@ void InsetInclude::doDispatch(LCursor & cur, FuncRequest & cmd)
                InsetCommandParams p("include");
                InsetIncludeMailer::string2params(to_utf8(cmd.argument()), p);
                if (!p.getCmdName().empty()) {
+                       if (isListings(p)){
+                               InsetListingsParams par_old(params().getOptions());
+                               InsetListingsParams par_new(p.getOptions());
+                               if (par_old.getParamValue("label") !=
+                                   par_new.getParamValue("label")
+                                   && !par_new.getParamValue("label").empty())
+                                       cur.bv().buffer()->changeRefsIfUnique(
+                                               from_utf8(par_old.getParamValue("label")),
+                                               from_utf8(par_new.getParamValue("label")),
+                                               Inset::REF_CODE);
+                       }
                        set(p, cur.buffer());
                        cur.buffer().updateBibfilesCache();
                } else
@@ -144,13 +165,13 @@ void InsetInclude::doDispatch(LCursor & cur, FuncRequest & cmd)
                break;
 
        default:
-               InsetBase::doDispatch(cur, cmd);
+               Inset::doDispatch(cur, cmd);
                break;
        }
 }
 
 
-bool InsetInclude::getStatus(LCursor & cur, FuncRequest const & cmd,
+bool InsetInclude::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
        switch (cmd.action) {
@@ -161,7 +182,7 @@ bool InsetInclude::getStatus(LCursor & cur, FuncRequest const & cmd,
                return true;
 
        default:
-               return InsetBase::getStatus(cur, cmd, flag);
+               return Inset::getStatus(cur, cmd, flag);
        }
 }
 
@@ -179,7 +200,8 @@ enum Types {
        INCLUDE = 0,
        VERB = 1,
        INPUT = 2,
-       VERBAST = 3
+       VERBAST = 3,
+       LISTINGS = 4,
 };
 
 
@@ -193,6 +215,8 @@ Types type(InsetCommandParams const & params)
                return VERB;
        if  (command_name == "verbatiminput*")
                return VERBAST;
+       if  (command_name == "lstinputlisting")
+               return LISTINGS;
        return INCLUDE;
 }
 
@@ -243,9 +267,9 @@ void InsetInclude::set(InsetCommandParams const & p, Buffer const & buffer)
 }
 
 
-auto_ptr<InsetBase> InsetInclude::doClone() const
+auto_ptr<Inset> InsetInclude::doClone() const
 {
-       return auto_ptr<InsetBase>(new InsetInclude(*this));
+       return auto_ptr<Inset>(new InsetInclude(*this));
 }
 
 
@@ -262,13 +286,13 @@ void InsetInclude::write(ostream & os) const
 }
 
 
-void InsetInclude::read(Buffer const &, LyXLex & lex)
+void InsetInclude::read(Buffer const &, Lexer & lex)
 {
        read(lex);
 }
 
 
-void InsetInclude::read(LyXLex & lex)
+void InsetInclude::read(Lexer & lex)
 {
        if (lex.isOK()) {
                lex.eatLine();
@@ -294,22 +318,25 @@ void InsetInclude::read(LyXLex & lex)
 }
 
 
-docstring const InsetInclude::getScreenLabel(Buffer const &) const
+docstring const InsetInclude::getScreenLabel(Buffer const & buf) const
 {
        docstring temp;
 
        switch (type(params_)) {
                case INPUT:
-                       temp += _("Input");
+                       temp += buf.B_("Input");
                        break;
                case VERB:
-                       temp += _("Verbatim Input");
+                       temp += buf.B_("Verbatim Input");
                        break;
                case VERBAST:
-                       temp += _("Verbatim Input*");
+                       temp += buf.B_("Verbatim Input*");
                        break;
                case INCLUDE:
-                       temp += _("Include");
+                       temp += buf.B_("Include");
+                       break;
+               case LISTINGS:
+                       temp += buf.B_("Program Listing");
                        break;
        }
 
@@ -329,7 +356,7 @@ namespace {
 /// return the child buffer if the file is a LyX doc and is loaded
 Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params)
 {
-       if (isVerbatim(params))
+       if (isVerbatim(params) || isListings(params))
                return 0;
 
        string const included_file = includedFilename(buffer, params).absFilename();
@@ -343,7 +370,7 @@ Buffer * getChildBuffer(Buffer const & buffer, InsetCommandParams const & params
 /// return true if the file is or got loaded.
 bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
 {
-       if (isVerbatim(params))
+       if (isVerbatim(params) || isListings(params))
                return false;
 
        FileName const included_file = includedFilename(buffer, params);
@@ -412,8 +439,8 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
                                                "has textclass `%2$s'\n"
                                                             "while parent file has textclass `%3$s'."),
                                              makeDisplayPath(included_file.absFilename()),
-                                             from_utf8(tmp->params().getLyXTextClass().name()),
-                                             from_utf8(m_buffer->params().getLyXTextClass().name()));
+                                             from_utf8(tmp->params().getTextClass().name()),
+                                             from_utf8(m_buffer->params().getTextClass().name()));
                        Alert::warning(_("Different textclasses"), text);
                        //return 0;
                }
@@ -478,6 +505,14 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
                        os << '\\' << from_ascii(params_.getCmdName())
                           << '{' << from_utf8(incfile) <<  '}';
                }
+       } else if (type(params_) == LISTINGS) {
+               os << '\\' << from_ascii(params_.getCmdName());
+               string opt = params_.getOptions();
+               // opt is set in QInclude dialog and should have passed validation.
+               InsetListingsParams params(opt);
+               if (!params.params().empty())
+                       os << "[" << from_utf8(params.encodedString()) << "]";
+               os << '{'  << from_utf8(incfile) << '}';
        } else {
                runparams.exportdata->addExternalFile(tex_format, writefile,
                                                      exportfile);
@@ -498,7 +533,7 @@ int InsetInclude::latex(Buffer const & buffer, odocstream & os,
 int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
                             OutputParams const &) const
 {
-       if (isVerbatim(params_)) {
+       if (isVerbatim(params_) || isListings(params_)) {
                os << '[' << getScreenLabel(buffer) << '\n';
                // FIXME: We don't know the encoding of the file
                docstring const str =
@@ -550,7 +585,7 @@ int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
        runparams.exportdata->addExternalFile("docbook-xml", writefile,
                                              exportfile);
 
-       if (isVerbatim(params_)) {
+       if (isVerbatim(params_) || isListings(params_)) {
                os << "<inlinegraphic fileref=\""
                   << '&' << include_label << ';'
                   << "\" format=\"linespecific\">";
@@ -575,7 +610,7 @@ void InsetInclude::validate(LaTeXFeatures & features) const
        else
                writefile = included_file;
 
-       if (!features.runparams().nice && !isVerbatim(params_)) {
+       if (!features.runparams().nice && !isVerbatim(params_) && !isListings(params_)) {
                incfile = DocFileName(writefile).mangledFilename();
                writefile = makeAbsPath(incfile,
                                        buffer.getMasterBuffer()->temppath()).absFilename();
@@ -585,6 +620,8 @@ void InsetInclude::validate(LaTeXFeatures & features) const
 
        if (isVerbatim(params_))
                features.require("verbatim");
+       else if (isListings(params_))
+               features.require("listings");
 
        // Here we must do the fun stuff...
        // Load the file in the include if it needs
@@ -607,7 +644,13 @@ void InsetInclude::validate(LaTeXFeatures & features) const
 void InsetInclude::getLabelList(Buffer const & buffer,
                                std::vector<docstring> & list) const
 {
-       if (loadIfNeeded(buffer, params_)) {
+       if (isListings(params_)) {
+               InsetListingsParams params(params_.getOptions());
+               string label = params.getParamValue("label");
+               if (!label.empty())
+                       list.push_back(from_utf8(label));
+       }
+       else if (loadIfNeeded(buffer, params_)) {
                string const included_file = includedFilename(buffer, params_).absFilename();
                Buffer * tmp = theBufferList().getBuffer(included_file);
                tmp->setParentName("");
@@ -706,9 +749,10 @@ void InsetInclude::draw(PainterInfo & pi, int x, int y) const
                button_.draw(pi, x, y);
 }
 
-bool InsetInclude::display() const
+
+Inset::DisplayType InsetInclude::display() const
 {
-       return type(params_) != INPUT;
+       return type(params_) == INPUT ? Inline : AlignCenter;
 }
 
 
@@ -826,7 +870,7 @@ void InsetIncludeMailer::string2params(string const & in,
                return;
 
        istringstream data(in);
-       LyXLex lex(0,0);
+       Lexer lex(0,0);
        lex.setStream(data);
 
        string name;
@@ -835,7 +879,7 @@ void InsetIncludeMailer::string2params(string const & in,
                return print_mailer_error("InsetIncludeMailer", in, 1, name_);
 
        // This is part of the inset proper that is usually swallowed
-       // by LyXText::readInset
+       // by Text::readInset
        string id;
        lex >> id;
        if (!lex || id != "Include")