]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetIndex.cpp
Routines for calculating numerical labels for BibTeX citations.
[lyx.git] / src / insets / InsetIndex.cpp
index 495b612e9ff1533a760d8cc970ac67eb36328f87..4fdbfc502ef028667e99fbcf04f76d88be367bdd 100644 (file)
@@ -14,6 +14,7 @@
 
 #include "Buffer.h"
 #include "BufferParams.h"
+#include "BufferView.h"
 #include "ColorSet.h"
 #include "DispatchResult.h"
 #include "Encoding.h"
@@ -23,6 +24,7 @@
 #include "LaTeXFeatures.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
+#include "output_latex.h"
 #include "sgml.h"
 #include "TocBackend.h"
 
@@ -47,14 +49,17 @@ namespace lyx {
 ///////////////////////////////////////////////////////////////////////
 
 
-InsetIndex::InsetIndex(Buffer const & buf, InsetIndexParams const & params)
+InsetIndex::InsetIndex(Buffer * buf, InsetIndexParams const & params)
        : InsetCollapsable(buf), params_(params)
 {}
 
 
 int InsetIndex::latex(odocstream & os,
-                     OutputParams const & runparams) const
+                     OutputParams const & runparams_in) const
 {
+       OutputParams runparams(runparams_in);
+       runparams.inIndexEntry = true;
+
        if (buffer().masterBuffer()->params().use_indices && !params_.index.empty()
            && params_.index != "idx") {
                os << "\\sindex[";
@@ -170,6 +175,20 @@ int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
 }
 
 
+docstring InsetIndex::xhtml(XHTMLStream &, OutputParams const &) const
+{
+       return docstring();
+}
+
+
+bool InsetIndex::showInsetDialog(BufferView * bv) const
+{
+       bv->showDialog("index", params2string(params_),
+                       const_cast<InsetIndex *>(this));
+       return true;
+}
+
+
 void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        switch (cmd.action) {
@@ -177,11 +196,18 @@ void InsetIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_INSET_MODIFY: {
                if (cmd.getArg(0) == "changetype") {
                        params_.index = from_utf8(cmd.getArg(1));
-                       setLayout(cur.buffer()->params());
                        break;
                }
+               InsetIndexParams params;
+               InsetIndex::string2params(to_utf8(cmd.argument()), params);
+               params_.index = params.index;
+               break;
        }
 
+       case LFUN_INSET_DIALOG_UPDATE:
+               cur.bv().updateDialog("index", params2string(params_));
+               break;
+
        default:
                InsetCollapsable::doDispatch(cur, cmd);
                break;
@@ -205,6 +231,14 @@ bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
                                from_utf8(cmd.getArg(1)) == params_.index);
                        return true;
                }
+               flag.setEnabled(true);
+               return true;
+
+       case LFUN_INSET_DIALOG_UPDATE: {
+               Buffer const & realbuffer = *buffer().masterBuffer();
+               flag.setEnabled(realbuffer.params().use_indices);
+               return true;
+       }
 
        default:
                return InsetCollapsable::getStatus(cur, cmd, flag);
@@ -212,13 +246,15 @@ bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
 }
 
 
-docstring const InsetIndex::buttonLabel(BufferView const & bv) const
+ColorCode InsetIndex::labelColor() const
 {
-       docstring s = _("Idx");
-       if (decoration() == InsetLayout::CLASSIC)
-               return isOpen(bv) ? s : getNewLabel(s);
-       else
-               return getNewLabel(s);
+       if (params_.index.empty() || params_.index == from_ascii("idx"))
+               return InsetCollapsable::labelColor();
+       // FIXME UNICODE
+       ColorCode c = lcolor.getFromLyXName(to_utf8(params_.index));
+       if (c == Color_none)
+               c = InsetCollapsable::labelColor();
+       return c;
 }
 
 
@@ -241,10 +277,7 @@ docstring InsetIndex::toolTip(BufferView const &, int, int) const
        odocstringstream ods;
        InsetText::plaintext(ods, rp);
        tip += ods.str();
-       // shorten it if necessary
-       if (tip.size() > 200)
-               tip = tip.substr(0, 200) + "...";
-       return tip;
+       return wrapParas(tip);
 }
 
 
@@ -263,6 +296,30 @@ void InsetIndex::read(Lexer & lex)
 }
 
 
+string InsetIndex::params2string(InsetIndexParams const & params)
+{
+       ostringstream data;
+       data << "index";
+       params.write(data);
+       return data.str();
+}
+
+
+void InsetIndex::string2params(string const & in, InsetIndexParams & params)
+{
+       params = InsetIndexParams();
+       if (in.empty())
+               return;
+
+       istringstream data(in);
+       Lexer lex;
+       lex.setStream(data);
+       lex.setContext("InsetIndex::string2params");
+       lex >> "index";
+       params.read(lex);
+}
+
+
 void InsetIndex::addToToc(DocIterator const & cpit)
 {
        DocIterator pit = cpit;
@@ -289,6 +346,21 @@ docstring InsetIndex::contextMenu(BufferView const &, int, int) const
 }
 
 
