]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Merge remote-tracking branch 'origin/master' into features/latexargs
[lyx.git] / src / insets / InsetArgument.cpp
1 /**
2  * \file InsetArgument.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetArgument.h"
14
15 #include "InsetList.h"
16 #include "Layout.h"
17 #include "Lexer.h"
18 #include "ParIterator.h"
19
20 #include "support/convert.h"
21 #include "support/debug.h"
22 #include "support/docstream.h"
23 #include "support/gettext.h"
24 #include "support/lstrings.h"
25
26 using namespace std;
27
28 namespace lyx {
29
30
31 InsetArgument::InsetArgument(Buffer * buf, string const & name)
32     : InsetCollapsable(buf), name_(name), labelstring_(docstring())
33 {}
34
35
36 void InsetArgument::write(ostream & os) const
37 {
38         os << "Argument " << name_ << "\n";
39         InsetCollapsable::write(os);
40 }
41
42 void InsetArgument::read(Lexer & lex)
43 {
44         lex >> name_;
45         InsetCollapsable::read(lex);
46 }
47
48 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
49 {
50         Layout::LaTeXArgMap args;
51         bool const insetlayout = &it.inset() && it.paragraph().layout().latexargs().empty();
52         if (insetlayout)
53                 args = it.inset().getLayout().latexargs();
54         else
55                 args = it.paragraph().layout().latexargs();
56
57         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
58         if (name_ == "999") {
59                 int req = insetlayout ? it.inset().getLayout().requiredArgs()
60                                       : it.paragraph().layout().requiredArgs();
61                 int opts = insetlayout ? it.inset().getLayout().optArgs()
62                                       : it.paragraph().layout().optArgs();
63                 int nr = 0;
64                 int ours = 0;
65                 InsetList::const_iterator parit = it.paragraph().insetList().begin();
66                 InsetList::const_iterator parend = it.paragraph().insetList().end();
67                 for (; parit != parend; ++parit) {
68                         if (parit->inset->lyxCode() == ARG_CODE) {
69                                 ++nr;
70                                 if (parit->inset == this)
71                                         ours = nr;
72                         }
73                 }
74                 bool done = false;
75                 int realopts = 0;
76                 if (nr > req) {
77                         // We have optional arguments
78                         realopts = nr - req;
79                         if (ours <= realopts) {
80                                 name_ = convert<string>(ours);
81                                 done = true;
82                         }
83                 }
84                 if (!done) {
85                         // This is a mandatory argument. We have to consider
86                         // non-given optional arguments for the numbering
87                         int offset = opts - realopts;
88                         ours += offset;
89                         name_ = convert<string>(ours);
90                 }
91         }
92         Layout::LaTeXArgMap::const_iterator const lait =
93                         args.find(convert<unsigned int>(name_));
94         if (lait != args.end()) {
95                 labelstring_ = translateIfPossible((*lait).second.labelstring);
96                 tooltip_ = translateIfPossible((*lait).second.tooltip);
97         } else {
98                 labelstring_ = _("Unknown Argument");
99                 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
100         }
101         setButtonLabel();
102         InsetCollapsable::updateBuffer(it, utype);
103 }
104
105 void InsetArgument::setButtonLabel()
106 {
107         setLabel(labelstring_);
108 }
109
110 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
111 {
112         if (isOpen(bv))
113                 return tooltip_;
114         return toolTipText(tooltip_ + from_ascii(":\n"));
115 }
116
117 void InsetArgument::latexArgument(otexstream & os,
118                 OutputParams const & runparams, docstring const & ldelim,
119                 docstring const & rdelim) const
120 {
121         TexRow texrow;
122         odocstringstream ss;
123         otexstream ots(ss, texrow);
124         InsetText::latex(ots, runparams);
125         docstring str = ss.str();
126         if (ldelim != "{" && support::contains(str, rdelim))
127                 str = '{' + str + '}';
128         os << ldelim << str << rdelim;
129 }
130
131
132 } // namespace lyx