]> git.lyx.org Git - lyx.git/blob - src/toc.C
Fix bug 886 and others not reported related with the document paper size.
[lyx.git] / src / toc.C
1 /**
2  * \file toc.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "toc.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "FloatList.h"
19 #include "funcrequest.h"
20 #include "gettext.h"
21 #include "LyXAction.h"
22 #include "paragraph.h"
23 #include "pariterator.h"
24
25 #include "frontends/LyXView.h"
26
27 #include "insets/insetfloat.h"
28 #include "insets/insetoptarg.h"
29 #include "insets/insetwrap.h"
30
31 #include "support/convert.h"
32
33 using std::vector;
34 using std::max;
35 using std::ostream;
36 using std::string;
37
38 namespace lyx {
39 namespace toc {
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 = convert<string>(id_);
50         lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
51 }
52
53
54 FuncRequest TocItem::action() const
55 {
56         return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(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 string const getGuiName(string const & cmdName, Buffer const & buffer)
71 {
72         FloatList const & floats =
73                 buffer.params().getLyXTextClass().floats();
74         if (floats.typeExist(cmdName))
75                 return _(floats.getType(cmdName).name());
76         else
77                 return getType(cmdName);
78 }
79
80
81 TocList const getTocList(Buffer const & buf)
82 {
83         TocList toclist;
84
85         BufferParams const & bufparams = buf.params();
86         const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
87
88         ParConstIterator pit = buf.par_iterator_begin();
89         ParConstIterator end = buf.par_iterator_end();
90         for (; pit != end; ++pit) {
91
92                 // the string that goes to the toc (could be the optarg)
93                 string tocstring;
94
95                 // For each paragraph, traverse its insets and look for
96                 // FLOAT_CODE or WRAP_CODE
97                 InsetList::const_iterator it = pit->insetlist.begin();
98                 InsetList::const_iterator end = pit->insetlist.end();
99                 for (; it != end; ++it) {
100                         switch (it->inset->lyxCode()) {
101                         case InsetBase::FLOAT_CODE:
102                                 static_cast<InsetFloat*>(it->inset)
103                                         ->addToToc(toclist, buf);
104                                 break;
105                         case InsetBase::WRAP_CODE:
106                                 static_cast<InsetWrap*>(it->inset)
107                                         ->addToToc(toclist, buf);
108                                 break;
109                         case InsetBase::OPTARG_CODE: {
110                                 if (!tocstring.empty())
111                                         break;
112                                 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
113                                 if (!pit->getLabelstring().empty())
114                                         tocstring = pit->getLabelstring()
115                                                 + ' ';
116                                 tocstring += par.asString(buf, false);
117                                 break;
118                         }
119                         default:
120                                 break;
121                         }
122                 }
123
124                 /// now the toc entry for the paragraph
125                 int const toclevel = pit->layout()->toclevel;
126                 if (toclevel >= min_toclevel
127                     && toclevel <= bufparams.tocdepth) {
128                         // insert this into the table of contents
129                         if (tocstring.empty())
130                                 tocstring = pit->asString(buf, true);
131                         TocItem const item(pit->id(), toclevel - min_toclevel,
132                                            tocstring);
133                         toclist[_("TOC")].push_back(item);
134                 }
135         }
136         return toclist;
137 }
138
139
140 vector<string> const getTypes(Buffer const & buffer)
141 {
142         vector<string> types;
143
144         TocList const tmp = getTocList(buffer);
145
146         TocList::const_iterator cit = tmp.begin();
147         TocList::const_iterator end = tmp.end();
148
149         for (; cit != end; ++cit) {
150                 types.push_back(cit->first);
151         }
152
153         return types;
154 }
155
156
157 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
158 {
159         TocList const toc_list = getTocList(buffer);
160         TocList::const_iterator cit = toc_list.find(type);
161         if (cit != toc_list.end()) {
162                 Toc::const_iterator ccit = cit->second.begin();
163                 Toc::const_iterator end = cit->second.end();
164                 for (; ccit != end; ++ccit)
165                         os << ccit->asString() << '\n';
166         }
167 }
168
169
170 } // namespace toc
171 } // namespace lyx