]> git.lyx.org Git - lyx.git/blob - src/toc.C
The std::string mammoth path.
[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 using std::string;
34
35 namespace lyx {
36 namespace toc {
37
38 string const TocItem::asString() const
39 {
40         return string(4 * depth, ' ') + str;
41 }
42
43
44 void TocItem::goTo(LyXView & lv_) const
45 {
46         string const tmp = tostr(id_);
47         lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
48 }
49
50
51 FuncRequest TocItem::action() const
52 {
53         return FuncRequest(LFUN_GOTO_PARAGRAPH, 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
78                 int const toclevel = pit->layout()->toclevel;
79                 if (toclevel > 0 &&  toclevel <= bufparams.tocdepth) {
80                         // insert this into the table of contents
81                         TocItem const item(pit->id(), toclevel - 1, pit->asString(buf, true));
82                         toclist["TOC"].push_back(item);
83                 }
84
85                 // For each paragraph, traverse its insets and look for
86                 // FLOAT_CODE or WRAP_CODE
87                 InsetList::const_iterator it = pit->insetlist.begin();
88                 InsetList::const_iterator end = pit->insetlist.end();
89                 for (; it != end; ++it) {
90                         if (it->inset->lyxCode() == InsetOld::FLOAT_CODE) {
91                                 InsetFloat * il =
92                                         static_cast<InsetFloat*>(it->inset);
93                                 il->addToToc(toclist, buf);
94                         } else if (it->inset->lyxCode() == InsetOld::WRAP_CODE) {
95                                 InsetWrap * il =
96                                         static_cast<InsetWrap*>(it->inset);
97
98                                 il->addToToc(toclist, buf);
99                         }
100                 }
101         }
102         return toclist;
103 }
104
105
106 vector<string> const getTypes(Buffer const & buffer)
107 {
108         vector<string> types;
109
110         TocList const tmp = getTocList(buffer);
111
112         TocList::const_iterator cit = tmp.begin();
113         TocList::const_iterator end = tmp.end();
114
115         for (; cit != end; ++cit) {
116                 types.push_back(cit->first);
117         }
118
119         return types;
120 }
121
122
123 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
124 {
125         TocList const toc_list = getTocList(buffer);
126         TocList::const_iterator cit = toc_list.find(type);
127         if (cit != toc_list.end()) {
128                 Toc::const_iterator ccit = cit->second.begin();
129                 Toc::const_iterator end = cit->second.end();
130                 for (; ccit != end; ++ccit)
131                         os << ccit->asString() << '\n';
132         }
133 }
134
135
136 } // namespace toc
137 } // namespace lyx