]> git.lyx.org Git - lyx.git/blob - src/toc.C
5627acb83c25d08972da387f318769ed483ea952
[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 #ifdef __GNUG__
19 #pragma implementation
20 #endif
21
22 #include "support/lstrings.h"
23 #include "toc.h"
24 #include "buffer.h"
25 #include "frontends/LyXView.h"
26 #include "lyxfunc.h"
27 #include "LyXAction.h"
28 #include "paragraph.h"
29 #include "insets/insetfloat.h"
30 #include "debug.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(par->id());
49         lv_.getLyXFunc()->dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
50 }
51
52
53 int TocItem::action() const
54 {
55         return lyxaction.getPseudoAction(LFUN_GOTO_PARAGRAPH,
56                                          tostr(par->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         Paragraph * par = &*(buf->paragraphs.begin());
76
77         LyXTextClass const & textclass = buf->params.getLyXTextClass();
78
79         while (par) {
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, 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
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                         }
104                 }
105
106                 par = par->next();
107         }
108         return toclist;
109 }
110
111
112 vector<string> const getTypes(Buffer const * buffer)
113 {
114         vector<string> types;
115
116         TocList const tmp = getTocList(buffer);
117
118         TocList::const_iterator cit = tmp.begin();
119         TocList::const_iterator end = tmp.end();
120
121         for (; cit != end; ++cit) {
122                 types.push_back(cit->first);
123         }
124
125         return types;
126 }
127
128
129 void asciiTocList(string const & type, Buffer const * buffer, ostream & os)
130 {
131         TocList const toc_list = getTocList(buffer);
132         TocList::const_iterator cit = toc_list.find(type);
133         if (cit != toc_list.end()) {
134                 Toc::const_iterator ccit = cit->second.begin();
135                 Toc::const_iterator end = cit->second.end();
136                 for (; ccit != end; ++ccit)
137                         os << ccit->asString() << '\n';
138         }
139 }
140
141
142 } // namespace toc