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