]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Hack to display section symbol
[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::init(Paragraph const & par)
67 {
68         Inset const & ininset = par.inInset();
69         bool const insetlayout = !par.layout().hasArgs();
70         Layout::LaTeXArgMap const args = insetlayout ?
71                 ininset.getLayout().args() : par.layout().args();
72         pass_thru_context_ = insetlayout ?
73                 ininset.getLayout().isPassThru() : par.layout().pass_thru;
74         // Record PassThru status in order to act on changes.
75         bool const former_pass_thru = pass_thru_;
76
77         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
78         // "999" is the conventional name given to those by lyx2lyx
79         if (name_ == "999") {
80                 int const req = insetlayout ? ininset.getLayout().requiredArgs()
81                                       : par.layout().requiredArgs();
82                 int const opts = insetlayout ? ininset.getLayout().optArgs()
83                                       : par.layout().optArgs();
84                 int nr = 0;
85                 int ours = 0;
86                 for (InsetList::Element const & elt : par.insetList()) {
87                         if (elt.inset->lyxCode() == ARG_CODE) {
88                                 ++nr;
89                                 if (elt.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                 docbookargumentaftermaintag_ = (*lait).second.docbookargumentaftermaintag;
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                                 ? ininset.getLayout().tocType()
136                                 : par.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                 Language const * l  = ininset.buffer().language();
163                 fixParagraphLanguage(l);
164         }
165
166         setButtonLabel();
167 }
168
169 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
170 {
171         init(it.paragraph());
172         InsetCollapsible::updateBuffer(it, utype, deleted);
173 }
174
175
176 void InsetArgument::setButtonLabel()
177 {
178         setLabel(labelstring_);
179 }
180
181
182 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
183 {
184         if (isOpen(bv))
185                 return tooltip_;
186         return toolTipText(tooltip_ + from_ascii(":\n"));
187 }
188
189
190 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
191 {
192         switch (cmd.action()) {
193
194         case LFUN_INSET_MODIFY: {
195                 string const first_arg = cmd.getArg(0);
196                 bool const change_type = first_arg == "changetype";
197                 if (!change_type) {
198                         // not for us
199                         // this will not be handled higher up
200                         cur.undispatched();
201                         return;
202                 }
203                 cur.recordUndoInset(this);
204                 name_ = cmd.getArg(1);
205                 cur.forceBufferUpdate();
206                 break;
207         }
208
209         case LFUN_PASTE:
210         case LFUN_CLIPBOARD_PASTE:
211         case LFUN_SELECTION_PASTE:
212         case LFUN_PRIMARY_SELECTION_PASTE:
213         case LFUN_SELF_INSERT:
214                 // With (only) inherited pass_thru, call Text::dispatch()
215                 // directly to avoid call for fixParagraphsFont() and/or
216                 // forcing to latex_language in InsetText::dispatch(),
217                 // since this does not play nicely with inherited pass_thru
218                 // (see #8471).
219                 if (pass_thru_ && !pass_thru_local_) {
220                         text().dispatch(cur, cmd);
221                         // For the paste operations, check if we have
222                         // non-latex_language, and if so, fix.
223                         if (cmd.action() != LFUN_SELF_INSERT)
224                                 fixParagraphLanguage(buffer().params().language);
225                 }
226                 else
227                         InsetCollapsible::doDispatch(cur, cmd);
228                 break;
229
230         default:
231                 InsetCollapsible::doDispatch(cur, cmd);
232                 break;
233         }
234 }
235
236
237 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
238                 FuncStatus & flag) const
239 {
240         switch (cmd.action()) {
241
242         case LFUN_INSET_MODIFY: {
243                 string const first_arg = cmd.getArg(0);
244                 if (first_arg == "changetype") {
245                         string const type = cmd.getArg(1);
246                         flag.setOnOff(type == name_);
247                         if (type == name_) {
248                                 flag.setEnabled(true);
249                                 return true;
250                         }
251                         Layout::LaTeXArgMap args;
252                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
253                         if (insetlayout)
254                                 args = cur.inset().getLayout().latexargs();
255                         else
256                                 args = cur.paragraph().layout().latexargs();
257                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
258                         if (lait != args.end()) {
259                                 flag.setEnabled(true);
260                                 for (auto const & table : cur.paragraph().insetList())
261                                         if (InsetArgument const * ins = table.inset->asInsetArgument())
262                                                 if (ins->name() == type) {
263                                                         // we have this already
264                                                         flag.setEnabled(false);
265                                                         return true;
266                                                 }
267                         } else
268                                 flag.setEnabled(false);
269                         return true;
270                 }
271                 return InsetCollapsible::getStatus(cur, cmd, flag);
272         }
273
274         default:
275                 return InsetCollapsible::getStatus(cur, cmd, flag);
276         }
277 }
278
279
280 string InsetArgument::contextMenuName() const
281 {
282         if (decoration() == InsetDecoration::CONGLOMERATE)
283                 return "context-argument-conglomerate";
284         else
285                 return "context-argument";
286 }
287
288
289 FontInfo InsetArgument::getFont() const
290 {
291         if (font_ != inherit_font)
292                 return font_;
293         return InsetCollapsible::getFont();
294 }
295
296
297 FontInfo InsetArgument::getLabelfont() const
298 {
299         if (labelfont_ != inherit_font)
300                 return labelfont_;
301         return InsetCollapsible::getLabelfont();
302 }
303
304
305 ColorCode InsetArgument::labelColor() const {
306         if (labelfont_.color() != Color_inherit)
307                 return labelfont_.color();
308         return InsetCollapsible::labelColor();
309 }
310
311
312 InsetDecoration InsetArgument::decoration() const
313 {
314         InsetDecoration dec = getLayout().decoration();
315         if (!decoration_.empty())
316                 dec = translateDecoration(decoration_);
317         return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
318 }
319
320
321 void InsetArgument::docbook(XMLStream & xs, OutputParams const & rp) const {
322         // Ignore arguments that have already been output or are planned to be output elsewhere.
323         if (rp.docbook_prepended_arguments.find(this) != rp.docbook_prepended_arguments.end())
324                 return;
325         if (rp.docbook_appended_arguments.find(this) != rp.docbook_appended_arguments.end())
326                 return;
327
328         if (docbooktag_ != from_ascii("NONE") && docbooktag_ != from_ascii("IGNORE")) {
329                 // TODO: implement docbooktagtype_.
330                 xs << xml::StartTag(docbooktag_, docbookattr_);
331                 InsetText::docbook(xs, rp);
332                 xs << xml::EndTag(docbooktag_);
333                 xs << xml::CR();
334         }
335 }
336
337
338 void InsetArgument::latexArgument(otexstream & os,
339                 OutputParams const & runparams_in, docstring const & ldelim,
340                 docstring const & rdelim, docstring const & presetarg) const
341 {
342         otexstringstream ots;
343         OutputParams runparams = runparams_in;
344         if (!pass_thru_chars_.empty())
345                 runparams.pass_thru_chars += pass_thru_chars_;
346         if (!newline_cmd_.empty())
347                 runparams.newlinecmd = newline_cmd_;
348         runparams.pass_thru = isPassThru();
349         InsetText::latex(ots, runparams);
350         TexString ts = ots.release();
351         bool const add_braces = !ldelim.empty() && ldelim != "{"
352                         && support::contains(ts.str, rdelim);
353         os << ldelim;
354         if (add_braces)
355                 os << '{';
356         os << presetarg;
357         if (!presetarg.empty() && !ts.str.empty())
358                 os << ", ";
359         os << std::move(ts);
360         if (add_braces)
361                 os << '}';
362         os << rdelim;
363 }
364
365
366 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
367                              UpdateType utype, TocBackend & backend) const
368 {
369         if (!caption_of_toc_.empty()) {
370                 docstring str;
371                 text().forOutliner(str, TOC_ENTRY_LENGTH);
372                 backend.builder(caption_of_toc_).argumentItem(str);
373         }
374         // Proceed with the rest of the inset.
375         InsetText::addToToc(dit, output_active, utype, backend);
376 }
377
378
379 void InsetArgument::fixParagraphLanguage(Language const * l)
380 {
381         Font font(inherit_font, l);
382         if (pass_thru_)
383                 font.setLanguage(latex_language);
384         paragraphs().front().resetFonts(font);
385 }
386
387
388 } // namespace lyx