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