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