]> git.lyx.org Git - lyx.git/blob - src/toc.C
removed a spurious static_cast
[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                 Paragraph const * par = *pit;
82
83 #ifdef WITH_WARNINGS
84 #warning bogus type (Lgb)
85 #endif
86                 char const labeltype = par->layout()->labeltype;
87
88                 if (labeltype >= LABEL_COUNTER_CHAPTER
89                     && labeltype <= LABEL_COUNTER_CHAPTER + buf->params.tocdepth) {
90                                 // insert this into the table of contents
91                         const int depth = max(0, labeltype - textclass.maxcounter());
92                         TocItem const item(par->id(), depth,
93                                            par->asString(buf, true));
94                         toclist["TOC"].push_back(item);
95                 }
96
97                 // For each paragraph, traverse its insets and look for
98                 // FLOAT_CODE or WRAP_CODE
99                 InsetList::iterator it = par->insetlist.begin();
100                 InsetList::iterator end = par->insetlist.end();
101                 for (; it != end; ++it) {
102                         if (it.getInset()->lyxCode() == Inset::FLOAT_CODE) {
103                                 InsetFloat * il =
104                                         static_cast<InsetFloat*>(it.getInset());
105                                 il->addToToc(toclist, buf);
106                         } else if (it.getInset()->lyxCode() == Inset::WRAP_CODE) {
107                                 InsetWrap * il =
108                                         static_cast<InsetWrap*>(it.getInset());
109                                 il->addToToc(toclist, buf);
110                         }
111                 }
112         }
113         return toclist;
114 }
115
116
117 vector<string> const getTypes(Buffer const * buffer)
118 {
119         vector<string> types;
120
121         TocList const tmp = getTocList(buffer);
122
123         TocList::const_iterator cit = tmp.begin();
124         TocList::const_iterator end = tmp.end();
125
126         for (; cit != end; ++cit) {
127                 types.push_back(cit->first);
128         }
129
130         return types;
131 }
132
133
134 void asciiTocList(string const & type, Buffer const * buffer, ostream & os)
135 {
136         TocList const toc_list = getTocList(buffer);
137         TocList::const_iterator cit = toc_list.find(type);
138         if (cit != toc_list.end()) {
139                 Toc::const_iterator ccit = cit->second.begin();
140                 Toc::const_iterator end = cit->second.end();
141                 for (; ccit != end; ++ccit)
142                         os << ccit->asString() << '\n';
143         }
144 }
145
146
147 } // namespace toc