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