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)
53 : InsetText(buf, InsetText::PlainLayout)
55 setAutoBreakRows(true);
57 setFrameColor(Color_collapsableframe);
61 void InsetCaption::write(ostream & os) const
68 docstring InsetCaption::name() const
71 return from_ascii("Caption");
72 return from_utf8("Caption:" + type_);
76 void InsetCaption::read(Lexer & lex)
79 // We will enably this check again when the compability
80 // code is removed from Buffer::Read (Lgb)
81 lex.setContext("InsetCaption::Read: consistency check");
88 void InsetCaption::cursorPos(BufferView const & bv,
89 CursorSlice const & sl, bool boundary, int & x, int & y) const
91 InsetText::cursorPos(bv, sl, boundary, x, y);
96 void InsetCaption::setCustomLabel(docstring const & label)
98 if (!isAscii(label) || label.empty())
99 // This must be a user defined layout. We cannot translate
100 // this, since gettext accepts only ascii keys.
101 custom_label_ = label;
103 custom_label_ = _(to_ascii(label));
107 void InsetCaption::addToToc(DocIterator const & cpit) const
112 DocIterator pit = cpit;
113 pit.push_back(CursorSlice(const_cast<InsetCaption &>(*this)));
115 Toc & toc = buffer().tocBackend().toc(type_);
116 docstring str = full_label_ + ". ";
117 text().forToc(str, TOC_ENTRY_LENGTH);
118 toc.push_back(TocItem(pit, 0, str));
120 // Proceed with the rest of the inset.
121 InsetText::addToToc(cpit);
125 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
127 FontInfo tmpfont = mi.base.font;
128 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
129 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
130 // add some space to separate the label from the inset text
131 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
132 dim.wid = labelwidth_;
134 // Correct for button and label width
135 mi.base.textwidth -= dim.wid;
136 InsetText::metrics(mi, textdim);
137 mi.base.font = tmpfont;
138 mi.base.textwidth += dim.wid;
139 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
140 dim.asc = textdim.asc;
141 dim.wid += textdim.wid;
145 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
147 TextMetrics & tm = pi.base.bv->textMetrics(&text());
148 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
149 int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
150 pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
154 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
156 // We must draw the label, we should get the label string
157 // from the enclosing float inset.
158 // The question is: Who should draw the label, the caption inset,
159 // the text inset or the paragraph?
160 // We should also draw the float number (Lgb)
162 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
164 FontInfo tmpfont = pi.base.font;
165 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
166 pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
167 int const xx = x + TEXT_TO_INSET_OFFSET;
168 pi.pain.text(xx, y, full_label_, pi.base.font);
169 InsetText::draw(pi, x + labelwidth_, y);
170 pi.base.font = tmpfont;
174 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
177 InsetText::edit(cur, front, entry_from);
181 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
184 return InsetText::editXY(cur, x, y);
188 bool InsetCaption::insetAllowed(InsetCode code) const
191 // code that is not allowed in a caption
202 return InsetText::insetAllowed(code);
207 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
208 FuncStatus & status) const
210 switch (cmd.action()) {
212 case LFUN_BREAK_PARAGRAPH:
213 status.setEnabled(false);
216 case LFUN_ARGUMENT_INSERT:
217 status.setEnabled(cur.paragraph().insetList().find(ARG_CODE) == -1);
220 case LFUN_INSET_TOGGLE:
221 // pass back to owner
226 return InsetText::getStatus(cur, cmd, status);
231 int InsetCaption::latex(otexstream & os,
232 OutputParams const & runparams_in) const
234 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
235 // caption is output as an optional argument
237 // This is a bit too simplistic to take advantage of
238 // caption options we must add more later. (Lgb)
239 // This code is currently only able to handle the simple
240 // \caption{...}, later we will make it take advantage
241 // of the one of the caption packages. (Lgb)
242 OutputParams runparams = runparams_in;
243 // FIXME: actually, it is moving only when there is no
244 // optional argument.
245 runparams.moving_arg = true;
247 int l = latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
249 l += InsetText::latex(os, runparams);
251 runparams_in.encoding = runparams.encoding;
256 int InsetCaption::plaintext(odocstream & os,
257 OutputParams const & runparams) const
259 os << '[' << full_label_ << "\n";
260 InsetText::plaintext(os, runparams);
263 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
267 int InsetCaption::docbook(odocstream & os,
268 OutputParams const & runparams) const
272 ret = InsetText::docbook(os, runparams);
278 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
280 if (rp.html_disable_captions)
282 string attr = "class='float-caption";
284 attr += " float-caption-" + type_;
286 xs << html::StartTag("div", attr);
287 docstring def = getCaptionAsHTML(xs, rp);
288 xs << html::EndTag("div");
293 int InsetCaption::getArgument(otexstream & os,
294 OutputParams const & runparams) const
296 return InsetText::latex(os, runparams);
300 int InsetCaption::getOptArg(otexstream & os,
301 OutputParams const & runparams) const
303 return latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
307 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
308 OutputParams const & runparams) const
310 os << full_label_ << ' ';
311 return InsetText::plaintext(os, runparams);
315 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
316 OutputParams const & runparams) const
318 xs << full_label_ << ' ';
319 InsetText::XHTMLOptions const opts =
320 InsetText::WriteLabel | InsetText::WriteInnerTag;
321 return InsetText::insetAsXHTML(xs, runparams, opts);
325 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
327 Buffer const & master = *buffer().masterBuffer();
328 DocumentClass const & tclass = master.params().documentClass();
329 string const & lang = it.paragraph().getParLanguage(master.params())->code();
330 Counters & cnts = tclass.counters();
331 string const & type = cnts.current_float();
332 if (utype == OutputUpdate) {
333 // counters are local to the caption
334 cnts.saveLastCounter();
336 // Memorize type for addToToc().
339 full_label_ = master.B_("Senseless!!! ");
341 // FIXME: life would be _much_ simpler if listings was
342 // listed in Floating.
344 if (type == "listing")
345 name = master.B_("Listing");
347 name = master.B_(tclass.floats().getType(type).name());
348 docstring counter = from_utf8(type);
349 if (cnts.isSubfloat()) {
350 counter = "sub-" + from_utf8(type);
351 name = bformat(_("Sub-%1$s"),
352 master.B_(tclass.floats().getType(type).name()));
354 if (cnts.hasCounter(counter)) {
355 cnts.step(counter, utype);
356 full_label_ = bformat(from_ascii("%1$s %2$s:"),
358 cnts.theCounter(counter, lang));
360 full_label_ = bformat(from_ascii("%1$s #:"), name);
363 // Do the real work now.
364 InsetText::updateBuffer(it, utype);
365 if (utype == OutputUpdate)
366 cnts.restoreLastCounter();