]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
df1344e6267a5537bcc4e972ea4b0afacb83133f
[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     : InsetCollapsible(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()), is_toc_caption_(false)
47 {}
48
49
50 void InsetArgument::write(ostream & os) const
51 {
52         os << "Argument " << name_ << "\n";
53         InsetCollapsible::write(os);
54 }
55
56
57 void InsetArgument::read(Lexer & lex)
58 {
59         lex >> name_;
60         InsetCollapsible::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         // Record PassThru status in order to act on changes.
72         bool const former_pass_thru = pass_thru_;
73
74         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
75         if (name_ == "999") {
76                 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
77                                       : it.paragraph().layout().requiredArgs();
78                 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
79                                       : it.paragraph().layout().optArgs();
80                 unsigned int nr = 0;
81                 unsigned int ours = 0;
82                 InsetList::const_iterator parit = it.paragraph().insetList().begin();
83                 InsetList::const_iterator parend = it.paragraph().insetList().end();
84                 for (; parit != parend; ++parit) {
85                         if (parit->inset->lyxCode() == ARG_CODE) {
86                                 ++nr;
87                                 if (parit->inset == this)
88                                         ours = nr;
89                         }
90                 }
91                 bool done = false;
92                 unsigned int realopts = 0;
93                 if (nr > req) {
94                         // We have optional arguments
95                         realopts = nr - req;
96                         if (ours <= realopts) {
97                                 name_ = convert<string>(ours);
98                                 done = true;
99                         }
100                 }
101                 if (!done) {
102                         // This is a mandatory argument. We have to consider
103                         // non-given optional arguments for the numbering
104                         int offset = opts - realopts;
105                         ours += offset;
106                         name_ = convert<string>(ours);
107                 }
108         }
109         Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
110         caption_of_toc_ = string();
111         if (lait != args.end()) {
112                 docstring label = translateIfPossible((*lait).second.labelstring);
113                 docstring striplabel;
114                 support::rsplit(label, striplabel, '|');
115                 labelstring_ = striplabel.empty() ? label: striplabel;
116                 tooltip_ = translateIfPossible((*lait).second.tooltip);
117                 font_ = (*lait).second.font;
118                 labelfont_ = (*lait).second.labelfont;
119                 decoration_ = (*lait).second.decoration;
120                 pass_thru_chars_ = (*lait).second.pass_thru_chars;
121                 pass_thru_local_ = false;
122                 if (lait->second.is_toc_caption) {
123                         is_toc_caption_ = true;
124                         // empty if AddToToc is not set
125                         caption_of_toc_ = insetlayout
126                                 ? it.inset().getLayout().tocType()
127                                 : it.paragraph().layout().tocType();
128                 }
129
130                 switch ((*lait).second.passthru) {
131                         case PT_INHERITED:
132                                 pass_thru_ = pass_thru_context_;
133                                 break;
134                         case PT_TRUE:
135                                 pass_thru_ = true;
136                                 pass_thru_local_ = true;
137                                 break;
138                         case PT_FALSE:
139                                 pass_thru_ = false;
140                                 break;
141                 }
142         } else {
143                 labelstring_ = _("Unknown Argument");
144                 tooltip_ = _("Argument not known in this Layout. Will be suppressed in the output.");
145         }
146
147         if (former_pass_thru != pass_thru_) {
148                 // PassThru status changed. We might need to update
149                 // the language of the contents
150                 Language const * l  = insetlayout
151                         ? it.inset().buffer().language()
152                         : it.buffer()->language();
153                 fixParagraphLanguage(l);
154         }
155
156         setButtonLabel();
157         InsetCollapsible::updateBuffer(it, utype);
158 }
159
160
161 void InsetArgument::setButtonLabel()
162 {
163         setLabel(labelstring_);
164 }
165
166
167 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
168 {
169         if (isOpen(bv))
170                 return tooltip_;
171         return toolTipText(tooltip_ + from_ascii(":\n"));
172 }
173
174
175 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
176 {
177         switch (cmd.action()) {
178
179         case LFUN_INSET_MODIFY: {
180                 string const first_arg = cmd.getArg(0);
181                 bool const change_type = first_arg == "changetype";
182                 if (!change_type) {
183                         // not for us
184                         // this will not be handled higher up
185                         cur.undispatched();
186                         return;
187                 }
188                 cur.recordUndoInset(this);
189                 name_ = cmd.getArg(1);
190                 cur.forceBufferUpdate();
191                 break;
192         }
193
194         case LFUN_PASTE:
195         case LFUN_CLIPBOARD_PASTE:
196         case LFUN_SELECTION_PASTE:
197         case LFUN_PRIMARY_SELECTION_PASTE:
198         case LFUN_SELF_INSERT:
199                 // With (only) inherited pass_thru, call Text::dispatch()
200                 // directly to avoid call for fixParagraphsFont() and/or
201                 // forcing to latex_language in InsetText::dispatch(),
202                 // since this does not play nicely with inherited pass_thru
203                 // (see #8471).
204                 if (pass_thru_ && !pass_thru_local_) {
205                         text().dispatch(cur, cmd);
206                         // For the paste operations, check if we have
207                         // non-latex_language, and if so, fix.
208                         if (cmd.action() != LFUN_SELF_INSERT)
209                                 fixParagraphLanguage(buffer().params().language);
210                 }
211                 else
212                         InsetCollapsible::doDispatch(cur, cmd);
213                 break;
214
215         default:
216                 InsetCollapsible::doDispatch(cur, cmd);
217                 break;
218         }
219 }
220
221
222 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
223                 FuncStatus & flag) const
224 {
225         switch (cmd.action()) {
226
227         case LFUN_INSET_MODIFY: {
228                 string const first_arg = cmd.getArg(0);
229                 if (first_arg == "changetype") {
230                         string const type = cmd.getArg(1);
231                         flag.setOnOff(type == name_);
232                         if (type == name_) {
233                                 flag.setEnabled(true);
234                                 return true;
235                         }
236                         Layout::LaTeXArgMap args;
237                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
238                         if (insetlayout)
239                                 args = cur.inset().getLayout().latexargs();
240                         else
241                                 args = cur.paragraph().layout().latexargs();
242                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
243                         if (lait != args.end()) {
244                                 flag.setEnabled(true);
245                                 for (auto const & table : cur.paragraph().insetList())
246                                         if (InsetArgument const * ins = table.inset->asInsetArgument())
247                                                 if (ins->name() == type) {
248                                                         // we have this already
249                                                         flag.setEnabled(false);
250                                                         return true;
251                                                 }
252                         } else
253                                 flag.setEnabled(false);
254                         return true;
255                 }
256                 return InsetCollapsible::getStatus(cur, cmd, flag);
257         }
258
259         default:
260                 return InsetCollapsible::getStatus(cur, cmd, flag);
261         }
262 }
263
264
265 string InsetArgument::contextMenuName() const
266 {
267         if (decoration() == InsetLayout::CONGLOMERATE)
268                 return "context-argument-conglomerate";
269         else
270                 return "context-argument";
271 }
272
273
274 FontInfo InsetArgument::getFont() const
275 {
276         if (font_ != inherit_font)
277                 return font_;
278         return InsetCollapsible::getFont();
279 }
280
281
282 FontInfo InsetArgument::getLabelfont() const
283 {
284         if (labelfont_ != inherit_font)
285                 return labelfont_;
286         return InsetCollapsible::getLabelfont();
287 }
288
289
290 ColorCode InsetArgument::labelColor() const {
291         if (labelfont_.color() != Color_inherit)
292                 return labelfont_.color();
293         return InsetCollapsible::labelColor();
294 }
295
296
297 InsetLayout::InsetDecoration InsetArgument::decoration() const
298 {
299         InsetLayout::InsetDecoration dec = getLayout().decoration();
300         if (!decoration_.empty())
301                 dec = translateDecoration(decoration_);
302         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
303 }
304
305
306 void InsetArgument::latexArgument(otexstream & os,
307                 OutputParams const & runparams_in, docstring const & ldelim,
308                 docstring const & rdelim, docstring const & presetarg) const
309 {
310         otexstringstream ots;
311         OutputParams runparams = runparams_in;
312         if (!pass_thru_chars_.empty())
313                 runparams.pass_thru_chars += pass_thru_chars_;
314         runparams.pass_thru = isPassThru();
315         InsetText::latex(ots, runparams);
316         TexString ts = ots.release();
317         bool const add_braces = ldelim != "{" && support::contains(ts.str, rdelim);
318         os << ldelim;
319         if (add_braces)
320                 os << '{';
321         os << presetarg;
322         if (!presetarg.empty() && !ts.str.empty())
323                 os << ", ";
324         os << move(ts);
325         if (add_braces)
326                 os << '}';
327         os << rdelim;
328 }
329
330
331 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
332                              UpdateType utype, TocBackend & backend) const
333 {
334         if (!caption_of_toc_.empty()) {
335                 docstring str;
336                 text().forOutliner(str, TOC_ENTRY_LENGTH);
337                 backend.builder(caption_of_toc_).argumentItem(str);
338         }
339         // Proceed with the rest of the inset.
340         InsetText::addToToc(dit, output_active, utype, backend);
341 }
342
343
344 void InsetArgument::fixParagraphLanguage(Language const * l)
345 {
346         Font font(inherit_font, l);
347         if (pass_thru_)
348                 font.setLanguage(latex_language);
349         paragraphs().front().resetFonts(font);
350 }
351
352
353 } // namespace lyx