]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTOC.cpp
Do not repeatedly call main_font_encoding()
[lyx.git] / src / insets / InsetTOC.cpp
1 /**
2  * \file InsetTOC.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetTOC.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "DispatchResult.h"
20 #include "Font.h"
21 #include "FuncRequest.h"
22 #include "Language.h"
23 #include "LaTeXFeatures.h"
24 #include "output_xhtml.h"
25 #include "Paragraph.h"
26 #include "ParagraphParameters.h"
27 #include "TextClass.h"
28 #include "TocBackend.h"
29
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33 #include "support/lassert.h"
34
35 #include <ostream>
36
37 using namespace std;
38
39 namespace lyx {
40
41 namespace {
42 string cmd2type(string const & cmd)
43 {
44         if (cmd == "lstlistoflistings")
45                 return "listing";
46         return cmd;
47 }
48 } // namespace
49
50
51 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
52         : InsetCommand(buf, p)
53 {}
54
55
56 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
57 {
58         static ParamInfo param_info_;
59         if (param_info_.empty()) {
60                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
61         }
62         return param_info_;
63 }
64
65
66 bool InsetTOC::isCompatibleCommand(string const & cmd)
67 {
68         return cmd == defaultCommand() || cmd == "lstlistoflistings";
69 }
70
71
72 docstring InsetTOC::screenLabel() const
73 {
74         if (getCmdName() == "tableofcontents")
75                 return buffer().B_("Table of Contents");
76         if (getCmdName() == "lstlistoflistings")
77                 return buffer().B_("List of Listings");
78         return _("Unknown TOC type");
79 }
80
81
82 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
83         switch (cmd.action()) {
84         case LFUN_MOUSE_RELEASE:
85                 if (!cur.selection() && cmd.button() == mouse_button::button1) {
86                         cur.bv().showDialog("toc", params2string(params()));
87                         cur.dispatched();
88                 }
89                 break;
90
91         default:
92                 InsetCommand::doDispatch(cur, cmd);
93         }
94 }
95
96
97 docstring InsetTOC::layoutName() const
98 {
99         if (getCmdName() == "lstlistoflistings") {
100                 if (buffer().params().use_minted)
101                         return from_ascii("TOC:MintedListings");
102                 else
103                         return from_ascii("TOC:Listings");
104         }
105         return from_ascii("TOC");
106 }
107
108
109 void InsetTOC::validate(LaTeXFeatures & features) const
110 {
111         InsetCommand::validate(features);
112         features.useInsetLayout(getLayout());
113         if (getCmdName() == "lstlistoflistings") {
114                 if (buffer().params().use_minted)
115                         features.require("minted");
116                 else
117                         features.require("listings");
118         }
119 }
120
121
122 int InsetTOC::plaintext(odocstringstream & os,
123         OutputParams const &, size_t max_length) const
124 {
125         os << screenLabel() << "\n\n";
126         buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
127         return PLAINTEXT_NEWLINE;
128 }
129
130
131 void InsetTOC::docbook(XMLStream &, OutputParams const &) const
132 {
133         // TOC are generated automatically by the DocBook processor.
134         return;
135 }
136
137
138 void InsetTOC::makeTOCEntry(XMLStream & xs,
139                 Paragraph const & par, OutputParams const & op) const
140 {
141         string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
142         xs << xml::StartTag("a", attr);
143
144         // First the label, if there is one
145         docstring const & label = par.params().labelString();
146         if (!label.empty())
147                 xs << label << " ";
148         // Now the content of the TOC entry, taken from the paragraph itself
149         OutputParams ours = op;
150         ours.for_toc = true;
151         Font const dummy;
152         par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
153
154         xs << xml::EndTag("a") << xml::CR();
155 }
156
157
158 void InsetTOC::makeTOCWithDepth(XMLStream & xs,
159                 Toc const & toc, OutputParams const & op) const
160 {
161         Toc::const_iterator it = toc.begin();
162         Toc::const_iterator const en = toc.end();
163         int lastdepth = 0;
164         for (; it != en; ++it) {
165                 // do not output entries that are not actually included in the output,
166                 // e.g., stuff in non-active branches or notes or whatever.
167                 if (!it->isOutput())
168                         continue;
169
170                 // First, we need to manage increases and decreases of depth
171                 // If there's no depth to deal with, we artificially set it to 1.
172                 int const depth = it->depth();
173
174                 // Ignore stuff above the tocdepth
175                 if (depth > buffer().params().tocdepth)
176                         continue;
177
178                 if (depth > lastdepth) {
179                         xs << xml::CR();
180                         // open as many tags as we need to open to get to this level
181                         // this includes the tag for the current level
182                         for (int i = lastdepth + 1; i <= depth; ++i) {
183                                 stringstream attr;
184                                 attr << "class='lyxtoc-" << i << "'";
185                                 xs << xml::StartTag("div", attr.str()) << xml::CR();
186                         }
187                         lastdepth = depth;
188                 }
189                 else if (depth < lastdepth) {
190                         // close as many as we have to close to get back to this level
191                         // this includes closing the last tag at this level
192                         for (int i = lastdepth; i >= depth; --i)
193                                 xs << xml::EndTag("div") << xml::CR();
194                         // now open our tag
195                         stringstream attr;
196                         attr << "class='lyxtoc-" << depth << "'";
197                         xs << xml::StartTag("div", attr.str()) << xml::CR();
198                         lastdepth = depth;
199                 } else {
200                         // no change of level, so close and open
201                         xs << xml::EndTag("div") << xml::CR();
202                         stringstream attr;
203                         attr << "class='lyxtoc-" << depth << "'";
204                         xs << xml::StartTag("div", attr.str()) << xml::CR();
205                 }
206
207                 // Now output TOC info for this entry
208                 Paragraph const & par = it->dit().innerParagraph();
209                 makeTOCEntry(xs, par, op);
210         }
211         for (int i = lastdepth; i > 0; --i)
212                 xs << xml::EndTag("div") << xml::CR();
213 }
214
215
216 void InsetTOC::makeTOCNoDepth(XMLStream & xs,
217                 Toc const & toc, const OutputParams & op) const
218 {
219         Toc::const_iterator it = toc.begin();
220         Toc::const_iterator const en = toc.end();
221         for (; it != en; ++it) {
222                 // do not output entries that are not actually included in the output,
223                 // e.g., stuff in non-active branches or notes or whatever.
224                 if (!it->isOutput())
225                         continue;
226
227                 xs << xml::StartTag("div", "class='lyxtoc-flat'") << xml::CR();
228
229                 Paragraph const & par = it->dit().innerParagraph();
230                 makeTOCEntry(xs, par, op);
231
232                 xs << xml::EndTag("div");
233         }
234 }
235
236
237 docstring InsetTOC::xhtml(XMLStream &, OutputParams const & op) const
238 {
239         string const & command = getCmdName();
240         if (command != "tableofcontents" && command != "lstlistoflistings") {
241                 LYXERR0("TOC type " << command << " not yet implemented.");
242                 LASSERT(false, return docstring());
243         }
244
245         shared_ptr<Toc const> toc =
246                 buffer().masterBuffer()->tocBackend().toc(cmd2type(command));
247         if (toc->empty())
248                 return docstring();
249
250         // we'll use our own stream, because we are going to defer everything.
251         // that's how we deal with the fact that we're probably inside a standard
252         // paragraph, and we don't want to be.
253         odocstringstream ods;
254         XMLStream xs(ods);
255
256         xs << xml::StartTag("div", "class='toc'");
257
258         // Title of TOC
259         InsetLayout const & il = getLayout();
260         string const & tag = il.htmltag();
261         docstring title = screenLabel();
262         Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
263         string const & tocclass = lay.defaultCSSClass();
264         string const tocattr = "class='tochead " + tocclass + "'";
265         xs << xml::StartTag(tag, tocattr)
266                  << title
267                  << xml::EndTag(tag);
268
269         // with lists of listings, at least, there is no depth
270         // to worry about. so the code can be simpler.
271         bool const use_depth = (command == "tableofcontents");
272
273         // Output of TOC
274         if (use_depth)
275                 makeTOCWithDepth(xs, *toc, op);
276         else
277                 makeTOCNoDepth(xs, *toc, op);
278
279         xs << xml::EndTag("div") << xml::CR();
280         return ods.str();
281 }
282
283
284 } // namespace lyx