]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Cleanup headers
[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.getCmdName().empty())
220                         cur.noScreenUpdate();
221                 else {
222                         cur.recordUndo();
223                         if (buffer().masterParams().track_changes) {
224                                 // With change tracking, we insert a new inset and
225                                 // delete the old one
226                                 string const data = InsetCommand::params2string(p);
227                                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
228                                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
229                         } else
230                                 setParams(p);
231                 }
232                 // FIXME We might also want to check here if this one is in the TOC.
233                 // But I think most of those are labeled.
234                 if (isLabeled())
235                         cur.forceBufferUpdate();
236                 break;
237         }
238
239         case LFUN_INSET_DIALOG_UPDATE: {
240                 string const name = to_utf8(cmd.argument());
241                 cur.bv().updateDialog(name, params2string(params()));
242                 break;
243         }
244
245         default:
246                 Inset::doDispatch(cur, cmd);
247                 break;
248         }
249
250 }
251
252
253 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
254         FuncStatus & status) const
255 {
256         switch (cmd.action()) {
257         // suppress these
258         case LFUN_ERT_INSERT:
259                 status.setEnabled(false);
260                 return true;
261
262         // we handle these
263         case LFUN_INSET_MODIFY:
264                 if (cmd.getArg(0) == "changetype") {
265                         string const newtype = cmd.getArg(1);
266                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
267                         status.setOnOff(newtype == p_.getCmdName());
268                 }
269                 status.setEnabled(true);
270                 return true;
271
272         case LFUN_INSET_DIALOG_UPDATE:
273                 status.setEnabled(true);
274                 return true;
275
276         default:
277                 return Inset::getStatus(cur, cmd, status);
278         }
279 }
280
281
282 string InsetCommand::contextMenuName() const
283 {
284         return "context-" + insetName(p_.code());
285 }
286
287
288 bool InsetCommand::showInsetDialog(BufferView * bv) const
289 {
290         if (p_.code() != NO_CODE)
291                 bv->showDialog(insetName(p_.code()), params2string(p_),
292                         const_cast<InsetCommand *>(this));
293         return true;
294 }
295
296
297 bool InsetCommand::string2params(string const & data,
298         InsetCommandParams & params)
299 {
300         params.clear();
301         if (data.empty())
302                 return false;
303         // This happens when inset-insert is called without argument except for the
304         // inset type; ex:
305         // "inset-insert toc"
306         string const name = insetName(params.code());
307         if (data == name)
308                 return true;
309         istringstream dstream(data);
310         Lexer lex;
311         lex.setStream(dstream);
312         lex.setContext("InsetCommand::string2params");
313         lex >> name.c_str(); // check for name
314         lex >> "CommandInset";
315         params.read(lex);
316         return true;
317 }
318
319
320 string InsetCommand::params2string(InsetCommandParams const & params)
321 {
322         ostringstream data;
323         data << insetName(params.code()) << ' ';
324         params.write(data);
325         data << "\\end_inset\n";
326         return data.str();
327 }
328
329
330 bool decodeInsetParam(string const & name, string & data,
331         Buffer const & buffer)
332 {
333         InsetCode const code = insetCode(name);
334         switch (code) {
335         case BIBITEM_CODE:
336         case BIBTEX_CODE:
337         case INDEX_PRINT_CODE:
338         case LABEL_CODE:
339         case LINE_CODE:
340         case NOMENCL_CODE:
341         case NOMENCL_PRINT_CODE:
342         case REF_CODE:
343         case TOC_CODE:
344         case HYPERLINK_CODE:
345         case COUNTER_CODE: {
346                 InsetCommandParams p(code);
347                 data = InsetCommand::params2string(p);
348                 break;
349         }
350         case INCLUDE_CODE: {
351                 // data is the include type: one of "include",
352                 // "input", "verbatiminput" or "verbatiminput*"
353                 if (data.empty())
354                         // default type is requested
355                         data = "include";
356                 InsetCommandParams p(INCLUDE_CODE, data);
357                 data = InsetCommand::params2string(p);
358                 break;
359         }
360         case BOX_CODE: {
361                 // \c data == "Boxed" || "Frameless" etc
362                 InsetBoxParams p(data);
363                 data = InsetBox::params2string(p);
364                 break;
365         }
366         case BRANCH_CODE: {
367                 InsetBranchParams p;
368                 data = InsetBranch::params2string(p);
369                 break;
370         }
371         case CITE_CODE: {
372                 InsetCommandParams p(CITE_CODE);
373                 data = InsetCommand::params2string(p);
374                 break;
375         }
376         case ERT_CODE: {
377                 data = InsetERT::params2string(InsetCollapsible::Open);
378                 break;
379         }
380         case EXTERNAL_CODE: {
381                 InsetExternalParams p;
382                 data = InsetExternal::params2string(p, buffer);
383                 break;
384         }
385         case FLOAT_CODE:  {
386                 InsetFloatParams p;
387                 data = InsetFloat::params2string(p);
388                 break;
389         }
390         case INDEX_CODE: {
391                 InsetIndexParams p;
392                 data = InsetIndex::params2string(p);
393                 break;
394         }
395         case LISTINGS_CODE: {
396                 InsetListingsParams p;
397                 data = InsetListings::params2string(p);
398                 break;
399         }
400         case GRAPHICS_CODE: {
401                 InsetGraphicsParams p;
402                 data = InsetGraphics::params2string(p, buffer);
403                 break;
404         }
405         case MATH_SPACE_CODE: {
406                 InsetSpaceParams p(true);
407                 data = InsetSpace::params2string(p);
408                 break;
409         }
410         case NOTE_CODE: {
411                 InsetNoteParams p;
412                 data = InsetNote::params2string(p);
413                 break;
414         }
415         case PHANTOM_CODE: {
416                 InsetPhantomParams p;
417                 data = InsetPhantom::params2string(p);
418                 break;
419         }
420         case SPACE_CODE: {
421                 InsetSpaceParams p;
422                 data = InsetSpace::params2string(p);
423                 break;
424         }
425         case VSPACE_CODE: {
426                 VSpace space;
427                 data = InsetVSpace::params2string(space);
428                 break;
429         }
430         case WRAP_CODE: {
431                 InsetWrapParams p;
432                 data = InsetWrap::params2string(p);
433                 break;
434         }
435         default:
436                 return false;
437         } // end switch(code)
438         return true;
439 }
440
441 } // namespace lyx