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 "TextClass.h"
35 #include "TextMetrics.h"
36 #include "TocBackend.h"
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
47 using namespace lyx::support;
52 InsetCaption::InsetCaption(Buffer * buf, string const & type)
53 : InsetText(buf, InsetText::PlainLayout), type_(type)
55 setAutoBreakRows(true);
57 setFrameColor(Color_collapsableframe);
61 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 if (!isAscii(label) || label.empty())
92 // This must be a user defined layout. We cannot translate
93 // this, since gettext accepts only ascii keys.
94 custom_label_ = label;
96 custom_label_ = _(to_ascii(label));
100 void InsetCaption::addToToc(DocIterator const & cpit, bool output_active) const
102 if (floattype_.empty())
105 DocIterator pit = cpit;
106 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
108 Toc & toc = buffer().tocBackend().toc(floattype_);
109 docstring str = full_label_;
110 int length = output_active ? INT_MAX : TOC_ENTRY_LENGTH;
111 text().forToc(str, length);
112 toc.push_back(TocItem(pit, 0, str, output_active));
114 // Proceed with the rest of the inset.
115 InsetText::addToToc(cpit, output_active);
119 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
121 FontInfo tmpfont = mi.base.font;
122 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
123 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
124 // add some space to separate the label from the inset text
125 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
126 dim.wid = labelwidth_;
128 // Correct for button and label width
129 mi.base.textwidth -= dim.wid;
130 InsetText::metrics(mi, textdim);
131 mi.base.font = tmpfont;
132 mi.base.textwidth += dim.wid;
133 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
134 dim.asc = textdim.asc;
135 dim.wid += textdim.wid;
139 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
141 TextMetrics & tm = pi.base.bv->textMetrics(&text());
142 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
143 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
144 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
148 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
150 // We must draw the label, we should get the label string
151 // from the enclosing float inset.
152 // The question is: Who should draw the label, the caption inset,
153 // the text inset or the paragraph?
154 // We should also draw the float number (Lgb)
156 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
158 FontInfo tmpfont = pi.base.font;
159 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
160 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
161 int const xx = x + TEXT_TO_INSET_OFFSET;
162 pi.pain.text(xx, y, full_label_, pi.base.font);
163 InsetText::draw(pi, x + labelwidth_, y);
164 pi.base.font = tmpfont;
168 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
171 InsetText::edit(cur, front, entry_from);
175 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
178 return InsetText::editXY(cur, x, y);
182 bool InsetCaption::insetAllowed(InsetCode code) const
185 // code that is not allowed in a caption
196 return InsetText::insetAllowed(code);
201 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
203 switch (cmd.action()) {
205 case LFUN_INSET_MODIFY: {
206 string const first_arg = cmd.getArg(0);
207 bool const change_type = first_arg == "changetype";
209 cur.recordUndoInset(ATOMIC_UNDO, this);
210 type_ = cmd.getArg(1);
211 cur.forceBufferUpdate();
217 InsetText::doDispatch(cur, cmd);
223 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
224 FuncStatus & status) const
226 switch (cmd.action()) {
228 case LFUN_INSET_MODIFY: {
229 string const first_arg = cmd.getArg(0);
230 if (first_arg == "changetype") {
231 string const type = cmd.getArg(1);
232 status.setOnOff(type == type_);
233 bool varia = type != "LongTableNoNumber";
234 // check if the immediate parent inset allows caption variation
235 if (cur.depth() > 1) {
236 if (&cur[cur.depth() - 2].inset())
237 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
239 status.setEnabled(varia
240 && buffer().params().documentClass().hasInsetLayout(
241 from_ascii("Caption:" + type)));
244 return InsetText::getStatus(cur, cmd, status);
247 case LFUN_PARAGRAPH_BREAK:
248 status.setEnabled(false);
251 case LFUN_INSET_TOGGLE:
252 // pass back to owner
257 return InsetText::getStatus(cur, cmd, status);
262 void InsetCaption::latex(otexstream & os,
263 OutputParams const & runparams_in) const
265 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
266 // caption is output as an optional argument
268 // This is a bit too simplistic to take advantage of
269 // caption options we must add more later. (Lgb)
270 // This code is currently only able to handle the simple
271 // \caption{...}, later we will make it take advantage
272 // of the one of the caption packages. (Lgb)
273 OutputParams runparams = runparams_in;
274 // FIXME: actually, it is moving only when there is no
275 // optional argument.
276 runparams.moving_arg = !runparams.inTableCell;
277 InsetText::latex(os, runparams);
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 int InsetCaption::docbook(odocstream & os,
294 OutputParams const & runparams) const
298 ret = InsetText::docbook(os, runparams);
304 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
306 if (rp.html_disable_captions)
308 InsetLayout const & il = getLayout();
309 string const tag = il.htmltag();
310 string attr = il.htmlattr();
311 if (!type_.empty()) {
312 string const our_class = "float-caption-" + type_;
313 size_t const loc = attr.find("class='");
314 if (loc != string::npos)
315 attr.insert(loc + 1, our_class);
317 xs << html::StartTag(tag, attr);
318 docstring def = getCaptionAsHTML(xs, rp);
319 xs << html::EndTag(tag);
324 void InsetCaption::getArgument(otexstream & os,
325 OutputParams const & runparams) const
327 InsetLayout const & il = getLayout();
329 if (!il.leftdelim().empty())
330 os << il.leftdelim();
332 OutputParams rp = runparams;
335 if (il.isNeedProtect())
336 rp.moving_arg = true;
338 rp.par_end = paragraphs().size();
340 // Output the contents of the inset
341 latexParagraphs(buffer(), text(), os, rp);
342 runparams.encoding = rp.encoding;
344 if (!il.rightdelim().empty())
345 os << il.rightdelim();
349 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
350 OutputParams const & runparams) const
352 os << full_label_ << ' ';
353 odocstringstream ods;
354 int const retval = InsetText::plaintext(ods, runparams);
360 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
361 OutputParams const & runparams) const
363 xs << full_label_ << ' ';
364 InsetText::XHTMLOptions const opts =
365 InsetText::WriteLabel | InsetText::WriteInnerTag;
366 return InsetText::insetAsXHTML(xs, runparams, opts);
370 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
372 Buffer const & master = *buffer().masterBuffer();
373 DocumentClass const & tclass = master.params().documentClass();
374 string const & lang = it.paragraph().getParLanguage(master.params())->code();
375 Counters & cnts = tclass.counters();
376 string const & type = cnts.current_float();
377 if (utype == OutputUpdate) {
378 // counters are local to the caption
379 cnts.saveLastCounter();
381 // Memorize type for addToToc().
384 full_label_ = master.B_("Senseless!!! ");
386 // FIXME: life would be _much_ simpler if listings was
387 // listed in Floating.
389 if (type == "listing")
390 name = master.B_("Listing");
392 name = master.B_(tclass.floats().getType(type).name());
393 docstring counter = from_utf8(type);
394 if (cnts.isSubfloat()) {
395 // only standard captions allowed in subfloats
397 counter = "sub-" + from_utf8(type);
398 name = bformat(_("Sub-%1$s"),
399 master.B_(tclass.floats().getType(type).name()));
402 if (cnts.hasCounter(counter)) {
403 cnts.step(counter, utype);
404 sec = cnts.theCounter(counter, lang);
406 if (getLayout().labelstring() != master.B_("standard")) {
408 sec += from_ascii(" ");
409 sec += bformat(from_ascii("(%1$s)"), getLayout().labelstring());
412 full_label_ = bformat(from_ascii("%1$s %2$s:"), name, sec);
414 full_label_ = bformat(from_ascii("%1$s #:"), name);
417 // Do the real work now.
418 InsetText::updateBuffer(it, utype);
419 if (utype == OutputUpdate)
420 cnts.restoreLastCounter();
424 string InsetCaption::contextMenuName() const
426 return "context-caption";