]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Whitespace
[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     : InsetCollapsable(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())
47 {}
48
49
50 void InsetArgument::write(ostream & os) const
51 {
52         os << "Argument " << name_ << "\n";
53         InsetCollapsable::write(os);
54 }
55
56
57 void InsetArgument::read(Lexer & lex)
58 {
59         lex >> name_;
60         InsetCollapsable::read(lex);
61 }
62
63
64 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
65 {
66         Layout::LaTeXArgMap args = it.paragraph().layout().args();
67         pass_thru_context_ = it.paragraph().layout().pass_thru;
68         bool const insetlayout = args.empty();
69         if (insetlayout) {
70                 args = it.inset().getLayout().args();
71                 pass_thru_context_ = it.inset().getLayout().isPassThru();
72         }
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                         // empty if AddToToc is not set
124                         caption_of_toc_ = insetlayout
125                                 ? it.inset().getLayout().tocType()
126                                 : it.paragraph().layout().tocType();
127
128                 switch ((*lait).second.passthru) {
129                         case PT_INHERITED:
130                                 pass_thru_ = pass_thru_context_;
131                                 break;
132                         case PT_TRUE:
133                                 pass_thru_ = true;
134                                 pass_thru_local_ = true;
135                                 break;
136                         case PT_FALSE:
137                                 pass_thru_ = false;
138                                 break;
139                 }
140         } else {
141                 labelstring_ = _("Unknown Argument");
142                 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
143         }
144         setButtonLabel();
145         InsetCollapsable::updateBuffer(it, utype);
146 }
147
148
149 void InsetArgument::setButtonLabel()
150 {
151         setLabel(labelstring_);
152 }
153
154
155 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
156 {
157         if (isOpen(bv))
158                 return tooltip_;
159         return toolTipText(tooltip_ + from_ascii(":\n"));
160 }
161
162
163 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
164 {
165         switch (cmd.action()) {
166
167         case LFUN_INSET_MODIFY: {
168                 string const first_arg = cmd.getArg(0);
169                 bool const change_type = first_arg == "changetype";
170                 if (!change_type) {
171                         // not for us
172                         // this will not be handled higher up
173                         cur.undispatched();
174                         return;
175                 }
176                 cur.recordUndoInset(this);
177                 name_ = cmd.getArg(1);
178                 cur.forceBufferUpdate();
179                 break;
180         }
181
182         case LFUN_PASTE:
183         case LFUN_CLIPBOARD_PASTE:
184         case LFUN_SELECTION_PASTE:
185         case LFUN_PRIMARY_SELECTION_PASTE:
186         case LFUN_SELF_INSERT:
187                 // With (only) inherited pass_thru, call Text::dispatch()
188                 // directly to avoid call for fixParagraphsFont() and/or
189                 // forcing to latex_language in InsetText::dispatch(),
190                 // since this does not play nicely with inherited pass_thru
191                 // (see #8471).
192                 if (pass_thru_ && !pass_thru_local_)
193                         text().dispatch(cur, cmd);
194                 else
195                         InsetCollapsable::doDispatch(cur, cmd);
196                 break;
197
198         default:
199                 InsetCollapsable::doDispatch(cur, cmd);
200                 break;
201         }
202 }
203
204
205 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
206                 FuncStatus & flag) const
207 {
208         switch (cmd.action()) {
209
210         case LFUN_INSET_MODIFY: {
211                 string const first_arg = cmd.getArg(0);
212                 if (first_arg == "changetype") {
213                         string const type = cmd.getArg(1);
214                         flag.setOnOff(type == name_);
215                         if (type == name_) {
216                                 flag.setEnabled(true);
217                                 return true;
218                         }
219                         Layout::LaTeXArgMap args;
220                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
221                         if (insetlayout)
222                                 args = cur.inset().getLayout().latexargs();
223                         else
224                                 args = cur.paragraph().layout().latexargs();
225                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
226                         if (lait != args.end()) {
227                                 flag.setEnabled(true);
228                                 InsetList::const_iterator it = cur.paragraph().insetList().begin();
229                                 InsetList::const_iterator end = cur.paragraph().insetList().end();
230                                 for (; it != end; ++it) {
231                                         if (it->inset->lyxCode() == ARG_CODE) {
232                                                 InsetArgument const * ins =
233                                                         static_cast<InsetArgument const *>(it->inset);
234                                                 if (ins->name() == type) {
235                                                         // we have this already
236                                                         flag.setEnabled(false);
237                                                         return true;
238                                                 }
239                                         }
240                                 }
241                         } else
242                                 flag.setEnabled(false);
243                         return true;
244                 }
245                 return InsetCollapsable::getStatus(cur, cmd, flag);
246         }
247
248         default:
249                 return InsetCollapsable::getStatus(cur, cmd, flag);
250         }
251 }
252
253
254 string InsetArgument::contextMenuName() const
255 {
256         if (decoration() == InsetLayout::CONGLOMERATE)
257                 return "context-argument-conglomerate";
258         else
259                 return "context-argument";
260 }
261
262
263 FontInfo InsetArgument::getFont() const
264 {
265         if (font_ != inherit_font)
266                 return font_;
267         return InsetCollapsable::getFont();
268 }
269
270
271 FontInfo InsetArgument::getLabelfont() const
272 {
273         if (labelfont_ != inherit_font)
274                 return labelfont_;
275         return InsetCollapsable::getLabelfont();
276 }
277
278
279 ColorCode InsetArgument::labelColor() const {
280         if (labelfont_.color() != Color_inherit)
281                 return labelfont_.color();
282         return InsetCollapsable::labelColor();
283 }
284
285
286 InsetLayout::InsetDecoration InsetArgument::decoration() const
287 {
288         InsetLayout::InsetDecoration dec = getLayout().decoration();
289         if (!decoration_.empty())
290                 dec = translateDecoration(decoration_);
291         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
292 }
293
294
295 void InsetArgument::latexArgument(otexstream & os,
296                 OutputParams const & runparams_in, docstring const & ldelim,
297                 docstring const & rdelim, docstring const & presetarg) const
298 {
299         otexstringstream ots;
300         OutputParams runparams = runparams_in;
301         if (!pass_thru_chars_.empty())
302                 runparams.pass_thru_chars += pass_thru_chars_;
303         runparams.pass_thru = isPassThru();
304         InsetText::latex(ots, runparams);
305         TexString ts = ots.release();
306         bool const add_braces = ldelim != "{" && support::contains(ts.str, rdelim);
307         os << ldelim;
308         if (add_braces)
309                 os << '{';
310         os << presetarg;
311         if (!presetarg.empty() && !ts.str.empty())
312                 os << ", ";
313         os << move(ts);
314         if (add_braces)
315                 os << '}';
316         os << rdelim;
317 }
318
319
320 void InsetArgument::addToToc(DocIterator const & dit, bool output_active,
321                              UpdateType utype, TocBackend & backend) const
322 {
323         if (!caption_of_toc_.empty()) {
324                 docstring str;
325                 text().forOutliner(str, TOC_ENTRY_LENGTH);
326                 backend.builder(caption_of_toc_).argumentItem(str);
327         }
328         // Proceed with the rest of the inset.
329         InsetText::addToToc(dit, output_active, utype, backend);
330 }
331
332
333 } // namespace lyx