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"
28 #include "InsetList.h"
29 #include "MetricsInfo.h"
30 #include "output_latex.h"
31 #include "OutputParams.h"
32 #include "Paragraph.h"
33 #include "paragraph_funcs.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
37 #include "frontends/FontMetrics.h"
38 #include "frontends/Painter.h"
40 #include "support/lstrings.h"
53 InsetCaption::InsetCaption(InsetCaption const & ic)
54 : InsetText(ic), textclass_(ic.textclass_)
56 setAutoBreakRows(true);
58 setFrameColor(Color_captionframe);
61 InsetCaption::InsetCaption(BufferParams const & bp)
62 : InsetText(bp), textclass_(bp.getTextClass())
64 setAutoBreakRows(true);
66 setFrameColor(Color_captionframe);
70 void InsetCaption::write(Buffer const & buf, ostream & os) const
77 void InsetCaption::read(Buffer const & buf, Lexer & lex)
80 // We will enably this check again when the compability
81 // code is removed from Buffer::Read (Lgb)
82 string const token = lex.GetString();
83 if (token != "Caption") {
84 lyxerr << "InsetCaption::Read: consistency check failed."
88 InsetText::read(buf, lex);
92 docstring const InsetCaption::editMessage() const
94 return _("Opened Caption Inset");
98 void InsetCaption::cursorPos(BufferView const & bv,
99 CursorSlice const & sl, bool boundary, int & x, int & y) const
101 InsetText::cursorPos(bv, sl, boundary, x, y);
106 void InsetCaption::setCustomLabel(docstring const & label)
108 if (!support::isAscii(label) || label.empty())
109 // This must be a user defined layout. We cannot translate
110 // this, since gettext accepts only ascii keys.
111 custom_label_ = label;
113 custom_label_ = _(to_ascii(label));
117 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
122 ParConstIterator pit = par_const_iterator_begin(*this);
124 Toc & toc = toclist[type_];
125 docstring const str = full_label_ + ". " + pit->asString(buf, false);
126 toc.push_back(TocItem(pit, 0, str));
130 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
132 FontInfo tmpfont = mi.base.font;
133 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
134 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
135 // add some space to separate the label from the inset text
136 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
137 dim.wid = labelwidth_;
139 // Correct for button and label width
140 mi.base.textwidth -= dim.wid;
141 InsetText::metrics(mi, textdim);
142 mi.base.font = tmpfont;
143 mi.base.textwidth += dim.wid;
144 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
145 dim.asc = textdim.asc;
146 dim.wid += textdim.wid;
150 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
152 // We must draw the label, we should get the label string
153 // from the enclosing float inset.
154 // The question is: Who should draw the label, the caption inset,
155 // the text inset or the paragraph?
156 // We should also draw the float number (Lgb)
158 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
160 FontInfo tmpfont = pi.base.font;
161 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
162 pi.pain.text(x, y, full_label_, pi.base.font);
163 InsetText::draw(pi, x + labelwidth_, y);
164 pi.base.font = tmpfont;
168 void InsetCaption::edit(Cursor & cur, bool left)
171 InsetText::edit(cur, left);
175 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
178 return InsetText::editXY(cur, x, y);
182 bool InsetCaption::insetAllowed(InsetCode code) const
192 return InsetText::insetAllowed(code);
197 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
198 FuncStatus & status) const
200 switch (cmd.action) {
202 case LFUN_BREAK_PARAGRAPH:
203 case LFUN_BREAK_PARAGRAPH_SKIP:
204 status.enabled(false);
207 case LFUN_OPTIONAL_INSERT:
208 status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
212 return InsetText::getStatus(cur, cmd, status);
217 int InsetCaption::latex(Buffer const & buf, odocstream & os,
218 OutputParams const & runparams_in) const
220 // This is a bit too simplistic to take advantage of
221 // caption options we must add more later. (Lgb)
222 // This code is currently only able to handle the simple
223 // \caption{...}, later we will make it take advantage
224 // of the one of the caption packages. (Lgb)
225 OutputParams runparams = runparams_in;
226 // FIXME: actually, it is moving only when there is no
227 // optional argument.
228 runparams.moving_arg = true;
230 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
232 l += InsetText::latex(buf, os, runparams);
234 runparams_in.encoding = runparams.encoding;
239 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
240 OutputParams const & runparams) const
242 os << '[' << full_label_ << "\n";
243 InsetText::plaintext(buf, os, runparams);
246 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
250 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
251 OutputParams const & runparams) const
255 ret = InsetText::docbook(buf, os, runparams);
261 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
262 OutputParams const & runparams) const
264 return InsetText::latex(buf, os, runparams);
268 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
269 OutputParams const & runparams) const
271 return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
275 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
277 using support::bformat;
278 TextClass const & tclass = buf.params().getTextClass();
279 Counters & cnts = tclass.counters();
280 string const & type = cnts.current_float();
282 full_label_ = buf.B_("Senseless!!! ");
284 // FIXME: life would be _much_ simpler if listings was
285 // listed in Floating.
287 if (type == "listing")
288 name = buf.B_("Listing");
290 name = buf.B_(tclass.floats().getType(type).name());
291 if (cnts.hasCounter(from_utf8(type))) {
292 cnts.step(from_utf8(type));
293 full_label_ = bformat(from_ascii("%1$s %2$s:"),
295 cnts.theCounter(from_utf8(type)));
297 full_label_ = bformat(from_ascii("%1$s #:"), name);
300 // Do the real work now.
301 InsetText::updateLabels(buf, it);
305 Inset * InsetCaption::clone() const
307 return new InsetCaption(*this);