]> git.lyx.org Git - lyx.git/blob - src/toc.C
79c528c1549907c9cae9ed24155f1e80a26506e9
[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 using std::max;
35 using std::endl;
36 using std::ostream;
37
38 extern LyXAction lyxaction;
39
40 namespace toc
41 {
42
43 string const TocItem::asString() const
44 {
45         return string(4 * depth, ' ') + str;
46 }
47
48         
49 void TocItem::goTo(LyXView & lv_) const
50 {
51         string const tmp = tostr(par->id());
52         lv_.getLyXFunc()->dispatch(LFUN_GOTO_PARAGRAPH, tmp, false);
53 }
54
55
56 int TocItem::action() const
57 {
58         return lyxaction.getPseudoAction(LFUN_GOTO_PARAGRAPH,
59                                          tostr(par->id()));
60 }
61
62
63 string const getType(string const & cmdName)
64 {
65         // special case
66         if (cmdName == "tableofcontents")
67                 return "TOC";
68         else
69                 return cmdName;
70 }
71
72
73 TocList const getTocList(Buffer const * buf)
74 {
75         TocList toclist;
76         Paragraph * par = buf->paragraph;
77
78         LyXTextClass const & textclass = textclasslist[buf->params.textclass];
79         bool found = textclass.hasLayout("Caption");
80         string const layout("Caption");
81
82         while (par) {
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, depth,
93                                            par->asString(buf, true));
94                         toclist["TOC"].push_back(item);
95                 }
96                 // For each paragraph, traverse its insets and look for
97                 // FLOAT_CODE
98
99                 if (found) {
100                         Paragraph::inset_iterator it =
101                                 par->inset_iterator_begin();
102                         Paragraph::inset_iterator end =
103                                 par->inset_iterator_end();
104
105                         for (; it != end; ++it) {
106                                 if ((*it)->lyxCode() == Inset::FLOAT_CODE) {
107                                         InsetFloat * il =
108                                                 static_cast<InsetFloat*>(*it);
109
110                                         string const type = il->type();
111
112                                         // Now find the caption in the float...
113                                         // We now tranverse the paragraphs of
114                                         // the inset...
115                                         Paragraph * tmp = il->inset.paragraph();
116                                         while (tmp) {
117                                                 if (tmp->layout()->name() == layout) {
118                                                         string const str =
119                                                                 tostr(toclist[type].size()+1) + ". " + tmp->asString(buf, false);
120                                                         TocItem const item(tmp, 0 , str);
121                                                         toclist[type].push_back(item);
122                                                 }
123                                                 tmp = tmp->next();
124                                         }
125                                 }
126                         }
127                 } else {
128                         lyxerr << "Caption not found" << endl;
129                 }
130
131                 par = par->next();
132         }
133         return toclist;
134 }
135
136
137 vector<string> const getTypes(Buffer const * buffer)
138 {
139         vector<string> types;
140
141         TocList const tmp = getTocList(buffer);
142
143         TocList::const_iterator cit = tmp.begin();
144         TocList::const_iterator end = tmp.end();
145
146         for (; cit != end; ++cit) {
147                 types.push_back(cit->first);
148         }
149
150         return types;
151 }
152
153
154 void asciiTocList(string const & type, Buffer const * buffer, ostream & os)
155 {
156         TocList const toc_list = getTocList(buffer);
157         TocList::const_iterator cit = toc_list.find(type);
158         if (cit != toc_list.end()) {
159                 Toc::const_iterator ccit = cit->second.begin();
160                 Toc::const_iterator end = cit->second.end();
161                 for (; ccit != end; ++ccit)
162                         os << ccit->asString() << '\n';
163         }
164 }
165
166
167 } // namespace toc
168