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