]> git.lyx.org Git - lyx.git/blob - src/toc.C
53e55d80f392cdd8117b3e88b72aec777bddd88c
[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 "lyxtextclasslist.h"
30 #include "insets/insetfloat.h"
31 #include "debug.h"
32
33 using std::vector;
34
35 extern LyXAction lyxaction;
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(LFUN_GOTO_PARAGRAPH, tmp, false);
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         Paragraph * par = buf->paragraph;
74
75         LyXTextClass const & textclass = textclasslist[buf->params.textclass];
76         bool found = textclass.hasLayout("Caption");
77         string const layout("Caption");
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                 // For each paragraph, traverse its insets and look for
94                 // FLOAT_CODE
95
96                 if (found) {
97                         Paragraph::inset_iterator it =
98                                 par->inset_iterator_begin();
99                         Paragraph::inset_iterator end =
100                                 par->inset_iterator_end();
101
102                         for (; it != end; ++it) {
103                                 if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
104                                         InsetFloat * il =
105                                                 static_cast<InsetFloat*>(*it);
106
107                                         string const type = il->type();
108
109                                         // Now find the caption in the float...
110                                         // We now tranverse the paragraphs of
111                                         // the inset...
112                                         Paragraph * tmp = il->inset.paragraph();
113                                         while (tmp) {
114                                                 if (tmp->layout()->name() == layout) {
115                                                         string const str =
116                                                                 tostr(toclist[type].size()+1) + ". " + tmp->asString(buf, false);
117                                                         TocItem const item(tmp, 0 , str);
118                                                         toclist[type].push_back(item);
119                                                 }
120                                                 tmp = tmp->next();
121                                         }
122                                 }
123                         }
124                 } else {
125                         lyxerr << "Caption not found" << endl;
126                 }
127
128                 par = par->next();
129         }
130         return toclist;
131 }
132
133
134 vector<string> const getTypes(Buffer const * buffer)
135 {
136         vector<string> types;
137
138         TocList const tmp = getTocList(buffer);
139
140         TocList::const_iterator cit = tmp.begin();
141         TocList::const_iterator end = tmp.end();
142
143         for (; cit != end; ++cit) {
144                 types.push_back(cit->first);
145         }
146
147         return types;
148 }
149
150
151 void asciiTocList(string const & type, Buffer const * buffer, ostream & os)
152 {
153         TocList const toc_list = getTocList(buffer);
154         TocList::const_iterator cit = toc_list.find(type);
155         if (cit != toc_list.end()) {
156                 Toc::const_iterator ccit = cit->second.begin();
157                 Toc::const_iterator end = cit->second.end();
158                 for (; ccit != end; ++ccit)
159                         os << ccit->asString() << '\n';
160         }
161 }
162
163
164 } // namespace toc
165