]> git.lyx.org Git - lyx.git/blob - src/toc.C
e0398fb09d208bbf1f4032d86938a17ea6300169
[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 extern LyXAction lyxaction;
38
39 namespace toc
40 {
41
42 string const TocItem::asString() const
43 {
44         return string(4 * depth, ' ') + str;
45 }
46
47
48 void TocItem::goTo(LyXView & lv_) const
49 {
50         string const tmp = tostr(par->id());
51         lv_.getLyXFunc()->dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
52 }
53
54
55 int TocItem::action() const
56 {
57         return lyxaction.getPseudoAction(LFUN_GOTO_PARAGRAPH,
58                                          tostr(par->id()));
59 }
60
61
62 string const getType(string const & cmdName)
63 {
64         // special case
65         if (cmdName == "tableofcontents")
66                 return "TOC";
67         else
68                 return cmdName;
69 }
70
71
72 TocList const getTocList(Buffer const * buf)
73 {
74         TocList toclist;
75         if (!buf)
76                 return toclist;
77         Paragraph * par = buf->paragraph;
78
79         LyXTextClass const & textclass = buf->params.getLyXTextClass();
80
81         while (par) {
82 #ifdef WITH_WARNINGS
83 #warning bogus type (Lgb)
84 #endif
85                 char const labeltype = par->layout()->labeltype;
86
87                 if (labeltype >= LABEL_COUNTER_CHAPTER
88                     && labeltype <= LABEL_COUNTER_CHAPTER + buf->params.tocdepth) {
89                                 // insert this into the table of contents
90                         const int depth = max(0, labeltype - textclass.maxcounter());
91                         TocItem const item(par, depth,
92                                            par->asString(buf, true));
93                         toclist["TOC"].push_back(item);
94                 }
95
96                 // For each paragraph, traverse its insets and look for
97                 // FLOAT_CODE
98                 Paragraph::inset_iterator it = par->inset_iterator_begin();
99                 Paragraph::inset_iterator end = par->inset_iterator_end();
100                 for (; it != end; ++it) {
101                         if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
102                                 InsetFloat * il =
103                                         static_cast<InsetFloat*>(*it);
104                                 il->addToToc(toclist, buf);
105                         }
106                 }
107
108                 par = par->next();
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