]> git.lyx.org Git - lyx.git/blob - src/toc.C
Pimpl Buffer.
[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 "iterators.h"
20 #include "LyXAction.h"
21 #include "paragraph.h"
22
23 #include "frontends/LyXView.h"
24
25 #include "insets/insetfloat.h"
26 #include "insets/insetwrap.h"
27
28 #include "support/tostr.h"
29
30 using std::vector;
31 using std::max;
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         BufferParams const & bufparams = buf.params();
72         LyXTextClass const & textclass = bufparams.getLyXTextClass();
73
74         ParConstIterator pit = buf.par_iterator_begin();
75         ParConstIterator end = buf.par_iterator_end();
76         for (; pit != end; ++pit) {
77 #ifdef WITH_WARNINGS
78 #warning bogus type (Lgb)
79 #endif
80                 char const labeltype = pit->layout()->labeltype;
81
82                 if (labeltype >= LABEL_COUNTER_CHAPTER
83                     && labeltype <= LABEL_COUNTER_CHAPTER + bufparams.tocdepth) {
84                                 // insert this into the table of contents
85                         const int depth = max(0, labeltype - textclass.maxcounter());
86                         TocItem const item(pit->id(), depth,
87                                            pit->asString(buf, true));
88                         toclist["TOC"].push_back(item);
89                 }
90
91                 // For each paragraph, traverse its insets and look for
92                 // FLOAT_CODE or WRAP_CODE
93                 InsetList::const_iterator it = pit->insetlist.begin();
94                 InsetList::const_iterator end = pit->insetlist.end();
95                 for (; it != end; ++it) {
96                         if (it->inset->lyxCode() == InsetOld::FLOAT_CODE) {
97                                 InsetFloat * il =
98                                         static_cast<InsetFloat*>(it->inset);
99                                 il->addToToc(toclist, buf);
100                         } else if (it->inset->lyxCode() == InsetOld::WRAP_CODE) {
101                                 InsetWrap * il =
102                                         static_cast<InsetWrap*>(it->inset);
103
104                                 il->addToToc(toclist, buf);
105                         }
106                 }
107         }
108         return toclist;
109 }
110
111
112 vector<string> const getTypes(Buffer const & buffer)
113 {
114         vector<string> types;
115
116         TocList const tmp = getTocList(buffer);
117
118         TocList::const_iterator cit = tmp.begin();
119         TocList::const_iterator end = tmp.end();
120
121         for (; cit != end; ++cit) {
122                 types.push_back(cit->first);
123         }
124
125         return types;
126 }
127
128
129 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
130 {
131         TocList const toc_list = getTocList(buffer);
132         TocList::const_iterator cit = toc_list.find(type);
133         if (cit != toc_list.end()) {
134                 Toc::const_iterator ccit = cit->second.begin();
135                 Toc::const_iterator end = cit->second.end();
136                 for (; ccit != end; ++ccit)
137                         os << ccit->asString() << '\n';
138         }
139 }
140
141
142 } // namespace toc
143 } // namespace lyx