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