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"
22 #include "Dimension.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "InsetList.h"
29 #include "MetricsInfo.h"
30 #include "output_latex.h"
31 #include "output_xhtml.h"
32 #include "OutputParams.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), type_(type)
60 setFrameColor(Color_collapsableframe);
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::setCustomLabel(docstring const & label)
92 custom_label_ = translateIfPossible(label);
96 void InsetCaption::addToToc(DocIterator const & cpit, bool output_active,
97 UpdateType utype, TocBackend & backend) const
99 string const & type = floattype_.empty() ? "senseless" : floattype_;
100 DocIterator pit = cpit;
101 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
102 int length = (utype == OutputUpdate) ?
103 // For output (e.g. xhtml) all (bug #8603) or nothing
104 (output_active ? INT_MAX : 0) :
105 // TOC for LyX interface
110 text().forOutliner(str, length);
112 backend.builder(type).captionItem(pit, str, output_active);
113 // Proceed with the rest of the inset.
114 InsetText::addToToc(cpit, output_active, utype, backend);
118 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
120 FontInfo tmpfont = mi.base.font;
121 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
122 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
123 // add some space to separate the label from the inset text
124 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
125 dim.wid = labelwidth_;
127 // Correct for button and label width
128 mi.base.textwidth -= dim.wid;
129 InsetText::metrics(mi, textdim);
130 mi.base.font = tmpfont;
131 mi.base.textwidth += dim.wid;
132 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
133 dim.asc = textdim.asc;
134 dim.wid += textdim.wid;
138 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
140 TextMetrics & tm = pi.base.bv->textMetrics(&text());
141 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
142 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
143 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
147 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
149 // We must draw the label, we should get the label string
150 // from the enclosing float inset.
151 // The question is: Who should draw the label, the caption inset,
152 // the text inset or the paragraph?
153 // We should also draw the float number (Lgb)
155 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
157 FontInfo tmpfont = pi.base.font;
158 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
159 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
160 int const xx = x + TEXT_TO_INSET_OFFSET;
161 pi.pain.text(xx, y, full_label_, pi.base.font);
162 InsetText::draw(pi, x + labelwidth_, y);
163 pi.base.font = tmpfont;
167 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
170 InsetText::edit(cur, front, entry_from);
174 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
177 return InsetText::editXY(cur, x, y);
181 bool InsetCaption::insetAllowed(InsetCode code) const
184 // code that is not allowed in a caption
195 return InsetText::insetAllowed(code);
200 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
202 switch (cmd.action()) {
204 case LFUN_INSET_MODIFY: {
205 if (cmd.getArg(0) == "changetype") {
206 cur.recordUndoInset(this);
207 type_ = cmd.getArg(1);
208 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(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 InsetText::latex(os, runparams);
267 // Backwards compatibility: We always had a linebreak after
268 // the caption (see #8514)
270 runparams_in.encoding = runparams.encoding;
274 int InsetCaption::plaintext(odocstringstream & os,
275 OutputParams const & runparams, size_t max_length) const
277 os << '[' << full_label_ << "\n";
278 InsetText::plaintext(os, runparams, max_length);
281 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
285 int InsetCaption::docbook(odocstream & os,
286 OutputParams const & runparams) const
290 ret = InsetText::docbook(os, runparams);
296 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
298 if (rp.html_disable_captions)
300 InsetLayout const & il = getLayout();
301 string const & tag = il.htmltag();
302 string attr = il.htmlattr();
303 if (!type_.empty()) {
304 string const our_class = "float-caption-" + type_;
305 size_t const loc = attr.find("class='");
306 if (loc != string::npos)
307 attr.insert(loc + 7, our_class + " ");
309 attr = attr + " class='" + our_class + "'";
311 xs << html::StartTag(tag, attr);
312 docstring def = getCaptionAsHTML(xs, rp);
313 xs << html::EndTag(tag);
318 void InsetCaption::getArgument(otexstream & os,
319 OutputParams const & runparams) const
321 InsetLayout const & il = getLayout();
323 if (!il.leftdelim().empty())
324 os << il.leftdelim();
326 OutputParams rp = runparams;
329 if (il.isNeedProtect())
330 rp.moving_arg = true;
332 rp.par_end = paragraphs().size();
334 // Output the contents of the inset
335 if (!paragraphs().empty())
336 os.texrow().forceStart(paragraphs()[0].id(), 0);
337 latexParagraphs(buffer(), text(), os, rp);
338 runparams.encoding = rp.encoding;
340 if (!il.rightdelim().empty())
341 os << il.rightdelim();
345 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
346 OutputParams const & runparams) const
348 os << full_label_ << ' ';
349 odocstringstream ods;
350 int const retval = InsetText::plaintext(ods, runparams);
356 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
357 OutputParams const & runparams) const
359 xs << full_label_ << ' ';
360 InsetText::XHTMLOptions const opts =
361 InsetText::WriteLabel | InsetText::WriteInnerTag;
362 return InsetText::insetAsXHTML(xs, runparams, opts);
366 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
368 Buffer const & master = *buffer().masterBuffer();
369 DocumentClass const & tclass = master.params().documentClass();
370 string const & lang = it.paragraph().getParLanguage(master.params())->code();
371 Counters & cnts = tclass.counters();
372 string const & type = cnts.current_float();
373 if (utype == OutputUpdate) {
374 // counters are local to the caption
375 cnts.saveLastCounter();
377 // Memorize type for addToToc().
379 if (type.empty() || type == "senseless")
380 full_label_ = master.B_("Senseless!!! ");
382 // FIXME: life would be _much_ simpler if listings was
383 // listed in Floating.
385 if (type == "listing")
386 name = master.B_("Listing");
388 name = master.B_(tclass.floats().getType(type).name());
389 docstring counter = from_utf8(type);
390 if ((is_subfloat_ = cnts.isSubfloat())) {
391 // only standard captions allowed in subfloats
393 counter = "sub-" + from_utf8(type);
394 name = bformat(_("Sub-%1$s"),
395 master.B_(tclass.floats().getType(type).name()));
398 docstring const lstring = getLayout().labelstring();
399 docstring const labelstring = isAscii(lstring) ?
400 master.B_(to_ascii(lstring)) : lstring;
401 if (cnts.hasCounter(counter)) {
402 // for longtables, we step the counter upstream
403 if (!cnts.isLongtable())
404 cnts.step(counter, utype);
405 sec = cnts.theCounter(counter, lang);
407 if (labelstring != master.B_("standard")) {
409 sec += from_ascii(" ");
410 sec += bformat(from_ascii("(%1$s)"), labelstring);
413 full_label_ = bformat(from_ascii("%1$s %2$s: "), name, sec);
415 full_label_ = bformat(from_ascii("%1$s #: "), name);
418 // Do the real work now.
419 InsetText::updateBuffer(it, utype);
420 if (utype == OutputUpdate)
421 cnts.restoreLastCounter();
425 string InsetCaption::contextMenuName() const
427 return "context-caption";