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