]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Output docbook as utf8. Probably quite a bit more work needed, but then help form...
[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 using lyx::odocstream;
26
27 using lyx::odocstream;
28
29 using std::string;
30 using std::istringstream;
31 using std::ostream;
32 using std::ostringstream;
33
34
35 InsetCommand::InsetCommand(InsetCommandParams const & p,
36                            string const & mailer_name)
37         : p_(p),
38           mailer_name_(mailer_name),
39           updateButtonLabel_(true)
40 {}
41
42
43 InsetCommand::~InsetCommand()
44 {
45         if (!mailer_name_.empty())
46                 InsetCommandMailer(mailer_name_, *this).hideDialog();
47 }
48
49
50 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
51 {
52         if (updateButtonLabel_) {
53                 updateButtonLabel_ = false;
54                 button_.update(getScreenLabel(*mi.base.bv->buffer()),
55                                editable() != NOT_EDITABLE);
56         }
57         button_.metrics(mi, dim);
58         dim_ = dim;
59 }
60
61
62 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
63 {
64         setPosCache(pi, x, y);
65         button_.draw(pi, x, y);
66 }
67
68
69 void InsetCommand::setParams(InsetCommandParams const & p)
70 {
71         p_ = p;
72         updateButtonLabel_ = true;
73 }
74
75
76 int InsetCommand::latex(Buffer const &, odocstream & os,
77                         OutputParams const &) const
78 {
79         os << getCommand();
80         return 0;
81 }
82
83
84 int InsetCommand::plaintext(Buffer const &, odocstream &,
85                         OutputParams const &) const
86 {
87         return 0;
88 }
89
90
91 int InsetCommand::docbook(Buffer const &, odocstream &,
92                           OutputParams const &) const
93 {
94         return 0;
95 }
96
97
98 void InsetCommand::doDispatch(LCursor & cur, FuncRequest & cmd)
99 {
100         switch (cmd.action) {
101         case LFUN_INSET_REFRESH:
102                 updateButtonLabel_ = true;
103                 break;
104
105         case LFUN_INSET_MODIFY: {
106                 InsetCommandParams p(p_.getCmdName());
107                 InsetCommandMailer::string2params(mailer_name_, lyx::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 = lyx::to_utf8(cmd.argument());
117                 InsetCommandMailer(name, *this).updateDialog(&cur.bv());
118                 break;
119         }
120
121         case LFUN_MOUSE_RELEASE: {
122                 if (!mailer_name_.empty())
123                         InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
124                 break;
125         }
126
127         default:
128                 InsetBase::doDispatch(cur, cmd);
129                 break;
130         }
131
132 }
133
134
135 bool InsetCommand::getStatus(LCursor & 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 InsetBase::getStatus(cur, cmd, status);
151         }
152 }
153
154
155 void InsetCommand::replaceContents(std::string const & from, string const & to)
156 {
157         if (getContents() == from)
158                 setContents(to);
159 }
160
161
162 InsetCommandMailer::InsetCommandMailer(string const & name,
163                                        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 void InsetCommandMailer::string2params(string const & name,
175                                        string const & in,
176                                        InsetCommandParams & params)
177 {
178         params.clear();
179         if (in.empty())
180                 return;
181
182         istringstream data(in);
183         LyXLex lex(0,0);
184         lex.setStream(data);
185
186         string n;
187         lex >> n;
188         if (!lex || n != name)
189                 return print_mailer_error("InsetCommandMailer", in, 1, name);
190
191         // This is part of the inset proper that is usually swallowed
192         // by LyXText::readInset
193         string id;
194         lex >> id;
195         if (!lex || id != "LatexCommand")
196                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
197
198         params.read(lex);
199 }
200
201
202 string const
203 InsetCommandMailer::params2string(string const & name,
204                                   InsetCommandParams const & params)
205 {
206         ostringstream data;
207         data << name << ' ';
208         params.write(data);
209         data << "\\end_inset\n";
210         return data.str();
211 }