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