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 if (!isAscii(label) || label.empty())
93 // This must be a user defined layout. We cannot translate
94 // this, since gettext accepts only ascii keys.
95 custom_label_ = label;
97 custom_label_ = _(to_ascii(label));
101 void InsetCaption::addToToc(DocIterator const & cpit, bool output_active) const
103 if (floattype_.empty())
106 DocIterator pit = cpit;
107 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
109 Toc & toc = buffer().tocBackend().toc(floattype_);
110 docstring str = full_label_;
111 int length = output_active ? INT_MAX : TOC_ENTRY_LENGTH;
112 text().forToc(str, length);
113 toc.push_back(TocItem(pit, 0, str, output_active));
115 // Proceed with the rest of the inset.
116 InsetText::addToToc(cpit, output_active);
120 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
122 FontInfo tmpfont = mi.base.font;
123 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
124 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
125 // add some space to separate the label from the inset text
126 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
127 dim.wid = labelwidth_;
129 // Correct for button and label width
130 mi.base.textwidth -= dim.wid;
131 InsetText::metrics(mi, textdim);
132 mi.base.font = tmpfont;
133 mi.base.textwidth += dim.wid;
134 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
135 dim.asc = textdim.asc;
136 dim.wid += textdim.wid;
140 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
142 TextMetrics & tm = pi.base.bv->textMetrics(&text());
143 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
144 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
145 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
149 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
151 // We must draw the label, we should get the label string
152 // from the enclosing float inset.
153 // The question is: Who should draw the label, the caption inset,
154 // the text inset or the paragraph?
155 // We should also draw the float number (Lgb)
157 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
159 FontInfo tmpfont = pi.base.font;
160 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
161 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
162 int const xx = x + TEXT_TO_INSET_OFFSET;
163 pi.pain.text(xx, y, full_label_, pi.base.font);
164 InsetText::draw(pi, x + labelwidth_, y);
165 pi.base.font = tmpfont;
169 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
172 InsetText::edit(cur, front, entry_from);
176 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
179 return InsetText::editXY(cur, x, y);
183 bool InsetCaption::insetAllowed(InsetCode code) const
186 // code that is not allowed in a caption
197 return InsetText::insetAllowed(code);
202 void InsetCaption::doDispatch(Cursor & cur, FuncRequest & cmd)
204 switch (cmd.action()) {
206 case LFUN_INSET_MODIFY: {
207 string const first_arg = cmd.getArg(0);
208 bool const change_type = first_arg == "changetype";
210 cur.recordUndoInset(ATOMIC_UNDO, this);
211 type_ = cmd.getArg(1);
212 cur.forceBufferUpdate();
218 InsetText::doDispatch(cur, cmd);
224 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
225 FuncStatus & status) const
227 switch (cmd.action()) {
229 case LFUN_INSET_MODIFY: {
230 string const first_arg = cmd.getArg(0);
231 if (first_arg == "changetype") {
232 string const type = cmd.getArg(1);
233 status.setOnOff(type == type_);
234 bool varia = type != "LongTableNoNumber";
235 // check if the immediate parent inset allows caption variation
236 if (cur.depth() > 1) {
237 if (&cur[cur.depth() - 2].inset())
238 varia = cur[cur.depth() - 2].inset().allowsCaptionVariation(type);
240 status.setEnabled(varia
241 && buffer().params().documentClass().hasInsetLayout(
242 from_ascii("Caption:" + type)));
245 return InsetText::getStatus(cur, cmd, status);
248 case LFUN_PARAGRAPH_BREAK:
249 status.setEnabled(false);
252 case LFUN_INSET_TOGGLE:
253 // pass back to owner
258 return InsetText::getStatus(cur, cmd, status);
263 void InsetCaption::latex(otexstream & os,
264 OutputParams const & runparams_in) const
266 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
267 // caption is output as an optional argument
269 // This is a bit too simplistic to take advantage of
270 // caption options we must add more later. (Lgb)
271 // This code is currently only able to handle the simple
272 // \caption{...}, later we will make it take advantage
273 // of the one of the caption packages. (Lgb)
274 OutputParams runparams = runparams_in;
275 // FIXME: actually, it is moving only when there is no
276 // optional argument.
277 runparams.moving_arg = !runparams.inTableCell;
278 InsetText::latex(os, runparams);
279 runparams_in.encoding = runparams.encoding;
283 int InsetCaption::plaintext(odocstringstream & os,
284 OutputParams const & runparams, size_t max_length) const
286 os << '[' << full_label_ << "\n";
287 InsetText::plaintext(os, runparams, max_length);
290 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
294 int InsetCaption::docbook(odocstream & os,
295 OutputParams const & runparams) const
299 ret = InsetText::docbook(os, runparams);
305 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
307 if (rp.html_disable_captions)
309 InsetLayout const & il = getLayout();
310 string const tag = il.htmltag();
311 string attr = il.htmlattr();
312 if (!type_.empty()) {
313 string const our_class = "float-caption-" + type_;
314 size_t const loc = attr.find("class='");
315 if (loc != string::npos)
316 attr.insert(loc + 1, our_class);
318 xs << html::StartTag(tag, attr);
319 docstring def = getCaptionAsHTML(xs, rp);
320 xs << html::EndTag(tag);
325 void InsetCaption::getArgument(otexstream & os,
326 OutputParams const & runparams) const
328 InsetLayout const & il = getLayout();
330 if (!il.leftdelim().empty())
331 os << il.leftdelim();
333 OutputParams rp = runparams;
336 if (il.isNeedProtect())
337 rp.moving_arg = true;
339 rp.par_end = paragraphs().size();
341 // Output the contents of the inset
342 latexParagraphs(buffer(), text(), os, rp);
343 runparams.encoding = rp.encoding;
345 if (!il.rightdelim().empty())
346 os << il.rightdelim();
350 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
351 OutputParams const & runparams) const
353 os << full_label_ << ' ';
354 odocstringstream ods;
355 int const retval = InsetText::plaintext(ods, runparams);
361 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
362 OutputParams const & runparams) const
364 xs << full_label_ << ' ';
365 InsetText::XHTMLOptions const opts =
366 InsetText::WriteLabel | InsetText::WriteInnerTag;
367 return InsetText::insetAsXHTML(xs, runparams, opts);
371 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
373 Buffer const & master = *buffer().masterBuffer();
374 DocumentClass const & tclass = master.params().documentClass();
375 string const & lang = it.paragraph().getParLanguage(master.params())->code();
376 Counters & cnts = tclass.counters();
377 string const & type = cnts.current_float();
378 if (utype == OutputUpdate) {
379 // counters are local to the caption
380 cnts.saveLastCounter();
382 // Memorize type for addToToc().
385 full_label_ = master.B_("Senseless!!! ");
387 // FIXME: life would be _much_ simpler if listings was
388 // listed in Floating.
390 if (type == "listing")
391 name = master.B_("Listing");
393 name = master.B_(tclass.floats().getType(type).name());
394 docstring counter = from_utf8(type);
395 if (cnts.isSubfloat()) {
396 // only standard captions allowed in subfloats
398 counter = "sub-" + from_utf8(type);
399 name = bformat(_("Sub-%1$s"),
400 master.B_(tclass.floats().getType(type).name()));
403 if (cnts.hasCounter(counter)) {
404 cnts.step(counter, utype);
405 sec = cnts.theCounter(counter, lang);
407 if (getLayout().labelstring() != master.B_("standard")) {
409 sec += from_ascii(" ");
410 sec += bformat(from_ascii("(%1$s)"), getLayout().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";