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