+bool InsetIndex::hasSettings() const
+{
+       return buffer().masterBuffer()->params().use_indices;
+}
+
+
+
+
+/////////////////////////////////////////////////////////////////////
+//
+// InsetIndexParams
+//
+///////////////////////////////////////////////////////////////////////
+
+
 void InsetIndexParams::write(ostream & os) const
 {
        os << ' ';
@@ -315,8 +387,8 @@ void InsetIndexParams::read(Lexer & lex)
 //
 ///////////////////////////////////////////////////////////////////////
 
-InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
-       : InsetCommand(p, "index_print")
+InsetPrintIndex::InsetPrintIndex(Buffer * buf, InsetCommandParams const & p)
+       : InsetCommand(buf, p, "index_print")
 {}
 
 
@@ -333,37 +405,113 @@ ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
 
 docstring InsetPrintIndex::screenLabel() const
 {
-       if ((!buffer().masterBuffer()->params().use_indices
+       bool const printall = suffixIs(getCmdName(), '*');
+       bool const multind = buffer().masterBuffer()->params().use_indices;
+       if ((!multind
             && getParam("type") == from_ascii("idx"))
-           || getParam("type").empty())
+           || (getParam("type").empty() && !printall))
                return _("Index");
        Buffer const & realbuffer = *buffer().masterBuffer();
        IndicesList const & indiceslist = realbuffer.params().indiceslist();
        Index const * index = indiceslist.findShortcut(getParam("type"));
-       if (!index)
+       if (!index && !printall)
                return _("Unknown index type!");
-       docstring res = index->index();
-       if (!buffer().masterBuffer()->params().use_indices)
+       docstring res = printall ? _("All indices") : index->index();
+       if (!multind)
                res += " (" + _("non-active") + ")";
+       else if (contains(getCmdName(), "printsubindex"))
+               res += " (" + _("subindex") + ")";
        return res;
 }
 
 
+bool InsetPrintIndex::isCompatibleCommand(string const & s)
+{
+       return s == "printindex" || s == "printsubindex"
+               || s == "printindex*" || s == "printsubindex*";
+}
+
+
+void InsetPrintIndex::doDispatch(Cursor & cur, FuncRequest & cmd)
+{
+       switch (cmd.action) {
+
+       case LFUN_INSET_MODIFY: {
+               if (cmd.argument() == from_ascii("toggle-subindex")) {
+                       string cmd = getCmdName();
+                       if (contains(cmd, "printindex"))
+                               cmd = subst(cmd, "printindex", "printsubindex");
+                       else
+                               cmd = subst(cmd, "printsubindex", "printindex");
+                       setCmdName(cmd);
+                       break;
+               } else if (cmd.argument() == from_ascii("check-printindex*")) {
+                       string cmd = getCmdName();
+                       if (suffixIs(cmd, '*'))
+                               break;
+                       cmd += '*';
+                       setParam("type", docstring());
+                       setCmdName(cmd);
+                       break;
+               }
+               InsetCommandParams p(INDEX_PRINT_CODE);
+               // FIXME UNICODE
+               InsetCommand::string2params("index_print",
+                       to_utf8(cmd.argument()), p);
+               if (p.getCmdName().empty()) {
+                       cur.noUpdate();
+                       break;
+               }
+               setParams(p);
+               break;
+       }
+
+       default:
+               InsetCommand::doDispatch(cur, cmd);
+               break;
+       }
+}
+
+
 bool InsetPrintIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
        FuncStatus & status) const
 {
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
-               InsetCommandParams p(INDEX_PRINT_CODE);
-               InsetCommand::string2params("index_print", to_utf8(cmd.argument()), p);
-               Buffer const & realbuffer = *buffer().masterBuffer();
-               IndicesList const & indiceslist = realbuffer.params().indiceslist();
-               Index const * index = indiceslist.findShortcut(p["type"]);
-               status.setEnabled(index != 0);
-               status.setOnOff(p["type"] == getParam("type"));
+               if (cmd.argument() == from_ascii("toggle-subindex")) {
+                       status.setEnabled(buffer().masterBuffer()->params().use_indices);
+                       status.setOnOff(contains(getCmdName(), "printsubindex"));
+                       return true;
+               } else if (cmd.argument() == from_ascii("check-printindex*")) {
+                       status.setEnabled(buffer().masterBuffer()->params().use_indices);
+                       status.setOnOff(suffixIs(getCmdName(), '*'));
+                       return true;
+               } if (cmd.getArg(0) == "index_print"
+                   && cmd.getArg(1) == "CommandInset") {
+                       InsetCommandParams p(INDEX_PRINT_CODE);
+                       InsetCommand::string2params("index_print",
+                               to_utf8(cmd.argument()), p);
+                       if (suffixIs(p.getCmdName(), '*')) {
+                               status.setEnabled(true);
+                               status.setOnOff(false);
+                               return true;
+                       }
+                       Buffer const & realbuffer = *buffer().masterBuffer();
+                       IndicesList const & indiceslist =
+                               realbuffer.params().indiceslist();
+                       Index const * index = indiceslist.findShortcut(p["type"]);
+                       status.setEnabled(index != 0);
+                       status.setOnOff(p["type"] == getParam("type"));
+                       return true;
+               } else
+                       return InsetCommand::getStatus(cur, cmd, status);
+       }
+       
+       case LFUN_INSET_DIALOG_UPDATE: {
+               status.setEnabled(buffer().masterBuffer()->params().use_indices);
                return true;
-       } 
+       }
 
        default:
                return InsetCommand::getStatus(cur, cmd, status);
@@ -397,4 +545,16 @@ docstring InsetPrintIndex::contextMenu(BufferView const &, int, int) const
                from_ascii("context-indexprint") : docstring();
 }
 
+
+bool InsetPrintIndex::hasSettings() const
+{
+       return buffer().masterBuffer()->params().use_indices;
+}
+
+
+docstring InsetPrintIndex::xhtml(XHTMLStream &, OutputParams const &) const
+{
+       return docstring();
+}
+
 } // namespace lyx