]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Get rid of Qt resources
[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/gettext.h"
48 #include "support/lstrings.h"
49
50 #include "frontends/Application.h"
51
52 #include <sstream>
53
54 using namespace std;
55 using namespace lyx::support;
56
57
58 namespace lyx {
59
60 // FIXME Would it now be possible to use the InsetCode in
61 // place of the mailer name and recover that information?
62 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
63         : Inset(buf), p_(p), broken_(false)
64 {}
65
66
67 // The sole purpose of this copy constructor is to make sure
68 // that the mouse_hover_ map is not copied and remains empty.
69 InsetCommand::InsetCommand(InsetCommand const & rhs)
70         : Inset(rhs), p_(rhs.p_), broken_(false)
71 {}
72
73
74 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
75 {
76         if (&rhs == this)
77                 return *this;
78
79         Inset::operator=(rhs);
80         p_ = rhs.p_;
81         mouse_hover_.clear();
82         button_ = RenderButton();
83         broken_ = false;
84
85         return *this;
86 }
87
88
89 InsetCommand::~InsetCommand()
90 {
91         if (p_.code() != NO_CODE)
92                 hideDialogs(insetName(p_.code()), this);
93
94         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
95         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
96         for (; it != end; ++it)
97                 if (it->second)
98                         it->first->clearLastInset(this);
99 }
100
101
102 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
103 {
104         button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0),
105                        inheritFont(), broken_);
106         button_.metrics(mi, dim);
107 }
108
109
110 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
111         const
112 {
113         mouse_hover_[bv] = mouse_hover;
114         return true;
115 }
116
117
118 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
119 {
120         button_.setRenderState(mouse_hover_[pi.base.bv]);
121         button_.draw(pi, x, y);
122 }
123
124
125 void InsetCommand::setParam(string const & name, docstring const & value)
126 {
127         p_[name] = value;
128 }
129
130
131 docstring const & InsetCommand::getParam(string const & name) const
132 {
133         return p_[name];
134 }
135
136
137 void InsetCommand::setParams(InsetCommandParams const & p)
138 {
139         p_ = p;
140         initView();
141 }
142
143
144 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
145 {
146         OutputParams runparams = runparams_in;
147         docstring command = getCommand(runparams);
148         if (buffer().params().use_minted
149             && prefixIs(command, from_ascii("\\lstlistoflistings")))
150                 command.erase(1, 3);
151         os << command;
152 }
153
154
155 int InsetCommand::plaintext(odocstringstream & os,
156         OutputParams const &, size_t) const
157 {
158         docstring const str = "[" + buffer().B_("LaTeX Command: ")
159                 + from_utf8(getCmdName()) + "]";
160         os << str;
161         return str.size();
162 }
163
164
165 int InsetCommand::docbook(odocstream &, OutputParams const &) const
166 {
167         return 0;
168 }
169
170
171 void InsetCommand::validate(LaTeXFeatures & features) const
172 {
173         if (params().info().hasParam("literal")
174             && params()["literal"] == "true")
175                 return;
176
177         ParamInfo::const_iterator it = params().info().begin();
178         ParamInfo::const_iterator end = params().info().end();
179         for (; it != end; ++it) {
180                 if (it->handling() == ParamInfo::HANDLING_LATEXIFY) {
181                         docstring const text = params()[it->name()];
182                         // Validate the contents (if we LaTeXify, specific
183                         // macros might require packages)
184                         for (pos_type i = 0; i < int(text.size()) ; ++i)
185                                 BufferEncodings::validate(text[i], features);
186                 }
187         }
188 }
189
190
191 void InsetCommand::changeCmdName(string const & new_name)
192 {
193         string const old_name = getCmdName();
194         if (old_name == new_name)
195                 return;
196
197         if (buffer().masterParams().track_changes) {
198                 // With change tracking, we insert a new inset and
199                 // delete the old one
200                 InsetCommandParams p(p_.code());
201                 p = p_;
202                 p.setCmdName(new_name);
203                 string const data = InsetCommand::params2string(p);
204                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
205                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
206         } else
207                 p_.setCmdName(new_name);
208 }
209
210
211 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
212 {
213         switch (cmd.action()) {
214         case LFUN_INSET_MODIFY: {
215                 if (cmd.getArg(0) == "changetype") {
216                         cur.recordUndo();
217                         changeCmdName(cmd.getArg(1));
218                         cur.forceBufferUpdate();
219                         initView();
220                         break;
221                 }
222                 InsetCommandParams p(p_.code());
223                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
224                 if (p.getCmdName().empty())
225                         cur.noScreenUpdate();
226                 else {
227                         cur.recordUndo();
228                         if (buffer().masterParams().track_changes) {
229                                 // With change tracking, we insert a new inset and
230                                 // delete the old one
231                                 string const data = InsetCommand::params2string(p);
232                                 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
233                                 lyx::dispatch(FuncRequest(LFUN_CHAR_DELETE_FORWARD));
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