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