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