]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Fix bug 2744:
[lyx.git] / src / insets / insetcommand.C
1 /**
2  * \file insetcommand.C
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 "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "FuncStatus.h"
20 #include "lyxlex.h"
21 #include "metricsinfo.h"
22
23 #include <sstream>
24
25
26 using std::string;
27 using std::istringstream;
28 using std::ostream;
29 using std::ostringstream;
30
31
32 InsetCommand::InsetCommand(InsetCommandParams const & p,
33                            string const & mailer_name)
34         : p_(p.getCmdName(), p.getContents(), p.getOptions(), p.getSecOptions()),
35           mailer_name_(mailer_name),
36           updateButtonLabel_(true)
37 {}
38
39
40 InsetCommand::~InsetCommand()
41 {
42         if (!mailer_name_.empty())
43                 InsetCommandMailer(mailer_name_, *this).hideDialog();
44 }
45
46
47 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
48 {
49         if (updateButtonLabel_) {
50                 updateButtonLabel_ = false;
51                 button_.update(getScreenLabel(*mi.base.bv->buffer()),
52                                editable() != NOT_EDITABLE);
53         }
54         button_.metrics(mi, dim);
55         dim_ = dim;
56 }
57
58
59 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
60 {
61         setPosCache(pi, x, y);
62         button_.draw(pi, x, y);
63 }
64
65
66 void InsetCommand::setParams(InsetCommandParams const & p)
67 {
68         p_ = p;
69         updateButtonLabel_ = true;
70 }
71
72
73 int InsetCommand::latex(Buffer const &, ostream & os,
74                         OutputParams const &) const
75 {
76         os << getCommand();
77         return 0;
78 }
79
80
81 int InsetCommand::plaintext(Buffer const &, ostream &,
82                         OutputParams const &) const
83 {
84         return 0;
85 }
86
87
88 int InsetCommand::docbook(Buffer const &, ostream &,
89                           OutputParams const &) const
90 {
91         return 0;
92 }
93
94
95 void InsetCommand::doDispatch(LCursor & cur, FuncRequest & cmd)
96 {
97         switch (cmd.action) {
98         case LFUN_INSET_REFRESH:
99                 updateButtonLabel_ = true;
100                 break;
101
102         case LFUN_INSET_MODIFY: {
103                 InsetCommandParams p;
104                 InsetCommandMailer::string2params(mailer_name_, lyx::to_utf8(cmd.argument()), p);
105                 if (p.getCmdName().empty())
106                         cur.noUpdate();
107                 else
108                         setParams(p);
109                 break;
110         }
111
112         case LFUN_INSET_DIALOG_UPDATE: {
113                 string const name = lyx::to_utf8(cmd.argument());
114                 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
115                 break;
116         }
117
118         case LFUN_MOUSE_RELEASE: {
119                 if (!mailer_name_.empty())
120                         InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
121                 break;
122         }
123
124         default:
125                 InsetBase::doDispatch(cur, cmd);
126                 break;
127         }
128
129 }
130
131
132 bool InsetCommand::getStatus(LCursor & cur, FuncRequest const & cmd,
133         FuncStatus & status) const
134 {
135         switch (cmd.action) {
136         // suppress these
137         case LFUN_ERT_INSERT:
138                 status.enabled(false);
139                 return true;
140         // we handle these
141         case LFUN_INSET_REFRESH:
142         case LFUN_INSET_MODIFY:
143         case LFUN_INSET_DIALOG_UPDATE:
144                 status.enabled(true);
145                 return true;
146         default:
147                 return InsetBase::getStatus(cur, cmd, status);
148         }
149 }
150
151
152 void InsetCommand::replaceContents(std::string const & from, string const & to)
153 {
154         if (getContents() == from)
155                 setContents(to);
156 }
157
158
159 InsetCommandMailer::InsetCommandMailer(string const & name,
160                                        InsetCommand & inset)
161         : name_(name), inset_(inset)
162 {}
163
164
165 string const InsetCommandMailer::inset2string(Buffer const &) const
166 {
167         return params2string(name(), inset_.params());
168 }
169
170
171 void InsetCommandMailer::string2params(string const & name,
172                                        string const & in,
173                                        InsetCommandParams & params)
174 {
175         params = InsetCommandParams();
176         if (in.empty())
177                 return;
178
179         istringstream data(in);
180         LyXLex lex(0,0);
181         lex.setStream(data);
182
183         string n;
184         lex >> n;
185         if (!lex || n != name)
186                 return print_mailer_error("InsetCommandMailer", in, 1, name);
187
188         // This is part of the inset proper that is usually swallowed
189         // by LyXText::readInset
190         string id;
191         lex >> id;
192         if (!lex || id != "LatexCommand")
193                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
194
195         params.read(lex);
196 }
197
198
199 string const
200 InsetCommandMailer::params2string(string const & name,
201                                   InsetCommandParams const & params)
202 {
203         ostringstream data;
204         data << name << ' ';
205         params.write(data);
206         data << "\\end_inset\n";
207         return data.str();
208 }