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"
51 InsetCaption::InsetCaption(InsetCaption const & ic)
52 : InsetText(ic), textclass_(ic.textclass_)
54 setAutoBreakRows(true);
56 setFrameColor(Color::captionframe);
59 InsetCaption::InsetCaption(BufferParams const & bp)
60 : InsetText(bp), textclass_(bp.getTextClass())
62 setAutoBreakRows(true);
64 setFrameColor(Color::captionframe);
68 void InsetCaption::write(Buffer const & buf, ostream & os) const
75 void InsetCaption::read(Buffer const & buf, Lexer & lex)
78 // We will enably this check again when the compability
79 // code is removed from Buffer::Read (Lgb)
80 string const token = lex.GetString();
81 if (token != "Caption") {
82 lyxerr << "InsetCaption::Read: consistency check failed."
86 InsetText::read(buf, lex);
90 docstring const InsetCaption::editMessage() const
92 return _("Opened Caption Inset");
96 void InsetCaption::cursorPos(BufferView const & bv,
97 CursorSlice const & sl, bool boundary, int & x, int & y) const
99 InsetText::cursorPos(bv, sl, boundary, x, y);
104 void InsetCaption::setCustomLabel(docstring const & label)
106 if (!support::isAscii(label) || label.empty())
107 // This must be a user defined layout. We cannot translate
108 // this, since gettext accepts only ascii keys.
109 custom_label_ = label;
111 custom_label_ = _(to_ascii(label));
115 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
120 ParConstIterator pit = par_const_iterator_begin(*this);
122 Toc & toc = toclist[type_];
123 docstring const str = full_label_ + ". " + pit->asString(buf, false);
124 toc.push_back(TocItem(pit, 0, str));
128 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
130 int const width_offset = TEXT_TO_INSET_OFFSET / 2;
132 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
133 // add some space to separate the label from the inset text
134 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
135 dim.wid = labelwidth_;
137 dim.wid += width_offset;
138 // Correct for button and label width
139 mi.base.textwidth -= dim.wid;
140 InsetText::metrics(mi, textdim);
141 mi.base.textwidth += dim.wid;
142 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
143 dim.asc = textdim.asc;
144 dim.wid += textdim.wid;
145 dim.asc += TEXT_TO_INSET_OFFSET;
146 dim.des += TEXT_TO_INSET_OFFSET;
147 bool const changed = dim_ != dim;
153 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
155 // We must draw the label, we should get the label string
156 // from the enclosing float inset.
157 // The question is: Who should draw the label, the caption inset,
158 // the text inset or the paragraph?
159 // We should also draw the float number (Lgb)
161 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
163 labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
164 // add some space to separate the label from the inset text
165 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
166 InsetText::draw(pi, x + labelwidth_, y);
167 setPosCache(pi, x, y);
171 void InsetCaption::drawSelection(PainterInfo & pi, int x, int y) const
173 InsetText::drawSelection(pi, x + labelwidth_, y);
177 void InsetCaption::edit(Cursor & cur, bool left)
180 InsetText::edit(cur, left);
184 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
187 return InsetText::editXY(cur, x, y);
191 bool InsetCaption::insetAllowed(Inset::Code code) const
201 return InsetText::insetAllowed(code);
206 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
207 FuncStatus & status) const
209 switch (cmd.action) {
211 case LFUN_BREAK_PARAGRAPH:
212 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
213 case LFUN_BREAK_PARAGRAPH_SKIP:
214 status.enabled(false);
217 case LFUN_OPTIONAL_INSERT:
218 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
222 return InsetText::getStatus(cur, cmd, status);
227 int InsetCaption::latex(Buffer const & buf, odocstream & os,
228 OutputParams const & runparams_in) const
230 // This is a bit too simplistic to take advantage of
231 // caption options we must add more later. (Lgb)
232 // This code is currently only able to handle the simple
233 // \caption{...}, later we will make it take advantage
234 // of the one of the caption packages. (Lgb)
235 OutputParams runparams = runparams_in;
236 // FIXME: actually, it is moving only when there is no
237 // optional argument.
238 runparams.moving_arg = true;
240 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
242 l += InsetText::latex(buf, os, runparams);
244 runparams_in.encoding = runparams.encoding;
249 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
250 OutputParams const & runparams) const
252 os << '[' << full_label_ << "\n";
253 InsetText::plaintext(buf, os, runparams);
256 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
260 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
261 OutputParams const & runparams) const
265 ret = InsetText::docbook(buf, os, runparams);
271 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
272 OutputParams const & runparams) const
274 return InsetText::latex(buf, os, runparams);
278 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
279 OutputParams const & runparams) const
281 return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
285 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
287 using support::bformat;
288 TextClass const & tclass = buf.params().getTextClass();
289 Counters & cnts = tclass.counters();
290 string const & type = cnts.current_float();
292 full_label_ = buf.B_("Senseless!!! ");
294 // FIXME: life would be _much_ simpler if listings was
295 // listed in Floating.
297 if (type == "listing")
298 name = buf.B_("Listing");
300 name = buf.B_(tclass.floats().getType(type).name());
301 if (cnts.hasCounter(from_utf8(type))) {
302 cnts.step(from_utf8(type));
303 full_label_ = bformat(from_ascii("%1$s %2$s:"),
305 cnts.theCounter(from_utf8(type)));
307 full_label_ = bformat(from_ascii("%1$s #:"), name);
310 // Do the real work now.
311 InsetText::updateLabels(buf, it);
315 Inset * InsetCaption::clone() const
317 return new InsetCaption(*this);