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"
21 #include "BufferView.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
28 #include "MetricsInfo.h"
29 #include "output_latex.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "TocBackend.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
38 #include "support/lstrings.h"
39 #include "support/convert.h"
52 using support::bformat;
54 InsetCaption::InsetCaption(InsetCaption const & ic)
55 : InsetText(ic), textclass_(ic.textclass_)
57 setAutoBreakRows(true);
59 setFrameColor(Color::captionframe);
62 InsetCaption::InsetCaption(BufferParams const & bp)
63 : InsetText(bp), textclass_(bp.getTextClass())
65 setAutoBreakRows(true);
67 setFrameColor(Color::captionframe);
71 void InsetCaption::write(Buffer const & buf, ostream & os) const
78 void InsetCaption::read(Buffer const & buf, Lexer & lex)
81 // We will enably this check again when the compability
82 // code is removed from Buffer::Read (Lgb)
83 string const token = lex.GetString();
84 if (token != "Caption") {
85 lyxerr << "InsetCaption::Read: consistency check failed."
89 InsetText::read(buf, lex);
93 docstring const InsetCaption::editMessage() const
95 return _("Opened Caption Inset");
99 void InsetCaption::cursorPos(BufferView const & bv,
100 CursorSlice const & sl, bool boundary, int & x, int & y) const
102 InsetText::cursorPos(bv, sl, boundary, x, y);
107 void InsetCaption::setCustomLabel(docstring const & label)
109 if (!support::isAscii(label) || label.empty())
110 // This must be a user defined layout. We cannot translate
111 // this, since gettext accepts only ascii keys.
112 custom_label_ = label;
114 custom_label_ = _(to_ascii(label));
118 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf) const
123 ParConstIterator pit = par_const_iterator_begin(*this);
125 Toc & toc = toclist[type_];
126 docstring const str = convert<docstring>(counter_)
127 + ". " + pit->asString(buf, false);
128 toc.push_back(TocItem(pit, 0, str));
132 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
134 int const width_offset = TEXT_TO_INSET_OFFSET / 2;
135 mi.base.textwidth -= width_offset;
137 computeFullLabel(*mi.base.bv->buffer());
139 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
140 // add some space to separate the label from the inset text
141 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
142 dim.wid = labelwidth_;
144 InsetText::metrics(mi, textdim);
145 // Correct for button width, and re-fit
146 mi.base.textwidth -= dim.wid;
147 InsetText::metrics(mi, textdim);
148 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
149 dim.asc = textdim.asc;
150 dim.wid += textdim.wid;
151 dim.asc += TEXT_TO_INSET_OFFSET;
152 dim.des += TEXT_TO_INSET_OFFSET;
153 dim.wid += width_offset;
154 mi.base.textwidth += width_offset;
155 bool const changed = dim_ != dim;
161 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
163 // We must draw the label, we should get the label string
164 // from the enclosing float inset.
165 // The question is: Who should draw the label, the caption inset,
166 // the text inset or the paragraph?
167 // We should also draw the float number (Lgb)
169 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
171 labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
172 // add some space to separate the label from the inset text
173 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
174 InsetText::draw(pi, x + labelwidth_, y);
175 setPosCache(pi, x, y);
179 void InsetCaption::drawSelection(PainterInfo & pi, int x, int y) const
181 InsetText::drawSelection(pi, x + labelwidth_, y);
185 void InsetCaption::edit(Cursor & cur, bool left)
188 InsetText::edit(cur, left);
192 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
195 return InsetText::editXY(cur, x, y);
199 bool InsetCaption::insetAllowed(Inset::Code code) const
209 return InsetText::insetAllowed(code);
214 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
215 FuncStatus & status) const
217 switch (cmd.action) {
219 case LFUN_BREAK_PARAGRAPH:
220 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
221 case LFUN_BREAK_PARAGRAPH_SKIP:
222 status.enabled(false);
225 case LFUN_OPTIONAL_INSERT:
226 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
230 return InsetText::getStatus(cur, cmd, status);
235 int InsetCaption::latex(Buffer const & buf, odocstream & os,
236 OutputParams const & runparams_in) const
238 // This is a bit too simplistic to take advantage of
239 // caption options we must add more later. (Lgb)
240 // This code is currently only able to handle the simple
241 // \caption{...}, later we will make it take advantage
242 // of the one of the caption packages. (Lgb)
243 OutputParams runparams = runparams_in;
244 // FIXME: actually, it is moving only when there is no
245 // optional argument.
246 runparams.moving_arg = true;
248 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
250 l += InsetText::latex(buf, os, runparams);
252 runparams_in.encoding = runparams.encoding;
257 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
258 OutputParams const & runparams) const
260 computeFullLabel(buf);
262 os << '[' << full_label_ << "\n";
263 InsetText::plaintext(buf, os, runparams);
266 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
270 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
271 OutputParams const & runparams) const
275 ret = InsetText::docbook(buf, os, runparams);
281 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
282 OutputParams const & runparams) const
284 return InsetText::latex(buf, os, runparams);
288 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
289 OutputParams const & runparams) const
291 return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
295 void InsetCaption::computeFullLabel(Buffer const & buf) const
298 full_label_ = buf.B_("Senseless!!! ");
300 docstring const number = convert<docstring>(counter_);
301 docstring label = custom_label_.empty()? buf.B_(type_): custom_label_;
302 full_label_ = bformat(from_ascii("%1$s %2$s:"), label, number);
307 auto_ptr<Inset> InsetCaption::doClone() const
309 return auto_ptr<Inset>(new InsetCaption(*this));