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 void 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 latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
249 InsetText::latex(os, runparams);
251 runparams_in.encoding = runparams.encoding;
255 int InsetCaption::plaintext(odocstream & os,
256 OutputParams const & runparams) const
258 os << '[' << full_label_ << "\n";
259 InsetText::plaintext(os, runparams);
262 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
266 int InsetCaption::docbook(odocstream & os,
267 OutputParams const & runparams) const
271 ret = InsetText::docbook(os, runparams);
277 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
279 if (rp.html_disable_captions)
281 string attr = "class='float-caption";
283 attr += " float-caption-" + type_;
285 xs << html::StartTag("div", attr);
286 docstring def = getCaptionAsHTML(xs, rp);
287 xs << html::EndTag("div");
292 void InsetCaption::getArgument(otexstream & os,
293 OutputParams const & runparams) const
295 InsetText::latex(os, runparams);
299 void InsetCaption::getOptArg(otexstream & os,
300 OutputParams const & runparams) const
302 latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
306 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
307 OutputParams const & runparams) const
309 os << full_label_ << ' ';
310 return InsetText::plaintext(os, runparams);
314 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
315 OutputParams const & runparams) const
317 xs << full_label_ << ' ';
318 InsetText::XHTMLOptions const opts =
319 InsetText::WriteLabel | InsetText::WriteInnerTag;
320 return InsetText::insetAsXHTML(xs, runparams, opts);
324 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
326 Buffer const & master = *buffer().masterBuffer();
327 DocumentClass const & tclass = master.params().documentClass();
328 string const & lang = it.paragraph().getParLanguage(master.params())->code();
329 Counters & cnts = tclass.counters();
330 string const & type = cnts.current_float();
331 if (utype == OutputUpdate) {
332 // counters are local to the caption
333 cnts.saveLastCounter();
335 // Memorize type for addToToc().
338 full_label_ = master.B_("Senseless!!! ");
340 // FIXME: life would be _much_ simpler if listings was
341 // listed in Floating.
343 if (type == "listing")
344 name = master.B_("Listing");
346 name = master.B_(tclass.floats().getType(type).name());
347 docstring counter = from_utf8(type);
348 if (cnts.isSubfloat()) {
349 counter = "sub-" + from_utf8(type);
350 name = bformat(_("Sub-%1$s"),
351 master.B_(tclass.floats().getType(type).name()));
353 if (cnts.hasCounter(counter)) {
354 cnts.step(counter, utype);
355 full_label_ = bformat(from_ascii("%1$s %2$s:"),
357 cnts.theCounter(counter, lang));
359 full_label_ = bformat(from_ascii("%1$s #:"), name);
362 // Do the real work now.
363 InsetText::updateBuffer(it, utype);
364 if (utype == OutputUpdate)
365 cnts.restoreLastCounter();