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