]> 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 #include "texstream.h"
25
26 #include "insets/InsetBox.h"
27 #include "insets/InsetBranch.h"
28 #include "insets/InsetCommand.h"
29 #include "insets/InsetERT.h"
30 #include "insets/InsetExternal.h"
31 #include "insets/InsetFloat.h"
32 #include "insets/InsetGraphics.h"
33 #include "insets/InsetIndex.h"
34 #include "insets/InsetLine.h"
35 #include "insets/InsetListings.h"
36 #include "insets/InsetNote.h"
37 #include "insets/InsetPhantom.h"
38 #include "insets/InsetSpace.h"
39 #include "insets/InsetTabular.h"
40 #include "insets/InsetVSpace.h"
41 #include "insets/InsetWrap.h"
42
43 #include "support/debug.h"
44 #include "support/gettext.h"
45
46 #include "frontends/Application.h"
47
48 #include <sstream>
49
50 using namespace std;
51
52
53 namespace lyx {
54
55 // FIXME Would it now be possible to use the InsetCode in
56 // place of the mailer name and recover that information?
57 InsetCommand::InsetCommand(Buffer * buf, InsetCommandParams const & p)
58         : Inset(buf), p_(p)
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_)
66 {}
67
68
69 InsetCommand & InsetCommand::operator=(InsetCommand const & rhs)
70 {
71         if (&rhs == this)
72                 return *this;
73
74         Inset::operator=(rhs);
75         p_ = rhs.p_;
76         mouse_hover_.clear();
77         button_ = RenderButton();
78
79         return *this;
80 }
81
82
83 InsetCommand::~InsetCommand()
84 {
85         if (p_.code() != NO_CODE)
86                 hideDialogs(insetName(p_.code()), this);
87
88         map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
89         map<BufferView const *, bool>::iterator end = mouse_hover_.end();
90         for (; it != end; ++it)
91                 if (it->second)
92                         it->first->clearLastInset(this);
93 }
94
95
96 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
97 {
98         button_.update(screenLabel(), editable() || clickable(*mi.base.bv, 0, 0));
99         button_.metrics(mi, dim);
100 }
101
102
103 bool InsetCommand::setMouseHover(BufferView const * bv, bool mouse_hover)
104         const
105 {
106         mouse_hover_[bv] = mouse_hover;
107         return true;
108 }
109
110
111 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
112 {
113         button_.setRenderState(mouse_hover_[pi.base.bv]);
114         button_.draw(pi, x, y);
115 }
116
117
118 void InsetCommand::setParam(string const & name, docstring const & value)
119 {
120         p_[name] = value;
121 }
122
123
124 docstring const & InsetCommand::getParam(string const & name) const
125 {
126         return p_[name];
127 }
128
129
130 void InsetCommand::setParams(InsetCommandParams const & p)
131 {
132         p_ = p;
133         initView();
134 }
135
136
137 void InsetCommand::latex(otexstream & os, OutputParams const & runparams_in) const
138 {
139         OutputParams runparams = runparams_in;
140         os << getCommand(runparams);
141 }
142
143
144 int InsetCommand::plaintext(odocstringstream & os,
145         OutputParams const &, size_t) const
146 {
147         docstring const str = "[" + buffer().B_("LaTeX Command: ")
148                 + from_utf8(getCmdName()) + "]";
149         os << str;
150         return str.size();
151 }
152
153
154 int InsetCommand::docbook(odocstream &, OutputParams const &) const
155 {
156         return 0;
157 }
158
159
160 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
161 {
162         switch (cmd.action()) {
163         case LFUN_INSET_MODIFY: {
164                 if (cmd.getArg(0) == "changetype") {
165                         cur.recordUndo();
166                         p_.setCmdName(cmd.getArg(1));
167                         cur.forceBufferUpdate();
168                         initView();
169                         break;
170                 }
171                 InsetCommandParams p(p_.code());
172                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
173                 if (p.getCmdName().empty())
174                         cur.noScreenUpdate();
175                 else {
176                         cur.recordUndo();
177                         setParams(p);
178                 }
179                 // FIXME We might also want to check here if this one is in the TOC.
180                 // But I think most of those are labeled.
181                 if (isLabeled())
182                         cur.forceBufferUpdate();
183                 break;
184         }
185
186         case LFUN_INSET_DIALOG_UPDATE: {
187                 string const name = to_utf8(cmd.argument());
188                 cur.bv().updateDialog(name, params2string(params()));
189                 break;
190         }
191
192         default:
193                 Inset::doDispatch(cur, cmd);
194                 break;
195         }
196
197 }
198
199
200 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
201         FuncStatus & status) const
202 {
203         switch (cmd.action()) {
204         // suppress these
205         case LFUN_ERT_INSERT:
206                 status.setEnabled(false);
207                 return true;
208
209         // we handle these
210         case LFUN_INSET_MODIFY:
211                 if (cmd.getArg(0) == "changetype") {
212                         string const newtype = cmd.getArg(1);
213                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
214                         status.setOnOff(newtype == p_.getCmdName());
215                 }
216                 status.setEnabled(true);
217                 return true;
218
219         case LFUN_INSET_DIALOG_UPDATE:
220                 status.setEnabled(true);
221                 return true;
222
223         default:
224                 return Inset::getStatus(cur, cmd, status);
225         }
226 }
227
228
229 string InsetCommand::contextMenuName() const
230 {
231         return "context-" + insetName(p_.code());
232 }
233
234
235 bool InsetCommand::showInsetDialog(BufferView * bv) const
236 {
237         if (p_.code() != NO_CODE)
238                 bv->showDialog(insetName(p_.code()), params2string(p_),
239                         const_cast<InsetCommand *>(this));
240         return true;
241 }
242
243
244 bool InsetCommand::string2params(string const & data,
245         InsetCommandParams & params)
246 {
247         params.clear();
248         if (data.empty())
249                 return false;
250         // This happens when inset-insert is called without argument except for the
251         // inset type; ex:
252         // "inset-insert toc"
253         string const name = insetName(params.code());
254         if (data == name)
255                 return true;
256         istringstream dstream(data);
257         Lexer lex;
258         lex.setStream(dstream);
259         lex.setContext("InsetCommand::string2params");
260         lex >> name.c_str(); // check for name
261         lex >> "CommandInset";
262         params.read(lex);
263         return true;
264 }
265
266
267 string InsetCommand::params2string(InsetCommandParams const & params)
268 {
269         ostringstream data;
270         data << insetName(params.code()) << ' ';
271         params.write(data);
272         data << "\\end_inset\n";
273         return data.str();
274 }
275
276
277 bool decodeInsetParam(string const & name, string & data,
278         Buffer const & buffer)
279 {
280         InsetCode const code = insetCode(name);
281         switch (code) {
282         case BIBITEM_CODE:
283         case BIBTEX_CODE:
284         case INDEX_PRINT_CODE:
285         case LABEL_CODE:
286         case LINE_CODE:
287         case NOMENCL_CODE:
288         case NOMENCL_PRINT_CODE:
289         case REF_CODE:
290         case TOC_CODE:
291         case HYPERLINK_CODE: {
292                 InsetCommandParams p(code);
293                 data = InsetCommand::params2string(p);
294                 break;
295         }
296         case INCLUDE_CODE: {
297                 // data is the include type: one of "include",
298                 // "input", "verbatiminput" or "verbatiminput*"
299                 if (data.empty())
300                         // default type is requested
301                         data = "include";
302                 InsetCommandParams p(INCLUDE_CODE, data);
303                 data = InsetCommand::params2string(p);
304                 break;
305         }
306         case BOX_CODE: {
307                 // \c data == "Boxed" || "Frameless" etc
308                 InsetBoxParams p(data);
309                 data = InsetBox::params2string(p);
310                 break;
311         }
312         case BRANCH_CODE: {
313                 InsetBranchParams p;
314                 data = InsetBranch::params2string(p);
315                 break;
316         }
317         case CITE_CODE: {
318                 InsetCommandParams p(CITE_CODE);
319                 data = InsetCommand::params2string(p);
320                 break;
321         }
322         case ERT_CODE: {
323                 data = InsetERT::params2string(InsetCollapsable::Open);
324                 break;
325         }
326         case EXTERNAL_CODE: {
327                 InsetExternalParams p;
328                 data = InsetExternal::params2string(p, buffer);
329                 break;
330         }
331         case FLOAT_CODE:  {
332                 InsetFloatParams p;
333                 data = InsetFloat::params2string(p);
334                 break;
335         }
336         case INDEX_CODE: {
337                 InsetIndexParams p;
338                 data = InsetIndex::params2string(p);
339                 break;
340         }
341         case LISTINGS_CODE: {
342                 InsetListingsParams p;
343                 data = InsetListings::params2string(p);
344                 break;
345         }
346         case GRAPHICS_CODE: {
347                 InsetGraphicsParams p;
348                 data = InsetGraphics::params2string(p, buffer);
349                 break;
350         }
351         case MATH_SPACE_CODE: {
352                 InsetSpaceParams p(true);
353                 data = InsetSpace::params2string(p);
354                 break;
355         }
356         case NOTE_CODE: {
357                 InsetNoteParams p;
358                 data = InsetNote::params2string(p);
359                 break;
360         }
361         case PHANTOM_CODE: {
362                 InsetPhantomParams p;
363                 data = InsetPhantom::params2string(p);
364                 break;
365         }
366         case SPACE_CODE: {
367                 InsetSpaceParams p;
368                 data = InsetSpace::params2string(p);
369                 break;
370         }
371         case VSPACE_CODE: {
372                 VSpace space;
373                 data = InsetVSpace::params2string(space);
374                 break;
375         }
376         case WRAP_CODE: {
377                 InsetWrapParams p;
378                 data = InsetWrap::params2string(p);
379                 break;
380         }
381         default:
382                 return false;
383         } // end switch(code)
384         return true;
385 }
386
387 } // namespace lyx