]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Add comment.
[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/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         : Inset(buf), p_(p)
57 {}
58
59
60 // The sole purpose of this copy constructor is to make sure
61 // that the mouse_hover_ map is not copied and remains empty.
62 InsetCommand::InsetCommand(InsetCommand const & rhs)
63         : Inset(rhs), p_(rhs.p_)
64 {}
65
66
67 InsetCommand::~InsetCommand()
68 {
69         if (p_.code() != NO_CODE)
70                 hideDialogs(insetName(p_.code()), this);
71
72         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
73         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
74         for (; it != end; ++it)
75                 if (it->second)
76                         it->first->clearLastInset(this);
77 }
78
79
80 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
81 {
82         button_.update(screenLabel(), editable() || hasSettings());
83         button_.metrics(mi, dim);
84 }
85
86
87 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
88         const
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                         cur.forceBufferUpdate();
151                         initView();
152                         break;
153                 }
154                 InsetCommandParams p(p_.code());
155                 InsetCommand::string2params(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(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(insetName(p_.code()));
213 }
214
215
216 bool InsetCommand::showInsetDialog(BufferView * bv) const
217 {
218         if (p_.code() != NO_CODE)
219                 bv->showDialog(insetName(p_.code()), params2string(p_),
220                         const_cast<InsetCommand *>(this));
221         return true;
222 }
223
224
225 bool InsetCommand::string2params(string const & data,
226         InsetCommandParams & params)
227 {
228         params.clear();
229         if (data.empty())
230                 return false;
231         // This happens when inset-insert is called without argument except for the
232         // inset type; ex:
233         // "inset-insert toc"
234         string const name = insetName(params.code());
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 string InsetCommand::params2string(InsetCommandParams const & params)
249 {
250         ostringstream data;
251         data << insetName(params.code()) << ' ';
252         params.write(data);
253         data << "\\end_inset\n";
254         return data.str();
255 }
256
257
258 bool decodeInsetParam(string const & name, string & data,
259         Buffer const & buffer)
260 {
261         InsetCode const code = insetCode(name);
262         switch (code) {
263         case BIBITEM_CODE:
264         case BIBTEX_CODE:
265         case INDEX_CODE:
266         case LABEL_CODE:
267         case LINE_CODE:
268         case NOMENCL_CODE:
269         case NOMENCL_PRINT_CODE:
270         case REF_CODE:
271         case TOC_CODE:
272         case HYPERLINK_CODE: {
273                 InsetCommandParams p(code);
274                 data = InsetCommand::params2string(p);
275                 break;
276         }
277         case INCLUDE_CODE: {
278                 // data is the include type: one of "include",
279                 // "input", "verbatiminput" or "verbatiminput*"
280                 if (data.empty())
281                         // default type is requested
282                         data = "include";
283                 InsetCommandParams p(INCLUDE_CODE, data);
284                 data = InsetCommand::params2string(p);
285                 break;
286         }
287         case BOX_CODE: {
288                 // \c data == "Boxed" || "Frameless" etc
289                 InsetBoxParams p(data);
290                 data = InsetBox::params2string(p);
291                 break;
292         }
293         case BRANCH_CODE: {
294                 InsetBranchParams p;
295                 data = InsetBranch::params2string(p);
296                 break;
297         }
298         case CITE_CODE: {
299                 InsetCommandParams p(CITE_CODE);
300                 data = InsetCommand::params2string(p);
301                 break;
302         }
303         case ERT_CODE: {
304                 data = InsetERT::params2string(InsetCollapsable::Open);
305                 break;
306         }
307         case EXTERNAL_CODE: {
308                 InsetExternalParams p;
309                 data = InsetExternal::params2string(p, buffer);
310                 break;
311         }
312         case FLOAT_CODE:  {
313                 InsetFloatParams p;
314                 data = InsetFloat::params2string(p);
315                 break;
316         }
317         case LISTINGS_CODE: {
318                 InsetListingsParams p;
319                 data = InsetListings::params2string(p);
320                 break;
321         }
322         case GRAPHICS_CODE: {
323                 InsetGraphicsParams p;
324                 data = InsetGraphics::params2string(p, buffer);
325                 break;
326         }
327         case NOTE_CODE: {
328                 InsetNoteParams p;
329                 data = InsetNote::params2string(p);
330                 break;
331         }
332         case PHANTOM_CODE: {
333                 InsetPhantomParams p;
334                 data = InsetPhantom::params2string(p);
335                 break;
336         }
337         case SPACE_CODE: {
338                 InsetSpaceParams p;
339                 data = InsetSpace::params2string(p);
340                 break;
341         }
342         case VSPACE_CODE: {
343                 VSpace space;
344                 data = InsetVSpace::params2string(space);
345                 break;
346         }
347         case WRAP_CODE: {
348                 InsetWrapParams p;
349                 data = InsetWrap::params2string(p);
350                 break;
351         }
352         default:
353                 return false;
354         } // end switch(code)
355         return true;
356 }
357
358 } // namespace lyx