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 "OutputParams.h"
32 #include "Paragraph.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
39 #include "support/gettext.h"
40 #include "support/lstrings.h"
45 using namespace lyx::support;
50 InsetCaption::InsetCaption(Buffer const & buf)
51 : InsetText(buf, InsetText::PlainLayout)
53 setAutoBreakRows(true);
55 setFrameColor(Color_captionframe);
59 void InsetCaption::write(ostream & os) const
66 void InsetCaption::read(Lexer & lex)
69 // We will enably this check again when the compability
70 // code is removed from Buffer::Read (Lgb)
71 lex.setContext("InsetCaption::Read: consistency check");
78 void InsetCaption::cursorPos(BufferView const & bv,
79 CursorSlice const & sl, bool boundary, int & x, int & y) const
81 InsetText::cursorPos(bv, sl, boundary, x, y);
86 void InsetCaption::setCustomLabel(docstring const & label)
88 if (!isAscii(label) || label.empty())
89 // This must be a user defined layout. We cannot translate
90 // this, since gettext accepts only ascii keys.
91 custom_label_ = label;
93 custom_label_ = _(to_ascii(label));
97 void InsetCaption::addToToc(DocIterator const & cpit)
102 DocIterator pit = cpit;
103 pit.push_back(CursorSlice(*this));
105 Toc & toc = buffer().tocBackend().toc(type_);
106 docstring const str = full_label_ + ". " + text().getPar(0).asString();
107 toc.push_back(TocItem(pit, 0, str));
109 // Proceed with the rest of the inset.
110 InsetText::addToToc(cpit);
114 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
116 FontInfo tmpfont = mi.base.font;
117 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
118 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
119 // add some space to separate the label from the inset text
120 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
121 dim.wid = labelwidth_;
123 // Correct for button and label width
124 mi.base.textwidth -= dim.wid;
125 InsetText::metrics(mi, textdim);
126 mi.base.font = tmpfont;
127 mi.base.textwidth += dim.wid;
128 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
129 dim.asc = textdim.asc;
130 dim.wid += textdim.wid;
134 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
136 // We must draw the label, we should get the label string
137 // from the enclosing float inset.
138 // The question is: Who should draw the label, the caption inset,
139 // the text inset or the paragraph?
140 // We should also draw the float number (Lgb)
142 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
144 FontInfo tmpfont = pi.base.font;
145 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
146 pi.pain.text(x, y, full_label_, pi.base.font);
147 InsetText::draw(pi, x + labelwidth_, y);
148 pi.base.font = tmpfont;
152 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
155 InsetText::edit(cur, front, entry_from);
159 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
162 return InsetText::editXY(cur, x, y);
166 bool InsetCaption::insetAllowed(InsetCode code) const
177 return InsetText::insetAllowed(code);
182 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
183 FuncStatus & status) const
185 switch (cmd.action) {
187 case LFUN_BREAK_PARAGRAPH:
188 status.setEnabled(false);
191 case LFUN_OPTIONAL_INSERT:
192 status.setEnabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
195 case LFUN_INSET_TOGGLE:
196 // pass back to owner
201 return InsetText::getStatus(cur, cmd, status);
206 int InsetCaption::latex(odocstream & os,
207 OutputParams const & runparams_in) const
209 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
210 // caption is output as an optional argument
212 // This is a bit too simplistic to take advantage of
213 // caption options we must add more later. (Lgb)
214 // This code is currently only able to handle the simple
215 // \caption{...}, later we will make it take advantage
216 // of the one of the caption packages. (Lgb)
217 OutputParams runparams = runparams_in;
218 // FIXME: actually, it is moving only when there is no
219 // optional argument.
220 runparams.moving_arg = true;
222 int l = latexOptArgInsets(paragraphs()[0], os, runparams, 1);
224 l += InsetText::latex(os, runparams);
226 runparams_in.encoding = runparams.encoding;
231 int InsetCaption::plaintext(odocstream & os,
232 OutputParams const & runparams) const
234 os << '[' << full_label_ << "\n";
235 InsetText::plaintext(os, runparams);
238 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
242 int InsetCaption::docbook(odocstream & os,
243 OutputParams const & runparams) const
247 ret = InsetText::docbook(os, runparams);
253 docstring InsetCaption::xhtml(odocstream & os,
254 OutputParams const & rp) const
256 if (rp.html_disable_captions)
258 os << "<div class='float-caption'>\n";
259 docstring def = getCaptionAsHTML(os, rp);
264 int InsetCaption::getArgument(odocstream & os,
265 OutputParams const & runparams) const
267 return InsetText::latex(os, runparams);
271 int InsetCaption::getOptArg(odocstream & os,
272 OutputParams const & runparams) const
274 return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
278 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
279 OutputParams const & runparams) const
281 os << full_label_ << ' ';
282 return InsetText::plaintext(os, runparams);
286 docstring InsetCaption::getCaptionAsHTML(odocstream & os,
287 OutputParams const & runparams) const
289 os << full_label_ << ' ';
290 return InsetText::xhtml(os, runparams);
294 void InsetCaption::updateLabels(ParIterator const & it)
296 Buffer const & master = *buffer().masterBuffer();
297 DocumentClass const & tclass = master.params().documentClass();
298 string const & lang = it.paragraph().getParLanguage(master.params())->code();
299 Counters & cnts = tclass.counters();
300 string const & type = cnts.current_float();
301 // Memorize type for addToToc().
304 full_label_ = master.B_("Senseless!!! ");
306 // FIXME: life would be _much_ simpler if listings was
307 // listed in Floating.
309 if (type == "listing")
310 name = master.B_("Listing");
312 name = master.B_(tclass.floats().getType(type).name());
313 docstring counter = from_utf8(type);
314 if (cnts.isSubfloat()) {
315 counter = "sub-" + from_utf8(type);
316 name = bformat(_("Sub-%1$s"),
317 master.B_(tclass.floats().getType(type).name()));
319 if (cnts.hasCounter(counter)) {
321 full_label_ = bformat(from_ascii("%1$s %2$s:"),
323 cnts.theCounter(counter, lang));
325 full_label_ = bformat(from_ascii("%1$s #:"), name);
328 // Do the real work now.
329 InsetText::updateLabels(it);