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