]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCommand.cpp
This optional argument to the InsetCollapsable constructor
[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 "support/debug.h"
25 #include "support/gettext.h"
26
27 #include "frontends/Application.h"
28
29 #include <sstream>
30
31 using namespace std;
32
33
34 namespace lyx {
35
36 // FIXME Would it now be possible to use the InsetCode in 
37 // place of the mailer name and recover that information?
38 InsetCommand::InsetCommand(InsetCommandParams const & p,
39                            string const & mailer_name)
40         : p_(p),
41           mailer_name_(mailer_name),
42           mouse_hover_(false)
43 {}
44
45
46 InsetCommand::~InsetCommand()
47 {
48         if (!mailer_name_.empty())
49                 hideDialogs(mailer_name_, this);
50 }
51
52
53 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
54 {
55         button_.update(screenLabel(), editable() != NOT_EDITABLE);
56         button_.metrics(mi, dim);
57 }
58
59
60 bool InsetCommand::setMouseHover(bool mouse_hover)
61 {
62         mouse_hover_ = mouse_hover;
63         return true;
64 }
65
66
67 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
68 {
69         button_.setRenderState(mouse_hover_);
70         button_.draw(pi, x, y);
71 }
72
73
74 void InsetCommand::setParam(std::string const & name, docstring const & value)
75 {
76         p_[name] = value;
77 }
78
79
80 docstring const & InsetCommand::getParam(std::string const & name) const
81 {
82         return p_[name];
83 }
84
85
86 void InsetCommand::setParams(InsetCommandParams const & p)
87 {
88         p_ = p;
89         initView();
90 }
91
92
93 int InsetCommand::latex(odocstream & os, OutputParams const &) const
94 {
95         os << getCommand();
96         return 0;
97 }
98
99
100 int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
101 {
102         docstring const str = "[" + buffer().B_("LaTeX Command: ")
103                 + from_utf8(getCmdName()) + "]";
104         os << str;
105         return str.size();
106 }
107
108
109 int InsetCommand::docbook(odocstream &, OutputParams const &) const
110 {
111         return 0;
112 }
113
114
115 void InsetCommand::doDispatch(Cursor & cur, FuncRequest & cmd)
116 {
117         switch (cmd.action) {
118         case LFUN_INSET_MODIFY: {
119                 if (cmd.getArg(0) == "changetype") {
120                         p_.setCmdName(cmd.getArg(1));
121                         initView();
122                         break;
123                 }
124                 InsetCommandParams p(p_.code());
125                 InsetCommand::string2params(mailer_name_, to_utf8(cmd.argument()), p);
126                 if (p.getCmdName().empty())
127                         cur.noUpdate();
128                 else
129                         setParams(p);
130                 break;
131         }
132
133         case LFUN_INSET_DIALOG_UPDATE: {
134                 string const name = to_utf8(cmd.argument());
135                 cur.bv().updateDialog(name, params2string(name, params()));
136                 break;
137         }
138
139         case LFUN_MOUSE_RELEASE: {
140                 if (!cur.selection() && cmd.button() != mouse_button::button3)
141                         edit(cur, true);
142                 break;
143         }
144
145         default:
146                 Inset::doDispatch(cur, cmd);
147                 break;
148         }
149
150 }
151
152
153 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
154         FuncStatus & status) const
155 {
156         switch (cmd.action) {
157         // suppress these
158         case LFUN_ERT_INSERT:
159                 status.setEnabled(false);
160                 return true;
161         // we handle these
162         case LFUN_INSET_MODIFY:
163                 if (cmd.getArg(0) == "changetype") {
164                         string const newtype = cmd.getArg(1);
165                         status.setEnabled(p_.isCompatibleCommand(p_.code(), newtype));
166                         status.setOnOff(newtype == p_.getCmdName());
167                 } 
168                 status.setEnabled(true);
169                 return true;
170         case LFUN_INSET_DIALOG_UPDATE:
171                 status.setEnabled(true);
172                 return true;
173         default:
174                 return Inset::getStatus(cur, cmd, status);
175         }
176 }
177
178
179 docstring InsetCommand::contextMenu(BufferView const &, int, int) const
180 {
181         return from_ascii("context-") + from_ascii(mailer_name_);
182 }
183
184
185 void InsetCommand::edit(Cursor & cur, bool, EntryDirection)
186 {
187         if (!mailer_name_.empty())
188                 cur.bv().showDialog(mailer_name_, params2string(mailer_name_, p_), this);
189 }
190
191
192 // FIXME This could take an InsetCode instead of a string
193 bool InsetCommand::string2params(string const & name, string const & in,
194         InsetCommandParams & params)
195 {
196         params.clear();
197         if (in.empty())
198                 return false;
199         istringstream data(in);
200         Lexer lex;
201         lex.setStream(data);
202         lex.setContext("InsetCommand::string2params");
203         lex >> name.c_str(); // check for name
204         lex >> "CommandInset";
205         params.read(lex);
206         return true;
207 }
208
209
210 // FIXME This could take an InsetCode instead of a string
211 string InsetCommand::params2string(string const & name,
212                                   InsetCommandParams const & params)
213 {
214         ostringstream data;
215         data << name << ' ';
216         params.write(data);
217         data << "\\end_inset\n";
218         return data.str();
219 }
220
221
222 } // namespace lyx