]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
10d12d096703bd0cb4a7a90541e1e33230c89c94
[lyx.git] / src / insets / InsetWrap.cpp
1 /**
2  * \file InsetWrap.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetWrap.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "Cursor.h"
19 #include "debug.h"
20 #include "DispatchResult.h"
21 #include "Floating.h"
22 #include "FloatList.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "gettext.h"
26 #include "LaTeXFeatures.h"
27 #include "Color.h"
28 #include "Lexer.h"
29 #include "OutputParams.h"
30 #include "Paragraph.h"
31 #include "TocBackend.h"
32
33 #include "support/convert.h"
34
35
36 namespace lyx {
37
38 using std::string;
39 using std::endl;
40 using std::auto_ptr;
41 using std::istringstream;
42 using std::ostream;
43 using std::ostringstream;
44
45
46 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
47         : InsetCollapsable(bp), name_(from_utf8(type))
48 {
49         setLabel(_("wrap: ") + floatName(type, bp));
50         Font font(Font::ALL_SANE);
51         font.decSize();
52         font.decSize();
53         font.setColor(Color::collapsable);
54         setLabelFont(font);
55         params_.type = type;
56         params_.width = Length(50, Length::PCW);
57 }
58
59
60 InsetWrap::~InsetWrap()
61 {
62         InsetWrapMailer(*this).hideDialog();
63 }
64
65
66 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
67 {
68         switch (cmd.action) {
69         case LFUN_INSET_MODIFY: {
70                 InsetWrapParams params;
71                 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
72                 params_.placement = params.placement;
73                 params_.width     = params.width;
74                 break;
75         }
76
77         case LFUN_INSET_DIALOG_UPDATE:
78                 InsetWrapMailer(*this).updateDialog(&cur.bv());
79                 break;
80
81         case LFUN_MOUSE_RELEASE: {
82                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
83                         InsetWrapMailer(*this).showDialog(&cur.bv());
84                         break;
85                 }
86                 InsetCollapsable::doDispatch(cur, cmd);
87                 break;
88         }
89
90         default:
91                 InsetCollapsable::doDispatch(cur, cmd);
92                 break;
93         }
94 }
95
96
97 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
98                 FuncStatus & flag) const
99 {
100         switch (cmd.action) {
101         case LFUN_INSET_MODIFY:
102         case LFUN_INSET_DIALOG_UPDATE:
103                 flag.enabled(true);
104                 return true;
105
106         default:
107                 return InsetCollapsable::getStatus(cur, cmd, flag);
108         }
109 }
110
111
112 void InsetWrapParams::write(ostream & os) const
113 {
114         os << "Wrap " << type << '\n';
115
116         if (!placement.empty())
117                 os << "placement " << placement << "\n";
118
119         os << "width \"" << width.asString() << "\"\n";
120 }
121
122
123 void InsetWrapParams::read(Lexer & lex)
124 {
125         string token;
126         lex >> token;
127         if (token == "placement")
128                 lex >> placement;
129         else {
130                 // take countermeasures
131                 lex.pushToken(token);
132         }
133         if (!lex)
134                 return;
135         lex >> token;
136         if (token == "width") {
137                 lex.next();
138                 width = Length(lex.getString());
139         } else {
140                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
141                         << endl;
142                 // take countermeasures
143                 lex.pushToken(token);
144         }
145 }
146
147
148 void InsetWrap::write(Buffer const & buf, ostream & os) const
149 {
150         params_.write(os);
151         InsetCollapsable::write(buf, os);
152 }
153
154
155 void InsetWrap::read(Buffer const & buf, Lexer & lex)
156 {
157         params_.read(lex);
158         InsetCollapsable::read(buf, lex);
159 }
160
161
162 void InsetWrap::validate(LaTeXFeatures & features) const
163 {
164         features.require("floatflt");
165         InsetCollapsable::validate(features);
166 }
167
168
169 auto_ptr<Inset> InsetWrap::doClone() const
170 {
171         return auto_ptr<Inset>(new InsetWrap(*this));
172 }
173
174
175 docstring const InsetWrap::editMessage() const
176 {
177         return _("Opened Wrap Inset");
178 }
179
180
181 int InsetWrap::latex(Buffer const & buf, odocstream & os,
182                      OutputParams const & runparams) const
183 {
184         os << "\\begin{floating" << from_ascii(params_.type) << '}';
185         if (!params_.placement.empty())
186                 os << '[' << from_ascii(params_.placement) << ']';
187         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
188         int const i = InsetText::latex(buf, os, runparams);
189         os << "\\end{floating" << from_ascii(params_.type) << "}%\n";
190         return i + 2;
191 }
192
193
194 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
195                          OutputParams const & runparams) const
196 {
197         os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
198         InsetText::plaintext(buf, os, runparams);
199         os << "\n]";
200
201         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
202 }
203
204
205 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
206                        OutputParams const & runparams) const
207 {
208         // FIXME UNICODE
209         os << '<' << from_ascii(params_.type) << '>';
210         int const i = InsetText::docbook(buf, os, runparams);
211         os << "</" << from_ascii(params_.type) << '>';
212         return i;
213 }
214
215
216 bool InsetWrap::insetAllowed(Inset::Code code) const
217 {
218         switch(code) {
219         case FLOAT_CODE:
220         case FOOT_CODE:
221         case MARGIN_CODE:
222                 return false;
223         default:
224                 return InsetCollapsable::insetAllowed(code);
225         }
226 }
227
228
229 bool InsetWrap::showInsetDialog(BufferView * bv) const
230 {
231         if (!InsetText::showInsetDialog(bv))
232                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
233         return true;
234 }
235
236
237 void InsetWrap::addToToc(TocList & toclist, Buffer const & buf) const
238 {
239         // Is there a need to provide a list of wrap insets?
240         return;
241 }
242
243
244 string const InsetWrapMailer::name_("wrap");
245
246 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
247         : inset_(inset)
248 {}
249
250
251 string const InsetWrapMailer::inset2string(Buffer const &) const
252 {
253         return params2string(inset_.params());
254 }
255
256
257 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
258 {
259         params = InsetWrapParams();
260         if (in.empty())
261                 return;
262
263         istringstream data(in);
264         Lexer lex(0,0);
265         lex.setStream(data);
266
267         string name;
268         lex >> name;
269         if (!lex || name != name_)
270                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
271
272         // This is part of the inset proper that is usually swallowed
273         // by Text::readInset
274         string id;
275         lex >> id;
276         if (!lex || id != "Wrap")
277                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
278
279         // We have to read the type here!
280         lex >> params.type;
281         params.read(lex);
282 }
283
284
285 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
286 {
287         ostringstream data;
288         data << name_ << ' ';
289         params.write(data);
290         return data.str();
291 }
292
293
294 } // namespace lyx