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