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