]> git.lyx.org Git - lyx.git/blob - src/toc.C
Make buffer's member variables private; use accessor functions.
[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 "funcrequest.h"
18 #include "iterators.h"
19 #include "LyXAction.h"
20 #include "paragraph.h"
21
22 #include "frontends/LyXView.h"
23
24 #include "insets/insetfloat.h"
25 #include "insets/insetwrap.h"
26
27 #include "support/tostr.h"
28
29 using std::vector;
30 using std::max;
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         BufferParams const & bufparams = buf.params();
71         LyXTextClass const & textclass = bufparams.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 + bufparams.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