]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Changes due to the removal of using directives from support/std_sstream.h.
[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 "funcrequest.h"
18 #include "lyxlex.h"
19 #include "metricsinfo.h"
20
21 #include "support/std_sstream.h"
22
23 using std::istringstream;
24 using std::ostream;
25 using std::ostringstream;
26
27
28 InsetCommand::InsetCommand(InsetCommandParams const & p)
29         : p_(p.getCmdName(), p.getContents(), p.getOptions()),
30           set_label_(false)
31 {}
32
33
34 BufferView * InsetCommand::view() const
35 {
36         return button_.view();
37 }
38
39
40 void InsetCommand::metrics(MetricsInfo & mi, Dimension & dim) const
41 {
42         if (!set_label_) {
43                 set_label_ = true;
44                 button_.update(getScreenLabel(*mi.base.bv->buffer()),
45                                editable() != NOT_EDITABLE);
46         }
47         button_.metrics(mi, dim);
48         dim_ = dim;
49 }
50
51
52 void InsetCommand::draw(PainterInfo & pi, int x, int y) const
53 {
54         button_.draw(pi, x, y);
55 }
56
57
58 void InsetCommand::setParams(InsetCommandParams const & p)
59 {
60         p_.setCmdName(p.getCmdName());
61         p_.setContents(p.getContents());
62         p_.setOptions(p.getOptions());
63         set_label_ = false;
64 }
65
66
67 int InsetCommand::latex(Buffer const &, ostream & os,
68                         LatexRunParams const &) const
69 {
70         os << getCommand();
71         return 0;
72 }
73
74
75 int InsetCommand::ascii(Buffer const &, ostream &, int) const
76 {
77         return 0;
78 }
79
80
81 int InsetCommand::linuxdoc(Buffer const &, ostream &) const
82 {
83         return 0;
84 }
85
86
87 int InsetCommand::docbook(Buffer const &, ostream &, bool) const
88 {
89         return 0;
90 }
91
92
93 dispatch_result InsetCommand::localDispatch(FuncRequest const & cmd)
94 {
95         switch (cmd.action) {
96         case LFUN_INSET_MODIFY: {
97                 InsetCommandParams p;
98                 InsetCommandMailer::string2params(cmd.argument, p);
99                 if (p.getCmdName().empty())
100                         return UNDISPATCHED;
101
102                 setParams(p);
103                 cmd.view()->updateInset(this);
104                 return DISPATCHED;
105         }
106
107         case LFUN_INSET_DIALOG_UPDATE:
108                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
109                 return DISPATCHED;
110
111         case LFUN_MOUSE_RELEASE:
112                 return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
113
114         default:
115                 return InsetOld::localDispatch(cmd);
116         }
117
118 }
119
120
121 InsetCommandMailer::InsetCommandMailer(string const & name,
122                                        InsetCommand & inset)
123         : name_(name), inset_(inset)
124 {}
125
126
127 string const InsetCommandMailer::inset2string(Buffer const &) const
128 {
129         return params2string(name(), inset_.params());
130 }
131
132
133 void InsetCommandMailer::string2params(string const & in,
134                                        InsetCommandParams & params)
135 {
136         params.setCmdName(string());
137         params.setContents(string());
138         params.setOptions(string());
139
140         if (in.empty())
141                 return;
142
143         istringstream data(STRCONV(in));
144         LyXLex lex(0,0);
145         lex.setStream(data);
146
147         if (lex.isOK()) {
148                 lex.next();
149                 string const name = lex.getString();
150         }
151
152         // This is part of the inset proper that is usually swallowed
153         // by Buffer::readInset
154         if (lex.isOK()) {
155                 lex.next();
156                 string const token = lex.getString();
157                 if (token != "LatexCommand")
158                         return;
159         }
160         if (lex.isOK()) {
161                 params.read(lex);
162         }
163 }
164
165
166 string const InsetCommandMailer::params2string(string const & name,
167                                   InsetCommandParams const & params)
168 {
169         ostringstream data;
170         data << name << ' ';
171         params.write(data);
172         data << "\\end_inset\n";
173         return STRCONV(data.str());
174 }