]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Amend 7e121117bb3203
[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 #include "TocBackend.h"
30
31 #include "support/convert.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36
37 using namespace std;
38
39 namespace lyx {
40
41
42 InsetArgument::InsetArgument(Buffer * buf, string const & name)
43     : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
44       font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
45       pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
46       pass_thru_chars_(docstring())
47 {}
48
49
50 void InsetArgument::write(ostream & os) const
51 {
52         os << "Argument " << name_ << "\n";
53         InsetCollapsable::write(os);
54 }
55
56
57 void InsetArgument::read(Lexer & lex)
58 {
59         lex >> name_;
60         InsetCollapsable::read(lex);
61 }
62
63
64 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
65 {
66         bool const insetlayout = !it.paragraph().layout().hasArgs();
67         Layout::LaTeXArgMap const args = insetlayout ?
68                 it.inset().getLayout().args() : it.paragraph().layout().args();
69         pass_thru_context_ = insetlayout ?
70                 it.inset().getLayout().isPassThru() : it.paragraph().layout().pass_thru;
71
72         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
73         if (name_ == "999") {
74                 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
75                                       : it.paragraph().layout().requiredArgs();
76                 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
77                                       : it.paragraph().layout().optArgs();
78                 unsigned int nr = 0;
79                 unsigned int ours = 0;
80                 InsetList::const_iterator parit = it.paragraph().insetList().begin();
81                 InsetList::const_iterator parend = it.paragraph().insetList().end();
82                 for (; parit != parend; ++parit) {
83                         if (parit->inset->lyxCode() == ARG_CODE) {
84                                 ++nr;
85                                 if (parit->inset == this)
86                                         ours = nr;
87                         }
88                 }
89                 bool done = false;
90                 unsigned int realopts = 0;
91                 if (nr > req) {
92                         // We have optional arguments
93                         realopts = nr - req;
94                         if (ours <= realopts) {
95                                 name_ = convert<string>(ours);
96                                 done = true;
97                         }
98                 }
99                 if (!done) {
100                         // This is a mandatory argument. We have to consider
101                         // non-given optional arguments for the numbering
102                         int offset = opts - realopts;
103                         ours += offset;
104                         name_ = convert<string>(ours);
105                 }
106         }
107         Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
108         caption_of_toc_ = string();
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                 if (lait->second.is_toc_caption)
121                         // empty if AddToToc is not set
122                         caption_of_toc_ = insetlayout
123                                 ? it.inset().getLayout().tocType()
124                                 : it.paragraph().layout().tocType();
125
126                 switch ((*lait).second.passthru) {
127                         case PT_INHERITED:
128                                 pass_thru_ = pass_thru_context_;
129                                 break;
130                         case PT_TRUE:
131                                 pass_thru_ = true;
132                                 pass_thru_local_ = true;
133                                 break;
134                         case PT_FALSE:
135                                 pass_thru_ = false;
136                                 break;
137                 }
138         } else {
139                 labelstring_ = _("Unknown Argument");
140                 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
141         }
142         setButtonLabel();
143         InsetCollapsable::updateBuffer(it, utype);
144 }
145
146
147 void InsetArgument::setButtonLabel()
148 {
149         setLabel(labelstring_);
150 }
151
152
153 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
154 {
155         if (isOpen(bv))
156                 return tooltip_;
157         return toolTipText(tooltip_ + from_ascii(":\n"));
158 }
159
160
161 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
162 {
163         switch (cmd.action()) {
164
165         case LFUN_INSET_MODIFY: {
166                 string const first_arg = cmd.getArg(0);
167                 bool const change_type = first_arg == "changetype";
168                 if (!change_type) {
169                         // not for us
170                         // this will not be handled higher up
171                         cur.undispatched();
172                         return;
173                 }
174                 cur.recordUndoInset(this);
175                 name_ = cmd.getArg(1);
176                 cur.forceBufferUpdate();
177                 break;
178         }
179
180         case LFUN_PASTE:
181         case LFUN_CLIPBOARD_PASTE:
182         case LFUN_SELECTION_PASTE:
183         case LFUN_PRIMARY_SELECTION_PASTE:
184         case LFUN_SELF_INSERT:
185                 // With (only) inherited pass_thru, call Text::dispatch()
186                 // directly to avoid call for fixParagraphsFont() and/or
187                 // forcing to latex_language in InsetText::dispatch(),
188                 // since this does not play nicely with inherited pass_thru
189                 // (see #8471).
190                 if (pass_thru_ && !pass_thru_local_)
191                         text().dispatch(cur, cmd);
192                 else
193                         InsetCollapsable::doDispatch(cur, cmd);
194                 break;
195
196         default:
197                 InsetCollapsable::doDispatch(cur, cmd);
198                 break;
199         }
200 }
201
202
203 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
204                 FuncStatus & flag) const
205 {
206         switch (cmd.action()) {
207
208         case LFUN_INSET_MODIFY: {
209                 string const first_arg = cmd.getArg(0);
210                 if (first_arg == "changetype") {
211                         string const type = cmd.getArg(1);
212                         flag.setOnOff(type == name_);
213                         if (type == name_) {
214                                 flag.setEnabled(true);
215                                 return true;
216                         }
217                         Layout::LaTeXArgMap args;
218                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
219                         if (insetlayout)
220                                 args = cur.inset().getLayout().latexargs();
221                         else
222                                 args = cur.paragraph().layout().latexargs();
223                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
224                         if (lait != args.end()) {
225                                 flag.setEnabled(true);
226                                 for (auto const & table : cur.paragraph().insetList())
227                                         if (InsetArgument const * ins = table.inset->asInsetArgument())
228                                                 if (ins->name() == type) {
229                                                         // we have this already
230                                                         flag.setEnabled(false);
231                                                         return true;
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 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
313                              UpdateType utype, TocBackend & backend) const
314 {
315         if (!caption_of_toc_.empty()) {
316                 docstring str;
317                 text().forOutliner(str, TOC_ENTRY_LENGTH);
318                 backend.builder(caption_of_toc_).argumentItem(str);
319         }
320         // Proceed with the rest of the inset.
321         InsetText::addToToc(dit, output_active, utype, backend);
322 }
323
324
325 } // namespace lyx