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 "funcrequest.h"
19 #include "LyXAction.h"
20 #include "paragraph.h"
21 #include "pariterator.h"
23 #include "frontends/LyXView.h"
25 #include "insets/insetfloat.h"
26 #include "insets/insetoptarg.h"
27 #include "insets/insetwrap.h"
29 #include "support/convert.h"
39 string const TocItem::asString() const
41 return string(4 * depth, ' ') + str;
45 void TocItem::goTo(LyXView & lv_) const
47 string const tmp = convert<string>(id_);
48 lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
52 FuncRequest TocItem::action() const
54 return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
58 string const getType(string const & cmdName)
61 if (cmdName == "tableofcontents")
68 TocList const getTocList(Buffer const & buf)
72 BufferParams const & bufparams = buf.params();
73 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
75 ParConstIterator pit = buf.par_iterator_begin();
76 ParConstIterator end = buf.par_iterator_end();
77 for (; pit != end; ++pit) {
79 // the string that goes to the toc (could be the optarg)
82 // For each paragraph, traverse its insets and look for
83 // FLOAT_CODE or WRAP_CODE
84 InsetList::const_iterator it = pit->insetlist.begin();
85 InsetList::const_iterator end = pit->insetlist.end();
86 for (; it != end; ++it) {
87 switch (it->inset->lyxCode()) {
88 case InsetBase::FLOAT_CODE:
89 static_cast<InsetFloat*>(it->inset)
90 ->addToToc(toclist, buf);
92 case InsetBase::WRAP_CODE:
93 static_cast<InsetWrap*>(it->inset)
94 ->addToToc(toclist, buf);
96 case InsetBase::OPTARG_CODE: {
97 if (!tocstring.empty())
99 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
100 if (!pit->getLabelstring().empty())
101 tocstring = pit->getLabelstring()
103 tocstring += par.asString(buf, false);
111 /// now the toc entry for the paragraph
112 int const toclevel = pit->layout()->toclevel;
113 if (toclevel >= min_toclevel
114 && toclevel <= bufparams.tocdepth) {
115 // insert this into the table of contents
116 if (tocstring.empty())
117 tocstring = pit->asString(buf, true);
118 TocItem const item(pit->id(), toclevel - min_toclevel,
120 toclist["TOC"].push_back(item);
127 vector<string> const getTypes(Buffer const & buffer)
129 vector<string> types;
131 TocList const tmp = getTocList(buffer);
133 TocList::const_iterator cit = tmp.begin();
134 TocList::const_iterator end = tmp.end();
136 for (; cit != end; ++cit) {
137 types.push_back(cit->first);
144 void asciiTocList(string const & type, Buffer const & buffer, ostream & os)
146 TocList const toc_list = getTocList(buffer);
147 TocList::const_iterator cit = toc_list.find(type);
148 if (cit != toc_list.end()) {
149 Toc::const_iterator ccit = cit->second.begin();
150 Toc::const_iterator end = cit->second.end();
151 for (; ccit != end; ++ccit)
152 os << ccit->asString() << '\n';