]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
the dispatch 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 "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
88 InsetCommand::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
89 {
90         switch (cmd.action) {
91         case LFUN_INSET_MODIFY: {
92                 InsetCommandParams p;
93                 InsetCommandMailer::string2params(cmd.argument, p);
94                 if (p.getCmdName().empty())
95                         return UNDISPATCHED;
96
97                 setParams(p);
98                 cmd.view()->updateInset(this);
99                 return DISPATCHED;
100         }
101
102         case LFUN_INSET_DIALOG_UPDATE:
103                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
104                 return DISPATCHED;
105
106         case LFUN_MOUSE_RELEASE:
107                 return dispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
108
109         default:
110                 return UNDISPATCHED;
111         }
112
113 }
114
115
116 InsetCommandMailer::InsetCommandMailer(string const & name,
117                                        InsetCommand & inset)
118         : name_(name), inset_(inset)
119 {}
120
121
122 string const InsetCommandMailer::inset2string(Buffer const &) const
123 {
124         return params2string(name(), inset_.params());
125 }
126
127
128 void InsetCommandMailer::string2params(string const & in,
129                                        InsetCommandParams & params)
130 {
131         params = InsetCommandParams();
132
133         if (in.empty())
134                 return;
135
136         istringstream data(in);
137         LyXLex lex(0,0);
138         lex.setStream(data);
139
140         if (lex.isOK()) {
141                 lex.next();
142                 string const name = lex.getString();
143         }
144
145         // This is part of the inset proper that is usually swallowed
146         // by Buffer::readInset
147         if (lex.isOK()) {
148                 lex.next();
149                 string const token = lex.getString();
150                 if (token != "LatexCommand")
151                         return;
152         }
153         if (lex.isOK()) {
154                 params.read(lex);
155         }
156 }
157
158
159 string const InsetCommandMailer::params2string(string const & name,
160                                   InsetCommandParams const & params)
161 {
162         ostringstream data;
163         data << name << ' ';
164         params.write(data);
165         data << "\\end_inset\n";
166         return data.str();
167 }