]> git.lyx.org Git - lyx.git/blob - src/insets/InsetArgument.cpp
533c4239956bbb5170ce4603221cb3f0f0f2376d
[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         if (insetlayout) {
63                 args = it.inset().getLayout().latexargs();
64                 pass_thru_ = it.inset().getLayout().isPassThru();
65         } else {
66                 args = it.paragraph().layout().latexargs();
67                 pass_thru_ = it.paragraph().layout().pass_thru;
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 =
106                         args.find(convert<unsigned int>(name_));
107         if (lait != args.end()) {
108                 docstring label = translateIfPossible((*lait).second.labelstring);
109                 docstring striplabel;
110                 support::rsplit(label, striplabel, '|');
111                 labelstring_ = striplabel.empty() ? label: striplabel;
112                 tooltip_ = translateIfPossible((*lait).second.tooltip);
113                 font_ = (*lait).second.font;
114                 labelfont_ = (*lait).second.labelfont;
115                 decoration_ = (*lait).second.decoration;
116         } else {
117                 labelstring_ = _("Unknown Argument");
118                 tooltip_ = _("Argument not known in this Layout. Will be supressed in the output.");
119         }
120         setButtonLabel();
121         InsetCollapsable::updateBuffer(it, utype);
122 }
123
124 void InsetArgument::setButtonLabel()
125 {
126         setLabel(labelstring_);
127 }
128
129 docstring InsetArgument::toolTip(BufferView const & bv, int, int) const
130 {
131         if (isOpen(bv))
132                 return tooltip_;
133         return toolTipText(tooltip_ + from_ascii(":\n"));
134 }
135
136 void InsetArgument::doDispatch(Cursor & cur, FuncRequest & cmd)
137 {
138         switch (cmd.action()) {
139
140         case LFUN_INSET_MODIFY: {
141                 string const first_arg = cmd.getArg(0);
142                 bool const change_type = first_arg == "changetype";
143                 if (!change_type) {
144                         // not for us
145                         // this will not be handled higher up
146                         cur.undispatched();
147                         return;
148                 }
149                 cur.recordUndoInset(ATOMIC_UNDO, this);
150                 name_ = cmd.getArg(1);
151                 cur.forceBufferUpdate();
152                 break;
153         }
154
155         default:
156                 InsetCollapsable::doDispatch(cur, cmd);
157                 break;
158         }
159 }
160
161
162 bool InsetArgument::getStatus(Cursor & cur, FuncRequest const & cmd,
163                 FuncStatus & flag) const
164 {
165         switch (cmd.action()) {
166
167         case LFUN_INSET_MODIFY: {
168                 string const first_arg = cmd.getArg(0);
169                 if (first_arg == "changetype") {
170                         string const type = cmd.getArg(1);
171                         flag.setOnOff(type == name_);
172                         if (type == name_) {
173                                 flag.setEnabled(true);
174                                 return true;
175                         }
176                         Layout::LaTeXArgMap args;
177                         bool const insetlayout = &cur.inset() && cur.paragraph().layout().latexargs().empty();
178                         if (insetlayout)
179                                 args = cur.inset().getLayout().latexargs();
180                         else
181                                 args = cur.paragraph().layout().latexargs();
182                         Layout::LaTeXArgMap::const_iterator const lait =
183                                         args.find(convert<unsigned int>(type));
184                         if (lait != args.end()) {
185                                 flag.setEnabled(true);
186                                 InsetList::const_iterator it = cur.paragraph().insetList().begin();
187                                 InsetList::const_iterator end = cur.paragraph().insetList().end();
188                                 for (; it != end; ++it) {
189                                         if (it->inset->lyxCode() == ARG_CODE) {
190                                                 InsetArgument const * ins =
191                                                         static_cast<InsetArgument const *>(it->inset);
192                                                 if (ins->name() == type) {
193                                                         // we have this already
194                                                         flag.setEnabled(false);
195                                                         return true;
196                                                 }
197                                         }
198                                 }
199                         } else
200                                 flag.setEnabled(false);
201                         return true;
202                 }
203                 return InsetCollapsable::getStatus(cur, cmd, flag);
204         }
205
206         default:
207                 return InsetCollapsable::getStatus(cur, cmd, flag);
208         }
209 }
210
211 string InsetArgument::contextMenuName() const
212 {
213         return "context-argument";
214 }
215
216 FontInfo InsetArgument::getFont() const
217 {
218         if (font_ != inherit_font)
219                 return font_;
220         return getLayout().font();
221 }
222
223 FontInfo InsetArgument::getLabelfont() const
224 {
225         if (labelfont_ != inherit_font)
226                 return labelfont_;
227         return getLayout().labelfont();
228 }
229
230 namespace {
231
232 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
233 {
234         if (support::compare_ascii_no_case(str, "classic") == 0)
235                 return InsetLayout::CLASSIC;
236         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
237                 return InsetLayout::MINIMALISTIC;
238         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
239                 return InsetLayout::CONGLOMERATE;
240         return InsetLayout::DEFAULT;
241 }
242
243 }// namespace anon
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) 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         if (ldelim != "{" && support::contains(str, rdelim))
264                 str = '{' + str + '}';
265         os << ldelim << str << rdelim;
266 }
267
268
269 } // namespace lyx