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