]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
2e24d4d4e1d213f3d232fe6ee711f6c865099295
[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()["literal"] == "true")
164                 return;
165
166         ParamInfo::const_iterator it = params().info().begin();
167         ParamInfo::const_iterator end = params().info().end();
168         for (; it != end; ++it) {
169                 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
170                         docstring const text = params()[it->name()];
171                         // Validate the contents (if we LaTeXify, specific
172                         // macros might require packages)
173                         for (pos_type i = 0; i < int(text.size()) ; ++i)
174                                 BufferEncodings::validate(text[i], features);
175                 }
176         }
177 }
178
179
180 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
181 {
182         switch (cmd.action()) {
183         case LFUN_INSET_MODIFY: {
184                 if (cmd.getArg(0) == "changetype") {
185                         cur.recordUndo();
186                         p_.setCmdName(cmd.getArg(1));
187                         cur.forceBufferUpdate();
188                         initView();
189                         break;
190                 }
191                 InsetCommandParams p(p_.code());
192                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
193                 if (p.getCmdName().empty())
194                         cur.noScreenUpdate();
195                 else {
196                         cur.recordUndo();
197                         setParams(p);
198                 }
199                 // FIXME We might also want to check here if this one is in the TOC.
200                 // But I think most of those are labeled.
201                 if (isLabeled())
202                         cur.forceBufferUpdate();
203                 break;
204         }
205
206         case LFUN_INSET_DIALOG_UPDATE: {
207                 string const name = to_utf8(cmd.argument());
208                 cur.bv().updateDialog(name, params2string(params()));
209                 break;
210         }
211
212         default:
213                 Inset::doDispatch(cur, cmd);
214                 break;
215         }
216
217 }
218
219
220 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
221         FuncStatus & status) const
222 {
223         switch (cmd.action()) {
224         // suppress these
225         case LFUN_ERT_INSERT:
226                 status.setEnabled(false);
227                 return true;
228
229         // we handle these
230         case LFUN_INSET_MODIFY:
231                 if (cmd.getArg(0) == "changetype") {
232                         string const newtype = cmd.getArg(1);
233                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
234                         status.setOnOff(newtype == p_.getCmdName());
235                 }
236                 status.setEnabled(true);
237                 return true;
238
239         case LFUN_INSET_DIALOG_UPDATE:
240                 status.setEnabled(true);
241                 return true;
242
243         default:
244                 return Inset::getStatus(cur, cmd, status);
245         }
246 }
247
248
249 string InsetCommand::contextMenuName() const
250 {
251         return "context-" + insetName(p_.code());
252 }
253
254
255 bool InsetCommand::showInsetDialog(BufferView * bv) const
256 {
257         if (p_.code() != NO_CODE)
258                 bv->showDialog(insetName(p_.code()), params2string(p_),
259                         const_cast<InsetCommand *>(this));
260         return true;
261 }
262
263
264 bool InsetCommand::string2params(string const & data,
265         InsetCommandParams & params)
266 {
267         params.clear();
268         if (data.empty())
269                 return false;
270         // This happens when inset-insert is called without argument except for the
271         // inset type; ex:
272         // "inset-insert toc"
273         string const name = insetName(params.code());
274         if (data == name)
275                 return true;
276         istringstream dstream(data);
277         Lexer lex;
278         lex.setStream(dstream);
279         lex.setContext("InsetCommand::string2params");
280         lex >> name.c_str(); // check for name
281         lex >> "CommandInset";
282         params.read(lex);
283         return true;
284 }
285
286
287 string InsetCommand::params2string(InsetCommandParams const & params)
288 {
289         ostringstream data;
290         data << insetName(params.code()) << ' ';
291         params.write(data);
292         data << "\\end_inset\n";
293         return data.str();
294 }
295
296
297 bool decodeInsetParam(string const & name, string & data,
298         Buffer const & buffer)
299 {
300         InsetCode const code = insetCode(name);
301         switch (code) {
302         case BIBITEM_CODE:
303         case BIBTEX_CODE:
304         case INDEX_PRINT_CODE:
305         case LABEL_CODE:
306         case LINE_CODE:
307         case NOMENCL_CODE:
308         case NOMENCL_PRINT_CODE:
309         case REF_CODE:
310         case TOC_CODE:
311         case HYPERLINK_CODE: {
312                 InsetCommandParams p(code);
313                 data = InsetCommand::params2string(p);
314                 break;
315         }
316         case INCLUDE_CODE: {
317                 // data is the include type: one of "include",
318                 // "input", "verbatiminput" or "verbatiminput*"
319                 if (data.empty())
320                         // default type is requested
321                         data = "include";
322                 InsetCommandParams p(INCLUDE_CODE, data);
323                 data = InsetCommand::params2string(p);
324                 break;
325         }
326         case BOX_CODE: {
327                 // \c data == "Boxed" || "Frameless" etc
328                 InsetBoxParams p(data);
329                 data = InsetBox::params2string(p);
330                 break;
331         }
332         case BRANCH_CODE: {
333                 InsetBranchParams p;
334                 data = InsetBranch::params2string(p);
335                 break;
336         }
337         case CITE_CODE: {
338                 InsetCommandParams p(CITE_CODE);
339                 data = InsetCommand::params2string(p);
340                 break;
341         }
342         case ERT_CODE: {
343                 data = InsetERT::params2string(InsetCollapsable::Open);
344                 break;
345         }
346         case EXTERNAL_CODE: {
347                 InsetExternalParams p;
348                 data = InsetExternal::params2string(p, buffer);
349                 break;
350         }
351         case FLOAT_CODE:  {
352                 InsetFloatParams p;
353                 data = InsetFloat::params2string(p);
354                 break;
355         }
356         case INDEX_CODE: {
357                 InsetIndexParams p;
358                 data = InsetIndex::params2string(p);
359                 break;
360         }
361         case LISTINGS_CODE: {
362                 InsetListingsParams p;
363                 data = InsetListings::params2string(p);
364                 break;
365         }
366         case GRAPHICS_CODE: {
367                 InsetGraphicsParams p;
368                 data = InsetGraphics::params2string(p, buffer);
369                 break;
370         }
371         case MATH_SPACE_CODE: {
372                 InsetSpaceParams p(true);
373                 data = InsetSpace::params2string(p);
374                 break;
375         }
376         case NOTE_CODE: {
377                 InsetNoteParams p;
378                 data = InsetNote::params2string(p);
379                 break;
380         }
381         case PHANTOM_CODE: {
382                 InsetPhantomParams p;
383                 data = InsetPhantom::params2string(p);
384                 break;
385         }
386         case SPACE_CODE: {
387                 InsetSpaceParams p;
388                 data = InsetSpace::params2string(p);
389                 break;
390         }
391         case VSPACE_CODE: {
392                 VSpace space;
393                 data = InsetVSpace::params2string(space);
394                 break;
395         }
396         case WRAP_CODE: {
397                 InsetWrapParams p;
398                 data = InsetWrap::params2string(p);
399                 break;
400         }
401         default:
402                 return false;
403         } // end switch(code)
404         return true;
405 }
406
407 } // namespace lyx