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