]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
Reimplement inset-select-all in a generic way
[lyx.git] / src / TocBackend.cpp
1 /**
2  * \file TocBackend.cpp
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  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "TocBackend.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "FloatList.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
22 #include "Layout.h"
23 #include "LyXAction.h"
24 #include "Paragraph.h"
25 #include "ParIterator.h"
26 #include "TextClass.h"
27
28 #include "insets/InsetArgument.h"
29
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33
34 #include "support/lassert.h"
35
36 using namespace std;
37
38
39 namespace lyx {
40
41 ///////////////////////////////////////////////////////////////////////////
42 //
43 // TocItem implementation
44 //
45 ///////////////////////////////////////////////////////////////////////////
46
47 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
48         bool output_active, docstring const & t) :
49   dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active)
50 {
51 }
52
53
54 int TocItem::id() const
55 {
56         return dit_.paragraph().id();
57 }
58
59
60 int TocItem::depth() const
61 {
62         return depth_;
63 }
64
65
66 docstring const & TocItem::str() const
67 {
68         return str_;
69 }
70
71
72 docstring const & TocItem::tooltip() const
73 {
74         return tooltip_.empty() ? str_ : tooltip_;
75 }
76
77
78 docstring const TocItem::asString() const
79 {
80         return docstring(4 * depth_, ' ') + str_;
81 }
82
83
84 DocIterator const & TocItem::dit() const
85 {
86         return dit_;
87 }
88
89
90 FuncRequest TocItem::action() const
91 {
92         string const arg = convert<string>(dit_.paragraph().id())
93                 + ' ' + convert<string>(dit_.pos());
94         return FuncRequest(LFUN_PARAGRAPH_GOTO, arg);
95 }
96
97
98 ///////////////////////////////////////////////////////////////////////////
99 //
100 // TocBackend implementation
101 //
102 ///////////////////////////////////////////////////////////////////////////
103
104 Toc const & TocBackend::toc(string const & type) const
105 {
106         // Is the type already supported?
107         TocList::const_iterator it = tocs_.find(type);
108         LASSERT(it != tocs_.end(), { static Toc dummy; return dummy; });
109
110         return it->second;
111 }
112
113
114 Toc & TocBackend::toc(string const & type)
115 {
116         return tocs_[type];
117 }
118
119
120 bool TocBackend::updateItem(DocIterator const & dit)
121 {
122         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
123                 return false;
124
125         if (toc("tableofcontents").empty()) {
126                 // FIXME: should not happen, 
127                 // a call to TocBackend::update() is missing somewhere
128                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
129                 return false;
130         }
131
132         BufferParams const & bufparams = buffer_->params();
133         const int min_toclevel = bufparams.documentClass().min_toclevel();
134
135         TocIterator toc_item = item("tableofcontents", dit);
136
137         docstring tocstring;
138
139         // For each paragraph, traverse its insets and let them add
140         // their toc items
141         Paragraph & par = toc_item->dit_.paragraph();
142         InsetList::const_iterator it = par.insetList().begin();
143         InsetList::const_iterator end = par.insetList().end();
144         for (; it != end; ++it) {
145                 Inset & inset = *it->inset;
146                 if (inset.lyxCode() == ARG_CODE) {
147                         if (!tocstring.empty())
148                                 break;
149                         Paragraph const & inset_par =
150                                 *static_cast<InsetArgument&>(inset).paragraphs().begin();
151                         if (!par.labelString().empty())
152                                 tocstring = par.labelString() + ' ';
153                         tocstring += inset_par.asString(AS_STR_INSETS);
154                         break;
155                 }
156         }
157
158         int const toclevel = toc_item->dit_.text()->getTocLevel(toc_item->dit_.pit());
159         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
160                 && tocstring.empty())
161                         tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
162
163         const_cast<TocItem &>(*toc_item).str_ = tocstring;
164
165         buffer_->updateTocItem("tableofcontents", dit);
166         return true;
167 }
168
169
170 void TocBackend::update(bool output_active)
171 {
172         tocs_.clear();
173         if (!buffer_->isInternal()) {
174                 DocIterator dit;
175                 buffer_->inset().addToToc(dit, output_active);
176         }
177 }
178
179
180 TocIterator TocBackend::item(string const & type,
181                 DocIterator const & dit) const
182 {
183         TocList::const_iterator toclist_it = tocs_.find(type);
184         // Is the type supported?
185         // We will try to make the best of it in release mode
186         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
187         return toclist_it->second.item(dit);
188 }
189
190
191 TocIterator Toc::item(DocIterator const & dit) const
192 {
193         TocIterator last = begin();
194         TocIterator it = end();
195         if (it == last)
196                 return it;
197
198         --it;
199
200         DocIterator dit_text = dit;
201         if (dit_text.inMathed()) {
202                 // We are only interested in text so remove the math CursorSlice.
203                 while (dit_text.inMathed())
204                         dit_text.pop_back();
205         }
206
207         for (; it != last; --it) {
208                 // We verify that we don't compare contents of two
209                 // different document. This happens when you
210                 // have parent and child documents.
211                 if (&it->dit_[0].inset() != &dit_text[0].inset())
212                         continue;
213                 if (it->dit_ <= dit_text)
214                         return it;
215         }
216
217         // We are before the first Toc Item:
218         return last;
219 }
220
221
222 Toc::iterator Toc::item(int depth, docstring const & str)
223 {
224         if (empty())
225                 return end();
226         iterator it = begin();
227         iterator itend = end();
228         for (; it != itend; ++it) {
229                 if (it->depth() == depth && it->str() == str)
230                         break;
231         }
232         return it;
233 }
234
235
236 void TocBackend::writePlaintextTocList(string const & type,
237         odocstringstream & os, size_t max_length) const
238 {
239         TocList::const_iterator cit = tocs_.find(type);
240         if (cit != tocs_.end()) {
241                 TocIterator ccit = cit->second.begin();
242                 TocIterator end = cit->second.end();
243                 for (; ccit != end; ++ccit) {
244                         os << ccit->asString() << from_utf8("\n");
245                         if (os.str().size() > max_length)
246                                 break;
247                 }
248         }
249 }
250
251
252 } // namespace lyx