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