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 // do not output entries that are not actually included in the output,
163 // e.g., stuff in non-active branches or notes or whatever.
167 // First, we need to manage increases and decreases of depth
168 int const depth = it->depth();
170 // Ignore stuff above the tocdepth
171 if (depth > buffer().params().tocdepth)
174 if (depth > lastdepth) {
176 // open as many tags as we need to open to get to this level
177 // this includes the tag for the current level
178 for (int i = lastdepth + 1; i <= depth; ++i) {
180 attr << "class='lyxtoc-" << i << "'";
181 xs << html::StartTag("div", attr.str()) << html::CR();
185 else if (depth < lastdepth) {
186 // close as many as we have to close to get back to this level
187 // this includes closing the last tag at this level
188 for (int i = lastdepth; i >= depth; --i)
189 xs << html::EndTag("div") << html::CR();
192 attr << "class='lyxtoc-" << depth << "'";
193 xs << html::StartTag("div", attr.str()) << html::CR();
196 // no change of level, so close and open
197 xs << html::EndTag("div") << html::CR();
199 attr << "class='lyxtoc-" << depth << "'";
200 xs << html::StartTag("div", attr.str()) << html::CR();
203 // Now output TOC info for this entry
204 Paragraph const & par = it->dit().innerParagraph();
206 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
207 xs << html::StartTag("a", attr);
209 // First the label, if there is one
210 docstring const & label = par.params().labelString();
213 // Now the content of the TOC entry, taken from the paragraph itself
214 OutputParams ours = op;
217 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
219 xs << html::EndTag("a") << " ";
221 // Now a link to that paragraph
222 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
223 xs << html::StartTag("a", parattr);
225 // There ought to be a simple way to customize this.
226 // Maybe if we had an InsetLayout for TOC...
227 xs << XHTMLStream::ESCAPE_NONE << ">";
228 xs << html::EndTag("a");
230 for (int i = lastdepth; i > 0; --i)
231 xs << html::EndTag("div") << html::CR();
232 xs << html::EndTag("div") << html::CR();