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"
29 #include "MetricsInfo.h"
31 #include "output_latex.h"
32 #include "output_xhtml.h"
33 #include "Paragraph.h"
34 #include "ParIterator.h"
36 #include "texstream.h"
37 #include "TextClass.h"
38 #include "TextMetrics.h"
39 #include "TocBackend.h"
41 #include "frontends/FontMetrics.h"
42 #include "frontends/Painter.h"
44 #include "support/gettext.h"
45 #include "support/lstrings.h"
50 using namespace lyx::support;
55 InsetCaption::InsetCaption(Buffer * buf, string const & type)
56 : InsetText(buf, InsetText::PlainLayout),
57 labelwidth_(0), is_subfloat_(false), is_deleted_(false), type_(type)
60 setFrameColor(Color_collapsibleframe);
64 void InsetCaption::write(ostream & os) const
74 docstring InsetCaption::layoutName() const
77 return from_ascii("Caption");
78 return from_utf8("Caption:" + type_);
82 void InsetCaption::cursorPos(BufferView const & bv,
83 CursorSlice const & sl, bool boundary, int & x, int & y) const
85 InsetText::cursorPos(bv, sl, boundary, x, y);
90 void InsetCaption::addToToc(DocIterator const & cpit, bool output_active,
91 UpdateType utype, TocBackend & backend) const
93 string const & type = floattype_.empty() ? "senseless" : floattype_;
94 DocIterator pit = cpit;
95 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
96 int length = (utype == OutputUpdate) ?
97 // For output (e.g. xhtml) all (bug #8603) or nothing
98 (output_active ? INT_MAX : 0) :
99 // TOC for LyX interface
104 text().forOutliner(str, length);
106 backend.builder(type).captionItem(pit, str, output_active);
107 // Proceed with the rest of the inset.
108 InsetText::addToToc(cpit, output_active, utype, backend);
112 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
114 FontInfo tmpfont = mi.base.font;
115 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
116 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
117 // add some space to separate the label from the inset text
118 labelwidth_ += leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
119 dim.wid = labelwidth_;
121 // Correct for button and label width
122 mi.base.textwidth -= dim.wid;
123 InsetText::metrics(mi, textdim);
124 mi.base.font = tmpfont;
125 mi.base.textwidth += dim.wid;
126 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
127 dim.asc = textdim.asc;
128 dim.wid += textdim.wid;
132 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
134 TextMetrics & tm = pi.base.bv->textMetrics(&text());
135 int const h = tm.height() + topOffset(pi.base.bv) + bottomOffset(pi.base.bv);
136 int const yy = y - topOffset(pi.base.bv) - tm.ascent();
137 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
141 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
143 // We must draw the label, we should get the label string
144 // from the enclosing float inset.
145 // The question is: Who should draw the label, the caption inset,
146 // the text inset or the paragraph?
147 // We should also draw the float number (Lgb)
149 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
151 FontInfo tmpfont = pi.base.font;
152 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
153 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
155 pi.base.font.setStrikeout(FONT_ON);
156 else if (isChanged() && lyxrc.ct_additions_underlined)
157 pi.base.font.setUnderbar(FONT_ON);
158 int const xx = x + leftOffset(pi.base.bv);
159 pi.pain.text(xx, y, full_label_, pi.base.font);
160 InsetText::draw(pi, x + labelwidth_, y);
161 pi.base.font = tmpfont;
165 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
168 InsetText::edit(cur, front, entry_from);
172 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
175 return InsetText::editXY(cur, x, y);
179 bool InsetCaption::insetAllowed(InsetCode code) const
182 // code that is not allowed in a caption
193 return InsetText::insetAllowed(code);
198 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
200 switch (cmd.action()) {
202 case LFUN_INSET_MODIFY: {
203 if (cmd.getArg(0) == "changetype") {
204 cur.recordUndoInset(this);
205 type_ = cmd.getArg(1);
206 cur.forceBufferUpdate();
214 InsetText::doDispatch(cur, cmd);
220 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
221 FuncStatus & status) const
223 switch (cmd.action()) {
225 case LFUN_INSET_MODIFY: {
226 string const first_arg = cmd.getArg(0);
227 if (first_arg == "changetype") {
228 string const type = cmd.getArg(1);
229 status.setOnOff(type == type_);
230 bool varia = type != "Unnumbered";
231 // check if the immediate parent inset allows caption variation
232 if (cur.depth() > 1) {
233 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
235 status.setEnabled(!is_subfloat_ && varia
236 && buffer().params().documentClass().hasInsetLayout(
237 from_ascii("Caption:" + type)));
240 return InsetText::getStatus(cur, cmd, status);
243 case LFUN_INSET_TOGGLE:
244 // pass back to owner
249 return InsetText::getStatus(cur, cmd, status);
254 void InsetCaption::latex(otexstream & os,
255 OutputParams const & runparams_in) const
257 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
258 // caption is output as an optional argument
260 // This is a bit too simplistic to take advantage of
261 // caption options we must add more later. (Lgb)
262 // This code is currently only able to handle the simple
263 // \caption{...}, later we will make it take advantage
264 // of the one of the caption packages. (Lgb)
265 OutputParams runparams = runparams_in;
266 // Some fragile commands (labels, index entries)
267 // are output after the caption (#2154)
268 runparams.postpone_fragile_stuff = buffer().masterParams().postpone_fragile_content;
269 InsetText::latex(os, runparams);
270 if (!runparams.post_macro.empty()) {
271 // Output the stored fragile commands (labels, indices etc.)
272 // that need to be output after the caption.
273 os << runparams.post_macro;
274 runparams.post_macro.clear();
276 // Backwards compatibility: We always had a linebreak after
277 // the caption (see #8514)
279 runparams_in.encoding = runparams.encoding;
283 int InsetCaption::plaintext(odocstringstream & os,
284 OutputParams const & runparams, size_t max_length) const
286 os << '[' << full_label_ << "\n";
287 InsetText::plaintext(os, runparams, max_length);
290 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
294 void InsetCaption::docbook(XMLStream &, OutputParams const &) const
296 // This function should never be called (rather InsetFloat::docbook, the titles should be skipped in floats).
300 docstring InsetCaption::xhtml(XMLStream & 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 << xml::StartTag(tag, attr);
316 docstring def = getCaptionAsHTML(xs, rp);
317 xs << xml::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 void InsetCaption::getCaptionAsDocBook(XMLStream & xs,
363 OutputParams const & runparams) const
365 if (runparams.docbook_in_float)
368 // Ignore full_label_, as the DocBook processor will deal with the numbering.
369 InsetText::XHTMLOptions opts = InsetText::WriteInnerTag;
370 InsetText::docbook(xs, runparams, opts);
374 docstring InsetCaption::getCaptionAsHTML(XMLStream & xs,
375 OutputParams const & runparams) const
377 xs << full_label_ << ' ';
378 InsetText::XHTMLOptions const opts =
379 InsetText::WriteLabel | InsetText::WriteInnerTag;
380 return InsetText::insetAsXHTML(xs, runparams, opts);
384 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
386 Buffer const & master = *buffer().masterBuffer();
387 DocumentClass const & tclass = master.params().documentClass();
388 string const & lang = it.paragraph().getParLanguage(master.params())->code();
389 Counters & cnts = tclass.counters();
390 string const & type = cnts.current_float();
391 if (utype == OutputUpdate) {
392 // counters are local to the caption
393 cnts.saveLastCounter();
395 is_deleted_ = deleted;
396 // Memorize type for addToToc().
398 if (type.empty() || type == "senseless")
399 full_label_ = master.B_("Senseless!!! ");
401 // FIXME: life would be _much_ simpler if listings was
402 // listed in Floating.
404 if (type == "listing")
405 name = master.B_("Listing");
407 name = master.B_(tclass.floats().getType(type).name());
408 docstring counter = from_utf8(type);
409 is_subfloat_ = cnts.isSubfloat();
411 // only standard captions allowed in subfloats
413 counter = "sub-" + from_utf8(type);
414 name = bformat(_("Sub-%1$s"),
415 master.B_(tclass.floats().getType(type).name()));
418 docstring const lstring = getLayout().labelstring();
419 docstring const labelstring = isAscii(lstring) ?
420 master.B_(to_ascii(lstring)) : lstring;
421 if (cnts.hasCounter(counter)) {
422 int val = cnts.value(counter);
423 // for longtables, we step the counter upstream
424 if (!cnts.isLongtable())
425 cnts.step(counter, utype);
426 sec = cnts.theCounter(counter, lang);
427 if (deleted && !cnts.isLongtable())
428 // un-step after deleted counter
429 cnts.set(counter, val);
431 if (labelstring != master.B_("standard")) {
433 sec += from_ascii(" ");
434 sec += bformat(from_ascii("(%1$s)"), labelstring);
437 full_label_ = bformat(from_ascii("%1$s %2$s: "), name, sec);
439 full_label_ = bformat(from_ascii("%1$s #: "), name);
442 // Do the real work now.
443 InsetText::updateBuffer(it, utype, deleted);
444 if (utype == OutputUpdate)
445 cnts.restoreLastCounter();
449 string InsetCaption::contextMenuName() const
451 return "context-caption";