]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
f4f169232b2a8cfb50f55547348f843543b28c57
[lyx.git] / src / insets / InsetCommand.cpp
1 /**
2  * \file InsetCommand.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetCommand.h"
15
16 #include "Buffer.h"
17 #include "BufferEncodings.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Lexer.h"
24 #include "MetricsInfo.h"
25 #include "texstream.h"
26
27 #include "insets/InsetBox.h"
28 #include "insets/InsetBranch.h"
29 #include "insets/InsetCommand.h"
30 #include "insets/InsetERT.h"
31 #include "insets/InsetExternal.h"
32 #include "insets/InsetFloat.h"
33 #include "insets/InsetGraphics.h"
34 #include "insets/InsetIndex.h"
35 #include "insets/InsetLine.h"
36 #include "insets/InsetListings.h"
37 #include "insets/InsetNote.h"
38 #include "insets/InsetPhantom.h"
39 #include "insets/InsetSpace.h"
40 #include "insets/InsetTabular.h"
41 #include "insets/InsetVSpace.h"
42 #include "insets/InsetWrap.h"
43
44 #include "support/debug.h"
45 #include "support/gettext.h"
46
47 #include "frontends/Application.h"
48
49 #include <sstream>
50
51 using namespace std;
52
53
54 namespace lyx {
55
56 // FIXME Would it now be possible to use the InsetCode in
57 // place of the mailer name and recover that information?
58 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
59         : Inset(buf), p_(p)
60 {}
61
62
63 // The sole purpose of this copy constructor is to make sure
64 // that the mouse_hover_ map is not copied and remains empty.
65 InsetCommand::InsetCommand(InsetCommand const & rhs)
66         : Inset(rhs), p_(rhs.p_)
67 {}
68
69
70 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
71 {
72         if (&rhs == this)
73                 return *this;
74
75         Inset::operator=(rhs);
76         p_ = rhs.p_;
77         mouse_hover_.clear();
78         button_ = RenderButton();
79
80         return *this;
81 }
82
83
84 InsetCommand::~InsetCommand()
85 {
86         if (p_.code() != NO_CODE)
87                 hideDialogs(insetName(p_.code()), this);
88
89         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
90         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
91         for (; it != end; ++it)
92                 if (it->second)
93                         it->first->clearLastInset(this);
94 }
95
96
97 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
98 {
99         button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0));
100         button_.metrics(mi, dim);
101 }
102
103
104 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
105         const
106 {
107         mouse_hover_[bv] = mouse_hover;
108         return true;
109 }
110
111
112 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
113 {
114         button_.setRenderState(mouse_hover_[pi.base.bv]);
115         button_.draw(pi, x, y);
116 }
117
118
119 void InsetCommand::setParam(string const & name, docstring const & value)
120 {
121         p_[name] = value;
122 }
123
124
125 docstring const & InsetCommand::getParam(string const & name) const
126 {
127         return p_[name];
128 }
129
130
131 void InsetCommand::setParams(InsetCommandParams const & p)
132 {
133         p_ = p;
134         initView();
135 }
136
137
138 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
139 {
140         OutputParams runparams = runparams_in;
141         os << getCommand(runparams);
142 }
143
144
145 int InsetCommand::plaintext(odocstringstream & os,
146         OutputParams const &, size_t) const
147 {
148         docstring const str = "[" + buffer().B_("LaTeX Command: ")
149                 + from_utf8(getCmdName()) + "]";
150         os << str;
151         return str.size();
152 }
153
154
155 int InsetCommand::docbook(odocstream &, OutputParams const &) const
156 {
157         return 0;
158 }
159
160
161 void InsetCommand::validate(LaTeXFeatures & features) const
162 {
163         if (params().info().hasParam("literal")
164             && params()["literal"] == "true")
165                 return;
166
167         ParamInfo::const_iterator it = params().info().begin();
168         ParamInfo::const_iterator end = params().info().end();
169         for (; it != end; ++it) {
170                 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
171                         docstring const text = params()[it->name()];
172                         // Validate the contents (if we LaTeXify, specific
173                         // macros might require packages)
174                         for (pos_type i = 0; i < int(text.size()) ; ++i)
175                                 BufferEncodings::validate(text[i], features);
176                 }
177         }
178 }
179
180
181 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
182 {
183         switch (cmd.action()) {
184         case LFUN_INSET_MODIFY: {
185                 if (cmd.getArg(0) == "changetype") {
186                         cur.recordUndo();
187                         p_.setCmdName(cmd.getArg(1));
188                         cur.forceBufferUpdate();
189                         initView();
190                         break;
191                 }
192                 InsetCommandParams p(p_.code());
193                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
194                 if (p.getCmdName().empty())
195                         cur.noScreenUpdate();
196                 else {
197                         cur.recordUndo();
198                         setParams(p);
199                 }
200                 // FIXME We might also want to check here if this one is in the TOC.
201                 // But I think most of those are labeled.
202                 if (isLabeled())
203                         cur.forceBufferUpdate();
204                 break;
205         }
206
207         case LFUN_INSET_DIALOG_UPDATE: {
208                 string const name = to_utf8(cmd.argument());
209                 cur.bv().updateDialog(name, params2string(params()));
210                 break;
211         }
212
213         default:
214                 Inset::doDispatch(cur, cmd);
215                 break;
216         }
217
218 }
219
220
221 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
222         FuncStatus & status) const
223 {
224         switch (cmd.action()) {
225         // suppress these
226         case LFUN_ERT_INSERT:
227                 status.setEnabled(false);
228                 return true;
229
230         // we handle these
231         case LFUN_INSET_MODIFY:
232                 if (cmd.getArg(0) == "changetype") {
233                         string const newtype = cmd.getArg(1);
234                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
235                         status.setOnOff(newtype == p_.getCmdName());
236                 }
237                 status.setEnabled(true);
238                 return true;
239
240         case LFUN_INSET_DIALOG_UPDATE:
241                 status.setEnabled(true);
242                 return true;
243
244         default:
245                 return Inset::getStatus(cur, cmd, status);
246         }
247 }
248
249
250 string InsetCommand::contextMenuName() const
251 {
252         return "context-" + insetName(p_.code());
253 }
254
255
256 bool InsetCommand::showInsetDialog(BufferView * bv) const
257 {
258         if (p_.code() != NO_CODE)
259                 bv->showDialog(insetName(p_.code()), params2string(p_),
260                         const_cast<InsetCommand *>(this));
261         return true;
262 }
263
264
265 bool InsetCommand::string2params(string const & data,
266         InsetCommandParams & params)
267 {
268         params.clear();
269         if (data.empty())
270                 return false;
271         // This happens when inset-insert is called without argument except for the
272         // inset type; ex:
273         // "inset-insert toc"
274         string const name = insetName(params.code());
275         if (data == name)
276                 return true;
277         istringstream dstream(data);
278         Lexer lex;
279         lex.setStream(dstream);
280         lex.setContext("InsetCommand::string2params");
281         lex >> name.c_str(); // check for name
282         lex >> "CommandInset";
283         params.read(lex);
284         return true;
285 }
286
287
288 string InsetCommand::params2string(InsetCommandParams const & params)
289 {
290         ostringstream data;
291         data << insetName(params.code()) << ' ';
292         params.write(data);
293         data << "\\end_inset\n";
294         return data.str();
295 }
296
297
298 bool decodeInsetParam(string const & name, string & data,
299         Buffer const & buffer)
300 {
301         InsetCode const code = insetCode(name);
302         switch (code) {
303         case BIBITEM_CODE:
304         case BIBTEX_CODE:
305         case INDEX_PRINT_CODE:
306         case LABEL_CODE:
307         case LINE_CODE:
308         case NOMENCL_CODE:
309         case NOMENCL_PRINT_CODE:
310         case REF_CODE:
311         case TOC_CODE:
312         case HYPERLINK_CODE: {
313                 InsetCommandParams p(code);
314                 data = InsetCommand::params2string(p);
315                 break;
316         }
317         case INCLUDE_CODE: {
318                 // data is the include type: one of "include",
319                 // "input", "verbatiminput" or "verbatiminput*"
320                 if (data.empty())
321                         // default type is requested
322                         data = "include";
323                 InsetCommandParams p(INCLUDE_CODE, data);
324                 data = InsetCommand::params2string(p);
325                 break;
326         }
327         case BOX_CODE: {
328                 // \c data == "Boxed" || "Frameless" etc
329                 InsetBoxParams p(data);
330                 data = InsetBox::params2string(p);
331                 break;
332         }
333         case BRANCH_CODE: {
334                 InsetBranchParams p;
335                 data = InsetBranch::params2string(p);
336                 break;
337         }
338         case CITE_CODE: {
339                 InsetCommandParams p(CITE_CODE);
340                 data = InsetCommand::params2string(p);
341                 break;
342         }
343         case ERT_CODE: {
344                 data = InsetERT::params2string(InsetCollapsable::Open);
345                 break;
346         }
347         case EXTERNAL_CODE: {
348                 InsetExternalParams p;
349                 data = InsetExternal::params2string(p, buffer);
350                 break;
351         }
352         case FLOAT_CODE:  {
353                 InsetFloatParams p;
354                 data = InsetFloat::params2string(p);
355                 break;
356         }
357         case INDEX_CODE: {
358                 InsetIndexParams p;
359                 data = InsetIndex::params2string(p);
360                 break;
361         }
362         case LISTINGS_CODE: {
363                 InsetListingsParams p;
364                 data = InsetListings::params2string(p);
365                 break;
366         }
367         case GRAPHICS_CODE: {
368                 InsetGraphicsParams p;
369                 data = InsetGraphics::params2string(p, buffer);
370                 break;
371         }
372         case MATH_SPACE_CODE: {
373                 InsetSpaceParams p(true);
374                 data = InsetSpace::params2string(p);
375                 break;
376         }
377         case NOTE_CODE: {
378                 InsetNoteParams p;
379                 data = InsetNote::params2string(p);
380                 break;
381         }
382         case PHANTOM_CODE: {
383                 InsetPhantomParams p;
384                 data = InsetPhantom::params2string(p);
385                 break;
386         }
387         case SPACE_CODE: {
388                 InsetSpaceParams p;
389                 data = InsetSpace::params2string(p);
390                 break;
391         }
392         case VSPACE_CODE: {
393                 VSpace space;
394                 data = InsetVSpace::params2string(space);
395                 break;
396         }
397         case WRAP_CODE: {
398                 InsetWrapParams p;
399                 data = InsetWrap::params2string(p);
400                 break;
401         }
402         default:
403                 return false;
404         } // end switch(code)
405         return true;
406 }
407
408 } // namespace lyx