]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetIndex.cpp
Inset::addToToc(): change signature. Use DocIterator instead of ParConstIterator...
[lyx.git] / src / insets / InsetIndex.cpp
index 13537a0dfdc1c1285336ea9b2a9310384e9074e9..9733467679e0d8c2ba355f15abc659ca76f6e8f9 100644 (file)
 
 #include "InsetIndex.h"
 
+#include "Buffer.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "gettext.h"
 #include "LaTeXFeatures.h"
 #include "MetricsInfo.h"
 #include "sgml.h"
+#include "TocBackend.h"
 
-#include "support/std_ostream.h"
+#include "support/docstream.h"
+#include "support/gettext.h"
+#include "support/lstrings.h"
 
+#include <ostream>
 
-namespace lyx {
-
-using std::string;
-using std::ostream;
+using namespace std;
+using namespace lyx::support;
 
+namespace lyx {
 
-InsetIndex::InsetIndex(BufferParams const & bp)
-       : InsetCollapsable(bp)
-{
-       setLayout(bp);
-}
+/////////////////////////////////////////////////////////////////////
+//
+// InsetIndex
+//
+///////////////////////////////////////////////////////////////////////
 
 
-InsetIndex::InsetIndex(InsetIndex const & in)
-       : InsetCollapsable(in)
+InsetIndex::InsetIndex(Buffer const & buf)
+       : InsetCollapsable(buf)
 {}
 
 
-CommandInfo const * InsetIndex::findInfo(std::string const & /* cmdName */)
+int InsetIndex::latex(odocstream & os,
+                         OutputParams const & runparams) const
 {
-       static const char * const paramnames[] = {"name", ""};
-       static const bool isoptional[] = {false};
-       static const CommandInfo info = {1, paramnames, isoptional};
-       return &info;
+       os << "\\index";
+       os << '{';
+       odocstringstream ods;
+       int i = InsetText::latex(ods, runparams);
+       bool sorted = false;
+       // correctly sort macros and formatted strings
+       // if we do find a command, prepend a plain text
+       // version of the content to get sorting right,
+       // e.g. \index{LyX@\LyX}, \index{text@\textbf{text}}
+       // Don't do that if the user entered '@' himself, though.
+       if (contains(ods.str(), '\\') && !contains(ods.str(), '@')) {
+               odocstringstream odss;
+               if (InsetText::plaintext(odss, runparams) > 0) {
+                       // remove remaining \'s for the sorting part
+                       os << subst(odss.str(), from_ascii("\\"), docstring());
+                       os << '@';
+                       sorted = true;
+               }
+       }
+       // if a hierarchy tag '!' is used, ommit this in the post-@ part.
+       if (sorted && contains(ods.str(), '!')) {
+               string dummy;
+               // FIXME unicode
+               os << from_utf8(rsplit(to_utf8(ods.str()), dummy, '!'));
+       } else
+               i = InsetText::latex(os, runparams);
+       os << '}';
+       return i;
 }
 
 
-int InsetIndex::docbook(Buffer const & buf, odocstream & os,
-                       OutputParams const & runparams) const
+int InsetIndex::docbook(odocstream & os, OutputParams const & runparams) const
 {
        os << "<indexterm><primary>";
-       int const i = InsetText::docbook(buf, os, runparams);
+       int const i = InsetText::docbook(os, runparams);
        os << "</primary></indexterm>";
        return i;
 }
 
 
-Inset * InsetIndex::clone() const
-{
-       return new InsetIndex(*this);
-}
-
-
-void InsetIndex::write(Buffer const & buf, std::ostream & os) const
+void InsetIndex::write(ostream & os) const
 {
        os << to_utf8(name()) << "\n";
-       InsetCollapsable::write(buf, os);
-}
-
-
-void InsetIndex::metrics(MetricsInfo & mi, Dimension & dim) const
-{
-       Font tmpfont = mi.base.font;
-       getDrawFont(mi.base.font);
-       mi.base.font.realize(tmpfont);
-       InsetCollapsable::metrics(mi, dim);
-       mi.base.font = tmpfont;
-}
-
-
-void InsetIndex::draw(PainterInfo & pi, int x, int y) const
-{
-       Font tmpfont = pi.base.font;
-       getDrawFont(pi.base.font);
-       pi.base.font.realize(tmpfont);
-       InsetCollapsable::draw(pi, x, y);
-       pi.base.font = tmpfont;
+       InsetCollapsable::write(os);
 }
 
 
-void InsetIndex::getDrawFont(Font & font) const
+void InsetIndex::addToToc(DocIterator const & cpit)
 {
-       font = Font(Font::ALL_INHERIT);
-       font.realize(layout_.font);
-}
-
+       DocIterator pit = cpit;
+       pit.push_back(CursorSlice(*this));
 
-bool InsetIndex::getStatus(Cursor & cur, FuncRequest const & cmd,
-       FuncStatus & status) const
-{
-       switch (cmd.action) {
-               // paragraph breaks not allowed
-               case LFUN_BREAK_PARAGRAPH:
-               case LFUN_BREAK_PARAGRAPH_SKIP:
-                       status.enabled(false);
-                       return true;
-
-               default:
-                       return InsetCollapsable::getStatus(cur, cmd, status);
-               }
+       Toc & toc = buffer().tocBackend().toc("index");
+       docstring str;
+       str = getNewLabel(str);
+       toc.push_back(TocItem(pit, 0, str));
 }
 
 
+/////////////////////////////////////////////////////////////////////
+//
+// InsetPrintIndex
+//
+///////////////////////////////////////////////////////////////////////
 
 InsetPrintIndex::InsetPrintIndex(InsetCommandParams const & p)
        : InsetCommand(p, string())
 {}
 
 
-CommandInfo const * InsetPrintIndex::findInfo(std::string const & /* cmdName */)
+ParamInfo const & InsetPrintIndex::findInfo(string const & /* cmdName */)
 {
-       static const char * const paramnames[] = {"name", ""};
-       static const bool isoptional[] = {false};
-       static const CommandInfo info = {1, paramnames, isoptional};
-       return &info;
+       static ParamInfo param_info_;
+       if (param_info_.empty())
+               param_info_.add("name", ParamInfo::LATEX_REQUIRED);
+       return param_info_;
 }
 
 
-docstring const InsetPrintIndex::getScreenLabel(Buffer const &) const
+docstring InsetPrintIndex::screenLabel() const
 {
        return _("Index");
 }
@@ -147,5 +142,4 @@ InsetCode InsetPrintIndex::lyxCode() const
        return INDEX_PRINT_CODE;
 }
 
-
 } // namespace lyx