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