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