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