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 string cmd2type(string const & cmd)
43 if (cmd == "lstlistoflistings")
50 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
51 : InsetCommand(buf, p)
55 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
57 static ParamInfo param_info_;
58 if (param_info_.empty()) {
59 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
65 bool InsetTOC::isCompatibleCommand(string const & cmd)
67 return cmd == defaultCommand() || cmd == "lstlistoflistings";
71 docstring InsetTOC::screenLabel() const
73 if (getCmdName() == "tableofcontents")
74 return buffer().B_("Table of Contents");
75 if (getCmdName() == "lstlistoflistings")
76 return buffer().B_("List of Listings");
77 return _("Unknown TOC type");
81 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
82 switch (cmd.action()) {
83 case LFUN_MOUSE_RELEASE:
84 if (!cur.selection() && cmd.button() == mouse_button::button1) {
85 cur.bv().showDialog("toc", params2string(params()));
91 InsetCommand::doDispatch(cur, cmd);
96 docstring InsetTOC::layoutName() const
98 if (getCmdName() == "lstlistoflistings")
99 return from_ascii("TOC:Listings");
104 void InsetTOC::validate(LaTeXFeatures & features) const
106 InsetCommand::validate(features);
107 features.useInsetLayout(getLayout());
108 if (getCmdName() == "lstlistoflistings")
109 features.require("listings");
113 int InsetTOC::plaintext(odocstringstream & os,
114 OutputParams const &, size_t max_length) const
116 os << screenLabel() << "\n\n";
117 buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
118 return PLAINTEXT_NEWLINE;
122 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
124 if (getCmdName() == "tableofcontents")
130 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
132 if (getCmdName() != "tableofcontents")
135 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
136 string const & tocclass = lay.defaultCSSClass();
137 string const tocattr = "class='tochead " + tocclass + "'";
139 // we'll use our own stream, because we are going to defer everything.
140 // that's how we deal with the fact that we're probably inside a standard
141 // paragraph, and we don't want to be.
142 odocstringstream ods;
145 Toc const & toc = buffer().tocBackend().toc(cmd2type(getCmdName()));
149 xs << html::StartTag("div", "class='toc'");
152 docstring title = screenLabel();
153 xs << html::StartTag("div", tocattr)
155 << html::EndTag("div");
158 Toc::const_iterator it = toc.begin();
159 Toc::const_iterator const en = toc.end();
161 for (; it != en; ++it) {
162 // First, we need to manage increases and decreases of depth
163 int const depth = it->depth();
165 // Ignore stuff above the tocdepth
166 if (depth > buffer().params().tocdepth)
169 if (depth > lastdepth) {
171 // open as many tags as we need to open to get to this level
172 // this includes the tag for the current level
173 for (int i = lastdepth + 1; i <= depth; ++i) {
175 attr << "class='lyxtoc-" << i << "'";
176 xs << html::StartTag("div", attr.str()) << html::CR();
180 else if (depth < lastdepth) {
181 // close as many as we have to close to get back to this level
182 // this includes closing the last tag at this level
183 for (int i = lastdepth; i >= depth; --i)
184 xs << html::EndTag("div") << html::CR();
187 attr << "class='lyxtoc-" << depth << "'";
188 xs << html::StartTag("div", attr.str()) << html::CR();
191 // no change of level, so close and open
192 xs << html::EndTag("div") << html::CR();
194 attr << "class='lyxtoc-" << depth << "'";
195 xs << html::StartTag("div", attr.str()) << html::CR();
198 // Now output TOC info for this entry
199 Paragraph const & par = it->dit().innerParagraph();
201 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
202 xs << html::StartTag("a", attr);
204 // First the label, if there is one
205 docstring const & label = par.params().labelString();
208 // Now the content of the TOC entry, taken from the paragraph itself
209 OutputParams ours = op;
212 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
214 xs << html::EndTag("a") << " ";
216 // Now a link to that paragraph
217 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
218 xs << html::StartTag("a", parattr);
220 // There ought to be a simple way to customize this.
221 // Maybe if we had an InsetLayout for TOC...
222 xs << XHTMLStream::ESCAPE_NONE << ">";
223 xs << html::EndTag("a");
225 for (int i = lastdepth; i > 0; --i)
226 xs << html::EndTag("div") << html::CR();
227 xs << html::EndTag("div") << html::CR();