]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
more IU
[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         setPosCache(pi, x, y);
61         button_.draw(pi, x, y);
62 }
63
64
65 void InsetCommand::setParams(InsetCommandParams const & p)
66 {
67         p_ = p;
68         set_label_ = false;
69 }
70
71
72 int InsetCommand::latex(Buffer const &, ostream & os,
73                         OutputParams const &) const
74 {
75         os << getCommand();
76         return 0;
77 }
78
79
80 int InsetCommand::plaintext(Buffer const &, ostream &,
81                         OutputParams const &) const
82 {
83         return 0;
84 }
85
86
87 int InsetCommand::linuxdoc(Buffer const &, ostream &,
88                            OutputParams const &) const
89 {
90         return 0;
91 }
92
93
94 int InsetCommand::docbook(Buffer const &, ostream &,
95                           OutputParams const &) const
96 {
97         return 0;
98 }
99
100
101 DispatchResult
102 InsetCommand::priv_dispatch(LCursor & cur, FuncRequest const & cmd)
103 {
104         switch (cmd.action) {
105         case LFUN_INSET_MODIFY: {
106                 InsetCommandParams p;
107                 InsetCommandMailer::string2params(mailer_name_, cmd.argument, p);
108                 if (p.getCmdName().empty())
109                         return DispatchResult(false);
110                 setParams(p);
111                 cur.bv().update();
112                 return DispatchResult(true, true);
113         }
114
115         case LFUN_INSET_DIALOG_UPDATE:
116                 InsetCommandMailer(cmd.argument, *this).updateDialog(&cur.bv());
117                 return DispatchResult(true, true);
118
119         case LFUN_INSET_DIALOG_SHOW:
120         case LFUN_MOUSE_RELEASE: {
121                 if (!mailer_name_.empty())
122                         InsetCommandMailer(mailer_name_, *this).showDialog(&cur.bv());
123                 return DispatchResult(true);
124         }
125
126         default:
127                 return DispatchResult(false);
128         }
129
130 }
131
132
133 InsetCommandMailer::InsetCommandMailer(string const & name,
134                                        InsetCommand & inset)
135         : name_(name), inset_(inset)
136 {}
137
138
139 string const InsetCommandMailer::inset2string(Buffer const &) const
140 {
141         return params2string(name(), inset_.params());
142 }
143
144
145 void InsetCommandMailer::string2params(string const & name,
146                                        string const & in,
147                                        InsetCommandParams & params)
148 {
149         params = InsetCommandParams();
150         if (in.empty())
151                 return;
152
153         istringstream data(in);
154         LyXLex lex(0,0);
155         lex.setStream(data);
156
157         string n;
158         lex >> n;
159         if (!lex || n != name)
160                 return print_mailer_error("InsetCommandMailer", in, 1, name);
161
162         // This is part of the inset proper that is usually swallowed
163         // by LyXText::readInset
164         string id;
165         lex >> id;
166         if (!lex || id != "LatexCommand")
167                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
168
169         params.read(lex);
170 }
171
172
173 string const
174 InsetCommandMailer::params2string(string const & name,
175                                   InsetCommandParams const & params)
176 {
177         ostringstream data;
178         data << name << ' ';
179         params.write(data);
180         data << "\\end_inset\n";
181         return data.str();
182 }