]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
the monster patch
[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(BufferView & bv, FuncRequest const & cmd)
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                 setParams(p);
112                 bv.update();
113                 return DispatchResult(true, true);
114         }
115
116         case LFUN_INSET_DIALOG_UPDATE:
117                 InsetCommandMailer(cmd.argument, *this).updateDialog(&bv);
118                 return DispatchResult(true, true);
119
120         case LFUN_INSET_DIALOG_SHOW:
121         case LFUN_MOUSE_RELEASE: {
122                 if (!mailer_name_.empty())
123                         InsetCommandMailer(mailer_name_, *this).showDialog(&bv);
124                 return DispatchResult(true);
125         }
126
127         default:
128                 return DispatchResult(false);
129         }
130
131 }
132
133
134 InsetCommandMailer::InsetCommandMailer(string const & name,
135                                        InsetCommand & inset)
136         : name_(name), inset_(inset)
137 {}
138
139
140 string const InsetCommandMailer::inset2string(Buffer const &) const
141 {
142         return params2string(name(), inset_.params());
143 }
144
145
146 void InsetCommandMailer::string2params(string const & name,
147                                        string const & in,
148                                        InsetCommandParams & params)
149 {
150         params = InsetCommandParams();
151         if (in.empty())
152                 return;
153
154         istringstream data(in);
155         LyXLex lex(0,0);
156         lex.setStream(data);
157
158         string n;
159         lex >> n;
160         if (!lex || n != name)
161                 return print_mailer_error("InsetCommandMailer", in, 1, name);
162
163         // This is part of the inset proper that is usually swallowed
164         // by LyXText::readInset
165         string id;
166         lex >> id;
167         if (!lex || id != "LatexCommand")
168                 return print_mailer_error("InsetCommandMailer", in, 2, "LatexCommand");
169
170         params.read(lex);
171 }
172
173
174 string const
175 InsetCommandMailer::params2string(string const & name,
176                                   InsetCommandParams const & params)
177 {
178         ostringstream data;
179         data << name << ' ';
180         params.write(data);
181         data << "\\end_inset\n";
182         return data.str();
183 }