]> git.lyx.org Git - lyx.git/blob - src/insets/insetcommand.C
coordinate cache IU
[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 "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         xo_ = x;
52         yo_ = y;
53         button_.draw(pi, x, y);
54 }
55
56
57 void InsetCommand::setParams(InsetCommandParams const & p)
58 {
59         p_ = p;
60         set_label_ = false;
61 }
62
63
64 int InsetCommand::latex(Buffer const &, ostream & os,
65                         OutputParams const &) const
66 {
67         os << getCommand();
68         return 0;
69 }
70
71
72 int InsetCommand::plaintext(Buffer const &, ostream &,
73                         OutputParams const &) const
74 {
75         return 0;
76 }
77
78
79 int InsetCommand::linuxdoc(Buffer const &, ostream &,
80                            OutputParams const &) const
81 {
82         return 0;
83 }
84
85
86 int InsetCommand::docbook(Buffer const &, ostream &,
87                           OutputParams const &) const
88 {
89         return 0;
90 }
91
92
93 DispatchResult
94 InsetCommand::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
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 DispatchResult(false);
102
103                 setParams(p);
104                 cmd.view()->update();
105                 return DispatchResult(true, true);
106         }
107
108         case LFUN_INSET_DIALOG_UPDATE:
109                 InsetCommandMailer(cmd.argument, *this).updateDialog(cmd.view());
110                 return DispatchResult(true, true);
111
112         case LFUN_MOUSE_RELEASE:
113                 edit(cmd.view(), true);
114                 return DispatchResult(true);
115
116         default:
117                 return DispatchResult(false);
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 = InsetCommandParams();
139
140         if (in.empty())
141                 return;
142
143         istringstream data(in);
144         LyXLex lex(0,0);
145         lex.setStream(data);
146
147         if (lex.isOK()) {
148                 lex.next();
149                 string const name = lex.getString();
150         }
151
152         // This is part of the inset proper that is usually swallowed
153         // by Buffer::readInset
154         if (lex.isOK()) {
155                 lex.next();
156                 string const token = lex.getString();
157                 if (token != "LatexCommand")
158                         return;
159         }
160         if (lex.isOK()) {
161                 params.read(lex);
162         }
163 }
164
165
166 string const InsetCommandMailer::params2string(string const & name,
167                                   InsetCommandParams const & params)
168 {
169         ostringstream data;
170         data << name << ' ';
171         params.write(data);
172         data << "\\end_inset\n";
173         return data.str();
174 }