]> git.lyx.org Git - features.git/blob - src/insets/InsetTOC.cpp
Allow for the possibility of an "InsetLayout TOC".
[features.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 "OutputParams.h"
25 #include "output_xhtml.h"
26 #include "Paragraph.h"
27 #include "ParagraphParameters.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
30
31 #include "support/debug.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 }
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                 return from_ascii("TOC:Listings");
101         return from_ascii("TOC");
102 }
103
104
105 void InsetTOC::validate(LaTeXFeatures & features) const
106 {
107         InsetCommand::validate(features);
108         features.useInsetLayout(getLayout());
109         if (getCmdName() == "lstlistoflistings")
110                 features.require("listings");
111 }
112
113
114 int InsetTOC::plaintext(odocstringstream & os,
115         OutputParams const &, size_t max_length) const
116 {
117         os << screenLabel() << "\n\n";
118         buffer().tocBackend().writePlaintextTocList(cmd2type(getCmdName()), os, max_length);
119         return PLAINTEXT_NEWLINE;
120 }
121
122
123 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
124 {
125         if (getCmdName() == "tableofcontents")
126                 os << "<toc></toc>";
127         return 0;
128 }
129
130
131 void InsetTOC::makeTOCWithDepth(XHTMLStream xs, 
132                 Toc toc, OutputParams const & op) const
133 {
134         Toc::const_iterator it = toc.begin();
135         Toc::const_iterator const en = toc.end();
136         int lastdepth = 0;
137         for (; it != en; ++it) {
138                 // do not output entries that are not actually included in the output,
139                 // e.g., stuff in non-active branches or notes or whatever.
140                 if (!it->isOutput())
141                         continue;
142
143                 // First, we need to manage increases and decreases of depth
144                 // If there's no depth to deal with, we artifically set it to 1.
145                 int const depth = it->depth();
146                 
147                 // Ignore stuff above the tocdepth
148                 if (depth > buffer().params().tocdepth)
149                         continue;
150                 
151                 if (depth > lastdepth) {
152                         xs << html::CR();
153                         // open as many tags as we need to open to get to this level
154                         // this includes the tag for the current level
155                         for (int i = lastdepth + 1; i <= depth; ++i) {
156                                 stringstream attr;
157                                 attr << "class='lyxtoc-" << i << "'";
158                                 xs << html::StartTag("div", attr.str()) << html::CR();
159                         }
160                         lastdepth = depth;
161                 }
162                 else if (depth < lastdepth) {
163                         // close as many as we have to close to get back to this level
164                         // this includes closing the last tag at this level
165                         for (int i = lastdepth; i >= depth; --i) 
166                                 xs << html::EndTag("div") << html::CR();
167                         // now open our tag
168                         stringstream attr;
169                         attr << "class='lyxtoc-" << depth << "'";
170                         xs << html::StartTag("div", attr.str()) << html::CR();
171                         lastdepth = depth;
172                 } else {
173                         // no change of level, so close and open
174                         xs << html::EndTag("div") << html::CR();
175                         stringstream attr;
176                         attr << "class='lyxtoc-" << depth << "'";
177                         xs << html::StartTag("div", attr.str()) << html::CR();
178                 }
179                 
180                 // Now output TOC info for this entry
181                 Paragraph const & par = it->dit().innerParagraph();
182
183                 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
184                 xs << html::StartTag("a", attr);
185
186                 // First the label, if there is one
187                 docstring const & label = par.params().labelString();
188                 if (!label.empty())
189                         xs << label << " ";
190                 // Now the content of the TOC entry, taken from the paragraph itself
191                 OutputParams ours = op;
192                 ours.for_toc = true;
193                 Font const dummy;
194                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
195
196                 xs << html::EndTag("a") << " ";
197
198                 // Now a link to that paragraph
199                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
200                 xs << html::StartTag("a", parattr);
201                 // FIXME XHTML 
202                 // There ought to be a simple way to customize this.
203                 // Maybe if we had an InsetLayout for TOC...
204                 xs << XHTMLStream::ESCAPE_NONE << "&gt;";
205                 xs << html::EndTag("a");                
206         }
207         for (int i = lastdepth; i > 0; --i) 
208                 xs << html::EndTag("div") << html::CR();
209 }
210
211
212 void InsetTOC::makeTOCNoDepth(XHTMLStream xs, 
213                 Toc toc, const OutputParams & op) const
214 {
215         Toc::const_iterator it = toc.begin();
216         Toc::const_iterator const en = toc.end();
217         for (; it != en; ++it) {
218                 // do not output entries that are not actually included in the output,
219                 // e.g., stuff in non-active branches or notes or whatever.
220                 if (!it->isOutput())
221                         continue;
222
223                 xs << html::StartTag("div", "class='lyxtop-1'") << html::CR();
224
225                 Paragraph const & par = it->dit().innerParagraph();
226                 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
227                 xs << html::StartTag("a", attr);
228
229                 // First the label, if there is one
230                 docstring const & label = par.params().labelString();
231                 if (!label.empty())
232                         xs << label << " ";
233                 // Now the content of the TOC entry, taken from the paragraph itself
234                 OutputParams ours = op;
235                 ours.for_toc = true;
236                 Font const dummy;
237                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
238
239                 xs << html::EndTag("a") << " ";
240
241                 // Now a link to that paragraph
242                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
243                 xs << html::StartTag("a", parattr);
244                 // FIXME XHTML
245                 // There ought to be a simple way to customize this.
246                 // Maybe if we had an InsetLayout for TOC...
247                 xs << XHTMLStream::ESCAPE_NONE << "&gt;";
248                 xs << html::EndTag("a");
249                 xs << html::EndTag("div");
250         }
251 }
252
253
254 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
255 {
256         string const & command = getCmdName();
257         if (command != "tableofcontents" && command != "lstlistoflistings") {
258                 LYXERR0("TOC type " << command << " not yet implemented.");
259                 LASSERT(false, /* */);
260                 return docstring();
261         }
262
263         // with lists of listings, at least, there is no depth
264         // to worry about. so the code can be simpler.
265         bool const use_depth = (command == "tableofcontents");
266
267         Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
268         string const & tocclass = lay.defaultCSSClass();
269         string const tocattr = "class='tochead " + tocclass + "'";
270
271         // we'll use our own stream, because we are going to defer everything.
272         // that's how we deal with the fact that we're probably inside a standard
273         // paragraph, and we don't want to be.
274         odocstringstream ods;
275         XHTMLStream xs(ods);
276
277         Toc const & toc = buffer().tocBackend().toc(cmd2type(command));
278         if (toc.empty())
279                 return docstring();
280
281         xs << html::StartTag("div", "class='toc'");
282
283         // Title of TOC
284         docstring title = screenLabel();
285         xs << html::StartTag("div", tocattr)
286                  << title
287                  << html::EndTag("div");
288
289         // Output of TOC
290         if (use_depth)
291                 makeTOCWithDepth(xs, toc, op);
292         else
293                 makeTOCNoDepth(xs, toc, op);
294
295         xs << html::EndTag("div") << html::CR();
296         return ods.str();
297 }
298
299
300 } // namespace lyx