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"
21 #include "LyXAction.h"
22 #include "paragraph.h"
23 #include "pariterator.h"
25 #include "frontends/LyXView.h"
27 #include "insets/insetfloat.h"
28 #include "insets/insetoptarg.h"
29 #include "insets/insetwrap.h"
31 #include "support/convert.h"
41 string const TocItem::asString() const
43 return string(4 * depth, ' ') + str;
47 void TocItem::goTo(LyXView & lv_) const
49 string const tmp = convert<string>(id_);
50 lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
54 FuncRequest TocItem::action() const
56 return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(id_));
60 string const getType(string const & cmdName)
63 if (cmdName == "tableofcontents")
70 string const getGuiName(string const & cmdName, Buffer const & buffer)
72 FloatList const & floats =
73 buffer.params().getLyXTextClass().floats();
74 if (floats.typeExist(cmdName))
75 return _(floats.getType(cmdName).name());
77 return getType(cmdName);
81 TocList const getTocList(Buffer const & buf)
85 BufferParams const & bufparams = buf.params();
86 const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
88 ParConstIterator pit = buf.par_iterator_begin();
89 ParConstIterator end = buf.par_iterator_end();
90 for (; pit != end; ++pit) {
92 // the string that goes to the toc (could be the optarg)
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);
105 case InsetBase::WRAP_CODE:
106 static_cast<InsetWrap*>(it->inset)
107 ->addToToc(toclist, buf);
109 case InsetBase::OPTARG_CODE: {
110 if (!tocstring.empty())
112 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
113 if (!pit->getLabelstring().empty())
114 tocstring = pit->getLabelstring()
116 tocstring += par.asString(buf, false);
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,
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';