1 /**
2 * \file InsetFloat.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
5 *
6 * \author Jürgen Vigna
7 * \author Lars Gullik Bjønnes
8 * \author Jürgen Spitzmüller
9 *
10 * Full author contact details are available in file CREDITS.
11 */
12
13 #include <config.h>
14
15 #include "InsetFloat.h"
16 #include "InsetCaption.h"
17
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "Counters.h"
22 #include "Cursor.h"
23 #include "DispatchResult.h"
24 #include "Floating.h"
25 #include "FloatList.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "output_xhtml.h"
31 #include "ParIterator.h"
32 #include "TexRow.h"
33 #include "texstream.h"
34 #include "TextClass.h"
35
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40
41 #include "frontends/Application.h"
42
43 using namespace std;
44 using namespace lyx::support;
45
46
47 namespace lyx {
48
49 // With this inset it will be possible to support the latex package
50 // float.sty, and I am sure that with this and some additional support
51 // classes we can support similar functionality in other formats
52 // (read DocBook).
53 // By using float.sty we will have the same handling for all floats, both
54 // for those already in existance (table and figure) and all user created
55 // ones¹. So suddenly we give the users the possibility of creating new
56 // kinds of floats on the fly. (and with a uniform look)
57 //
58 // API to float.sty:
59 // \newfloat{type}{placement}{ext}[within]
60 // type - The "type" of the new class of floats, like program or
61 // algorithm. After the appropriate \newfloat, commands
62 // such as \begin{program} or \end{algorithm*} will be
63 // available.
64 // placement - The default placement for the given class of floats.
65 // They are like in standard LaTeX: t, b, p and h for top,
66 // bottom, page, and here, respectively. On top of that
67 // there is a new type, H, which does not really correspond
68 // to a float, since it means: put it "here" and nowhere else.
69 // Note, however that the H specifier is special and, because
70 // of implementation details cannot be used in the second
71 // argument of \newfloat.
72 // ext - The file name extension of an auxiliary file for the list
73 // of figures (or whatever). LaTeX writes the captions to
74 // this file.
75 // within - This (optional) argument determines whether floats of this
76 // class will be numbered within some sectional unit of the
77 // document. For example, if within is equal to chapter, the
78 // floats will be numbered within chapters.
79 // \floatstyle{style}
80 // style - plain, boxed, ruled
81 // \floatname{float}{floatname}
82 // float -
83 // floatname -
84 // \floatplacement{float}{placement}
85 // float -
86 // placement -
87 // \restylefloat{float}
88 // float -
89 // \listof{type}{title}
90 // title -
91
92 // ¹ the algorithm float is defined using the float.sty package. Like this
93 // \floatstyle{ruled}
94 // \newfloat{algorithm}{htbp}{loa}[<sect>]
95 // \floatname{algorithm}{Algorithm}
96 //
97 // The intention is that floats should be definable from two places:
98 // - layout files
99 // - the "gui" (i.e. by the user)
100 //
101 // From layout files.
102 // This should only be done for floats defined in a documentclass and that
103 // does not need any additional packages. The two most known floats in this
104 // category is "table" and "figure". Floats defined in layout files are only
105 // stored in lyx files if the user modifies them.
106 //
107 // By the user.
108 // There should be a gui dialog (and also a collection of lyxfuncs) where
109 // the user can modify existing floats and/or create new ones.
110 //
111 // The individual floats will also have some settable
112 // variables: wide and placement.
113 //
114 // Lgb
115
116 //FIXME: why do we set in stone the type here?
InsetFloat(Buffer * buf,string params_str)117 InsetFloat::InsetFloat(Buffer * buf, string params_str)
118 : InsetCaptionable(buf)
119 {
120 string2params(params_str, params_);
121 setCaptionType(params_.type);
122 }
123
124
125 // Enforce equality of float type and caption type.
setCaptionType(std::string const & type)126 void InsetFloat::setCaptionType(std::string const & type)
127 {
128 InsetCaptionable::setCaptionType(type);
129 params_.type = captionType();
130 // check if the float type exists
131 if (buffer().params().documentClass().floats().typeExist(params_.type))
132 setNewLabel();
133 else
134 setLabel(bformat(_("ERROR: Unknown float type: %1$s"), from_utf8(params_.type)));
135 }
136
137
layoutName() const138 docstring InsetFloat::layoutName() const
139 {
140 return "Float:" + from_utf8(params_.type);
141 }
142
143
toolTip(BufferView const & bv,int x,int y) const144 docstring InsetFloat::toolTip(BufferView const & bv, int x, int y) const
145 {
146 if (isOpen(bv))
147 return InsetCaptionable::toolTip(bv, x, y);
148
149 OutputParams rp(&buffer().params().encoding());
150 return getCaptionText(rp);
151 }
152
153
doDispatch(Cursor & cur,FuncRequest & cmd)154 void InsetFloat::doDispatch(Cursor & cur, FuncRequest & cmd)
155 {
156 switch (cmd.action()) {
157
158 case LFUN_INSET_MODIFY: {
159 InsetFloatParams params;
160 string2params(to_utf8(cmd.argument()), params);
161 cur.recordUndoInset(this);
162
163 // placement, wide and sideways are not used for subfloats
164 if (!params_.subfloat) {
165 params_.placement = params.placement;
166 params_.wide = params.wide;
167 params_.sideways = params.sideways;
168 }
169 setNewLabel();
170 if (params_.type != params.type)
171 setCaptionType(params.type);
172 // what we really want here is a TOC update, but that means
173 // a full buffer update
174 cur.forceBufferUpdate();
175 break;
176 }
177
178 case LFUN_INSET_DIALOG_UPDATE: {
179 cur.bv().updateDialog("float", params2string(params()));
180 break;
181 }
182
183 default:
184 InsetCaptionable::doDispatch(cur, cmd);
185 break;
186 }
187 }
188
189
getStatus(Cursor & cur,FuncRequest const & cmd,FuncStatus & flag) const190 bool InsetFloat::getStatus(Cursor & cur, FuncRequest const & cmd,
191 FuncStatus & flag) const
192 {
193 switch (cmd.action()) {
194
195 case LFUN_INSET_MODIFY:
196 case LFUN_INSET_DIALOG_UPDATE:
197 flag.setEnabled(true);
198 return true;
199
200 case LFUN_INSET_SETTINGS:
201 if (InsetCaptionable::getStatus(cur, cmd, flag)) {
202 flag.setEnabled(flag.enabled() && !params_.subfloat);
203 return true;
204 } else
205 return false;
206
207 case LFUN_NEWLINE_INSERT:
208 if (params_.subfloat) {
209 flag.setEnabled(false);
210 return true;
211 }
212 // no subfloat:
213 // fall through
214
215 default:
216 return InsetCaptionable::getStatus(cur, cmd, flag);
217 }
218 }
219
220
hasSubCaptions(ParIterator const & it) const221 bool InsetFloat::hasSubCaptions(ParIterator const & it) const
222 {
223 return (it.innerInsetOfType(FLOAT_CODE) || it.innerInsetOfType(WRAP_CODE));
224 }
225
226
write(ostream & os) const227 void InsetFloatParams::write(ostream & os) const
228 {
229 if (type.empty()) {
230 // Better this than creating a parse error. This in fact happens in the
231 // parameters dialog via InsetFloatParams::params2string.
232 os << "senseless" << '\n';
233 } else
234 os << type << '\n';
235
236 if (!placement.empty())
237 os << "placement " << placement << "\n";
238
239 if (wide)
240 os << "wide true\n";
241 else
242 os << "wide false\n";
243
244 if (sideways)
245 os << "sideways true\n";
246 else
247 os << "sideways false\n";
248 }
249
250
read(Lexer & lex)251 void InsetFloatParams::read(Lexer & lex)
252 {
253 lex.setContext("InsetFloatParams::read");
254 lex >> type;
255 if (lex.checkFor("placement"))
256 lex >> placement;
257 lex >> "wide" >> wide;
258 lex >> "sideways" >> sideways;
259 }
260
261
write(ostream & os) const262 void InsetFloat::write(ostream & os) const
263 {
264 os << "Float ";
265 params_.write(os);
266 InsetCaptionable::write(os);
267 }
268
269
read(Lexer & lex)270 void InsetFloat::read(Lexer & lex)
271 {
272 params_.read(lex);
273 InsetCaptionable::read(lex);
274 setCaptionType(params_.type);
275 }
276
277
validate(LaTeXFeatures & features) const278 void InsetFloat::validate(LaTeXFeatures & features) const
279 {
280 if (support::contains(params_.placement, 'H'))
281 features.require("float");
282
283 if (params_.sideways)
284 features.require("rotfloat");
285
286 if (features.inFloat())
287 features.require("subfig");
288
289 features.useFloat(params_.type, features.inFloat());
290 features.inFloat(true);
291 InsetCaptionable::validate(features);
292 features.inFloat(false);
293 }
294
295
xhtml(XHTMLStream & xs,OutputParams const & rp) const296 docstring InsetFloat::xhtml(XHTMLStream & xs, OutputParams const & rp) const
297 {
298 FloatList const & floats = buffer().params().documentClass().floats();
299 Floating const & ftype = floats.getType(params_.type);
300 string const & htmltype = ftype.htmlTag();
301 string const & attr = ftype.htmlAttrib();
302
303 odocstringstream ods;
304 XHTMLStream newxs(ods);
305 newxs << html::StartTag(htmltype, attr);
306 InsetText::XHTMLOptions const opts =
307 InsetText::WriteLabel | InsetText::WriteInnerTag;
308 docstring deferred = InsetText::insetAsXHTML(newxs, rp, opts);
309 newxs << html::EndTag(htmltype);
310
311 if (rp.inFloat == OutputParams::NONFLOAT) {
312 // In this case, this float needs to be deferred, but we'll put it
313 // before anything the text itself deferred.
314 deferred = ods.str() + '\n' + deferred;
315 } else {
316 // In this case, the whole thing is already being deferred, so
317 // we can write to the stream.
318 // Note that things will already have been escaped, so we do not
319 // want to escape them again.
320 xs << XHTMLStream::ESCAPE_NONE << ods.str();
321 }
322 return deferred;
323 }
324
325
latex(otexstream & os,OutputParams const & runparams_in) const326 void InsetFloat::latex(otexstream & os, OutputParams const & runparams_in) const
327 {
328 if (runparams_in.inFloat != OutputParams::NONFLOAT) {
329 if (!paragraphs().empty() && !runparams_in.nice)
330 // improve TexRow precision in non-nice mode
331 os << safebreakln;
332
333 if (runparams_in.moving_arg)
334 os << "\\protect";
335 os << "\\subfloat";
336
337 OutputParams rp = runparams_in;
338 rp.moving_arg = true;
339 os << getCaption(rp);
340 os << '{';
341 // The main argument is the contents of the float. This is not a moving argument.
342 rp.moving_arg = false;
343 rp.inFloat = OutputParams::SUBFLOAT;
344 InsetText::latex(os, rp);
345 os << "}";
346
347 return;
348 }
349 OutputParams runparams(runparams_in);
350 runparams.inFloat = OutputParams::MAINFLOAT;
351
352 FloatList const & floats = buffer().params().documentClass().floats();
353 string tmptype = params_.type;
354 if (params_.sideways && floats.allowsSideways(params_.type))
355 tmptype = "sideways" + params_.type;
356 if (params_.wide && floats.allowsWide(params_.type)
357 && (!params_.sideways ||
358 params_.type == "figure" ||
359 params_.type == "table"))
360 tmptype += "*";
361 // Figure out the float placement to use.
362 // From lowest to highest:
363 // - float default placement
364 // - document wide default placement
365 // - specific float placement
366 string tmpplacement;
367 string const buf_placement = buffer().params().float_placement;
368 string const def_placement = floats.defaultPlacement(params_.type);
369 if (!params_.placement.empty()
370 && params_.placement != def_placement) {
371 tmpplacement = params_.placement;
372 } else if (params_.placement.empty()
373 && !buf_placement.empty()
374 && buf_placement != def_placement) {
375 tmpplacement = buf_placement;
376 }
377
378 // Check if placement is allowed by this float
379 string const allowed_placement =
380 floats.allowedPlacement(params_.type);
381 string placement;
382 string::const_iterator lit = tmpplacement.begin();
383 string::const_iterator end = tmpplacement.end();
384 for (; lit != end; ++lit) {
385 if (contains(allowed_placement, *lit))
386 placement += *lit;
387 }
388
389 // Force \begin{<floatname>} to appear in a new line.
390 os << breakln << "\\begin{" << from_ascii(tmptype) << '}';
391 if (runparams.lastid != -1)
392 os.texrow().start(runparams.lastid, runparams.lastpos);
393 // We only output placement if different from the def_placement.
394 // sidewaysfloats always use their own page,
395 // therefore don't output the p option that is always set
396 if (!placement.empty()
397 && (!params_.sideways || (params_.sideways && from_ascii(placement) != "p")))
398 os << '[' << from_ascii(placement) << ']';
399 os << '\n';
400
401 InsetText::latex(os, runparams);
402
403 // Force \end{<floatname>} to appear in a new line.
404 os << breakln << "\\end{" << from_ascii(tmptype) << "}\n";
405 }
406
407
plaintext(odocstringstream & os,OutputParams const & runparams,size_t max_length) const408 int InsetFloat::plaintext(odocstringstream & os, OutputParams const & runparams, size_t max_length) const
409 {
410 os << '[' << buffer().B_("float") << ' '
411 << floatName(params_.type) << ":\n";
412 InsetText::plaintext(os, runparams, max_length);
413 os << "\n]";
414
415 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
416 }
417
418
docbook(odocstream & os,OutputParams const & runparams) const419 int InsetFloat::docbook(odocstream & os, OutputParams const & runparams) const
420 {
421 // FIXME Implement subfloat!
422 // FIXME UNICODE
423 os << '<' << from_ascii(params_.type) << '>';
424 int const i = InsetText::docbook(os, runparams);
425 os << "</" << from_ascii(params_.type) << '>';
426
427 return i;
428 }
429
430
insetAllowed(InsetCode code) const431 bool InsetFloat::insetAllowed(InsetCode code) const
432 {
433 // The case that code == FLOAT_CODE is handled in Text3.cpp,
434 // because we need to know what type of float is meant.
435 switch(code) {
436 case WRAP_CODE:
437 case FOOT_CODE:
438 case MARGIN_CODE:
439 return false;
440 default:
441 return InsetCaptionable::insetAllowed(code);
442 }
443 }
444
445
setWide(bool w,bool update_label)446 void InsetFloat::setWide(bool w, bool update_label)
447 {
448 if (!buffer().params().documentClass().floats().allowsWide(params_.type))
449 params_.wide = false;
450 else
451 params_.wide = w;
452 if (update_label)
453 setNewLabel();
454 }
455
456
setSideways(bool s,bool update_label)457 void InsetFloat::setSideways(bool s, bool update_label)
458 {
459 if (!buffer().params().documentClass().floats().allowsSideways(params_.type))
460 params_.sideways = false;
461 else
462 params_.sideways = s;
463 if (update_label)
464 setNewLabel();
465 }
466
467
setSubfloat(bool s,bool update_label)468 void InsetFloat::setSubfloat(bool s, bool update_label)
469 {
470 params_.subfloat = s;
471 if (update_label)
472 setNewLabel();
473 }
474
475
setNewLabel()476 void InsetFloat::setNewLabel()
477 {
478 docstring lab = _("float: ");
479
480 if (params_.subfloat)
481 lab = _("subfloat: ");
482
483 lab += floatName(params_.type);
484
485 FloatList const & floats = buffer().params().documentClass().floats();
486
487 if (params_.wide && floats.allowsWide(params_.type))
488 lab += '*';
489
490 if (params_.sideways && floats.allowsSideways(params_.type))
491 lab += _(" (sideways)");
492
493 setLabel(lab);
494 }
495
496
allowsCaptionVariation(std::string const & newtype) const497 bool InsetFloat::allowsCaptionVariation(std::string const & newtype) const
498 {
499 return !params_.subfloat && newtype != "Unnumbered";
500 }
501
502
getCaption(OutputParams const & runparams) const503 TexString InsetFloat::getCaption(OutputParams const & runparams) const
504 {
505 InsetCaption const * ins = getCaptionInset();
506 if (ins == 0)
507 return TexString();
508
509 otexstringstream os;
510 ins->getArgs(os, runparams);
511
512 if (!runparams.nice)
513 // increase TexRow precision in non-nice mode
514 os << safebreakln;
515 os << '[';
516 otexstringstream os2;
517 ins->getArgument(os2, runparams);
518 TexString ts = os2.release();
519 docstring & arg = ts.str;
520 // Protect ']'
521 if (arg.find(']') != docstring::npos)
522 arg = '{' + arg + '}';
523 os << move(ts);
524 os << ']';
525 if (!runparams.nice)
526 os << safebreakln;
527 return os.release();
528 }
529
530
string2params(string const & in,InsetFloatParams & params)531 void InsetFloat::string2params(string const & in, InsetFloatParams & params)
532 {
533 params = InsetFloatParams();
534 if (in.empty())
535 return;
536
537 istringstream data(in);
538 Lexer lex;
539 lex.setStream(data);
540 lex.setContext("InsetFloat::string2params");
541 params.read(lex);
542 }
543
544
params2string(InsetFloatParams const & params)545 string InsetFloat::params2string(InsetFloatParams const & params)
546 {
547 ostringstream data;
548 params.write(data);
549 return data.str();
550 }
551
552
553 } // namespace lyx
554