3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Jean-Marc Lasgouttes
7 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
17 #include "bufferparams.h"
18 #include "FloatList.h"
19 #include "funcrequest.h"
20 #include "LyXAction.h"
21 #include "paragraph.h"
22 #include "pariterator.h"
24 #include "frontends/LyXView.h"
26 #include "insets/insetfloat.h"
27 #include "insets/insetoptarg.h"
28 #include "insets/insetwrap.h"
30 #include "support/convert.h"
40 string const TocItem::asString() const
42 return string(4 * depth, ' ') + str;
46 void TocItem::goTo(LyXView & lv_) const
48 string const tmp = convert<string>(id_);
49 lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
53 FuncRequest TocItem::action() const
55 return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
59 string const getType(string const & cmdName)
62 if (cmdName == "tableofcontents")
69 string const getGuiName(string const & type, Buffer const & buffer)
71 FloatList const & floats =
72 buffer.params().getLyXTextClass().floats();
73 if (floats.typeExist(type))
74 return floats.getType(type).name();
80 TocList const getTocList(Buffer const & buf)
84 BufferParams const & bufparams = buf.params();
85 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
87 ParConstIterator pit = buf.par_iterator_begin();
88 ParConstIterator end = buf.par_iterator_end();
89 for (; pit != end; ++pit) {
91 // the string that goes to the toc (could be the optarg)
94 // For each paragraph, traverse its insets and look for
95 // FLOAT_CODE or WRAP_CODE
96 InsetList::const_iterator it = pit->insetlist.begin();
97 InsetList::const_iterator end = pit->insetlist.end();
98 for (; it != end; ++it) {
99 switch (it->inset->lyxCode()) {
100 case InsetBase::FLOAT_CODE:
101 static_cast<InsetFloat*>(it->inset)
102 ->addToToc(toclist, buf);
104 case InsetBase::WRAP_CODE:
105 static_cast<InsetWrap*>(it->inset)
106 ->addToToc(toclist, buf);
108 case InsetBase::OPTARG_CODE: {
109 if (!tocstring.empty())
111 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
112 if (!pit->getLabelstring().empty())
113 tocstring = pit->getLabelstring()
115 tocstring += par.asString(buf, false);
123 /// now the toc entry for the paragraph
124 int const toclevel = pit->layout()->toclevel;
125 if (toclevel != LyXLayout::NOT_IN_TOC
126 && 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,
133 toclist["TOC"].push_back(item);
140 vector<string> const getTypes(Buffer const & buffer)
142 vector<string> types;
144 TocList const tmp = getTocList(buffer);
146 TocList::const_iterator cit = tmp.begin();
147 TocList::const_iterator end = tmp.end();
149 for (; cit != end; ++cit) {
150 types.push_back(cit->first);
157 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
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';