]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
Whitespace
[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() || clickable(0, 0));
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(odocstringstream & os,
130         OutputParams const &, size_t) 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                         cur.recordUndo();
151                         p_.setCmdName(cmd.getArg(1));
152                         cur.forceBufferUpdate();
153                         initView();
154                         break;
155                 }
156                 InsetCommandParams p(p_.code());
157                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
158                 if (p.getCmdName().empty())
159                         cur.noScreenUpdate();
160                 else {
161                         cur.recordUndo();
162                         setParams(p);
163                 }
164                 // FIXME We might also want to check here if this one is in the TOC.
165                 // But I think most of those are labeled.
166                 if (isLabeled())
167                         cur.forceBufferUpdate();
168                 break;
169         }
170
171         case LFUN_INSET_DIALOG_UPDATE: {
172                 string const name = to_utf8(cmd.argument());
173                 cur.bv().updateDialog(name, params2string(params()));
174                 break;
175         }
176
177         default:
178                 Inset::doDispatch(cur, cmd);
179                 break;
180         }
181
182 }
183
184
185 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
186         FuncStatus & status) const
187 {
188         switch (cmd.action()) {
189         // suppress these
190         case LFUN_ERT_INSERT:
191                 status.setEnabled(false);
192                 return true;
193         
194         // we handle these
195         case LFUN_INSET_MODIFY:
196                 if (cmd.getArg(0) == "changetype") {
197                         string const newtype = cmd.getArg(1);
198                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
199                         status.setOnOff(newtype == p_.getCmdName());
200                 }
201                 status.setEnabled(true);
202                 return true;
203         
204         case LFUN_INSET_DIALOG_UPDATE:
205                 status.setEnabled(true);
206                 return true;
207         
208         default:
209                 return Inset::getStatus(cur, cmd, status);
210         }
211 }
212
213
214 string InsetCommand::contextMenuName() const
215 {
216         return "context-" + insetName(p_.code());
217 }
218
219
220 bool InsetCommand::showInsetDialog(BufferView * bv) const
221 {
222         if (p_.code() != NO_CODE)
223                 bv->showDialog(insetName(p_.code()), params2string(p_),
224                         const_cast<InsetCommand *>(this));
225         return true;
226 }
227
228
229 bool InsetCommand::string2params(string const & data,
230         InsetCommandParams & params)
231 {
232         params.clear();
233         if (data.empty())
234                 return false;
235         // This happens when inset-insert is called without argument except for the
236         // inset type; ex:
237         // "inset-insert toc"
238         string const name = insetName(params.code());
239         if (data == name)
240                 return true;
241         istringstream dstream(data);
242         Lexer lex;
243         lex.setStream(dstream);
244         lex.setContext("InsetCommand::string2params");
245         lex >> name.c_str(); // check for name
246         lex >> "CommandInset";
247         params.read(lex);
248         return true;
249 }
250
251
252 string InsetCommand::params2string(InsetCommandParams const & params)
253 {
254         ostringstream data;
255         data << insetName(params.code()) << ' ';
256         params.write(data);
257         data << "\\end_inset\n";
258         return data.str();
259 }
260
261
262 bool decodeInsetParam(string const & name, string & data,
263         Buffer const & buffer)
264 {
265         InsetCode const code = insetCode(name);
266         switch (code) {
267         case BIBITEM_CODE:
268         case BIBTEX_CODE:
269         case INDEX_PRINT_CODE:
270         case LABEL_CODE:
271         case LINE_CODE:
272         case NOMENCL_CODE:
273         case NOMENCL_PRINT_CODE:
274         case REF_CODE:
275         case TOC_CODE:
276         case HYPERLINK_CODE: {
277                 InsetCommandParams p(code);
278                 data = InsetCommand::params2string(p);
279                 break;
280         }
281         case INCLUDE_CODE: {
282                 // data is the include type: one of "include",
283                 // "input", "verbatiminput" or "verbatiminput*"
284                 if (data.empty())
285                         // default type is requested
286                         data = "include";
287                 InsetCommandParams p(INCLUDE_CODE, data);
288                 data = InsetCommand::params2string(p);
289                 break;
290         }
291         case BOX_CODE: {
292                 // \c data == "Boxed" || "Frameless" etc
293                 InsetBoxParams p(data);
294                 data = InsetBox::params2string(p);
295                 break;
296         }
297         case BRANCH_CODE: {
298                 InsetBranchParams p;
299                 data = InsetBranch::params2string(p);
300                 break;
301         }
302         case CITE_CODE: {
303                 InsetCommandParams p(CITE_CODE);
304                 data = InsetCommand::params2string(p);
305                 break;
306         }
307         case ERT_CODE: {
308                 data = InsetERT::params2string(InsetCollapsable::Open);
309                 break;
310         }
311         case EXTERNAL_CODE: {
312                 InsetExternalParams p;
313                 data = InsetExternal::params2string(p, buffer);
314                 break;
315         }
316         case FLOAT_CODE:  {
317                 InsetFloatParams p;
318                 data = InsetFloat::params2string(p);
319                 break;
320         }
321         case INDEX_CODE: {
322                 InsetIndexParams p;
323                 data = InsetIndex::params2string(p);
324                 break;
325         }
326         case LISTINGS_CODE: {
327                 InsetListingsParams p;
328                 data = InsetListings::params2string(p);
329                 break;
330         }
331         case GRAPHICS_CODE: {
332                 InsetGraphicsParams p;
333                 data = InsetGraphics::params2string(p, buffer);
334                 break;
335         }
336         case MATH_SPACE_CODE: {
337             InsetSpaceParams p(true);
338                 data = InsetSpace::params2string(p);
339                 break;
340         }
341         case NOTE_CODE: {
342                 InsetNoteParams p;
343                 data = InsetNote::params2string(p);
344                 break;
345         }
346         case PHANTOM_CODE: {
347                 InsetPhantomParams p;
348                 data = InsetPhantom::params2string(p);
349                 break;
350         }
351         case SPACE_CODE: {
352                 InsetSpaceParams p;
353                 data = InsetSpace::params2string(p);
354                 break;
355         }
356         case VSPACE_CODE: {
357                 VSpace space;
358                 data = InsetVSpace::params2string(space);
359                 break;
360         }
361         case WRAP_CODE: {
362                 InsetWrapParams p;
363                 data = InsetWrap::params2string(p);
364                 break;
365         }
366         default:
367                 return false;
368         } // end switch(code)
369         return true;
370 }
371
372 } // namespace lyx