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