]> git.lyx.org Git - features.git/blob - src/insets/InsetCommand.cpp
Sanitize a bit the way InsetRef and InsetCommand button are drawn.
[features.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_REFRESH:
103                 break;
104
105         case LFUN_INSET_MODIFY: {
106                 InsetCommandParams p(p_.code());
107                 InsetCommandMailer::string2params(mailer_name_, to_utf8(cmd.argument()), p);
108                 if (p.getCmdName().empty())
109                         cur.noUpdate();
110                 else
111                         setParams(p);
112                 break;
113         }
114
115         case LFUN_INSET_DIALOG_UPDATE: {
116                 string const name = to_utf8(cmd.argument());
117                 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
118                 break;
119         }
120
121         case LFUN_MOUSE_RELEASE: {
122                 if (!cur.selection())
123                         edit(cur, true);
124                 break;
125         }
126
127         default:
128                 Inset::doDispatch(cur, cmd);
129                 break;
130         }
131
132 }
133
134
135 bool InsetCommand::getStatus(Cursor & cur, FuncRequest const & cmd,
136         FuncStatus & status) const
137 {
138         switch (cmd.action) {
139         // suppress these
140         case LFUN_ERT_INSERT:
141                 status.enabled(false);
142                 return true;
143         // we handle these
144         case LFUN_INSET_REFRESH:
145         case LFUN_INSET_MODIFY:
146         case LFUN_INSET_DIALOG_UPDATE:
147                 status.enabled(true);
148                 return true;
149         default:
150                 return Inset::getStatus(cur, cmd, status);
151         }
152 }
153
154
155 void InsetCommand::edit(Cursor & cur, bool, EntryDirection)
156 {
157         if (!mailer_name_.empty())
158                 InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
159 }
160
161
162 InsetCommandMailer::InsetCommandMailer(
163         string const & name, InsetCommand & inset)
164         : name_(name), inset_(inset)
165 {}
166
167
168 string const InsetCommandMailer::inset2string(Buffer const &) const
169 {
170         return params2string(name(), inset_.params());
171 }
172
173
174 //FIXME This could take an InsetCode instead of a string
175 bool InsetCommandMailer::string2params(
176         string const & name, string const & in, InsetCommandParams & params)
177 {
178         params.clear();
179         if (in.empty())
180                 return false;
181
182         istringstream data(in);
183         Lexer lex(0,0);
184         lex.setStream(data);
185
186         string n;
187         lex >> n;
188         if (!lex || n != name) {
189                 print_mailer_error("InsetCommandMailer", in, 1, name);
190                 return false;
191         }
192
193         // This is part of the inset proper that is usually swallowed
194         // by Text::readInset
195         string id;
196         lex >> id;
197         if (!lex || id != "CommandInset") {
198                 print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
199                 return false;
200         }
201
202         params.read(lex);
203         return true;
204 }
205
206
207 //FIXME This could take an InsetCode instead of a string
208 string const
209 InsetCommandMailer::params2string(string const & name,
210                                   InsetCommandParams const & params)
211 {
212         ostringstream data;
213         data << name << ' ';
214         params.write(data);
215         data << "\\end_inset\n";
216         return data.str();
217 }
218
219
220 } // namespace lyx