]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
fix #9826: Outline disclosure of subsection content disappears one second after
[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  * \author Guillaume Munch
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13
14 #include <config.h>
15
16 #include "TocBackend.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "Cursor.h"
21 #include "FloatList.h"
22 #include "FuncRequest.h"
23 #include "InsetList.h"
24 #include "Layout.h"
25 #include "LyXAction.h"
26 #include "Paragraph.h"
27 #include "ParIterator.h"
28 #include "TextClass.h"
29
30 #include "insets/InsetArgument.h"
31
32 #include "support/convert.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lassert.h"
37 #include "support/lstrings.h"
38
39 using namespace std;
40
41
42 namespace lyx {
43
44
45 ///////////////////////////////////////////////////////////////////////////
46 //
47 // TocItem implementation
48 //
49 ///////////////////////////////////////////////////////////////////////////
50
51 TocItem::TocItem(DocIterator const & dit, int d, docstring const & s,
52                         bool output_active, docstring const & t, FuncRequest action) :
53         dit_(dit), depth_(d), str_(s), tooltip_(t), output_(output_active),
54         action_(action)
55 {
56 }
57
58
59 int TocItem::id() const
60 {
61         return dit_.paragraph().id();
62 }
63
64
65 docstring const & TocItem::tooltip() const
66 {
67         return tooltip_.empty() ? str_ : tooltip_;
68 }
69
70
71 docstring const TocItem::asString() const
72 {
73         static char_type const cross = 0x2716; // ✖ U+2716 HEAVY MULTIPLICATION X
74         static char_type const thin = 0x2009; // U+2009 THIN SPACE
75         docstring prefix;
76         if (!output_) {
77                 prefix += cross;
78                 prefix += thin;
79         }
80         return prefix + str_;
81 }
82
83 namespace {
84
85 // convert a DocIterator into an argument to LFUN_PARAGRAPH_GOTO 
86 docstring paragraph_goto_arg(DocIterator const & dit)
87 {
88         CursorSlice const & s = dit.innerTextSlice();
89         return convert<docstring>(s.paragraph().id()) + ' ' +
90                 convert<docstring>(s.pos());
91 }
92
93 } // namespace anon
94
95 FuncRequest TocItem::action() const
96 {
97         if (action_.action() == LFUN_UNKNOWN_ACTION) {
98                 return FuncRequest(LFUN_PARAGRAPH_GOTO, paragraph_goto_arg(dit_));
99         } else
100                 return action_;
101 }
102
103
104 ///////////////////////////////////////////////////////////////////////////
105 //
106 // Toc implementation
107 //
108 ///////////////////////////////////////////////////////////////////////////
109
110 Toc::const_iterator TocBackend::findItem(Toc const & toc,
111                                          DocIterator const & dit)
112 {
113         Toc::const_iterator last = toc.begin();
114         Toc::const_iterator it = toc.end();
115         if (it == last)
116                 return it;
117         --it;
118         DocIterator dit_text = dit.getInnerText();
119
120         for (; it != last; --it) {
121                 // We verify that we don't compare contents of two
122                 // different document. This happens when you
123                 // have parent and child documents.
124                 if (&it->dit_[0].inset() != &dit_text[0].inset())
125                         continue;
126                 if (it->dit_ <= dit_text)
127                         return it;
128         }
129
130         // We are before the first Toc Item:
131         return last;
132 }
133
134
135 Toc::iterator TocBackend::findItem(Toc & toc, int depth, docstring const & str)
136 {
137         if (toc.empty())
138                 return toc.end();
139         Toc::iterator it = toc.begin();
140         Toc::iterator itend = toc.end();
141         for (; it != itend; ++it) {
142                 if (it->depth() == depth && it->str() == str)
143                         break;
144         }
145         return it;
146 }
147
148
149 ///////////////////////////////////////////////////////////////////////////
150 //
151 // TocBuilder implementation
152 //
153 ///////////////////////////////////////////////////////////////////////////
154
155 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
156         : toc_(toc ? toc : lyx::make_shared<Toc>()),
157           stack_()
158 {
159         LATTEST(toc);
160 }
161
162 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
163                                                   bool output_active, bool is_captioned)
164 {
165         toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
166         frame f = {
167                 toc_->size() - 1, //pos
168                 is_captioned, //is_captioned
169         };
170         stack_.push(f);
171 }
172
173 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
174                                                          bool output_active)
175 {
176         // first show the float before moving to the caption
177         docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
178         if (!stack_.empty())
179                 arg = "paragraph-goto " +
180                         paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
181         FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
182
183         if (!stack_.empty() && !stack_.top().is_captioned) {
184                 // The float we entered has not yet been assigned a caption.
185                 // Assign the caption string to it.
186                 TocItem & captionable = (*toc_)[stack_.top().pos];
187                 captionable.str(s);
188                 captionable.setAction(func);
189                 stack_.top().is_captioned = true;
190         } else {
191                 // This is a new entry.
192                 pop();
193                 // the dit is at the float's level, e.g. for the contextual menu of
194                 // outliner entries
195                 DocIterator captionable_dit = dit;
196                 captionable_dit.pop_back();
197                 pushItem(captionable_dit, s, output_active, true);
198                 (*toc_)[stack_.top().pos].setAction(func);
199         }
200 }
201
202 void TocBuilder::pop()
203 {
204         if (!stack_.empty())
205                 stack_.pop();
206 }
207
208
209
210 ///////////////////////////////////////////////////////////////////////////
211 //
212 // TocBuilderStore implementation
213 //
214 ///////////////////////////////////////////////////////////////////////////
215
216 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
217                                                                                         shared_ptr<Toc> toc)
218 {
219         map_t::const_iterator it = map_.find(type);
220         if (it == map_.end()) {
221                 it = map_.insert(std::make_pair(type,
222                                                                         lyx::make_shared<TocBuilder>(toc))).first;
223         }
224         return it->second;
225 }
226
227
228
229 ///////////////////////////////////////////////////////////////////////////
230 //
231 // TocBackend implementation
232 //
233 ///////////////////////////////////////////////////////////////////////////
234
235 shared_ptr<Toc const> TocBackend::toc(string const & type) const
236 {
237         // Is the type already supported?
238         TocList::const_iterator it = tocs_.find(type);
239         LASSERT(it != tocs_.end(), { return lyx::make_shared<Toc>(); });
240         return it->second;
241 }
242
243
244 shared_ptr<Toc> TocBackend::toc(string const & type)
245 {
246         TocList::const_iterator it = tocs_.find(type);
247         if (it == tocs_.end()) {
248                 it = tocs_.insert(std::make_pair(type, lyx::make_shared<Toc>())).first;
249         }
250         return it->second;
251 }
252
253
254 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
255 {
256         return builders_.get(type, toc(type));
257 }
258
259
260 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
261 // Both have their own way of computing the TocItem for "tableofcontents". The
262 // TocItem creation and update should be made in a dedicated function and
263 // updateItem should be rewritten to uniformly update the matching items from
264 // all TOCs.
265 bool TocBackend::updateItem(DocIterator const & dit_in)
266 {
267         // we need a text
268         DocIterator dit = dit_in.getInnerText();
269
270         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
271                 return false;
272
273         if (toc("tableofcontents")->empty()) {
274                 // FIXME: should not happen, 
275                 // a call to TocBackend::update() is missing somewhere
276                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
277                 return false;
278         }
279
280         BufferParams const & bufparams = buffer_->params();
281         const int min_toclevel = bufparams.documentClass().min_toclevel();
282
283         Toc::const_iterator toc_item = item("tableofcontents", dit);
284
285         docstring tocstring;
286
287         // For each paragraph, traverse its insets and let them add
288         // their toc items
289         //
290         // FIXME: This is supposed to accomplish the same as the body of
291         // InsetText::iterateForToc(), probably
292         Paragraph & par = toc_item->dit_.paragraph();
293         InsetList::const_iterator it = par.insetList().begin();
294         InsetList::const_iterator end = par.insetList().end();
295         for (; it != end; ++it) {
296                 Inset & inset = *it->inset;
297                 if (inset.lyxCode() == ARG_CODE) {
298                         tocstring = par.labelString();
299                         if (!tocstring.empty())
300                                 tocstring += ' ';
301                         inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
302                         break;
303                 }
304         }
305
306         int const toclevel = toc_item->dit_.text()->
307                 getTocLevel(toc_item->dit_.pit());
308         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
309                 && tocstring.empty())
310                 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
311
312         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
313         const_cast<TocItem &>(*toc_item).str(tocstring);
314
315         buffer_->updateTocItem("tableofcontents", dit);
316         return true;
317 }
318
319
320 void TocBackend::update(bool output_active, UpdateType utype)
321 {
322         for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
323                 it->second->clear();
324         tocs_.clear();
325         builders_.clear();
326         if (!buffer_->isInternal()) {
327                 DocIterator dit;
328                 buffer_->inset().addToToc(dit, output_active, utype);
329         }
330 }
331
332
333 Toc::const_iterator TocBackend::item(string const & type,
334                                      DocIterator const & dit) const
335 {
336         TocList::const_iterator toclist_it = tocs_.find(type);
337         // Is the type supported?
338         // We will try to make the best of it in release mode
339         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
340         return findItem(*toclist_it->second, dit);
341 }
342
343
344 void TocBackend::writePlaintextTocList(string const & type,
345         odocstringstream & os, size_t max_length) const
346 {
347         TocList::const_iterator cit = tocs_.find(type);
348         if (cit != tocs_.end()) {
349                 Toc::const_iterator ccit = cit->second->begin();
350                 Toc::const_iterator end = cit->second->end();
351                 for (; ccit != end; ++ccit) {
352                         os << ccit->asString() << from_utf8("\n");
353                         if (os.str().size() > max_length)
354                                 break;
355                 }
356         }
357 }
358
359
360 docstring TocBackend::outlinerName(std::string const & type) const
361 {
362         return translateIfPossible(
363             buffer_->params().documentClass().outlinerName(type));
364 }
365
366
367 } // namespace lyx