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