]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Move Author.h, Format.h from BufferParams.h
[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       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                 pass_thru_local_ = false;
126                 if (lait->second.is_toc_caption) {
127                         is_toc_caption_ = true;
128                         // empty if AddToToc is not set
129                         caption_of_toc_ = insetlayout
130                                 ? it.inset().getLayout().tocType()
131                                 : it.paragraph().layout().tocType();
132                 }
133
134                 switch ((*lait).second.passthru) {
135                         case PT_INHERITED:
136                                 pass_thru_ = pass_thru_context_;
137                                 break;
138                         case PT_TRUE:
139                                 pass_thru_ = true;
140                                 pass_thru_local_ = true;
141                                 break;
142                         case PT_FALSE:
143                                 pass_thru_ = false;
144                                 break;
145                 }
146         } else {
147                 labelstring_ = _("Unknown Argument");
148                 tooltip_ = _("Argument not known in this Layout. Will be suppressed in the output.");
149         }
150
151         if (former_pass_thru != pass_thru_) {
152                 // PassThru status changed. We might need to update
153                 // the language of the contents
154                 Language const * l  = insetlayout
155                         ? it.inset().buffer().language()
156                         : it.buffer()->language();
157                 fixParagraphLanguage(l);
158         }
159
160         setButtonLabel();
161         InsetCollapsible::updateBuffer(it, utype, deleted);
162 }
163
164
165 void InsetArgument::setButtonLabel()
166 {
167         setLabel(labelstring_);
168 }
169
170
171 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
172 {
173         if (isOpen(bv))
174                 return tooltip_;
175         return toolTipText(tooltip_ + from_ascii(":\n"));
176 }
177
178
179 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
180 {
181         switch (cmd.action()) {
182
183         case LFUN_INSET_MODIFY: {
184                 string const first_arg = cmd.getArg(0);
185                 bool const change_type = first_arg == "changetype";
186                 if (!change_type) {
187                         // not for us
188                         // this will not be handled higher up
189                         cur.undispatched();
190                         return;
191                 }
192                 cur.recordUndoInset(this);
193                 name_ = cmd.getArg(1);
194                 cur.forceBufferUpdate();
195                 break;
196         }
197
198         case LFUN_PASTE:
199         case LFUN_CLIPBOARD_PASTE:
200         case LFUN_SELECTION_PASTE:
201         case LFUN_PRIMARY_SELECTION_PASTE:
202         case LFUN_SELF_INSERT:
203                 // With (only) inherited pass_thru, call Text::dispatch()
204                 // directly to avoid call for fixParagraphsFont() and/or
205                 // forcing to latex_language in InsetText::dispatch(),
206                 // since this does not play nicely with inherited pass_thru
207                 // (see #8471).
208                 if (pass_thru_ && !pass_thru_local_) {
209                         text().dispatch(cur, cmd);
210                         // For the paste operations, check if we have
211                         // non-latex_language, and if so, fix.
212                         if (cmd.action() != LFUN_SELF_INSERT)
213                                 fixParagraphLanguage(buffer().params().language);
214                 }
215                 else
216                         InsetCollapsible::doDispatch(cur, cmd);
217                 break;
218
219         default:
220                 InsetCollapsible::doDispatch(cur, cmd);
221                 break;
222         }
223 }
224
225
226 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
227                 FuncStatus & flag) const
228 {
229         switch (cmd.action()) {
230
231         case LFUN_INSET_MODIFY: {
232                 string const first_arg = cmd.getArg(0);
233                 if (first_arg == "changetype") {
234                         string const type = cmd.getArg(1);
235                         flag.setOnOff(type == name_);
236                         if (type == name_) {
237                                 flag.setEnabled(true);
238                                 return true;
239                         }
240                         Layout::LaTeXArgMap args;
241                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
242                         if (insetlayout)
243                                 args = cur.inset().getLayout().latexargs();
244                         else
245                                 args = cur.paragraph().layout().latexargs();
246                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
247                         if (lait != args.end()) {
248                                 flag.setEnabled(true);
249                                 for (auto const & table : cur.paragraph().insetList())
250                                         if (InsetArgument const * ins = table.inset->asInsetArgument())
251                                                 if (ins->name() == type) {
252                                                         // we have this already
253                                                         flag.setEnabled(false);
254                                                         return true;
255                                                 }
256                         } else
257                                 flag.setEnabled(false);
258                         return true;
259                 }
260                 return InsetCollapsible::getStatus(cur, cmd, flag);
261         }
262
263         default:
264                 return InsetCollapsible::getStatus(cur, cmd, flag);
265         }
266 }
267
268
269 string InsetArgument::contextMenuName() const
270 {
271         if (decoration() == InsetLayout::CONGLOMERATE)
272                 return "context-argument-conglomerate";
273         else
274                 return "context-argument";
275 }
276
277
278 FontInfo InsetArgument::getFont() const
279 {
280         if (font_ != inherit_font)
281                 return font_;
282         return InsetCollapsible::getFont();
283 }
284
285
286 FontInfo InsetArgument::getLabelfont() const
287 {
288         if (labelfont_ != inherit_font)
289                 return labelfont_;
290         return InsetCollapsible::getLabelfont();
291 }
292
293
294 ColorCode InsetArgument::labelColor() const {
295         if (labelfont_.color() != Color_inherit)
296                 return labelfont_.color();
297         return InsetCollapsible::labelColor();
298 }
299
300
301 InsetLayout::InsetDecoration InsetArgument::decoration() const
302 {
303         InsetLayout::InsetDecoration dec = getLayout().decoration();
304         if (!decoration_.empty())
305                 dec = translateDecoration(decoration_);
306         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
307 }
308
309
310 void InsetArgument::latexArgument(otexstream & os,
311                 OutputParams const & runparams_in, docstring const & ldelim,
312                 docstring const & rdelim, docstring const & presetarg) const
313 {
314         otexstringstream ots;
315         OutputParams runparams = runparams_in;
316         if (!pass_thru_chars_.empty())
317                 runparams.pass_thru_chars += pass_thru_chars_;
318         if (!newline_cmd_.empty())
319                 runparams.newlinecmd = newline_cmd_;
320         runparams.pass_thru = isPassThru();
321         InsetText::latex(ots, runparams);
322         TexString ts = ots.release();
323         bool const add_braces = !ldelim.empty() && ldelim != "{"
324                         && support::contains(ts.str, rdelim);
325         os << ldelim;
326         if (add_braces)
327                 os << '{';
328         os << presetarg;
329         if (!presetarg.empty() && !ts.str.empty())
330                 os << ", ";
331         os << move(ts);
332         if (add_braces)
333                 os << '}';
334         os << rdelim;
335 }
336
337
338 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
339                              UpdateType utype, TocBackend & backend) const
340 {
341         if (!caption_of_toc_.empty()) {
342                 docstring str;
343                 text().forOutliner(str, TOC_ENTRY_LENGTH);
344                 backend.builder(caption_of_toc_).argumentItem(str);
345         }
346         // Proceed with the rest of the inset.
347         InsetText::addToToc(dit, output_active, utype, backend);
348 }
349
350
351 void InsetArgument::fixParagraphLanguage(Language const * l)
352 {
353         Font font(inherit_font, l);
354         if (pass_thru_)
355                 font.setLanguage(latex_language);
356         paragraphs().front().resetFonts(font);
357 }
358
359
360 } // namespace lyx