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