]> git.lyx.org Git - lyx.git/blob - src/insets/InsetTOC.cpp
Extend the notermination flag to math as well.
[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 "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
34 #include <ostream>
35
36 using namespace std;
37
38 namespace lyx {
39
40
41 InsetTOC::InsetTOC(Buffer * buf, InsetCommandParams const & p)
42         : InsetCommand(buf, p)
43 {}
44
45
46 ParamInfo const & InsetTOC::findInfo(string const & /* cmdName */)
47 {
48         static ParamInfo param_info_;
49         if (param_info_.empty()) {
50                 param_info_.add("type", ParamInfo::LATEX_REQUIRED);
51         }
52         return param_info_;
53 }
54
55
56 docstring InsetTOC::screenLabel() const
57 {
58         if (getCmdName() == "tableofcontents")
59                 return buffer().B_("Table of Contents");
60         return _("Unknown TOC type");
61 }
62
63
64 void InsetTOC::doDispatch(Cursor & cur, FuncRequest & cmd) {
65         switch (cmd.action()) {
66         case LFUN_MOUSE_RELEASE:
67                 if (!cur.selection() && cmd.button() == mouse_button::button1) {
68                         cur.bv().showDialog("toc", params2string(params()));
69                         cur.dispatched();
70                 }
71                 break;
72         
73         default:
74                 InsetCommand::doDispatch(cur, cmd);
75         }
76 }
77
78
79 int InsetTOC::plaintext(odocstream & os, OutputParams const &) const
80 {
81         os << screenLabel() << "\n\n";
82         buffer().tocBackend().writePlaintextTocList(getCmdName(), os);
83         return PLAINTEXT_NEWLINE;
84 }
85
86
87 int InsetTOC::docbook(odocstream & os, OutputParams const &) const
88 {
89         if (getCmdName() == "tableofcontents")
90                 os << "<toc></toc>";
91         return 0;
92 }
93
94
95 docstring InsetTOC::xhtml(XHTMLStream &, OutputParams const & op) const
96 {
97         Layout const & lay = buffer().params().documentClass().htmlTOCLayout();
98         string const & tocclass = lay.defaultCSSClass();
99         string const tocattr = "class='tochead " + tocclass + "'";
100         
101         // we'll use our own stream, because we are going to defer everything.
102         // that's how we deal with the fact that we're probably inside a standard
103         // paragraph, and we don't want to be.
104         odocstringstream ods;
105         XHTMLStream xs(ods);
106
107         Toc const & toc = buffer().tocBackend().toc("tableofcontents");
108         if (toc.empty())
109                 return docstring();
110
111         xs << html::StartTag("div", "class='toc'");
112
113         // Title of TOC
114         static string toctitle = N_("Table of Contents");
115         docstring title = buffer().B_(toctitle);
116         xs << html::StartTag("div", tocattr)
117                  << title
118                  << html::EndTag("div");
119
120         // Output of TOC
121         Toc::const_iterator it = toc.begin();
122         Toc::const_iterator const en = toc.end();
123         int lastdepth = 0;
124         for (; it != en; ++it) {
125                 // First, we need to manage increases and decreases of depth
126                 int const depth = it->depth();
127                 
128                 // Ignore stuff above the tocdepth
129                 if (depth > buffer().params().tocdepth)
130                         continue;
131                 
132                 if (depth > lastdepth) {
133                         xs << html::CR();
134                         // open as many tags as we need to open to get to this level
135                         // this includes the tag for the current level
136                         for (int i = lastdepth + 1; i <= depth; ++i) {
137                                 stringstream attr;
138                                 attr << "class='lyxtoc-" << i << "'";
139                                 xs << html::StartTag("div", attr.str()) << html::CR();
140                         }
141                         lastdepth = depth;
142                 }
143                 else if (depth < lastdepth) {
144                         // close as many as we have to close to get back to this level
145                         // this includes closing the last tag at this level
146                         for (int i = lastdepth; i >= depth; --i) 
147                                 xs << html::EndTag("div") << html::CR();
148                         // now open our tag
149                         stringstream attr;
150                         attr << "class='lyxtoc-" << depth << "'";
151                         xs << html::StartTag("div", attr.str()) << html::CR();
152                         lastdepth = depth;
153                 } else {
154                         // no change of level, so close and open
155                         xs << html::EndTag("div") << html::CR();
156                         stringstream attr;
157                         attr << "class='lyxtoc-" << depth << "'";
158                         xs << html::StartTag("div", attr.str()) << html::CR();
159                 }
160                 
161                 // Now output TOC info for this entry
162                 Paragraph const & par = it->dit().innerParagraph();
163
164                 string const attr = "href='#" + par.magicLabel() + "' class='tocentry'";
165                 xs << html::StartTag("a", attr);
166
167                 // First the label, if there is one
168                 docstring const & label = par.params().labelString();
169                 if (!label.empty())
170                         xs << label << " ";
171                 // Now the content of the TOC entry, taken from the paragraph itself
172                 OutputParams ours = op;
173                 ours.for_toc = true;
174                 Font const dummy;
175                 par.simpleLyXHTMLOnePar(buffer(), xs, ours, dummy);
176
177                 xs << html::EndTag("a") << " ";
178
179                 // Now a link to that paragraph
180                 string const parattr = "href='#" + par.magicLabel() + "' class='tocarrow'";
181                 xs << html::StartTag("a", parattr);
182                 // FIXME XHTML 
183                 // There ought to be a simple way to customize this.
184                 // Maybe if we had an InsetLayout for TOC...
185                 xs << XHTMLStream::ESCAPE_NONE << "&gt;";
186                 xs << html::EndTag("a");                
187         }
188         for (int i = lastdepth; i > 0; --i) 
189                 xs << html::EndTag("div") << html::CR();
190         xs << html::EndTag("div") << html::CR();
191         return ods.str();
192 }
193
194
195 } // namespace lyx