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