]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
Math.lyx, Tutorial.lyx: fix some typos spotted by a user
[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         default:
153                 InsetCollapsable::doDispatch(cur, cmd);
154                 break;
155         }
156 }
157
158
159 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
160                 FuncStatus & flag) const
161 {
162         switch (cmd.action()) {
163
164         case LFUN_INSET_MODIFY: {
165                 string const first_arg = cmd.getArg(0);
166                 if (first_arg == "changetype") {
167                         string const type = cmd.getArg(1);
168                         flag.setOnOff(type == name_);
169                         if (type == name_) {
170                                 flag.setEnabled(true);
171                                 return true;
172                         }
173                         Layout::LaTeXArgMap args;
174                         bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
175                         if (insetlayout)
176                                 args = cur.inset().getLayout().latexargs();
177                         else
178                                 args = cur.paragraph().layout().latexargs();
179                         Layout::LaTeXArgMap::const_iterator const lait = args.find(type);
180                         if (lait != args.end()) {
181                                 flag.setEnabled(true);
182                                 InsetList::const_iterator it = cur.paragraph().insetList().begin();
183                                 InsetList::const_iterator end = cur.paragraph().insetList().end();
184                                 for (; it != end; ++it) {
185                                         if (it->inset->lyxCode() == ARG_CODE) {
186                                                 InsetArgument const * ins =
187                                                         static_cast<InsetArgument const *>(it->inset);
188                                                 if (ins->name() == type) {
189                                                         // we have this already
190                                                         flag.setEnabled(false);
191                                                         return true;
192                                                 }
193                                         }
194                                 }
195                         } else
196                                 flag.setEnabled(false);
197                         return true;
198                 }
199                 return InsetCollapsable::getStatus(cur, cmd, flag);
200         }
201
202         default:
203                 return InsetCollapsable::getStatus(cur, cmd, flag);
204         }
205 }
206
207 string InsetArgument::contextMenuName() const
208 {
209         return "context-argument";
210 }
211
212 FontInfo InsetArgument::getFont() const
213 {
214         if (font_ != inherit_font)
215                 return font_;
216         return getLayout().font();
217 }
218
219 FontInfo InsetArgument::getLabelfont() const
220 {
221         if (labelfont_ != inherit_font)
222                 return labelfont_;
223         return getLayout().labelfont();
224 }
225
226 InsetLayout::InsetDecoration InsetArgument::decoration() const
227 {
228         InsetLayout::InsetDecoration dec = getLayout().decoration();
229         if (!decoration_.empty())
230                 dec = translateDecoration(decoration_);
231         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
232 }
233
234 void InsetArgument::latexArgument(otexstream & os,
235                 OutputParams const & runparams_in, docstring const & ldelim,
236                 docstring const & rdelim, docstring const & presetarg) const
237 {
238         TexRow texrow;
239         odocstringstream ss;
240         otexstream ots(ss, texrow);
241         OutputParams runparams = runparams_in;
242         InsetText::latex(ots, runparams);
243         docstring str = ss.str();
244         docstring const sep = str.empty() ? docstring() : from_ascii(", ");
245         if (!presetarg.empty())
246                 str = presetarg + sep + str;
247         if (ldelim != "{" && support::contains(str, rdelim))
248                 str = '{' + str + '}';
249         os << ldelim << str << rdelim;
250 }
251
252
253 } // namespace lyx