]> git.lyx.org Git - lyx.git/blob - src/toc.C
GTK graphics dialog: Default to scaling 100% when no scaling or size is given
[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 "LyXAction.h"
21 #include "paragraph.h"
22 #include "pariterator.h"
23
24 #include "frontends/LyXView.h"
25
26 #include "insets/insetfloat.h"
27 #include "insets/insetoptarg.h"
28 #include "insets/insetwrap.h"
29
30 #include "support/convert.h"
31
32 using std::vector;
33 using std::max;
34 using std::ostream;
35 using std::string;
36
37 namespace lyx {
38 namespace toc {
39
40 string const TocItem::asString() const
41 {
42         return string(4 * depth, ' ') + str;
43 }
44
45
46 void TocItem::goTo(LyXView & lv_) const
47 {
48         string const tmp = convert<string>(id_);
49         lv_.dispatch(FuncRequest(LFUN_GOTO_PARAGRAPH, tmp));
50 }
51
52
53 FuncRequest TocItem::action() const
54 {
55         return FuncRequest(LFUN_GOTO_PARAGRAPH, convert<string>(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 string const getGuiName(string const & type, Buffer const & buffer)
70 {
71         FloatList const & floats =
72                 buffer.params().getLyXTextClass().floats();
73         if (floats.typeExist(type))
74                 return floats.getType(type).name();
75         else
76                 return type;
77 }
78
79
80 TocList const getTocList(Buffer const & buf)
81 {
82         TocList toclist;
83
84         BufferParams const & bufparams = buf.params();
85         const int min_toclevel = bufparams.getLyXTextClass().min_toclevel();
86
87         ParConstIterator pit = buf.par_iterator_begin();
88         ParConstIterator end = buf.par_iterator_end();
89         for (; pit != end; ++pit) {
90
91                 // the string that goes to the toc (could be the optarg)
92                 string tocstring;
93
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);
103                                 break;
104                         case InsetBase::WRAP_CODE:
105                                 static_cast<InsetWrap*>(it->inset)
106                                         ->addToToc(toclist, buf);
107                                 break;
108                         case InsetBase::OPTARG_CODE: {
109                                 if (!tocstring.empty())
110                                         break;
111                                 Paragraph const & par = *static_cast<InsetOptArg*>(it->inset)->paragraphs().begin();
112                                 if (!pit->getLabelstring().empty())
113                                         tocstring = pit->getLabelstring()
114                                                 + ' ';
115                                 tocstring += par.asString(buf, false);
116                                 break;
117                         }
118                         default:
119                                 break;
120                         }
121                 }
122
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,
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