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"
17 #include "BufferView.h"
19 #include "DispatchResult.h"
21 #include "FuncRequest.h"
23 #include "LaTeXFeatures.h"
24 #include "OutputParams.h"
25 #include "output_xhtml.h"
26 #include "Paragraph.h"
27 #include "ParagraphParameters.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
31 #include "support/debug.h"
32 #include "support/gettext.h"
41 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
42 : InsetCommand(buf, p)
46 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
48 static ParamInfo param_info_;
49 if (param_info_.empty()) {
50 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
56 docstring InsetTOC::screenLabel() const
58 if (getCmdName() == "tableofcontents")
59 return buffer().B_("Table of Contents");
60 return _("Unknown TOC type");
64 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
65 switch (cmd.action()) {
66 case LFUN_MOUSE_RELEASE:
67 if (!cur.selection() && cmd.button() == mouse_button::button1) {
68 cur.bv().showDialog("toc", params2string(params()));
74 InsetCommand::doDispatch(cur, cmd);
79 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
81 os << screenLabel() << "\n\n";
82 buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
83 return PLAINTEXT_NEWLINE;
87 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
89 if (getCmdName() == "tableofcontents")
95 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
97 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
98 string const & tocclass = lay.defaultCSSClass();
99 string const tocattr = "class='tochead " + tocclass + "'";
101 // we'll use our own stream, because we are going to defer everything.
102 // that's how we deal with the fact that we're probably inside a standard
103 // paragraph, and we don't want to be.
104 odocstringstream ods;
107 Toc const & toc = buffer().tocBackend().toc("tableofcontents");
111 xs << html::StartTag("div", "class='toc'");
114 static string toctitle = N_("Table of Contents");
115 docstring title = buffer().B_(toctitle);
116 xs << html::StartTag("div", tocattr)
118 << html::EndTag("div");
121 Toc::const_iterator it = toc.begin();
122 Toc::const_iterator const en = toc.end();
124 for (; it != en; ++it) {
125 // First, we need to manage increases and decreases of depth
126 int const depth = it->depth();
128 // Ignore stuff above the tocdepth
129 if (depth > buffer().params().tocdepth)
132 if (depth > lastdepth) {
134 // open as many tags as we need to open to get to this level
135 // this includes the tag for the current level
136 for (int i = lastdepth + 1; i <= depth; ++i) {
138 attr << "class='lyxtoc-" << i << "'";
139 xs << html::StartTag("div", attr.str()) << html::CR();
143 else if (depth < lastdepth) {
144 // close as many as we have to close to get back to this level
145 // this includes closing the last tag at this level
146 for (int i = lastdepth; i >= depth; --i)
147 xs << html::EndTag("div") << html::CR();
150 attr << "class='lyxtoc-" << depth << "'";
151 xs << html::StartTag("div", attr.str()) << html::CR();
154 // no change of level, so close and open
155 xs << html::EndTag("div") << html::CR();
157 attr << "class='lyxtoc-" << depth << "'";
158 xs << html::StartTag("div", attr.str()) << html::CR();
161 // Now output TOC info for this entry
162 Paragraph const & par = it->dit().innerParagraph();
164 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
165 xs << html::StartTag("a", attr);
167 // First the label, if there is one
168 docstring const & label = par.params().labelString();
171 // Now the content of the TOC entry, taken from the paragraph itself
172 OutputParams ours = op;
175 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
177 xs << html::EndTag("a") << " ";
179 // Now a link to that paragraph
180 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
181 xs << html::StartTag("a", parattr);
183 // There ought to be a simple way to customize this.
184 // Maybe if we had an InsetLayout for TOC...
185 xs << XHTMLStream::ESCAPE_NONE << ">";
186 xs << html::EndTag("a");
188 for (int i = lastdepth; i > 0; --i)
189 xs << html::EndTag("div") << html::CR();
190 xs << html::EndTag("div") << html::CR();