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