]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
Small clean-up.
[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_ = p;
61         set_label_ = false;
62 }
63
64
65 int InsetCommand::latex(Buffer const &, ostream & os,
66                         LatexRunParams const &) const
67 {
68         os << getCommand();
69         return 0;
70 }
71
72
73 int InsetCommand::ascii(Buffer const &, ostream &, int) const
74 {
75         return 0;
76 }
77
78
79 int InsetCommand::linuxdoc(Buffer const &, ostream &) const
80 {
81         return 0;
82 }
83
84
85 int InsetCommand::docbook(Buffer const &, ostream &, bool) const
86 {
87         return 0;
88 }
89
90
91 dispatch_result InsetCommand::localDispatch(FuncRequest const & cmd)
92 {
93         switch (cmd.action) {
94         case LFUN_INSET_MODIFY: {
95                 InsetCommandParams p;
96                 InsetCommandMailer::string2params(cmd.argument, p);
97                 if (p.getCmdName().empty())
98                         return UNDISPATCHED;
99
100                 setParams(p);
101                 cmd.view()->updateInset(this);
102                 return DISPATCHED;
103         }
104
105         case LFUN_INSET_DIALOG_UPDATE:
106                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
107                 return DISPATCHED;
108
109         case LFUN_MOUSE_RELEASE:
110                 return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
111
112         default:
113                 return InsetOld::localDispatch(cmd);
114         }
115
116 }
117
118
119 InsetCommandMailer::InsetCommandMailer(string const & name,
120                                        InsetCommand & inset)
121         : name_(name), inset_(inset)
122 {}
123
124
125 string const InsetCommandMailer::inset2string(Buffer const &) const
126 {
127         return params2string(name(), inset_.params());
128 }
129
130
131 void InsetCommandMailer::string2params(string const & in,
132                                        InsetCommandParams & params)
133 {
134         params.setCmdName(string());
135         params.setContents(string());
136         params.setOptions(string());
137
138         if (in.empty())
139                 return;
140
141         istringstream data(in);
142         LyXLex lex(0,0);
143         lex.setStream(data);
144
145         if (lex.isOK()) {
146                 lex.next();
147                 string const name = lex.getString();
148         }
149
150         // This is part of the inset proper that is usually swallowed
151         // by Buffer::readInset
152         if (lex.isOK()) {
153                 lex.next();
154                 string const token = lex.getString();
155                 if (token != "LatexCommand")
156                         return;
157         }
158         if (lex.isOK()) {
159                 params.read(lex);
160         }
161 }
162
163
164 string const InsetCommandMailer::params2string(string const & name,
165                                   InsetCommandParams const & params)
166 {
167         ostringstream data;
168         data << name << ' ';
169         params.write(data);
170         data << "\\end_inset\n";
171         return data.str();
172 }