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