]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Fix crash (missing Buffer update)
[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 "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Lexer.h"
24 #include "LyX.h"
25 #include "MetricsInfo.h"
26 #include "texstream.h"
27
28 #include "insets/InsetBox.h"
29 #include "insets/InsetBranch.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/InsetListings.h"
36 #include "insets/InsetNote.h"
37 #include "insets/InsetPhantom.h"
38 #include "insets/InsetSpace.h"
39 #include "insets/InsetVSpace.h"
40 #include "insets/InsetWrap.h"
41
42 #include "support/debug.h"
43 #include "support/lstrings.h"
44
45 #include "frontends/Application.h"
46
47 #include <sstream>
48
49 using namespace std;
50 using namespace lyx::support;
51
52
53 namespace lyx {
54
55 // FIXME Would it now be possible to use the InsetCode in
56 // place of the mailer name and recover that information?
57 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
58         : Inset(buf), p_(p), broken_(false)
59 {}
60
61
62 // The sole purpose of this copy constructor is to make sure
63 // that the mouse_hover_ map is not copied and remains empty.
64 InsetCommand::InsetCommand(InsetCommand const & rhs)
65         : Inset(rhs), p_(rhs.p_), broken_(false)
66 {}
67
68
69 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
70 {
71         if (&rhs == this)
72                 return *this;
73
74         Inset::operator=(rhs);
75         p_ = rhs.p_;
76         mouse_hover_.clear();
77         button_ = RenderButton();
78         broken_ = false;
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(), broken_);
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         docstring command = getCommand(runparams);
143         if (buffer().params().use_minted
144             && prefixIs(command, from_ascii("\\lstlistoflistings")))
145                 command.erase(1, 3);
146         os << command;
147 }
148
149
150 int InsetCommand::plaintext(odocstringstream & os,
151         OutputParams const &, size_t) const
152 {
153         docstring const str = "[" + buffer().B_("LaTeX Command: ")
154                 + from_utf8(getCmdName()) + "]";
155         os << str;
156         return str.size();
157 }
158
159
160 void InsetCommand::docbook(XMLStream &, OutputParams const &) const
161 {
162         return;
163 }
164
165
166 void InsetCommand::validate(LaTeXFeatures & features) const
167 {
168         if (params().info().hasParam("literal")
169             && params()["literal"] == "true")
170                 return;
171
172         ParamInfo::const_iterator it = params().info().begin();
173         ParamInfo::const_iterator end = params().info().end();
174         for (; it != end; ++it) {
175                 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
176                         docstring const text = params()[it->name()];
177                         // Validate the contents (if we LaTeXify, specific
178                         // macros might require packages)
179                         for (pos_type i = 0; i < int(text.size()) ; ++i)
180                                 BufferEncodings::validate(text[i], features);
181                 }
182         }
183 }
184
185
186 void InsetCommand::changeCmdName(string const & new_name)
187 {
188         string const & old_name = getCmdName();
189         if (old_name == new_name)
190                 return;
191
192         if (buffer().masterParams().track_changes) {
193                 // With change tracking, we insert a new inset and
194                 // delete the old one
195                 InsetCommandParams p(p_.code());
196                 p = p_;
197                 p.setCmdName(new_name);
198                 string const data = InsetCommand::params2string(p);
199                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
200                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
201         } else
202                 p_.setCmdName(new_name);
203 }
204
205
206 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
207 {
208         switch (cmd.action()) {
209         case LFUN_INSET_MODIFY: {
210                 if (cmd.getArg(0) == "changetype") {
211                         cur.recordUndo();
212                         changeCmdName(cmd.getArg(1));
213                         cur.forceBufferUpdate();
214                         initView();
215                         break;
216                 }
217                 InsetCommandParams p(p_.code());
218                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
219                 if (p == p_)
220                         // no change
221                         break;
222                 if (p.getCmdName().empty())
223                         cur.noScreenUpdate();
224                 else {
225                         cur.recordUndo();
226                         if (buffer().masterParams().track_changes) {
227                                 // With change tracking, we insert a new inset and
228                                 // delete the old one
229                                 string const data = InsetCommand::params2string(p);
230                                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
231                                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
232                                 cur.forceBufferUpdate();
233                                 break;
234                         } else
235                                 setParams(p);
236                 }
237                 // FIXME We might also want to check here if this one is in the TOC.
238                 // But I think most of those are labeled.
239                 if (isLabeled())
240                         cur.forceBufferUpdate();
241                 break;
242         }
243
244         case LFUN_INSET_DIALOG_UPDATE: {
245                 string const name = to_utf8(cmd.argument());
246                 cur.bv().updateDialog(name, params2string(params()));
247                 break;
248         }
249
250         default:
251                 Inset::doDispatch(cur, cmd);
252                 break;
253         }
254
255 }
256
257
258 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
259         FuncStatus & status) const
260 {
261         switch (cmd.action()) {
262         // suppress these
263         case LFUN_ERT_INSERT:
264                 status.setEnabled(false);
265                 return true;
266
267         // we handle these
268         case LFUN_INSET_MODIFY:
269                 if (cmd.getArg(0) == "changetype") {
270                         string const newtype = cmd.getArg(1);
271                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
272                         status.setOnOff(newtype == p_.getCmdName());
273                 }
274                 status.setEnabled(true);
275                 return true;
276
277         case LFUN_INSET_DIALOG_UPDATE:
278                 status.setEnabled(true);
279                 return true;
280
281         default:
282                 return Inset::getStatus(cur, cmd, status);
283         }
284 }
285
286
287 string InsetCommand::contextMenuName() const
288 {
289         return "context-" + insetName(p_.code());
290 }
291
292
293 bool InsetCommand::showInsetDialog(BufferView * bv) const
294 {
295         if (p_.code() != NO_CODE)
296                 bv->showDialog(insetName(p_.code()), params2string(p_),
297                         const_cast<InsetCommand *>(this));
298         return true;
299 }
300
301
302 bool InsetCommand::string2params(string const & data,
303         InsetCommandParams & params)
304 {
305         params.clear();
306         if (data.empty())
307                 return false;
308         // This happens when inset-insert is called without argument except for the
309         // inset type; ex:
310         // "inset-insert toc"
311         string const name = insetName(params.code());
312         if (data == name)
313                 return true;
314         istringstream dstream(data);
315         Lexer lex;
316         lex.setStream(dstream);
317         lex.setContext("InsetCommand::string2params");
318         lex >> name.c_str(); // check for name
319         lex >> "CommandInset";
320         params.read(lex);
321         return true;
322 }
323
324
325 string InsetCommand::params2string(InsetCommandParams const & params)
326 {
327         ostringstream data;
328         data << insetName(params.code()) << ' ';
329         params.write(data);
330         data << "\\end_inset\n";
331         return data.str();
332 }
333
334
335 bool decodeInsetParam(string const & name, string & data,
336         Buffer const & buffer)
337 {
338         InsetCode const code = insetCode(name);
339         switch (code) {
340         case BIBITEM_CODE:
341         case BIBTEX_CODE:
342         case INDEX_PRINT_CODE:
343         case LABEL_CODE:
344         case LINE_CODE:
345         case NOMENCL_CODE:
346         case NOMENCL_PRINT_CODE:
347         case REF_CODE:
348         case TOC_CODE:
349         case HYPERLINK_CODE:
350         case COUNTER_CODE: {
351                 InsetCommandParams p(code);
352                 data = InsetCommand::params2string(p);
353                 break;
354         }
355         case INCLUDE_CODE: {
356                 // data is the include type: one of "include",
357                 // "input", "verbatiminput" or "verbatiminput*"
358                 if (data.empty())
359                         // default type is requested
360                         data = "include";
361                 InsetCommandParams p(INCLUDE_CODE, data);
362                 data = InsetCommand::params2string(p);
363                 break;
364         }
365         case BOX_CODE: {
366                 // \c data == "Boxed" || "Frameless" etc
367                 InsetBoxParams p(data);
368                 data = InsetBox::params2string(p);
369                 break;
370         }
371         case BRANCH_CODE: {
372                 InsetBranchParams p;
373                 data = InsetBranch::params2string(p);
374                 break;
375         }
376         case CITE_CODE: {
377                 InsetCommandParams p(CITE_CODE);
378                 data = InsetCommand::params2string(p);
379                 break;
380         }
381         case ERT_CODE: {
382                 data = InsetERT::params2string(InsetCollapsible::Open);
383                 break;
384         }
385         case EXTERNAL_CODE: {
386                 InsetExternalParams p;
387                 data = InsetExternal::params2string(p, buffer);
388                 break;
389         }
390         case FLOAT_CODE:  {
391                 InsetFloatParams p;
392                 data = InsetFloat::params2string(p);
393                 break;
394         }
395         case INDEX_CODE: {
396                 InsetIndexParams p;
397                 data = InsetIndex::params2string(p);
398                 break;
399         }
400         case LISTINGS_CODE: {
401                 InsetListingsParams p;
402                 data = InsetListings::params2string(p);
403                 break;
404         }
405         case GRAPHICS_CODE: {
406                 InsetGraphicsParams p;
407                 data = InsetGraphics::params2string(p, buffer);
408                 break;
409         }
410         case MATH_SPACE_CODE: {
411                 InsetSpaceParams p(true);
412                 data = InsetSpace::params2string(p);
413                 break;
414         }
415         case NOTE_CODE: {
416                 InsetNoteParams p;
417                 data = InsetNote::params2string(p);
418                 break;
419         }
420         case PHANTOM_CODE: {
421                 InsetPhantomParams p;
422                 data = InsetPhantom::params2string(p);
423                 break;
424         }
425         case SPACE_CODE: {
426                 InsetSpaceParams p;
427                 data = InsetSpace::params2string(p);
428                 break;
429         }
430         case VSPACE_CODE: {
431                 VSpace space;
432                 data = InsetVSpace::params2string(space);
433                 break;
434         }
435         case WRAP_CODE: {
436                 InsetWrapParams p;
437                 data = InsetWrap::params2string(p);
438                 break;
439         }
440         default:
441                 return false;
442         } // end switch(code)
443         return true;
444 }
445
446 } // namespace lyx