]> git.lyx.org Git - lyx.git/blob - src/insets/InsetWrap.cpp
Splitup Font in saner bits:
[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  * \author Uwe Stöhr
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetWrap.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Counters.h"
20 #include "Cursor.h"
21 #include "debug.h"
22 #include "DispatchResult.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "gettext.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "OutputParams.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::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         FontInfo font = sane_font;
50         font.decSize();
51         font.decSize();
52         font.setColor(Color_collapsable);
53         setLabelFont(font);
54         params_.type = type;
55         params_.lines = 0;
56         params_.placement = "o";
57         params_.overhang = Length(0, Length::PCW);
58         params_.width = Length(50, Length::PCW);
59 }
60
61
62 InsetWrap::~InsetWrap()
63 {
64         InsetWrapMailer(*this).hideDialog();
65 }
66
67
68 void InsetWrap::doDispatch(Cursor & cur, FuncRequest & cmd)
69 {
70         switch (cmd.action) {
71         case LFUN_INSET_MODIFY: {
72                 InsetWrapParams params;
73                 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
74                 params_.lines = params.lines;
75                 params_.placement = params.placement;
76                 params_.overhang = params.overhang;
77                 params_.width = params.width;
78                 break;
79         }
80
81         case LFUN_INSET_DIALOG_UPDATE:
82                 InsetWrapMailer(*this).updateDialog(&cur.bv());
83                 break;
84
85         case LFUN_MOUSE_RELEASE: {
86                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
87                         InsetWrapMailer(*this).showDialog(&cur.bv());
88                         break;
89                 }
90                 InsetCollapsable::doDispatch(cur, cmd);
91                 break;
92         }
93
94         default:
95                 InsetCollapsable::doDispatch(cur, cmd);
96                 break;
97         }
98 }
99
100
101 bool InsetWrap::getStatus(Cursor & cur, FuncRequest const & cmd,
102                 FuncStatus & flag) const
103 {
104         switch (cmd.action) {
105         case LFUN_INSET_MODIFY:
106         case LFUN_INSET_DIALOG_UPDATE:
107                 flag.enabled(true);
108                 return true;
109
110         default:
111                 return InsetCollapsable::getStatus(cur, cmd, flag);
112         }
113 }
114
115
116 void InsetWrap::updateLabels(Buffer const & buf, ParIterator const & it)
117 {
118         Counters & cnts = buf.params().getTextClass().counters();
119         string const saveflt = cnts.current_float();
120
121         // Tell to captions what the current float is
122         cnts.current_float(params().type);
123
124         InsetCollapsable::updateLabels(buf, it);
125
126         //reset afterwards
127         cnts.current_float(saveflt);
128 }
129
130
131 void InsetWrapParams::write(ostream & os) const
132 {
133         os << "Wrap " << type << '\n';
134         os << "lines " << lines << "\n";
135         os << "placement " << placement << "\n";
136         os << "overhang " << overhang.asString() << "\n";
137         os << "width \"" << width.asString() << "\"\n";
138 }
139
140
141 void InsetWrapParams::read(Lexer & lex)
142 {
143         string token;
144
145         lex >> token;
146         if (token == "lines")
147                 lex >> lines;
148         else {
149                 lyxerr << "InsetWrap::Read:: Missing 'lines'-tag!"
150                         << endl;
151                 // take countermeasures
152                 lex.pushToken(token);
153         }
154         if (!lex)
155                 return;
156         lex >> token;
157         if (token == "placement")
158                 lex >> placement;
159         else {
160                 lyxerr << "InsetWrap::Read:: Missing 'placement'-tag!"
161                         << endl;
162                 lex.pushToken(token);
163         }
164         if (!lex)
165                 return;
166         lex >> token;
167         if (token == "overhang") {
168                 lex.next();
169                 overhang = Length(lex.getString());
170         } else {
171                 lyxerr << "InsetWrap::Read:: Missing 'overhang'-tag!"
172                         << endl;
173                 lex.pushToken(token);
174         }
175         if (!lex)
176                 return;
177         lex >> token;
178         if (token == "width") {
179                 lex.next();
180                 width = Length(lex.getString());
181         } else {
182                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
183                         << endl;
184                 lex.pushToken(token);
185         }
186 }
187
188
189 void InsetWrap::write(Buffer const & buf, ostream & os) const
190 {
191         params_.write(os);
192         InsetCollapsable::write(buf, os);
193 }
194
195
196 void InsetWrap::read(Buffer const & buf, Lexer & lex)
197 {
198         params_.read(lex);
199         InsetCollapsable::read(buf, lex);
200 }
201
202
203 void InsetWrap::validate(LaTeXFeatures & features) const
204 {
205         features.require("wrapfig");
206         InsetCollapsable::validate(features);
207 }
208
209
210 Inset * InsetWrap::clone() const
211 {
212         return new InsetWrap(*this);
213 }
214
215
216 docstring const InsetWrap::editMessage() const
217 {
218         return _("Opened Wrap Inset");
219 }
220
221
222 int InsetWrap::latex(Buffer const & buf, odocstream & os,
223                      OutputParams const & runparams) const
224 {
225         os << "\\begin{wrap" << from_ascii(params_.type) << '}';
226         // no optional argument when lines are zero
227         if (params_.lines != 0)
228                 os << '[' << params_.lines << ']';
229         os << '{' << from_ascii(params_.placement) << '}';
230         Length over(params_.overhang);
231         // no optional argument when the value is zero
232         if (over.value() != 0)
233                 os << '[' << from_ascii(params_.overhang.asLatexString()) << ']';
234         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
235         int const i = InsetText::latex(buf, os, runparams);
236         os << "\\end{wrap" << from_ascii(params_.type) << "}%\n";
237         return i + 2;
238 }
239
240
241 int InsetWrap::plaintext(Buffer const & buf, odocstream & os,
242                          OutputParams const & runparams) const
243 {
244         os << '[' << buf.B_("wrap") << ' ' << floatName(params_.type, buf.params()) << ":\n";
245         InsetText::plaintext(buf, os, runparams);
246         os << "\n]";
247
248         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
249 }
250
251
252 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
253                        OutputParams const & runparams) const
254 {
255         // FIXME UNICODE
256         os << '<' << from_ascii(params_.type) << '>';
257         int const i = InsetText::docbook(buf, os, runparams);
258         os << "</" << from_ascii(params_.type) << '>';
259         return i;
260 }
261
262
263 bool InsetWrap::insetAllowed(InsetCode code) const
264 {
265         switch(code) {
266         case FLOAT_CODE:
267         case FOOT_CODE:
268         case MARGIN_CODE:
269                 return false;
270         default:
271                 return InsetCollapsable::insetAllowed(code);
272         }
273 }
274
275
276 bool InsetWrap::showInsetDialog(BufferView * bv) const
277 {
278         if (!InsetText::showInsetDialog(bv))
279                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
280         return true;
281 }
282
283
284 string const InsetWrapMailer::name_("wrap");
285
286 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
287         : inset_(inset)
288 {}
289
290
291 string const InsetWrapMailer::inset2string(Buffer const &) const
292 {
293         return params2string(inset_.params());
294 }
295
296
297 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
298 {
299         params = InsetWrapParams();
300         if (in.empty())
301                 return;
302
303         istringstream data(in);
304         Lexer lex(0,0);
305         lex.setStream(data);
306
307         string name;
308         lex >> name;
309         if (!lex || name != name_)
310                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
311
312         // This is part of the inset proper that is usually swallowed
313         // by Text::readInset
314         string id;
315         lex >> id;
316         if (!lex || id != "Wrap")
317                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
318
319         // We have to read the type here!
320         lex >> params.type;
321         params.read(lex);
322 }
323
324
325 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
326 {
327         ostringstream data;
328         data << name_ << ' ';
329         params.write(data);
330         return data.str();
331 }
332
333
334 } // namespace lyx