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