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 "TocBackend.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
46 using namespace lyx::support;
51 InsetCaption::InsetCaption(Buffer * buf)
52 : InsetText(buf, InsetText::PlainLayout)
54 setAutoBreakRows(true);
56 setFrameColor(Color_captionframe);
60 void InsetCaption::write(ostream & os) const
67 void InsetCaption::read(Lexer & lex)
70 // We will enably this check again when the compability
71 // code is removed from Buffer::Read (Lgb)
72 lex.setContext("InsetCaption::Read: consistency check");
79 void InsetCaption::cursorPos(BufferView const & bv,
80 CursorSlice const & sl, bool boundary, int & x, int & y) const
82 InsetText::cursorPos(bv, sl, boundary, x, y);
87 void InsetCaption::setCustomLabel(docstring const & label)
89 if (!isAscii(label) || label.empty())
90 // This must be a user defined layout. We cannot translate
91 // this, since gettext accepts only ascii keys.
92 custom_label_ = label;
94 custom_label_ = _(to_ascii(label));
98 void InsetCaption::addToToc(DocIterator const & cpit)
103 DocIterator pit = cpit;
104 pit.push_back(CursorSlice(*this));
106 Toc & toc = buffer().tocBackend().toc(type_);
107 docstring const str = full_label_ + ". " + text().getPar(0).asString();
108 toc.push_back(TocItem(pit, 0, str));
110 // Proceed with the rest of the inset.
111 InsetText::addToToc(cpit);
115 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
117 FontInfo tmpfont = mi.base.font;
118 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
119 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
120 // add some space to separate the label from the inset text
121 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
122 dim.wid = labelwidth_;
124 // Correct for button and label width
125 mi.base.textwidth -= dim.wid;
126 InsetText::metrics(mi, textdim);
127 mi.base.font = tmpfont;
128 mi.base.textwidth += dim.wid;
129 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
130 dim.asc = textdim.asc;
131 dim.wid += textdim.wid;
135 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
137 // We must draw the label, we should get the label string
138 // from the enclosing float inset.
139 // The question is: Who should draw the label, the caption inset,
140 // the text inset or the paragraph?
141 // We should also draw the float number (Lgb)
143 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
145 FontInfo tmpfont = pi.base.font;
146 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
147 pi.pain.text(x, y, full_label_, pi.base.font);
148 InsetText::draw(pi, x + labelwidth_, y);
149 pi.base.font = tmpfont;
153 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
156 InsetText::edit(cur, front, entry_from);
160 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
163 return InsetText::editXY(cur, x, y);
167 bool InsetCaption::insetAllowed(InsetCode code) const
170 // code that is not allowed in a caption
177 case MATH_AMSARRAY_CODE:
180 case MATH_MATRIX_CODE:
181 case MATH_OVERSET_CODE:
182 case MATH_SUBSTACK_CODE:
183 case MATH_UNDERSET_CODE:
184 case MATH_XYMATRIX_CODE:
187 return InsetText::insetAllowed(code);
192 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
193 FuncStatus & status) const
195 switch (cmd.action) {
197 case LFUN_BREAK_PARAGRAPH:
198 status.setEnabled(false);
201 case LFUN_OPTIONAL_INSERT:
202 status.setEnabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
205 case LFUN_INSET_TOGGLE:
206 // pass back to owner
211 return InsetText::getStatus(cur, cmd, status);
216 int InsetCaption::latex(odocstream & os,
217 OutputParams const & runparams_in) const
219 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
220 // caption is output as an optional argument
222 // This is a bit too simplistic to take advantage of
223 // caption options we must add more later. (Lgb)
224 // This code is currently only able to handle the simple
225 // \caption{...}, later we will make it take advantage
226 // of the one of the caption packages. (Lgb)
227 OutputParams runparams = runparams_in;
228 // FIXME: actually, it is moving only when there is no
229 // optional argument.
230 runparams.moving_arg = true;
232 int l = latexOptArgInsets(paragraphs()[0], os, runparams, 1);
234 l += InsetText::latex(os, runparams);
236 runparams_in.encoding = runparams.encoding;
241 int InsetCaption::plaintext(odocstream & os,
242 OutputParams const & runparams) const
244 os << '[' << full_label_ << "\n";
245 InsetText::plaintext(os, runparams);
248 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
252 int InsetCaption::docbook(odocstream & os,
253 OutputParams const & runparams) const
257 ret = InsetText::docbook(os, runparams);
263 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
265 if (rp.html_disable_captions)
267 xs << StartTag("div", "class='float-caption'");
268 docstring def = getCaptionAsHTML(xs, rp);
274 int InsetCaption::getArgument(odocstream & os,
275 OutputParams const & runparams) const
277 return InsetText::latex(os, runparams);
281 int InsetCaption::getOptArg(odocstream & os,
282 OutputParams const & runparams) const
284 return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
288 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
289 OutputParams const & runparams) const
291 os << full_label_ << ' ';
292 return InsetText::plaintext(os, runparams);
296 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
297 OutputParams const & runparams) const
299 xs << full_label_ << ' ';
300 InsetText::XHTMLOptions const opts =
301 InsetText::WriteLabel | InsetText::WriteInnerTag;
302 return InsetText::insetAsXHTML(xs, runparams, opts);
306 void InsetCaption::updateLabels(ParIterator const & it, bool out)
308 Buffer const & master = *buffer().masterBuffer();
309 DocumentClass const & tclass = master.params().documentClass();
310 string const & lang = it.paragraph().getParLanguage(master.params())->code();
311 Counters & cnts = tclass.counters();
312 string const & type = cnts.current_float();
313 // Memorize type for addToToc().
316 full_label_ = master.B_("Senseless!!! ");
318 // FIXME: life would be _much_ simpler if listings was
319 // listed in Floating.
321 if (type == "listing")
322 name = master.B_("Listing");
324 name = master.B_(tclass.floats().getType(type).name());
325 docstring counter = from_utf8(type);
326 if (cnts.isSubfloat()) {
327 counter = "sub-" + from_utf8(type);
328 name = bformat(_("Sub-%1$s"),
329 master.B_(tclass.floats().getType(type).name()));
331 if (cnts.hasCounter(counter)) {
333 full_label_ = bformat(from_ascii("%1$s %2$s:"),
335 cnts.theCounter(counter, lang));
337 full_label_ = bformat(from_ascii("%1$s #:"), name);
340 // Do the real work now.
341 InsetText::updateLabels(it, out);