]> git.lyx.org Git - lyx.git/blob - src/toc.C
Move #includes out of header files.
[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 #include "buffer.h"
16 #include "funcrequest.h"
17 #include "LyXAction.h"
18 #include "paragraph.h"
19 #include "debug.h"
20 #include "iterators.h"
21
22 #include "insets/insetfloat.h"
23 #include "insets/insetwrap.h"
24
25 #include "support/tostr.h"
26
27 #include "frontends/LyXView.h"
28
29 using std::vector;
30 using std::max;
31 using std::endl;
32 using std::ostream;
33
34 namespace lyx {
35 namespace toc {
36
37 string const TocItem::asString() const
38 {
39         return string(4 * depth, ' ') + str;
40 }
41
42
43 void TocItem::goTo(LyXView & lv_) const
44 {
45         string const tmp = tostr(id_);
46         lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
47 }
48
49
50 int TocItem::action() const
51 {
52         return lyxaction.getPseudoAction(LFUN_GOTO_PARAGRAPH,
53                                          tostr(id_));
54 }
55
56
57 string const getType(string const & cmdName)
58 {
59         // special case
60         if (cmdName == "tableofcontents")
61                 return "TOC";
62         else
63                 return cmdName;
64 }
65
66
67 TocList const getTocList(Buffer const & buf)
68 {
69         TocList toclist;
70
71         LyXTextClass const & textclass = buf.params.getLyXTextClass();
72
73         ParConstIterator pit = buf.par_iterator_begin();
74         ParConstIterator end = buf.par_iterator_end();
75         for (; pit != end; ++pit) {
76 #ifdef WITH_WARNINGS
77 #warning bogus type (Lgb)
78 #endif
79                 char const labeltype = pit->layout()->labeltype;
80
81                 if (labeltype >= LABEL_COUNTER_CHAPTER
82                     && labeltype <= LABEL_COUNTER_CHAPTER + buf.params.tocdepth) {
83                                 // insert this into the table of contents
84                         const int depth = max(0, labeltype - textclass.maxcounter());
85                         TocItem const item(pit->id(), depth,
86                                            pit->asString(buf, true));
87                         toclist["TOC"].push_back(item);
88                 }
89
90                 // For each paragraph, traverse its insets and look for
91                 // FLOAT_CODE or WRAP_CODE
92                 InsetList::const_iterator it = pit->insetlist.begin();
93                 InsetList::const_iterator end = pit->insetlist.end();
94                 for (; it != end; ++it) {
95                         if (it->inset->lyxCode() == InsetOld::FLOAT_CODE) {
96                                 InsetFloat * il =
97                                         static_cast<InsetFloat*>(it->inset);
98                                 il->addToToc(toclist, buf);
99                         } else if (it->inset->lyxCode() == InsetOld::WRAP_CODE) {
100                                 InsetWrap * il =
101                                         static_cast<InsetWrap*>(it->inset);
102
103                                 il->addToToc(toclist, buf);
104                         }
105                 }
106         }
107         return toclist;
108 }
109
110
111 vector<string> const getTypes(Buffer const & buffer)
112 {
113         vector<string> types;
114
115         TocList const tmp = getTocList(buffer);
116
117         TocList::const_iterator cit = tmp.begin();
118         TocList::const_iterator end = tmp.end();
119
120         for (; cit != end; ++cit) {
121                 types.push_back(cit->first);
122         }
123
124         return types;
125 }
126
127
128 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
129 {
130         TocList const toc_list = getTocList(buffer);
131         TocList::const_iterator cit = toc_list.find(type);
132         if (cit != toc_list.end()) {
133                 Toc::const_iterator ccit = cit->second.begin();
134                 Toc::const_iterator end = cit->second.end();
135                 for (; ccit != end; ++ccit)
136                         os << ccit->asString() << '\n';
137         }
138 }
139
140
141 } // namespace toc
142 } // namespace lyx