2 * \file InsetArgument.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Martin Vermeer
8 * Full author contact details are available in file CREDITS.
13 #include "InsetArgument.h"
16 #include "FuncStatus.h"
17 #include "FuncRequest.h"
18 #include "InsetList.h"
21 #include "ParIterator.h"
23 #include "support/convert.h"
24 #include "support/debug.h"
25 #include "support/docstream.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
34 InsetArgument::InsetArgument(Buffer * buf, string const & name)
35 : InsetCollapsable(buf), name_(name), labelstring_(docstring())
39 void InsetArgument::write(ostream & os) const
41 os << "Argument " << name_ << "\n";
42 InsetCollapsable::write(os);
45 void InsetArgument::read(Lexer & lex)
48 InsetCollapsable::read(lex);
51 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
53 Layout::LaTeXArgMap args;
54 bool const insetlayout = &it.inset() && it.paragraph().layout().latexargs().empty();
56 args = it.inset().getLayout().latexargs();
58 args = it.paragraph().layout().latexargs();
60 // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
62 int req = insetlayout ? it.inset().getLayout().requiredArgs()
63 : it.paragraph().layout().requiredArgs();
64 int opts = insetlayout ? it.inset().getLayout().optArgs()
65 : it.paragraph().layout().optArgs();
68 InsetList::const_iterator parit = it.paragraph().insetList().begin();
69 InsetList::const_iterator parend = it.paragraph().insetList().end();
70 for (; parit != parend; ++parit) {
71 if (parit->inset->lyxCode() == ARG_CODE) {
73 if (parit->inset == this)
80 // We have optional arguments
82 if (ours <= realopts) {
83 name_ = convert<string>(ours);
88 // This is a mandatory argument. We have to consider
89 // non-given optional arguments for the numbering
90 int offset = opts - realopts;
92 name_ = convert<string>(ours);
95 Layout::LaTeXArgMap::const_iterator const lait =
96 args.find(convert<unsigned int>(name_));
97 if (lait != args.end()) {
99 support::rsplit(translateIfPossible((*lait).second.labelstring), label, '|');
100 labelstring_ = label;
101 tooltip_ = translateIfPossible((*lait).second.tooltip);
103 labelstring_ = _("Unknown Argument");
104 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
107 InsetCollapsable::updateBuffer(it, utype);
110 void InsetArgument::setButtonLabel()
112 setLabel(labelstring_);
115 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
119 return toolTipText(tooltip_ + from_ascii(":\n"));
122 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
124 switch (cmd.action()) {
126 case LFUN_INSET_MODIFY: {
127 string const first_arg = cmd.getArg(0);
128 bool const change_type = first_arg == "changetype";
131 // this will not be handled higher up
135 cur.recordUndoInset(ATOMIC_UNDO, this);
136 name_ = cmd.getArg(1);
137 cur.forceBufferUpdate();
142 InsetCollapsable::doDispatch(cur, cmd);
148 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
149 FuncStatus & flag) const
151 switch (cmd.action()) {
153 case LFUN_INSET_MODIFY: {
154 string const first_arg = cmd.getArg(0);
155 if (first_arg == "changetype") {
156 string const type = cmd.getArg(1);
157 flag.setOnOff(type == name_);
159 flag.setEnabled(true);
162 Layout::LaTeXArgMap args;
163 bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
165 args = cur.inset().getLayout().latexargs();
167 args = cur.paragraph().layout().latexargs();
168 Layout::LaTeXArgMap::const_iterator const lait =
169 args.find(convert<unsigned int>(type));
170 if (lait != args.end()) {
171 flag.setEnabled(true);
172 InsetList::const_iterator it = cur.paragraph().insetList().begin();
173 InsetList::const_iterator end = cur.paragraph().insetList().end();
174 for (; it != end; ++it) {
175 if (it->inset->lyxCode() == ARG_CODE) {
176 InsetArgument const * ins =
177 static_cast<InsetArgument const *>(it->inset);
178 if (ins->name() == type) {
179 // we have this already
180 flag.setEnabled(false);
186 flag.setEnabled(false);
189 return InsetCollapsable::getStatus(cur, cmd, flag);
193 return InsetCollapsable::getStatus(cur, cmd, flag);
197 string InsetArgument::contextMenuName() const
199 return "context-argument";
202 void InsetArgument::latexArgument(otexstream & os,
203 OutputParams const & runparams, docstring const & ldelim,
204 docstring const & rdelim) const
208 otexstream ots(ss, texrow);
209 InsetText::latex(ots, runparams);
210 docstring str = ss.str();
211 if (ldelim != "{" && support::contains(str, rdelim))
212 str = '{' + str + '}';
213 os << ldelim << str << rdelim;