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::setCustomLabel(docstring const & label)
91 custom_label_ = translateIfPossible(label);
95 void InsetCaption::addToToc(DocIterator const & cpit, bool output_active,
96 UpdateType utype, TocBackend & backend) const
98 string const & type = floattype_.empty() ? "senseless" : floattype_;
99 DocIterator pit = cpit;
100 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
101 int length = (utype == OutputUpdate) ?
102 // For output (e.g. xhtml) all (bug #8603) or nothing
103 (output_active ? INT_MAX : 0) :
104 // TOC for LyX interface
109 text().forOutliner(str, length);
111 backend.builder(type).captionItem(pit, str, output_active);
112 // Proceed with the rest of the inset.
113 InsetText::addToToc(cpit, output_active, utype, backend);
117 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
119 FontInfo tmpfont = mi.base.font;
120 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
121 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
122 // add some space to separate the label from the inset text
123 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
124 dim.wid = labelwidth_;
126 // Correct for button and label width
127 mi.base.textwidth -= dim.wid;
128 InsetText::metrics(mi, textdim);
129 mi.base.font = tmpfont;
130 mi.base.textwidth += dim.wid;
131 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
132 dim.asc = textdim.asc;
133 dim.wid += textdim.wid;
137 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
139 TextMetrics & tm = pi.base.bv->textMetrics(&text());
140 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
141 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
142 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
146 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
148 // We must draw the label, we should get the label string
149 // from the enclosing float inset.
150 // The question is: Who should draw the label, the caption inset,
151 // the text inset or the paragraph?
152 // We should also draw the float number (Lgb)
154 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
156 FontInfo tmpfont = pi.base.font;
157 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
158 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
159 int const xx = x + TEXT_TO_INSET_OFFSET;
160 pi.pain.text(xx, y, full_label_, pi.base.font);
161 InsetText::draw(pi, x + labelwidth_, y);
162 pi.base.font = tmpfont;
166 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
169 InsetText::edit(cur, front, entry_from);
173 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
176 return InsetText::editXY(cur, x, y);
180 bool InsetCaption::insetAllowed(InsetCode code) const
183 // code that is not allowed in a caption
194 return InsetText::insetAllowed(code);
199 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
201 switch (cmd.action()) {
203 case LFUN_INSET_MODIFY: {
204 if (cmd.getArg(0) == "changetype") {
205 cur.recordUndoInset(this);
206 type_ = cmd.getArg(1);
207 cur.forceBufferUpdate();
215 InsetText::doDispatch(cur, cmd);
221 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
222 FuncStatus & status) const
224 switch (cmd.action()) {
226 case LFUN_INSET_MODIFY: {
227 string const first_arg = cmd.getArg(0);
228 if (first_arg == "changetype") {
229 string const type = cmd.getArg(1);
230 status.setOnOff(type == type_);
231 bool varia = type != "Unnumbered";
232 // check if the immediate parent inset allows caption variation
233 if (cur.depth() > 1) {
234 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
236 status.setEnabled(varia
237 && buffer().params().documentClass().hasInsetLayout(
238 from_ascii("Caption:" + type)));
241 return InsetText::getStatus(cur, cmd, status);
244 case LFUN_INSET_TOGGLE:
245 // pass back to owner
250 return InsetText::getStatus(cur, cmd, status);
255 void InsetCaption::latex(otexstream & os,
256 OutputParams const & runparams_in) const
258 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
259 // caption is output as an optional argument
261 // This is a bit too simplistic to take advantage of
262 // caption options we must add more later. (Lgb)
263 // This code is currently only able to handle the simple
264 // \caption{...}, later we will make it take advantage
265 // of the one of the caption packages. (Lgb)
266 OutputParams runparams = runparams_in;
267 InsetText::latex(os, runparams);
268 // Backwards compatibility: We always had a linebreak after
269 // the caption (see #8514)
271 runparams_in.encoding = runparams.encoding;
275 int InsetCaption::plaintext(odocstringstream & os,
276 OutputParams const & runparams, size_t max_length) const
278 os << '[' << full_label_ << "\n";
279 InsetText::plaintext(os, runparams, max_length);
282 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
286 int InsetCaption::docbook(odocstream & os,
287 OutputParams const & runparams) const
291 ret = InsetText::docbook(os, runparams);
297 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
299 if (rp.html_disable_captions)
301 InsetLayout const & il = getLayout();
302 string const & tag = il.htmltag();
303 string attr = il.htmlattr();
304 if (!type_.empty()) {
305 string const our_class = "float-caption-" + type_;
306 size_t const loc = attr.find("class='");
307 if (loc != string::npos)
308 attr.insert(loc + 7, our_class + " ");
310 attr = attr + " class='" + our_class + "'";
312 xs << html::StartTag(tag, attr);
313 docstring def = getCaptionAsHTML(xs, rp);
314 xs << html::EndTag(tag);
319 void InsetCaption::getArgument(otexstream & os,
320 OutputParams const & runparams) const
322 InsetLayout const & il = getLayout();
324 if (!il.leftdelim().empty())
325 os << il.leftdelim();
327 OutputParams rp = runparams;
330 if (il.isNeedProtect())
331 rp.moving_arg = true;
333 rp.par_end = paragraphs().size();
335 // Output the contents of the inset
336 if (!paragraphs().empty())
337 os.texrow().forceStart(paragraphs()[0].id(), 0);
338 latexParagraphs(buffer(), text(), os, rp);
339 runparams.encoding = rp.encoding;
341 if (!il.rightdelim().empty())
342 os << il.rightdelim();
346 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
347 OutputParams const & runparams) const
349 os << full_label_ << ' ';
350 odocstringstream ods;
351 int const retval = InsetText::plaintext(ods, runparams);
357 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
358 OutputParams const & runparams) const
360 xs << full_label_ << ' ';
361 InsetText::XHTMLOptions const opts =
362 InsetText::WriteLabel | InsetText::WriteInnerTag;
363 return InsetText::insetAsXHTML(xs, runparams, opts);
367 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
369 Buffer const & master = *buffer().masterBuffer();
370 DocumentClass const & tclass = master.params().documentClass();
371 string const & lang = it.paragraph().getParLanguage(master.params())->code();
372 Counters & cnts = tclass.counters();
373 string const & type = cnts.current_float();
374 if (utype == OutputUpdate) {
375 // counters are local to the caption
376 cnts.saveLastCounter();
378 // Memorize type for addToToc().
380 if (type.empty() || type == "senseless")
381 full_label_ = master.B_("Senseless!!! ");
383 // FIXME: life would be _much_ simpler if listings was
384 // listed in Floating.
386 if (type == "listing")
387 name = master.B_("Listing");
389 name = master.B_(tclass.floats().getType(type).name());
390 docstring counter = from_utf8(type);
391 is_subfloat_ = cnts.isSubfloat();
393 // only standard captions allowed in subfloats
395 counter = "sub-" + from_utf8(type);
396 name = bformat(_("Sub-%1$s"),
397 master.B_(tclass.floats().getType(type).name()));
400 docstring const lstring = getLayout().labelstring();
401 docstring const labelstring = isAscii(lstring) ?
402 master.B_(to_ascii(lstring)) : lstring;
403 if (cnts.hasCounter(counter)) {
404 // for longtables, we step the counter upstream
405 if (!cnts.isLongtable())
406 cnts.step(counter, utype);
407 sec = cnts.theCounter(counter, lang);
409 if (labelstring != master.B_("standard")) {
411 sec += from_ascii(" ");
412 sec += bformat(from_ascii("(%1$s)"), labelstring);
415 full_label_ = bformat(from_ascii("%1$s %2$s: "), name, sec);
417 full_label_ = bformat(from_ascii("%1$s #: "), name);
420 // Do the real work now.
421 InsetText::updateBuffer(it, utype);
422 if (utype == OutputUpdate)
423 cnts.restoreLastCounter();
427 string InsetCaption::contextMenuName() const
429 return "context-caption";