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 text().forToc(str, TOC_ENTRY_LENGTH);
111 toc.push_back(TocItem(pit, 0, str, output_active));
113 // Proceed with the rest of the inset.
114 InsetText::addToToc(cpit, output_active);
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 string const first_arg = cmd.getArg(0);
206 bool const change_type = first_arg == "changetype";
208 cur.recordUndoInset(ATOMIC_UNDO, this);
209 type_ = cmd.getArg(1);
210 cur.forceBufferUpdate();
216 InsetText::doDispatch(cur, cmd);
222 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
223 FuncStatus & status) const
225 switch (cmd.action()) {
227 case LFUN_INSET_MODIFY: {
228 string const first_arg = cmd.getArg(0);
229 if (first_arg == "changetype") {
230 string const type = cmd.getArg(1);
231 status.setOnOff(type == type_);
233 // check if the immediate parent inset allows caption variation
234 if (cur.depth() > 1) {
235 if (&cur[cur.depth() - 2].inset()
236 && !cur[cur.depth() - 2].inset().allowsCaptionVariation())
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 return 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 if (cnts.hasCounter(counter)) {
402 cnts.step(counter, utype);
403 sec = cnts.theCounter(counter, lang);
405 if (getLayout().labelstring() != master.B_("standard")) {
407 sec += from_ascii(" ");
408 sec += bformat(from_ascii("(%1$s)"), getLayout().labelstring());
411 full_label_ = bformat(from_ascii("%1$s %2$s:"), name, sec);
413 full_label_ = bformat(from_ascii("%1$s #:"), name);
416 // Do the real work now.
417 InsetText::updateBuffer(it, utype);
418 if (utype == OutputUpdate)
419 cnts.restoreLastCounter();
423 string InsetCaption::contextMenuName() const
425 return "context-caption";