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"
35 #include "TextClass.h"
36 #include "TextMetrics.h"
37 #include "TocBackend.h"
39 #include "frontends/FontMetrics.h"
40 #include "frontends/Painter.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
48 using namespace lyx::support;
53 InsetCaption::InsetCaption(Buffer * buf, string const & type)
54 : InsetText(buf, InsetText::PlainLayout), type_(type)
56 setAutoBreakRows(true);
58 setFrameColor(Color_collapsableframe);
62 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) const
98 if (floattype_.empty())
101 DocIterator pit = cpit;
102 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
104 Toc & toc = buffer().tocBackend().toc(floattype_);
105 docstring str = full_label_;
106 int length = output_active ? INT_MAX : TOC_ENTRY_LENGTH;
107 text().forOutliner(str, length);
108 toc.push_back(TocItem(pit, 0, str, output_active));
110 // Proceed with the rest of the inset.
111 InsetText::addToToc(cpit, output_active);
115 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
117 FontInfo tmpfont = mi.base.font;
118 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
119 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
120 // add some space to separate the label from the inset text
121 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
122 dim.wid = labelwidth_;
124 // Correct for button and label width
125 mi.base.textwidth -= dim.wid;
126 InsetText::metrics(mi, textdim);
127 mi.base.font = tmpfont;
128 mi.base.textwidth += dim.wid;
129 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
130 dim.asc = textdim.asc;
131 dim.wid += textdim.wid;
135 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
137 TextMetrics & tm = pi.base.bv->textMetrics(&text());
138 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
139 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
140 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
144 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
146 // We must draw the label, we should get the label string
147 // from the enclosing float inset.
148 // The question is: Who should draw the label, the caption inset,
149 // the text inset or the paragraph?
150 // We should also draw the float number (Lgb)
152 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
154 FontInfo tmpfont = pi.base.font;
155 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
156 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
157 int const xx = x + TEXT_TO_INSET_OFFSET;
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(ATOMIC_UNDO, this);
204 type_ = cmd.getArg(1);
205 cur.forceBufferUpdate();
211 InsetText::doDispatch(cur, cmd);
217 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
218 FuncStatus & status) const
220 switch (cmd.action()) {
222 case LFUN_INSET_MODIFY: {
223 string const first_arg = cmd.getArg(0);
224 if (first_arg == "changetype") {
225 string const type = cmd.getArg(1);
226 status.setOnOff(type == type_);
227 bool varia = type != "LongTableNoNumber";
228 // check if the immediate parent inset allows caption variation
229 if (cur.depth() > 1) {
230 if (&cur[cur.depth() - 2].inset())
231 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
233 status.setEnabled(varia
234 && buffer().params().documentClass().hasInsetLayout(
235 from_ascii("Caption:" + type)));
238 return InsetText::getStatus(cur, cmd, status);
241 case LFUN_PARAGRAPH_BREAK:
242 status.setEnabled(false);
245 case LFUN_INSET_TOGGLE:
246 // pass back to owner
251 return InsetText::getStatus(cur, cmd, status);
256 void InsetCaption::latex(otexstream & os,
257 OutputParams const & runparams_in) const
259 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
260 // caption is output as an optional argument
262 // This is a bit too simplistic to take advantage of
263 // caption options we must add more later. (Lgb)
264 // This code is currently only able to handle the simple
265 // \caption{...}, later we will make it take advantage
266 // of the one of the caption packages. (Lgb)
267 OutputParams runparams = runparams_in;
268 // FIXME: actually, it is moving only when there is no
269 // optional argument.
270 runparams.moving_arg = !runparams.inTableCell;
271 InsetText::latex(os, runparams);
272 // Backwards compatibility: We always had a linebreak after
273 // the caption (see #8514)
275 runparams_in.encoding = runparams.encoding;
279 int InsetCaption::plaintext(odocstringstream & os,
280 OutputParams const & runparams, size_t max_length) const
282 os << '[' << full_label_ << "\n";
283 InsetText::plaintext(os, runparams, max_length);
286 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
290 int InsetCaption::docbook(odocstream & os,
291 OutputParams const & runparams) const
295 ret = InsetText::docbook(os, runparams);
301 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
303 if (rp.html_disable_captions)
305 InsetLayout const & il = getLayout();
306 string const tag = il.htmltag();
307 string attr = il.htmlattr();
308 if (!type_.empty()) {
309 string const our_class = "float-caption-" + type_;
310 size_t const loc = attr.find("class='");
311 if (loc != string::npos)
312 attr.insert(loc + 7, our_class+ " ");
314 attr = attr + " class='" + our_class + "'";
316 xs << html::StartTag(tag, attr);
317 docstring def = getCaptionAsHTML(xs, rp);
318 xs << html::EndTag(tag);
323 void InsetCaption::getArgument(otexstream & os,
324 OutputParams const & runparams) const
326 InsetLayout const & il = getLayout();
328 if (!il.leftdelim().empty())
329 os << il.leftdelim();
331 OutputParams rp = runparams;
334 if (il.isNeedProtect())
335 rp.moving_arg = true;
337 rp.par_end = paragraphs().size();
339 // Output the contents of the inset
340 latexParagraphs(buffer(), text(), os, rp);
341 runparams.encoding = rp.encoding;
343 if (!il.rightdelim().empty())
344 os << il.rightdelim();
348 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
349 OutputParams const & runparams) const
351 os << full_label_ << ' ';
352 odocstringstream ods;
353 int const retval = InsetText::plaintext(ods, runparams);
359 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
360 OutputParams const & runparams) const
362 xs << full_label_ << ' ';
363 InsetText::XHTMLOptions const opts =
364 InsetText::WriteLabel | InsetText::WriteInnerTag;
365 return InsetText::insetAsXHTML(xs, runparams, opts);
369 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
371 Buffer const & master = *buffer().masterBuffer();
372 DocumentClass const & tclass = master.params().documentClass();
373 string const & lang = it.paragraph().getParLanguage(master.params())->code();
374 Counters & cnts = tclass.counters();
375 string const & type = cnts.current_float();
376 if (utype == OutputUpdate) {
377 // counters are local to the caption
378 cnts.saveLastCounter();
380 // Memorize type for addToToc().
383 full_label_ = master.B_("Senseless!!! ");
385 // FIXME: life would be _much_ simpler if listings was
386 // listed in Floating.
388 if (type == "listing")
389 name = master.B_("Listing");
391 name = master.B_(tclass.floats().getType(type).name());
392 docstring counter = from_utf8(type);
393 if (cnts.isSubfloat()) {
394 // only standard captions allowed in subfloats
396 counter = "sub-" + from_utf8(type);
397 name = bformat(_("Sub-%1$s"),
398 master.B_(tclass.floats().getType(type).name()));
401 docstring const lstring = getLayout().labelstring();
402 docstring const labelstring = isAscii(lstring) ?
403 master.B_(to_ascii(lstring)) : lstring;
404 if (cnts.hasCounter(counter)) {
405 // for longtables, we step the counter upstream
406 if (!cnts.isLongtable())
407 cnts.step(counter, utype);
408 sec = cnts.theCounter(counter, lang);
410 if (labelstring != master.B_("standard")) {
412 sec += from_ascii(" ");
413 sec += bformat(from_ascii("(%1$s)"), labelstring);
416 full_label_ = bformat(from_ascii("%1$s %2$s:"), name, sec);
418 full_label_ = bformat(from_ascii("%1$s #:"), name);
421 // Do the real work now.
422 InsetText::updateBuffer(it, utype);
423 if (utype == OutputUpdate)
424 cnts.restoreLastCounter();
428 string InsetCaption::contextMenuName() const
430 return "context-caption";