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