]> git.lyx.org Git - lyx.git/blob - src/TocBuilder.cpp
Avoid full metrics computation with Update:FitCursor
[lyx.git] / src / TocBuilder.cpp
1 /**
2  * \file TocBuilder.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Guillaume Munch
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "TocBuilder.h"
14
15 #include "DocIterator.h"
16 #include "TocBackend.h"
17
18 #include "support/lassert.h"
19
20 using namespace std;
21
22
23 namespace lyx {
24
25
26
27 TocBuilder::TocBuilder(shared_ptr<Toc> toc)
28         : toc_(toc ? toc : make_shared<Toc>()),
29           stack_()
30 {
31         LATTEST(toc);
32 }
33
34 void TocBuilder::pushItem(DocIterator const & dit, docstring const & s,
35                           bool output_active, bool is_captioned)
36 {
37         toc_->push_back(TocItem(dit, stack_.size(), s, output_active));
38         frame f = {
39                 toc_->size() - 1, //pos
40                 is_captioned, //is_captioned
41         };
42         stack_.push(f);
43 }
44
45 void TocBuilder::captionItem(DocIterator const & dit, docstring const & s,
46                              bool output_active)
47 {
48         // first show the float before moving to the caption
49         docstring arg = "paragraph-goto " + dit.paragraphGotoArgument();
50         if (!stack_.empty())
51                 arg = "paragraph-goto " +
52                         (*toc_)[stack_.top().pos].dit().paragraphGotoArgument() + ";" + arg;
53         FuncRequest func(LFUN_COMMAND_SEQUENCE, arg);
54
55         if (!stack_.empty() && !stack_.top().is_captioned) {
56                 // The float we entered has not yet been assigned a caption.
57                 // Assign the caption string to it.
58                 TocItem & captionable = (*toc_)[stack_.top().pos];
59                 captionable.str(s);
60                 captionable.setAction(func);
61                 stack_.top().is_captioned = true;
62         } else {
63                 // This is a new entry.
64                 // the dit is at the float's level, e.g. for the contextual menu of
65                 // outliner entries
66                 DocIterator captionable_dit = dit;
67                 captionable_dit.pop_back();
68                 pushItem(captionable_dit, s, output_active, true);
69                 (*toc_)[stack_.top().pos].setAction(func);
70                 pop();
71         }
72 }
73
74 void TocBuilder::argumentItem(docstring const & arg_str)
75 {
76         if (stack_.empty() || arg_str.empty())
77                 return;
78         TocItem & item = (*toc_)[stack_.top().pos];
79         docstring const & str = item.str();
80         string const & delim =
81                 (str.empty() || !stack_.top().is_captioned) ? "" :  ", ";
82         item.str(str + from_ascii(delim) + arg_str);
83         stack_.top().is_captioned = true;
84 }
85
86 void TocBuilder::pop()
87 {
88         if (!stack_.empty())
89                 stack_.pop();
90 }
91
92
93 } // namespace lyx