3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
16 #include "BufferParams.h"
18 #include "DispatchResult.h"
20 #include "FuncRequest.h"
22 #include "LaTeXFeatures.h"
23 #include "OutputParams.h"
24 #include "output_xhtml.h"
25 #include "Paragraph.h"
26 #include "ParagraphParameters.h"
27 #include "TextClass.h"
28 #include "TocBackend.h"
30 #include "support/debug.h"
31 #include "support/gettext.h"
40 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
41 : InsetCommand(buf, p)
45 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
47 static ParamInfo param_info_;
48 if (param_info_.empty()) {
49 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
55 docstring InsetTOC::screenLabel() const
57 if (getCmdName() == "tableofcontents")
58 return buffer().B_("Table of Contents");
59 return _("Unknown TOC type");
63 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
64 switch (cmd.action()) {
65 case LFUN_MOUSE_RELEASE:
66 if (!cur.selection() && cmd.button() == mouse_button::button1) {
67 showInsetDialog(&cur.bv());
73 InsetCommand::doDispatch(cur, cmd);
78 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
80 os << screenLabel() << "\n\n";
81 buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
82 return PLAINTEXT_NEWLINE;
86 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
88 if (getCmdName() == "tableofcontents")
94 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
96 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
97 string const & tocclass = lay.defaultCSSClass();
98 string const tocattr = "class='tochead " + tocclass + "'";
100 // we'll use our own stream, because we are going to defer everything.
101 // that's how we deal with the fact that we're probably inside a standard
102 // paragraph, and we don't want to be.
103 odocstringstream ods;
106 Toc const & toc = buffer().tocBackend().toc("tableofcontents");
110 xs << html::StartTag("div", "class='toc'");
113 Language const * lang = buffer().params().language;
114 static string toctitle = N_("Table of Contents");
115 docstring title = lang
116 ? translateIfPossible(from_ascii(toctitle), lang->code())
117 : translateIfPossible(from_ascii(toctitle));
118 xs << html::StartTag("div", tocattr)
120 << html::EndTag("div");
123 Toc::const_iterator it = toc.begin();
124 Toc::const_iterator const en = toc.end();
126 for (; it != en; ++it) {
127 // First, we need to manage increases and decreases of depth
128 int const depth = it->depth();
130 // Ignore stuff above the tocdepth
131 if (depth > buffer().params().tocdepth)
134 if (depth > lastdepth) {
136 // open as many tags as we need to open to get to this level
137 // this includes the tag for the current level
138 for (int i = lastdepth + 1; i <= depth; ++i) {
140 attr << "class='lyxtoc-" << i << "'";
141 xs << html::StartTag("div", attr.str()) << html::CR();
145 else if (depth < lastdepth) {
146 // close as many as we have to close to get back to this level
147 // this includes closing the last tag at this level
148 for (int i = lastdepth; i >= depth; --i)
149 xs << html::EndTag("div") << html::CR();
152 attr << "class='lyxtoc-" << depth << "'";
153 xs << html::StartTag("div", attr.str()) << html::CR();
156 // no change of level, so close and open
157 xs << html::EndTag("div") << html::CR();
159 attr << "class='lyxtoc-" << depth << "'";
160 xs << html::StartTag("div", attr.str()) << html::CR();
163 // Now output TOC info for this entry
164 Paragraph const & par = it->dit().innerParagraph();
165 // First the label, if there is one
166 docstring const & label = par.params().labelString();
169 // Now the content of the TOC entry, taken from the paragraph itself
170 OutputParams ours = op;
173 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
175 // Now a link to that paragraph
176 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
177 xs << html::StartTag("a", parattr);
179 // There ought to be a simple way to customize this.
180 // Maybe if we had an InsetLayout for TOC...
181 xs << XHTMLStream::ESCAPE_NONE << ">";
182 xs << html::EndTag("a");
184 for (int i = lastdepth; i > 0; --i)
185 xs << html::EndTag("div") << html::CR();
186 xs << html::EndTag("div") << html::CR();