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(odocstream & os, OutputParams const &) const
115 os << screenLabel() << "\n\n";
116 buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os);
117 return PLAINTEXT_NEWLINE;
121 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
123 if (getCmdName() == "tableofcontents")
129 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
131 if (getCmdName() != "tableofcontents")
134 Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
135 string const & tocclass = lay.defaultCSSClass();
136 string const tocattr = "class='tochead " + tocclass + "'";
138 // we'll use our own stream, because we are going to defer everything.
139 // that's how we deal with the fact that we're probably inside a standard
140 // paragraph, and we don't want to be.
141 odocstringstream ods;
144 Toc const & toc = buffer().tocBackend().toc(cmd2type(getCmdName()));
148 xs << html::StartTag("div", "class='toc'");
151 docstring title = screenLabel();
152 xs << html::StartTag("div", tocattr)
154 << html::EndTag("div");
157 Toc::const_iterator it = toc.begin();
158 Toc::const_iterator const en = toc.end();
160 for (; it != en; ++it) {
161 // First, we need to manage increases and decreases of depth
162 int const depth = it->depth();
164 // Ignore stuff above the tocdepth
165 if (depth > buffer().params().tocdepth)
168 if (depth > lastdepth) {
170 // open as many tags as we need to open to get to this level
171 // this includes the tag for the current level
172 for (int i = lastdepth + 1; i <= depth; ++i) {
174 attr << "class='lyxtoc-" << i << "'";
175 xs << html::StartTag("div", attr.str()) << html::CR();
179 else if (depth < lastdepth) {
180 // close as many as we have to close to get back to this level
181 // this includes closing the last tag at this level
182 for (int i = lastdepth; i >= depth; --i)
183 xs << html::EndTag("div") << html::CR();
186 attr << "class='lyxtoc-" << depth << "'";
187 xs << html::StartTag("div", attr.str()) << html::CR();
190 // no change of level, so close and open
191 xs << html::EndTag("div") << html::CR();
193 attr << "class='lyxtoc-" << depth << "'";
194 xs << html::StartTag("div", attr.str()) << html::CR();
197 // Now output TOC info for this entry
198 Paragraph const & par = it->dit().innerParagraph();
200 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
201 xs << html::StartTag("a", attr);
203 // First the label, if there is one
204 docstring const & label = par.params().labelString();
207 // Now the content of the TOC entry, taken from the paragraph itself
208 OutputParams ours = op;
211 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
213 xs << html::EndTag("a") << " ";
215 // Now a link to that paragraph
216 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
217 xs << html::StartTag("a", parattr);
219 // There ought to be a simple way to customize this.
220 // Maybe if we had an InsetLayout for TOC...
221 xs << XHTMLStream::ESCAPE_NONE << ">";
222 xs << html::EndTag("a");
224 for (int i = lastdepth; i > 0; --i)
225 xs << html::EndTag("div") << html::CR();
226 xs << html::EndTag("div") << html::CR();