]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
15c990c8801fb60a613b8ad73e24c67bad0868f6
[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 InsetCommand::~InsetCommand()
63 {
64         if (!mailer_name_.empty())
65                 hideDialogs(mailer_name_, this);
66         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
67         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
68         for (; it != end; ++it)
69                 if (it->second)
70                         it->first->clearLastInset(this);
71 }
72
73
74 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
75 {
76         button_.update(screenLabel(), editable() || hasSettings());
77         button_.metrics(mi, dim);
78 }
79
80
81 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
82 {
83         mouse_hover_[bv] = mouse_hover;
84         return true;
85 }
86
87
88 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
89 {
90         button_.setRenderState(mouse_hover_[pi.base.bv]);
91         button_.draw(pi, x, y);
92 }
93
94
95 void InsetCommand::setParam(std::string const & name, docstring const & value)
96 {
97         p_[name] = value;
98 }
99
100
101 docstring const & InsetCommand::getParam(std::string const & name) const
102 {
103         return p_[name];
104 }
105
106
107 void InsetCommand::setParams(InsetCommandParams const & p)
108 {
109         p_ = p;
110         initView();
111 }
112
113
114 int InsetCommand::latex(odocstream & os, OutputParams const & runparams_in) const
115 {
116         OutputParams runparams = runparams_in;
117         os << getCommand(runparams);
118         return 0;
119 }
120
121
122 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
123 {
124         docstring const str = "[" + buffer().B_("LaTeX Command: ")
125                 + from_utf8(getCmdName()) + "]";
126         os << str;
127         return str.size();
128 }
129
130
131 int InsetCommand::docbook(odocstream &, OutputParams const &) const
132 {
133         return 0;
134 }
135
136
137 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
138 {
139         switch (cmd.action()) {
140         case LFUN_INSET_MODIFY: {
141                 if (cmd.getArg(0) == "changetype") {
142                         p_.setCmdName(cmd.getArg(1));
143                         initView();
144                         break;
145                 }
146                 InsetCommandParams p(p_.code());
147                 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
148                 if (p.getCmdName().empty())
149                         cur.noUpdate();
150                 else
151                         setParams(p);
152                 break;
153         }
154
155         case LFUN_INSET_DIALOG_UPDATE: {
156                 string const name = to_utf8(cmd.argument());
157                 cur.bv().updateDialog(name, params2string(name, params()));
158                 break;
159         }
160
161         default:
162                 Inset::doDispatch(cur, cmd);
163                 break;
164         }
165
166 }
167
168
169 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
170         FuncStatus & status) const
171 {
172         switch (cmd.action()) {
173         // suppress these
174         case LFUN_ERT_INSERT:
175                 status.setEnabled(false);
176                 return true;
177         
178         // we handle these
179         case LFUN_INSET_MODIFY:
180                 if (cmd.getArg(0) == "changetype") {
181                         string const newtype = cmd.getArg(1);
182                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
183                         status.setOnOff(newtype == p_.getCmdName());
184                 } 
185                 status.setEnabled(true);
186                 return true;
187         
188         case LFUN_INSET_DIALOG_UPDATE:
189                 status.setEnabled(true);
190                 return true;
191         
192         default:
193                 return Inset::getStatus(cur, cmd, status);
194         }
195 }
196
197
198 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
199 {
200         return from_ascii("context-") + from_ascii(mailer_name_);
201 }
202
203
204 bool InsetCommand::showInsetDialog(BufferView * bv) const
205 {
206         if (!mailer_name_.empty())
207                 bv->showDialog(mailer_name_, params2string(mailer_name_, p_),
208                         const_cast<InsetCommand *>(this));
209         return true;
210 }
211
212
213 // FIXME This could take an InsetCode instead of a string
214 bool InsetCommand::string2params(string const & name, string const & in,
215         InsetCommandParams & params)
216 {
217         params.clear();
218         if (in.empty())
219                 return false;
220         istringstream data(in);
221         Lexer lex;
222         lex.setStream(data);
223         lex.setContext("InsetCommand::string2params");
224         lex >> name.c_str(); // check for name
225         lex >> "CommandInset";
226         params.read(lex);
227         return true;
228 }
229
230
231 // FIXME This could take an InsetCode instead of a string
232 string InsetCommand::params2string(string const & name,
233                                   InsetCommandParams const & params)
234 {
235         ostringstream data;
236         data << name << ' ';
237         params.write(data);
238         data << "\\end_inset\n";
239         return data.str();
240 }
241
242
243 bool decodeInsetParam(string const & name, string & data,
244         Buffer const & buffer)
245 {
246         InsetCode const code = insetCode(name);
247         switch (code) {
248         case BIBITEM_CODE:
249         case BIBTEX_CODE:
250         case INDEX_CODE:
251         case LABEL_CODE:
252         case NOMENCL_CODE:
253         case NOMENCL_PRINT_CODE:
254         case REF_CODE:
255         case TOC_CODE:
256         case HYPERLINK_CODE: {
257                 InsetCommandParams p(code);
258                 data = InsetCommand::params2string(name, p);
259                 break;
260         }
261         case INCLUDE_CODE: {
262                 // data is the include type: one of "include",
263                 // "input", "verbatiminput" or "verbatiminput*"
264                 if (data.empty())
265                         // default type is requested
266                         data = "include";
267                 InsetCommandParams p(INCLUDE_CODE, data);
268                 data = InsetCommand::params2string("include", p);
269                 break;
270         }
271         case BOX_CODE: {
272                 // \c data == "Boxed" || "Frameless" etc
273                 InsetBoxParams p(data);
274                 data = InsetBox::params2string(p);
275                 break;
276         }
277         case BRANCH_CODE: {
278                 InsetBranchParams p;
279                 data = InsetBranch::params2string(p);
280                 break;
281         }
282         case CITE_CODE: {
283                 InsetCommandParams p(CITE_CODE);
284                 data = InsetCommand::params2string(name, p);
285                 break;
286         }
287         case ERT_CODE: {
288                 data = InsetERT::params2string(InsetCollapsable::Open);
289                 break;
290         }
291         case EXTERNAL_CODE: {
292                 InsetExternalParams p;
293                 data = InsetExternal::params2string(p, buffer);
294                 break;
295         }
296         case FLOAT_CODE:  {
297                 InsetFloatParams p;
298                 data = InsetFloat::params2string(p);
299                 break;
300         }
301         case LISTINGS_CODE: {
302                 InsetListingsParams p;
303                 data = InsetListings::params2string(p);
304                 break;
305         }
306         case GRAPHICS_CODE: {
307                 InsetGraphicsParams p;
308                 data = InsetGraphics::params2string(p, buffer);
309                 break;
310         }
311         case NOTE_CODE: {
312                 InsetNoteParams p;
313                 data = InsetNote::params2string(p);
314                 break;
315         }
316         case PHANTOM_CODE: {
317                 InsetPhantomParams p;
318                 data = InsetPhantom::params2string(p);
319                 break;
320         }
321         case SPACE_CODE: {
322                 InsetSpaceParams p;
323                 data = InsetSpace::params2string(p);
324                 break;
325         }
326         case VSPACE_CODE: {
327                 VSpace space;
328                 data = InsetVSpace::params2string(space);
329                 break;
330         }
331         case WRAP_CODE: {
332                 InsetWrapParams p;
333                 data = InsetWrap::params2string(p);
334                 break;
335         }
336         default:
337                 return false;
338         } // end switch(code)
339         return true;
340 }
341
342 } // namespace lyx