]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
* InsetListings.cpp:
[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/InsetListings.h"
33 #include "insets/InsetNote.h"
34 #include "insets/InsetPhantom.h"
35 #include "insets/InsetSpace.h"
36 #include "insets/InsetTabular.h"
37 #include "insets/InsetVSpace.h"
38 #include "insets/InsetWrap.h"
39
40 #include "support/debug.h"
41 #include "support/gettext.h"
42
43 #include "frontends/Application.h"
44
45 #include <sstream>
46
47 using namespace std;
48
49
50 namespace lyx {
51
52 // FIXME Would it now be possible to use the InsetCode in 
53 // place of the mailer name and recover that information?
54 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p,
55                            string const & mailer_name)
56         : Inset(buf), p_(p),
57           mailer_name_(mailer_name),
58           mouse_hover_()
59 {}
60
61
62 // The sole purpose of this copy constructor is to make sure
63 // that the mouse_hover_ map is not copied and remains empty.
64 InsetCommand::InsetCommand(InsetCommand const & rhs)
65         : Inset(rhs), p_(rhs.p_), mailer_name_(rhs.mailer_name_)
66 {}
67
68
69 InsetCommand::~InsetCommand()
70 {
71         if (!mailer_name_.empty())
72                 hideDialogs(mailer_name_, this);
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 {
90         mouse_hover_[bv] = mouse_hover;
91         return true;
92 }
93
94
95 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
96 {
97         button_.setRenderState(mouse_hover_[pi.base.bv]);
98         button_.draw(pi, x, y);
99 }
100
101
102 void InsetCommand::setParam(std::string const & name, docstring const & value)
103 {
104         p_[name] = value;
105 }
106
107
108 docstring const & InsetCommand::getParam(std::string const & name) const
109 {
110         return p_[name];
111 }
112
113
114 void InsetCommand::setParams(InsetCommandParams const & p)
115 {
116         p_ = p;
117         initView();
118 }
119
120
121 int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const
122 {
123         OutputParams runparams = runparams_in;
124         os << getCommand(runparams);
125         return 0;
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                         p_.setCmdName(cmd.getArg(1));
150                         initView();
151                         break;
152                 }
153                 InsetCommandParams p(p_.code());
154                 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
155                 if (p.getCmdName().empty())
156                         cur.noUpdate();
157                 else
158                         setParams(p);
159                 break;
160         }
161
162         case LFUN_INSET_DIALOG_UPDATE: {
163                 string const name = to_utf8(cmd.argument());
164                 cur.bv().updateDialog(name, params2string(name, params()));
165                 break;
166         }
167
168         default:
169                 Inset::doDispatch(cur, cmd);
170                 break;
171         }
172
173 }
174
175
176 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
177         FuncStatus & status) const
178 {
179         switch (cmd.action()) {
180         // suppress these
181         case LFUN_ERT_INSERT:
182                 status.setEnabled(false);
183                 return true;
184         
185         // we handle these
186         case LFUN_INSET_MODIFY:
187                 if (cmd.getArg(0) == "changetype") {
188                         string const newtype = cmd.getArg(1);
189                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
190                         status.setOnOff(newtype == p_.getCmdName());
191                 } 
192                 status.setEnabled(true);
193                 return true;
194         
195         case LFUN_INSET_DIALOG_UPDATE:
196                 status.setEnabled(true);
197                 return true;
198         
199         default:
200                 return Inset::getStatus(cur, cmd, status);
201         }
202 }
203
204
205 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
206 {
207         return from_ascii("context-") + from_ascii(mailer_name_);
208 }
209
210
211 bool InsetCommand::showInsetDialog(BufferView * bv) const
212 {
213         if (!mailer_name_.empty())
214                 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
215                         const_cast<InsetCommand *>(this));
216         return true;
217 }
218
219
220 // FIXME This could take an InsetCode instead of a string
221 bool InsetCommand::string2params(string const & name, string const & in,
222         InsetCommandParams & params)
223 {
224         params.clear();
225         if (in.empty())
226                 return false;
227         istringstream data(in);
228         Lexer lex;
229         lex.setStream(data);
230         lex.setContext("InsetCommand::string2params");
231         lex >> name.c_str(); // check for name
232         lex >> "CommandInset";
233         params.read(lex);
234         return true;
235 }
236
237
238 // FIXME This could take an InsetCode instead of a string
239 string InsetCommand::params2string(string const & name,
240                                   InsetCommandParams const & params)
241 {
242         ostringstream data;
243         data << name << ' ';
244         params.write(data);
245         data << "\\end_inset\n";
246         return data.str();
247 }
248
249
250 bool decodeInsetParam(string const & name, string & data,
251         Buffer const & buffer)
252 {
253         InsetCode const code = insetCode(name);
254         switch (code) {
255         case BIBITEM_CODE:
256         case BIBTEX_CODE:
257         case INDEX_CODE:
258         case LABEL_CODE:
259         case NOMENCL_CODE:
260         case NOMENCL_PRINT_CODE:
261         case REF_CODE:
262         case TOC_CODE:
263         case HYPERLINK_CODE: {
264                 InsetCommandParams p(code);
265                 data = InsetCommand::params2string(name, p);
266                 break;
267         }
268         case INCLUDE_CODE: {
269                 // data is the include type: one of "include",
270                 // "input", "verbatiminput" or "verbatiminput*"
271                 if (data.empty())
272                         // default type is requested
273                         data = "include";
274                 InsetCommandParams p(INCLUDE_CODE, data);
275                 data = InsetCommand::params2string("include", p);
276                 break;
277         }
278         case BOX_CODE: {
279                 // \c data == "Boxed" || "Frameless" etc
280                 InsetBoxParams p(data);
281                 data = InsetBox::params2string(p);
282                 break;
283         }
284         case BRANCH_CODE: {
285                 InsetBranchParams p;
286                 data = InsetBranch::params2string(p);
287                 break;
288         }
289         case CITE_CODE: {
290                 InsetCommandParams p(CITE_CODE);
291                 data = InsetCommand::params2string(name, p);
292                 break;
293         }
294         case ERT_CODE: {
295                 data = InsetERT::params2string(InsetCollapsable::Open);
296                 break;
297         }
298         case EXTERNAL_CODE: {
299                 InsetExternalParams p;
300                 data = InsetExternal::params2string(p, buffer);
301                 break;
302         }
303         case FLOAT_CODE:  {
304                 InsetFloatParams p;
305                 data = InsetFloat::params2string(p);
306                 break;
307         }
308         case LISTINGS_CODE: {
309                 InsetListingsParams p;
310                 data = InsetListings::params2string(p);
311                 break;
312         }
313         case GRAPHICS_CODE: {
314                 InsetGraphicsParams p;
315                 data = InsetGraphics::params2string(p, buffer);
316                 break;
317         }
318         case NOTE_CODE: {
319                 InsetNoteParams p;
320                 data = InsetNote::params2string(p);
321                 break;
322         }
323         case PHANTOM_CODE: {
324                 InsetPhantomParams p;
325                 data = InsetPhantom::params2string(p);
326                 break;
327         }
328         case SPACE_CODE: {
329                 InsetSpaceParams p;
330                 data = InsetSpace::params2string(p);
331                 break;
332         }
333         case VSPACE_CODE: {
334                 VSpace space;
335                 data = InsetVSpace::params2string(space);
336                 break;
337         }
338         case WRAP_CODE: {
339                 InsetWrapParams p;
340                 data = InsetWrap::params2string(p);
341                 break;
342         }
343         default:
344                 return false;
345         } // end switch(code)
346         return true;
347 }
348
349 } // namespace lyx