]> git.lyx.org Git - lyx.git/blob - src/TocBackend.cpp
Bring window to front after loading a document
[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 TocIterator Toc::item(DocIterator const & dit) const
111 {
112         TocIterator last = begin();
113         TocIterator it = end();
114         if (it == last)
115                 return it;
116
117         --it;
118
119         DocIterator dit_text = dit;
120         if (dit_text.inMathed()) {
121                 // We are only interested in text so remove the math CursorSlice.
122                 while (dit_text.inMathed())
123                         dit_text.pop_back();
124         }
125
126         for (; it != last; --it) {
127                 // We verify that we don't compare contents of two
128                 // different document. This happens when you
129                 // have parent and child documents.
130                 if (&it->dit_[0].inset() != &dit_text[0].inset())
131                         continue;
132                 if (it->dit_ <= dit_text)
133                         return it;
134         }
135
136         // We are before the first Toc Item:
137         return last;
138 }
139
140
141 Toc::iterator Toc::item(int depth, docstring const & str)
142 {
143         if (empty())
144                 return end();
145         iterator it = begin();
146         iterator itend = end();
147         for (; it != itend; ++it) {
148                 if (it->depth() == depth && it->str() == str)
149                         break;
150         }
151         return it;
152 }
153
154
155 ///////////////////////////////////////////////////////////////////////////
156 //
157 // TocBuilder implementation
158 //
159 ///////////////////////////////////////////////////////////////////////////
160
161 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
162         : toc_(toc ? toc : lyx::make_shared<Toc>()),
163           stack_()
164 {
165         LATTEST(toc);
166 }
167
168 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
169                                                   bool output_active, bool is_captioned)
170 {
171         toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
172         frame f = {
173                 toc_->size() - 1, //pos
174                 is_captioned, //is_captioned
175         };
176         stack_.push(f);
177 }
178
179 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
180                                                          bool output_active)
181 {
182         // first show the float before moving to the caption
183         docstring arg = "paragraph-goto " + paragraph_goto_arg(dit);
184         if (!stack_.empty())
185                 arg = "paragraph-goto " +
186                         paragraph_goto_arg((*toc_)[stack_.top().pos].dit_) + ";" + arg;
187         FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
188         
189         if (!stack_.empty() && !stack_.top().is_captioned) {
190                 // The float we entered has not yet been assigned a caption.
191                 // Assign the caption string to it.
192                 TocItem & captionable = (*toc_)[stack_.top().pos];
193                 captionable.str(s);
194                 captionable.setAction(func);
195                 stack_.top().is_captioned = true;
196         } else {
197                 // This is a new entry.
198                 pop();
199                 // the dit is at the float's level, e.g. for the contextual menu of
200                 // outliner entries
201                 DocIterator captionable_dit = dit;
202                 captionable_dit.pop_back();
203                 pushItem(captionable_dit, s, output_active, true);
204                 (*toc_)[stack_.top().pos].setAction(func);
205         }
206 }
207
208 void TocBuilder::pop()
209 {
210         if (!stack_.empty())
211                 stack_.pop();
212 }
213
214
215
216 ///////////////////////////////////////////////////////////////////////////
217 //
218 // TocBuilderStore implementation
219 //
220 ///////////////////////////////////////////////////////////////////////////
221
222 shared_ptr<TocBuilder> TocBuilderStore::get(string const & type,
223                                                                                         shared_ptr<Toc> toc)
224 {
225         map_t::const_iterator it = map_.find(type);
226         if (it == map_.end()) {
227                 it = map_.insert(std::make_pair(type,
228                                                                         lyx::make_shared<TocBuilder>(toc))).first;
229         }
230         return it->second;
231 }
232
233
234
235 ///////////////////////////////////////////////////////////////////////////
236 //
237 // TocBackend implementation
238 //
239 ///////////////////////////////////////////////////////////////////////////
240
241 shared_ptr<Toc const> TocBackend::toc(string const & type) const
242 {
243         // Is the type already supported?
244         TocList::const_iterator it = tocs_.find(type);
245         LASSERT(it != tocs_.end(), { return lyx::make_shared<Toc>(); });
246         return it->second;
247 }
248
249
250 shared_ptr<Toc> TocBackend::toc(string const & type)
251 {
252         TocList::const_iterator it = tocs_.find(type);
253         if (it == tocs_.end()) {
254                 it = tocs_.insert(std::make_pair(type, lyx::make_shared<Toc>())).first;
255         }
256         return it->second;
257 }
258
259
260 shared_ptr<TocBuilder> TocBackend::builder(string const & type)
261 {
262         return builders_.get(type, toc(type));
263 }
264
265
266 // FIXME: This function duplicates functionality from InsetText::iterateForToc.
267 // Both have their own way of computing the TocItem for "tableofcontents". The
268 // TocItem creation and update should be made in a dedicated function and
269 // updateItem should be rewritten to uniformly update the matching items from
270 // all TOCs.
271 bool TocBackend::updateItem(DocIterator const & dit_in)
272 {
273         // we need a text
274         DocIterator dit = dit_in.getInnerText();
275
276         if (dit.text()->getTocLevel(dit.pit()) == Layout::NOT_IN_TOC)
277                 return false;
278
279         if (toc("tableofcontents")->empty()) {
280                 // FIXME: should not happen, 
281                 // a call to TocBackend::update() is missing somewhere
282                 LYXERR0("TocBackend::updateItem called but the TOC is empty!");
283                 return false;
284         }
285
286         BufferParams const & bufparams = buffer_->params();
287         const int min_toclevel = bufparams.documentClass().min_toclevel();
288
289         TocIterator toc_item = item("tableofcontents", dit);
290
291         docstring tocstring;
292
293         // For each paragraph, traverse its insets and let them add
294         // their toc items
295         //
296         // FIXME: This is supposed to accomplish the same as the body of
297         // InsetText::iterateForToc(), probably
298         Paragraph & par = toc_item->dit_.paragraph();
299         InsetList::const_iterator it = par.insetList().begin();
300         InsetList::const_iterator end = par.insetList().end();
301         for (; it != end; ++it) {
302                 Inset & inset = *it->inset;
303                 if (inset.lyxCode() == ARG_CODE) {
304                         tocstring = par.labelString();
305                         if (!tocstring.empty())
306                                 tocstring += ' ';
307                         inset.asInsetText()->text().forOutliner(tocstring,TOC_ENTRY_LENGTH);
308                         break;
309                 }
310         }
311
312         int const toclevel = toc_item->dit_.text()->
313                 getTocLevel(toc_item->dit_.pit());
314         if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel
315                 && tocstring.empty())
316                 par.forOutliner(tocstring, TOC_ENTRY_LENGTH);
317
318         support::truncateWithEllipsis(tocstring, TOC_ENTRY_LENGTH);
319         const_cast<TocItem &>(*toc_item).str(tocstring);
320
321         buffer_->updateTocItem("tableofcontents", dit);
322         return true;
323 }
324
325
326 void TocBackend::update(bool output_active, UpdateType utype)
327 {
328         for (TocList::iterator it = tocs_.begin(); it != tocs_.end(); ++it)
329                 it->second->clear();
330         tocs_.clear();
331         builders_.clear();
332         if (!buffer_->isInternal()) {
333                 DocIterator dit;
334                 buffer_->inset().addToToc(dit, output_active, utype);
335         }
336 }
337
338
339 TocIterator TocBackend::item(string const & type,
340                 DocIterator const & dit) const
341 {
342         TocList::const_iterator toclist_it = tocs_.find(type);
343         // Is the type supported?
344         // We will try to make the best of it in release mode
345         LASSERT(toclist_it != tocs_.end(), toclist_it = tocs_.begin());
346         return toclist_it->second->item(dit);
347 }
348
349
350 void TocBackend::writePlaintextTocList(string const & type,
351         odocstringstream & os, size_t max_length) const
352 {
353         TocList::const_iterator cit = tocs_.find(type);
354         if (cit != tocs_.end()) {
355                 TocIterator ccit = cit->second->begin();
356                 TocIterator end = cit->second->end();
357                 for (; ccit != end; ++ccit) {
358                         os << ccit->asString() << from_utf8("\n");
359                         if (os.str().size() > max_length)
360                                 break;
361                 }
362         }
363 }
364
365
366 docstring TocBackend::outlinerName(std::string const & type) const
367 {
368         return translateIfPossible(
369             buffer_->params().documentClass().outlinerName(type));
370 }
371
372
373 } // namespace lyx