]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Re-Fix #8471
[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  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetArgument.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "Cursor.h"
19 #include "FuncStatus.h"
20 #include "FuncRequest.h"
21 #include "InsetList.h"
22 #include "Language.h"
23 #include "Layout.h"
24 #include "Lexer.h"
25 #include "OutputParams.h"
26 #include "ParIterator.h"
27 #include "TexRow.h"
28 #include "texstream.h"
29
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 using namespace std;
37
38 namespace lyx {
39
40
41 InsetArgument::InsetArgument(Buffer * buf, string const & name)
42     : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
43       font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
44       pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
45       pass_thru_chars_(docstring())
46 {}
47
48
49 void InsetArgument::write(ostream & os) const
50 {
51         os << "Argument " << name_ << "\n";
52         InsetCollapsable::write(os);
53 }
54
55
56 void InsetArgument::read(Lexer & lex)
57 {
58         lex >> name_;
59         InsetCollapsable::read(lex);
60 }
61
62
63 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
64 {
65         Layout::LaTeXArgMap args = it.paragraph().layout().args();
66         pass_thru_context_ = it.paragraph().layout().pass_thru;
67         bool const insetlayout = args.empty();
68         if (insetlayout) {
69                 args = it.inset().getLayout().args();
70                 pass_thru_context_ = it.inset().getLayout().isPassThru();
71         }
72
73         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
74         if (name_ == "999") {
75                 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
76                                       : it.paragraph().layout().requiredArgs();
77                 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
78                                       : it.paragraph().layout().optArgs();
79                 unsigned int nr = 0;
80                 unsigned int ours = 0;
81                 InsetList::const_iterator parit = it.paragraph().insetList().begin();
82                 InsetList::const_iterator parend = it.paragraph().insetList().end();
83                 for (; parit != parend; ++parit) {
84                         if (parit->inset->lyxCode() == ARG_CODE) {
85                                 ++nr;
86                                 if (parit->inset == this)
87                                         ours = nr;
88                         }
89                 }
90                 bool done = false;
91                 unsigned int realopts = 0;
92                 if (nr > req) {
93                         // We have optional arguments
94                         realopts = nr - req;
95                         if (ours <= realopts) {
96                                 name_ = convert<string>(ours);
97                                 done = true;
98                         }
99                 }
100                 if (!done) {
101                         // This is a mandatory argument. We have to consider
102                         // non-given optional arguments for the numbering
103                         int offset = opts - realopts;
104                         ours += offset;
105                         name_ = convert<string>(ours);
106                 }
107         }
108         Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
109         if (lait != args.end()) {
110                 docstring label = translateIfPossible((*lait).second.labelstring);
111                 docstring striplabel;
112                 support::rsplit(label, striplabel, '|');
113                 labelstring_ = striplabel.empty() ? label: striplabel;
114                 tooltip_ = translateIfPossible((*lait).second.tooltip);
115                 font_ = (*lait).second.font;
116                 labelfont_ = (*lait).second.labelfont;
117                 decoration_ = (*lait).second.decoration;
118                 pass_thru_chars_ = (*lait).second.pass_thru_chars;
119                 pass_thru_local_ = false;
120                 switch ((*lait).second.passthru) {
121                         case PT_INHERITED:
122                                 pass_thru_ = pass_thru_context_;
123                                 break;
124                         case PT_TRUE:
125                                 pass_thru_ = true;
126                                 pass_thru_local_ = true;
127                                 break;
128                         case PT_FALSE:
129                                 pass_thru_ = false;
130                                 break;
131                 }
132         } else {
133                 labelstring_ = _("Unknown Argument");
134                 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
135         }
136         setButtonLabel();
137         InsetCollapsable::updateBuffer(it, utype);
138 }
139
140
141 void InsetArgument::setButtonLabel()
142 {
143         setLabel(labelstring_);
144 }
145
146
147 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
148 {
149         if (isOpen(bv))
150                 return tooltip_;
151         return toolTipText(tooltip_ + from_ascii(":\n"));
152 }
153
154
155 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
156 {
157         switch (cmd.action()) {
158
159         case LFUN_INSET_MODIFY: {
160                 string const first_arg = cmd.getArg(0);
161                 bool const change_type = first_arg == "changetype";
162                 if (!change_type) {
163                         // not for us
164                         // this will not be handled higher up
165                         cur.undispatched();
166                         return;
167                 }
168                 cur.recordUndoInset(this);
169                 name_ = cmd.getArg(1);
170                 cur.forceBufferUpdate();
171                 break;
172         }
173
174         case LFUN_PASTE:
175         case LFUN_CLIPBOARD_PASTE:
176         case LFUN_SELECTION_PASTE:
177         case LFUN_PRIMARY_SELECTION_PASTE:
178         case LFUN_SELF_INSERT:
179                 // With (only) inherited pass_thru, call Text::dispatch()
180                 // directly to avoid call for fixParagraphsFont() and/or
181                 // forcing to latex_language in InsetText::dispatch(),
182                 // since this does not play nicely with inherited pass_thru
183                 // (see #8471).
184                 if (pass_thru_ && !pass_thru_local_)
185                         text().dispatch(cur, cmd);
186                 else
187                         InsetCollapsable::doDispatch(cur, cmd);
188                 break;
189
190         default:
191                 InsetCollapsable::doDispatch(cur, cmd);
192                 break;
193         }
194 }
195
196
197 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
198                 FuncStatus & flag) const
199 {
200         switch (cmd.action()) {
201
202         case LFUN_INSET_MODIFY: {
203                 string const first_arg = cmd.getArg(0);
204                 if (first_arg == "changetype") {
205                         string const type = cmd.getArg(1);
206                         flag.setOnOff(type == name_);
207                         if (type == name_) {
208                                 flag.setEnabled(true);
209                                 return true;
210                         }
211                         Layout::LaTeXArgMap args;
212                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
213                         if (insetlayout)
214                                 args = cur.inset().getLayout().latexargs();
215                         else
216                                 args = cur.paragraph().layout().latexargs();
217                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
218                         if (lait != args.end()) {
219                                 flag.setEnabled(true);
220                                 InsetList::const_iterator it = cur.paragraph().insetList().begin();
221                                 InsetList::const_iterator end = cur.paragraph().insetList().end();
222                                 for (; it != end; ++it) {
223                                         if (it->inset->lyxCode() == ARG_CODE) {
224                                                 InsetArgument const * ins =
225                                                         static_cast<InsetArgument const *>(it->inset);
226                                                 if (ins->name() == type) {
227                                                         // we have this already
228                                                         flag.setEnabled(false);
229                                                         return true;
230                                                 }
231                                         }
232                                 }
233                         } else
234                                 flag.setEnabled(false);
235                         return true;
236                 }
237                 return InsetCollapsable::getStatus(cur, cmd, flag);
238         }
239
240         default:
241                 return InsetCollapsable::getStatus(cur, cmd, flag);
242         }
243 }
244
245
246 string InsetArgument::contextMenuName() const
247 {
248         if (decoration() == InsetLayout::CONGLOMERATE)
249                 return "context-argument-conglomerate";
250         else
251                 return "context-argument";
252 }
253
254
255 FontInfo InsetArgument::getFont() const
256 {
257         if (font_ != inherit_font)
258                 return font_;
259         return InsetCollapsable::getFont();
260 }
261
262
263 FontInfo InsetArgument::getLabelfont() const
264 {
265         if (labelfont_ != inherit_font)
266                 return labelfont_;
267         return InsetCollapsable::getLabelfont();
268 }
269
270
271 ColorCode InsetArgument::labelColor() const {
272         if (labelfont_.color() != Color_inherit)
273                 return labelfont_.color();
274         return InsetCollapsable::labelColor();
275 }
276
277
278 InsetLayout::InsetDecoration InsetArgument::decoration() const
279 {
280         InsetLayout::InsetDecoration dec = getLayout().decoration();
281         if (!decoration_.empty())
282                 dec = translateDecoration(decoration_);
283         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
284 }
285
286
287 void InsetArgument::latexArgument(otexstream & os,
288                 OutputParams const & runparams_in, docstring const & ldelim,
289                 docstring const & rdelim, docstring const & presetarg) const
290 {
291         otexstringstream ots;
292         OutputParams runparams = runparams_in;
293         if (!pass_thru_chars_.empty())
294                 runparams.pass_thru_chars += pass_thru_chars_;
295         runparams.pass_thru = isPassThru();
296         InsetText::latex(ots, runparams);
297         TexString ts = ots.release();
298         bool const add_braces = ldelim != "{" && support::contains(ts.str, rdelim);
299         os << ldelim;
300         if (add_braces)
301                 os << '{';
302         os << presetarg;
303         if (!presetarg.empty() && !ts.str.empty())
304                 os << ", ";
305         os << move(ts);
306         if (add_braces)
307                 os << '}';
308         os << rdelim;
309 }
310
311
312 } // namespace lyx