]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
70dc995699b30438dc36a229b65119b28b79c7b0
[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
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lstrings.h"
35
36 using namespace std;
37
38 namespace lyx {
39
40
41 InsetArgument::InsetArgument(Buffer * buf, string const & name)
42     : InsetCollapsable(buf), name_(name), labelstring_(docstring()),
43       font_(inherit_font), labelfont_(inherit_font), decoration_(string()),
44       pass_thru_context_(false), pass_thru_local_(false), pass_thru_(false),
45       pass_thru_chars_(docstring())
46 {}
47
48
49 void InsetArgument::write(ostream & os) const
50 {
51         os << "Argument " << name_ << "\n";
52         InsetCollapsable::write(os);
53 }
54
55
56 void InsetArgument::read(Lexer & lex)
57 {
58         lex >> name_;
59         InsetCollapsable::read(lex);
60 }
61
62
63 void InsetArgument::updateBuffer(ParIterator const & it, UpdateType utype)
64 {
65         Layout::LaTeXArgMap args = it.paragraph().layout().args();
66         pass_thru_context_ = it.paragraph().layout().pass_thru;
67         bool const insetlayout = args.empty();
68         if (insetlayout) {
69                 args = it.inset().getLayout().args();
70                 pass_thru_context_ = it.inset().getLayout().isPassThru();
71         }
72
73         // Handle pre 2.1 ArgInsets (lyx2lyx cannot classify them)
74         if (name_ == "999") {
75                 unsigned int const req = insetlayout ? it.inset().getLayout().requiredArgs()
76                                       : it.paragraph().layout().requiredArgs();
77                 unsigned int const opts = insetlayout ? it.inset().getLayout().optArgs()
78                                       : it.paragraph().layout().optArgs();
79                 unsigned int nr = 0;
80                 unsigned int ours = 0;
81                 InsetList::const_iterator parit = it.paragraph().insetList().begin();
82                 InsetList::const_iterator parend = it.paragraph().insetList().end();
83                 for (; parit != parend; ++parit) {
84                         if (parit->inset->lyxCode() == ARG_CODE) {
85                                 ++nr;
86                                 if (parit->inset == this)
87                                         ours = nr;
88                         }
89                 }
90                 bool done = false;
91                 unsigned int realopts = 0;
92                 if (nr > req) {
93                         // We have optional arguments
94                         realopts = nr - req;
95                         if (ours <= realopts) {
96                                 name_ = convert<string>(ours);
97                                 done = true;
98                         }
99                 }
100                 if (!done) {
101                         // This is a mandatory argument. We have to consider
102                         // non-given optional arguments for the numbering
103                         int offset = opts - realopts;
104                         ours += offset;
105                         name_ = convert<string>(ours);
106                 }
107         }
108         Layout::LaTeXArgMap::const_iterator const lait = args.find(name_);
109         if (lait != args.end()) {
110                 docstring label = translateIfPossible((*lait).second.labelstring);
111                 docstring striplabel;
112                 support::rsplit(label, striplabel, '|');
113                 labelstring_ = striplabel.empty() ? label: striplabel;
114                 tooltip_ = translateIfPossible((*lait).second.tooltip);
115                 font_ = (*lait).second.font;
116                 labelfont_ = (*lait).second.labelfont;
117                 decoration_ = (*lait).second.decoration;
118                 pass_thru_chars_ = (*lait).second.pass_thru_chars;
119                 pass_thru_local_ = false;
120                 switch ((*lait).second.passthru) {
121                         case PT_INHERITED:
122                                 pass_thru_ = pass_thru_context_;
123                                 break;
124                         case PT_TRUE:
125                                 pass_thru_ = true;
126                                 pass_thru_local_ = true;
127                                 break;
128                         case PT_FALSE:
129                                 pass_thru_ = false;
130                                 break;
131                 }
132         } else {
133                 labelstring_ = _("Unknown Argument");
134                 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
135         }
136         setButtonLabel();
137         InsetCollapsable::updateBuffer(it, utype);
138 }
139
140
141 void InsetArgument::setButtonLabel()
142 {
143         setLabel(labelstring_);
144 }
145
146
147 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
148 {
149         if (isOpen(bv))
150                 return tooltip_;
151         return toolTipText(tooltip_ + from_ascii(":\n"));
152 }
153
154
155 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
156 {
157         switch (cmd.action()) {
158
159         case LFUN_INSET_MODIFY: {
160                 string const first_arg = cmd.getArg(0);
161                 bool const change_type = first_arg == "changetype";
162                 if (!change_type) {
163                         // not for us
164                         // this will not be handled higher up
165                         cur.undispatched();
166                         return;
167                 }
168                 cur.recordUndoInset(this);
169                 name_ = cmd.getArg(1);
170                 cur.forceBufferUpdate();
171                 break;
172         }
173
174         case LFUN_PASTE:
175         case LFUN_CLIPBOARD_PASTE:
176         case LFUN_SELECTION_PASTE:
177         case LFUN_PRIMARY_SELECTION_PASTE:
178                 // Do not call InsetCollapsable::doDispatch(cur, cmd)
179                 // with (inherited) pass_thru to avoid call for
180                 // fixParagraphsFont(), which does not play nicely with
181                 // inherited pass_thru (see #8471).
182                 if (pass_thru_ && !pass_thru_local_)
183                         text().dispatch(cur, cmd);
184                 else
185                         InsetCollapsable::doDispatch(cur, cmd);
186                 break;
187
188         default:
189                 InsetCollapsable::doDispatch(cur, cmd);
190                 break;
191         }
192 }
193
194
195 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
196                 FuncStatus & flag) const
197 {
198         switch (cmd.action()) {
199
200         case LFUN_INSET_MODIFY: {
201                 string const first_arg = cmd.getArg(0);
202                 if (first_arg == "changetype") {
203                         string const type = cmd.getArg(1);
204                         flag.setOnOff(type == name_);
205                         if (type == name_) {
206                                 flag.setEnabled(true);
207                                 return true;
208                         }
209                         Layout::LaTeXArgMap args;
210                         bool const insetlayout = cur.paragraph().layout().latexargs().empty();
211                         if (insetlayout)
212                                 args = cur.inset().getLayout().latexargs();
213                         else
214                                 args = cur.paragraph().layout().latexargs();
215                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
216                         if (lait != args.end()) {
217                                 flag.setEnabled(true);
218                                 InsetList::const_iterator it = cur.paragraph().insetList().begin();
219                                 InsetList::const_iterator end = cur.paragraph().insetList().end();
220                                 for (; it != end; ++it) {
221                                         if (it->inset->lyxCode() == ARG_CODE) {
222                                                 InsetArgument const * ins =
223                                                         static_cast<InsetArgument const *>(it->inset);
224                                                 if (ins->name() == type) {
225                                                         // we have this already
226                                                         flag.setEnabled(false);
227                                                         return true;
228                                                 }
229                                         }
230                                 }
231                         } else
232                                 flag.setEnabled(false);
233                         return true;
234                 }
235                 return InsetCollapsable::getStatus(cur, cmd, flag);
236         }
237
238         default:
239                 return InsetCollapsable::getStatus(cur, cmd, flag);
240         }
241 }
242
243
244 string InsetArgument::contextMenuName() const
245 {
246         if (decoration() == InsetLayout::CONGLOMERATE)
247                 return "context-argument-conglomerate";
248         else
249                 return "context-argument";
250 }
251
252
253 FontInfo InsetArgument::getFont() const
254 {
255         if (font_ != inherit_font)
256                 return font_;
257         return InsetCollapsable::getFont();
258 }
259
260
261 FontInfo InsetArgument::getLabelfont() const
262 {
263         if (labelfont_ != inherit_font)
264                 return labelfont_;
265         return InsetCollapsable::getLabelfont();
266 }
267
268
269 ColorCode InsetArgument::labelColor() const {
270         if (labelfont_.color() != Color_inherit)
271                 return labelfont_.color();
272         return InsetCollapsable::labelColor();
273 }
274
275
276 InsetLayout::InsetDecoration InsetArgument::decoration() const
277 {
278         InsetLayout::InsetDecoration dec = getLayout().decoration();
279         if (!decoration_.empty())
280                 dec = translateDecoration(decoration_);
281         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
282 }
283
284
285 void InsetArgument::latexArgument(otexstream & os,
286                 OutputParams const & runparams_in, docstring const & ldelim,
287                 docstring const & rdelim, docstring const & presetarg) const
288 {
289         otexstringstream ots;
290         OutputParams runparams = runparams_in;
291         if (!pass_thru_chars_.empty())
292                 runparams.pass_thru_chars += pass_thru_chars_;
293         runparams.pass_thru = isPassThru();
294         InsetText::latex(ots, runparams);
295         TexString ts = ots.release();
296         bool const add_braces = ldelim != "{" && support::contains(ts.str, rdelim);
297         os << ldelim;
298         if (add_braces)
299                 os << '{';
300         os << presetarg;
301         if (!presetarg.empty() && !ts.str.empty())
302                 os << ", ";
303         os << move(ts);
304         if (add_braces)
305                 os << '}';
306         os << rdelim;
307 }
308
309
310 } // namespace lyx