]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
80d5ae8d12b2270f51ea265516f3ec5462b7685e
[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 "lyxlex.h"
20 #include "metricsinfo.h"
21
22 #include "support/std_sstream.h"
23
24
25 using std::string;
26 using std::istringstream;
27 using std::ostream;
28 using std::ostringstream;
29
30
31 InsetCommand::InsetCommand(InsetCommandParams const & p,
32                            string const & mailer_name)
33         : p_(p.getCmdName(), p.getContents(), p.getOptions()),
34           mailer_name_(mailer_name),
35           set_label_(false)
36 {}
37
38
39 InsetCommand::~InsetCommand()
40 {
41         if (!mailer_name_.empty())
42                 InsetCommandMailer(mailer_name_, *this).hideDialog();
43 }
44
45
46 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
47 {
48         if (!set_label_) {
49                 set_label_ = true;
50                 button_.update(getScreenLabel(*mi.base.bv->buffer()),
51                                editable() != NOT_EDITABLE);
52         }
53         button_.metrics(mi, dim);
54         dim_ = dim;
55 }
56
57
58 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
59 {
60         xo_ = x;
61         yo_ = y;
62         button_.draw(pi, x, y);
63 }
64
65
66 void InsetCommand::setParams(InsetCommandParams const & p)
67 {
68         p_ = p;
69         set_label_ = false;
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::linuxdoc(Buffer const &, ostream &,
89                            OutputParams const &) const
90 {
91         return 0;
92 }
93
94
95 int InsetCommand::docbook(Buffer const &, ostream &,
96                           OutputParams const &) const
97 {
98         return 0;
99 }
100
101
102 DispatchResult
103 InsetCommand::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
104 {
105         switch (cmd.action) {
106         case LFUN_INSET_MODIFY: {
107                 InsetCommandParams p;
108                 InsetCommandMailer::string2params(mailer_name_, cmd.argument, p);
109                 if (p.getCmdName().empty())
110                         return DispatchResult(false);
111
112                 setParams(p);
113                 cmd.view()->update();
114                 return DispatchResult(true, true);
115         }
116
117         case LFUN_INSET_DIALOG_UPDATE:
118                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
119                 return DispatchResult(true, true);
120
121         case LFUN_INSET_DIALOG_SHOW:
122         case LFUN_MOUSE_RELEASE: {
123                 if (!mailer_name_.empty())
124                         InsetCommandMailer(mailer_name_, *this).
125                                 showDialog(cmd.view());
126                 return DispatchResult(true);
127         }
128
129         default:
130                 return DispatchResult(false);
131         }
132
133 }
134
135
136 InsetCommandMailer::InsetCommandMailer(string const & name,
137                                        InsetCommand & inset)
138         : name_(name), inset_(inset)
139 {}
140
141
142 string const InsetCommandMailer::inset2string(Buffer const &) const
143 {
144         return params2string(name(), inset_.params());
145 }
146
147
148 void InsetCommandMailer::string2params(string const & name,
149                                        string const & in,
150                                        InsetCommandParams & params)
151 {
152         params = InsetCommandParams();
153         if (in.empty())
154                 return;
155
156         istringstream data(in);
157         LyXLex lex(0,0);
158         lex.setStream(data);
159
160         string n;
161         lex >> n;
162         if (!lex || n != name)
163                 return print_mailer_error("InsetCommandMailer", in, 1, name);
164
165         // This is part of the inset proper that is usually swallowed
166         // by LyXText::readInset
167         string id;
168         lex >> id;
169         if (!lex || id != "LatexCommand")
170                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
171
172         params.read(lex);
173 }
174
175
176 string const
177 InsetCommandMailer::params2string(string const & name,
178                                   InsetCommandParams const & params)
179 {
180         ostringstream data;
181         data << name << ' ';
182         params.write(data);
183         data << "\\end_inset\n";
184         return data.str();
185 }