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