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