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), 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_ += leftOffset(mi.base.bv) + rightOffset(mi.base.bv);
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() + topOffset(pi.base.bv) + bottomOffset(pi.base.bv);
135 int const yy = y - topOffset(pi.base.bv) - 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);
154 pi.base.font.setStrikeout(FONT_ON);
155 else if (isChanged() && lyxrc.ct_additions_underlined)
156 pi.base.font.setUnderbar(FONT_ON);
157 int const xx = x + leftOffset(pi.base.bv);
158 pi.pain.text(xx, y, full_label_, pi.base.font);
159 InsetText::draw(pi, x + labelwidth_, y);
160 pi.base.font = tmpfont;
164 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
167 InsetText::edit(cur, front, entry_from);
171 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
174 return InsetText::editXY(cur, x, y);
178 bool InsetCaption::insetAllowed(InsetCode code) const
181 // code that is not allowed in a caption
192 return InsetText::insetAllowed(code);
197 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
199 switch (cmd.action()) {
201 case LFUN_INSET_MODIFY: {
202 if (cmd.getArg(0) == "changetype") {
203 cur.recordUndoInset(this);
204 type_ = cmd.getArg(1);
205 cur.forceBufferUpdate();
213 InsetText::doDispatch(cur, cmd);
219 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
220 FuncStatus & status) const
222 switch (cmd.action()) {
224 case LFUN_INSET_MODIFY: {
225 string const first_arg = cmd.getArg(0);
226 if (first_arg == "changetype") {
227 string const type = cmd.getArg(1);
228 status.setOnOff(type == type_);
229 bool varia = type != "Unnumbered";
230 // check if the immediate parent inset allows caption variation
231 if (cur.depth() > 1) {
232 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
234 status.setEnabled(!is_subfloat_ && varia
235 && buffer().params().documentClass().hasInsetLayout(
236 from_ascii("Caption:" + type)));
239 return InsetText::getStatus(cur, cmd, status);
242 case LFUN_INSET_TOGGLE:
243 // pass back to owner
248 return InsetText::getStatus(cur, cmd, status);
253 void InsetCaption::latex(otexstream & os,
254 OutputParams const & runparams_in) const
256 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
257 // caption is output as an optional argument
259 // This is a bit too simplistic to take advantage of
260 // caption options we must add more later. (Lgb)
261 // This code is currently only able to handle the simple
262 // \caption{...}, later we will make it take advantage
263 // of the one of the caption packages. (Lgb)
264 OutputParams runparams = runparams_in;
265 // Some fragile commands (labels, index entries)
266 // are output after the caption (#2154)
267 runparams.postpone_fragile_stuff = buffer().masterParams().postpone_fragile_content;
268 InsetText::latex(os, runparams);
269 if (!runparams.post_macro.empty()) {
270 // Output the stored fragile commands (labels, indices etc.)
271 // that need to be output after the caption.
272 os << runparams.post_macro;
273 runparams.post_macro.clear();
275 // Backwards compatibility: We always had a linebreak after
276 // the caption (see #8514)
278 runparams_in.encoding = runparams.encoding;
282 int InsetCaption::plaintext(odocstringstream & os,
283 OutputParams const & runparams, size_t max_length) const
285 os << '[' << full_label_ << "\n";
286 InsetText::plaintext(os, runparams, max_length);
289 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
293 void InsetCaption::docbook(XMLStream &, OutputParams const &) const
295 // This function should never be called (rather InsetFloat::docbook, the titles should be skipped in floats).
299 docstring InsetCaption::xhtml(XMLStream & xs, OutputParams const & rp) const
301 if (rp.html_disable_captions)
303 InsetLayout const & il = getLayout();
304 string const & tag = il.htmltag();
305 string attr = il.htmlattr();
306 if (!type_.empty()) {
307 string const our_class = "float-caption-" + type_;
308 size_t const loc = attr.find("class='");
309 if (loc != string::npos)
310 attr.insert(loc + 7, our_class + " ");
312 attr = attr + " class='" + our_class + "'";
314 xs << xml::StartTag(tag, attr);
315 docstring def = getCaptionAsHTML(xs, rp);
316 xs << xml::EndTag(tag);
321 void InsetCaption::getArgument(otexstream & os,
322 OutputParams const & runparams) const
324 InsetLayout const & il = getLayout();
326 if (!il.leftdelim().empty())
327 os << il.leftdelim();
329 OutputParams rp = runparams;
332 if (il.isNeedProtect())
333 rp.moving_arg = true;
334 if (il.isNeedMBoxProtect())
337 rp.par_end = paragraphs().size();
339 // Output the contents of the inset
340 if (!paragraphs().empty())
341 os.texrow().forceStart(paragraphs()[0].id(), 0);
342 latexParagraphs(buffer(), text(), os, rp);
343 runparams.encoding = rp.encoding;
345 if (!il.rightdelim().empty())
346 os << il.rightdelim();
350 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
351 OutputParams const & runparams) const
353 os << full_label_ << ' ';
354 odocstringstream ods;
355 int const retval = InsetText::plaintext(ods, runparams);
361 void InsetCaption::getCaptionAsDocBook(XMLStream & xs,
362 OutputParams const & runparams) const
364 if (runparams.docbook_in_float)
367 // Ignore full_label_, as the DocBook processor will deal with the numbering.
368 InsetText::XHTMLOptions opts = InsetText::WriteInnerTag;
369 InsetText::docbook(xs, runparams, opts);
373 docstring InsetCaption::getCaptionAsHTML(XMLStream & xs,
374 OutputParams const & runparams) const
376 xs << full_label_ << ' ';
377 InsetText::XHTMLOptions const opts =
378 InsetText::WriteLabel | InsetText::WriteInnerTag;
379 return InsetText::insetAsXHTML(xs, runparams, opts);
383 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
385 Buffer const & master = *buffer().masterBuffer();
386 DocumentClass const & tclass = master.params().documentClass();
387 string const & lang = it.paragraph().getParLanguage(master.params())->code();
388 Counters & cnts = tclass.counters();
389 string const & type = cnts.current_float();
390 if (utype == OutputUpdate) {
391 // counters are local to the caption
392 cnts.saveLastCounter();
394 is_deleted_ = deleted;
395 // Memorize type for addToToc().
397 if (type.empty() || type == "senseless")
398 full_label_ = master.B_("Senseless!!! ");
400 // FIXME: life would be _much_ simpler if listings was
401 // listed in Floating.
403 if (type == "listing")
404 name = master.B_("Listing");
406 name = master.B_(tclass.floats().getType(type).name());
407 docstring counter = from_utf8(type);
408 is_subfloat_ = cnts.isSubfloat();
410 // only standard captions allowed in subfloats
412 counter = "sub-" + from_utf8(type);
413 name = bformat(_("Sub-%1$s"),
414 master.B_(tclass.floats().getType(type).name()));
417 docstring const lstring = getLayout().labelstring();
418 docstring const labelstring = isAscii(lstring) ?
419 master.B_(to_ascii(lstring)) : lstring;
420 if (cnts.hasCounter(counter)) {
421 int val = cnts.value(counter);
422 // for longtables, we step the counter upstream
423 if (!cnts.isLongtable())
424 cnts.step(counter, utype);
425 sec = cnts.theCounter(counter, lang);
426 if (deleted && !cnts.isLongtable())
427 // un-step after deleted counter
428 cnts.set(counter, val);
430 if (labelstring != master.B_("standard")) {
432 sec += from_ascii(" ");
433 sec += bformat(from_ascii("(%1$s)"), labelstring);
436 full_label_ = bformat(from_ascii("%1$s %2$s: "), name, sec);
438 full_label_ = bformat(from_ascii("%1$s #: "), name);
441 // Do the real work now.
442 InsetText::updateBuffer(it, utype, deleted);
443 if (utype == OutputUpdate)
444 cnts.restoreLastCounter();
448 string InsetCaption::contextMenuName() const
450 return "context-caption";