]> git.lyx.org Git - lyx.git/blob - src/toc.C
reduce number of calls to LyXText::getFont
[lyx.git] / src / toc.C
1 /**
2  * \file toc.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "toc.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "funcrequest.h"
19 #include "LyXAction.h"
20 #include "paragraph.h"
21 #include "pariterator.h"
22
23 #include "frontends/LyXView.h"
24
25 #include "insets/insetfloat.h"
26 #include "insets/insetoptarg.h"
27 #include "insets/insetwrap.h"
28
29 #include "support/convert.h"
30
31 using std::vector;
32 using std::max;
33 using std::ostream;
34 using std::string;
35
36 namespace lyx {
37 namespace toc {
38
39 string const TocItem::asString() const
40 {
41         return string(4 * depth, ' ') + str;
42 }
43
44
45 void TocItem::goTo(LyXView & lv_) const
46 {
47         string const tmp = convert<string>(id_);
48         lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
49 }
50
51
52 FuncRequest TocItem::action() const
53 {
54         return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
55 }
56
57
58 string const getType(string const & cmdName)
59 {
60         // special case
61         if (cmdName == "tableofcontents")
62                 return "TOC";
63         else
64                 return cmdName;
65 }
66
67
68 TocList const getTocList(Buffer const & buf)
69 {
70         TocList toclist;
71
72         BufferParams const & bufparams = buf.params();
73         const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
74
75         ParConstIterator pit = buf.par_iterator_begin();
76         ParConstIterator end = buf.par_iterator_end();
77         for (; pit != end; ++pit) {
78
79                 // the string that goes to the toc (could be the optarg)
80                 string tocstring;
81
82                 // For each paragraph, traverse its insets and look for
83                 // FLOAT_CODE or WRAP_CODE
84                 InsetList::const_iterator it = pit->insetlist.begin();
85                 InsetList::const_iterator end = pit->insetlist.end();
86                 for (; it != end; ++it) {
87                         switch (it->inset->lyxCode()) {
88                         case InsetBase::FLOAT_CODE:
89                                 static_cast<InsetFloat*>(it->inset)
90                                         ->addToToc(toclist, buf);
91                                 break;
92                         case InsetBase::WRAP_CODE:
93                                 static_cast<InsetWrap*>(it->inset)
94                                         ->addToToc(toclist, buf);
95                                 break;
96                         case InsetBase::OPTARG_CODE: {
97                                 if (!tocstring.empty())
98                                         break;
99                                 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
100                                 if (!pit->getLabelstring().empty())
101                                         tocstring = pit->getLabelstring()
102                                                 + ' ';
103                                 tocstring += par.asString(buf, false);
104                                 break;
105                         }
106                         default:
107                                 break;
108                         }
109                 }
110
111                 /// now the toc entry for the paragraph
112                 int const toclevel = pit->layout()->toclevel;
113                 if (toclevel >= min_toclevel
114                     && toclevel <= bufparams.tocdepth) {
115                         // insert this into the table of contents
116                         if (tocstring.empty())
117                                 tocstring = pit->asString(buf, true);
118                         TocItem const item(pit->id(), toclevel - min_toclevel,
119                                            tocstring);
120                         toclist["TOC"].push_back(item);
121                 }
122         }
123         return toclist;
124 }
125
126
127 vector<string> const getTypes(Buffer const & buffer)
128 {
129         vector<string> types;
130
131         TocList const tmp = getTocList(buffer);
132
133         TocList::const_iterator cit = tmp.begin();
134         TocList::const_iterator end = tmp.end();
135
136         for (; cit != end; ++cit) {
137                 types.push_back(cit->first);
138         }
139
140         return types;
141 }
142
143
144 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
145 {
146         TocList const toc_list = getTocList(buffer);
147         TocList::const_iterator cit = toc_list.find(type);
148         if (cit != toc_list.end()) {
149                 Toc::const_iterator ccit = cit->second.begin();
150                 Toc::const_iterator end = cit->second.end();
151                 for (; ccit != end; ++ccit)
152                         os << ccit->asString() << '\n';
153         }
154 }
155
156
157 } // namespace toc
158 } // namespace lyx