2 * \file InsetCaption.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "InsetCaption.h"
14 #include "InsetFloat.h"
15 #include "InsetWrap.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
21 #include "Dimension.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetList.h"
28 #include "MetricsInfo.h"
29 #include "output_latex.h"
30 #include "output_xhtml.h"
31 #include "OutputParams.h"
32 #include "Paragraph.h"
33 #include "ParIterator.h"
35 #include "texstream.h"
36 #include "TextClass.h"
37 #include "TextMetrics.h"
38 #include "TocBackend.h"
40 #include "frontends/FontMetrics.h"
41 #include "frontends/Painter.h"
43 #include "support/gettext.h"
44 #include "support/lstrings.h"
49 using namespace lyx::support;
54 InsetCaption::InsetCaption(Buffer * buf, string const & type)
55 : InsetText(buf, InsetText::PlainLayout),
56 labelwidth_(0), is_subfloat_(false), type_(type)
59 setFrameColor(Color_collapsibleframe);
63 void InsetCaption::write(ostream & os) const
73 docstring InsetCaption::layoutName() const
76 return from_ascii("Caption");
77 return from_utf8("Caption:" + type_);
81 void InsetCaption::cursorPos(BufferView const & bv,
82 CursorSlice const & sl, bool boundary, int & x, int & y) const
84 InsetText::cursorPos(bv, sl, boundary, x, y);
89 void InsetCaption::addToToc(DocIterator const & cpit, bool output_active,
90 UpdateType utype, TocBackend & backend) const
92 string const & type = floattype_.empty() ? "senseless" : floattype_;
93 DocIterator pit = cpit;
94 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
95 int length = (utype == OutputUpdate) ?
96 // For output (e.g. xhtml) all (bug #8603) or nothing
97 (output_active ? INT_MAX : 0) :
98 // TOC for LyX interface
103 text().forOutliner(str, length);
105 backend.builder(type).captionItem(pit, str, output_active);
106 // Proceed with the rest of the inset.
107 InsetText::addToToc(cpit, output_active, utype, backend);
111 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
113 FontInfo tmpfont = mi.base.font;
114 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
115 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
116 // add some space to separate the label from the inset text
117 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
118 dim.wid = labelwidth_;
120 // Correct for button and label width
121 mi.base.textwidth -= dim.wid;
122 InsetText::metrics(mi, textdim);
123 mi.base.font = tmpfont;
124 mi.base.textwidth += dim.wid;
125 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
126 dim.asc = textdim.asc;
127 dim.wid += textdim.wid;
131 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
133 TextMetrics & tm = pi.base.bv->textMetrics(&text());
134 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
135 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
136 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
140 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
142 // We must draw the label, we should get the label string
143 // from the enclosing float inset.
144 // The question is: Who should draw the label, the caption inset,
145 // the text inset or the paragraph?
146 // We should also draw the float number (Lgb)
148 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
150 FontInfo tmpfont = pi.base.font;
151 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
152 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
153 int const xx = x + TEXT_TO_INSET_OFFSET;
154 pi.pain.text(xx, y, full_label_, pi.base.font);
155 InsetText::draw(pi, x + labelwidth_, y);
156 pi.base.font = tmpfont;
160 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
163 InsetText::edit(cur, front, entry_from);
167 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
170 return InsetText::editXY(cur, x, y);
174 bool InsetCaption::insetAllowed(InsetCode code) const
177 // code that is not allowed in a caption
188 return InsetText::insetAllowed(code);
193 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
195 switch (cmd.action()) {
197 case LFUN_INSET_MODIFY: {
198 if (cmd.getArg(0) == "changetype") {
199 cur.recordUndoInset(this);
200 type_ = cmd.getArg(1);
201 cur.forceBufferUpdate();
209 InsetText::doDispatch(cur, cmd);
215 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
216 FuncStatus & status) const
218 switch (cmd.action()) {
220 case LFUN_INSET_MODIFY: {
221 string const first_arg = cmd.getArg(0);
222 if (first_arg == "changetype") {
223 string const type = cmd.getArg(1);
224 status.setOnOff(type == type_);
225 bool varia = type != "Unnumbered";
226 // check if the immediate parent inset allows caption variation
227 if (cur.depth() > 1) {
228 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
230 status.setEnabled(varia
231 && buffer().params().documentClass().hasInsetLayout(
232 from_ascii("Caption:" + type)));
235 return InsetText::getStatus(cur, cmd, status);
238 case LFUN_INSET_TOGGLE:
239 // pass back to owner
244 return InsetText::getStatus(cur, cmd, status);
249 void InsetCaption::latex(otexstream & os,
250 OutputParams const & runparams_in) const
252 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
253 // caption is output as an optional argument
255 // This is a bit too simplistic to take advantage of
256 // caption options we must add more later. (Lgb)
257 // This code is currently only able to handle the simple
258 // \caption{...}, later we will make it take advantage
259 // of the one of the caption packages. (Lgb)
260 OutputParams runparams = runparams_in;
261 // Some fragile commands (labels, index entries)
262 // are output after the caption (#2154)
263 runparams.postpone_fragile_stuff = true;
264 InsetText::latex(os, runparams);
265 if (!runparams.post_macro.empty()) {
266 // Output the stored fragile commands (labels, indices etc.)
267 // that need to be output after the caption.
268 os << runparams.post_macro;
269 runparams.post_macro.clear();
271 // Backwards compatibility: We always had a linebreak after
272 // the caption (see #8514)
274 runparams_in.encoding = runparams.encoding;
278 int InsetCaption::plaintext(odocstringstream & os,
279 OutputParams const & runparams, size_t max_length) const
281 os << '[' << full_label_ << "\n";
282 InsetText::plaintext(os, runparams, max_length);
285 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
289 int InsetCaption::docbook(odocstream & os,
290 OutputParams const & runparams) const
294 ret = InsetText::docbook(os, runparams);
300 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
302 if (rp.html_disable_captions)
304 InsetLayout const & il = getLayout();
305 string const & tag = il.htmltag();
306 string attr = il.htmlattr();
307 if (!type_.empty()) {
308 string const our_class = "float-caption-" + type_;
309 size_t const loc = attr.find("class='");
310 if (loc != string::npos)
311 attr.insert(loc + 7, our_class + " ");
313 attr = attr + " class='" + our_class + "'";
315 xs << html::StartTag(tag, attr);
316 docstring def = getCaptionAsHTML(xs, rp);
317 xs << html::EndTag(tag);
322 void InsetCaption::getArgument(otexstream & os,
323 OutputParams const & runparams) const
325 InsetLayout const & il = getLayout();
327 if (!il.leftdelim().empty())
328 os << il.leftdelim();
330 OutputParams rp = runparams;
333 if (il.isNeedProtect())
334 rp.moving_arg = true;
335 if (il.isNeedMBoxProtect())
338 rp.par_end = paragraphs().size();
340 // Output the contents of the inset
341 if (!paragraphs().empty())
342 os.texrow().forceStart(paragraphs()[0].id(), 0);
343 latexParagraphs(buffer(), text(), os, rp);
344 runparams.encoding = rp.encoding;
346 if (!il.rightdelim().empty())
347 os << il.rightdelim();
351 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
352 OutputParams const & runparams) const
354 os << full_label_ << ' ';
355 odocstringstream ods;
356 int const retval = InsetText::plaintext(ods, runparams);
362 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
363 OutputParams const & runparams) const
365 xs << full_label_ << ' ';
366 InsetText::XHTMLOptions const opts =
367 InsetText::WriteLabel | InsetText::WriteInnerTag;
368 return InsetText::insetAsXHTML(xs, runparams, opts);
372 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
374 Buffer const & master = *buffer().masterBuffer();
375 DocumentClass const & tclass = master.params().documentClass();
376 string const & lang = it.paragraph().getParLanguage(master.params())->code();
377 Counters & cnts = tclass.counters();
378 string const & type = cnts.current_float();
379 if (utype == OutputUpdate) {
380 // counters are local to the caption
381 cnts.saveLastCounter();
383 // Memorize type for addToToc().
385 if (type.empty() || type == "senseless")
386 full_label_ = master.B_("Senseless!!! ");
388 // FIXME: life would be _much_ simpler if listings was
389 // listed in Floating.
391 if (type == "listing")
392 name = master.B_("Listing");
394 name = master.B_(tclass.floats().getType(type).name());
395 docstring counter = from_utf8(type);
396 is_subfloat_ = cnts.isSubfloat();
398 // only standard captions allowed in subfloats
400 counter = "sub-" + from_utf8(type);
401 name = bformat(_("Sub-%1$s"),
402 master.B_(tclass.floats().getType(type).name()));
405 docstring const lstring = getLayout().labelstring();
406 docstring const labelstring = isAscii(lstring) ?
407 master.B_(to_ascii(lstring)) : lstring;
408 if (cnts.hasCounter(counter)) {
409 // for longtables, we step the counter upstream
410 if (!cnts.isLongtable())
411 cnts.step(counter, utype);
412 sec = cnts.theCounter(counter, lang);
414 if (labelstring != master.B_("standard")) {
416 sec += from_ascii(" ");
417 sec += bformat(from_ascii("(%1$s)"), labelstring);
420 full_label_ = bformat(from_ascii("%1$s %2$s: "), name, sec);
422 full_label_ = bformat(from_ascii("%1$s #: "), name);
425 // Do the real work now.
426 InsetText::updateBuffer(it, utype);
427 if (utype == OutputUpdate)
428 cnts.restoreLastCounter();
432 string InsetCaption::contextMenuName() const
434 return "context-caption";