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"
22 #include "BufferView.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 "TocBackend.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
39 #include "support/lstrings.h"
52 InsetCaption::InsetCaption(InsetCaption const & ic)
53 : InsetText(ic), textclass_(ic.textclass_)
55 setAutoBreakRows(true);
57 setFrameColor(Color::captionframe);
60 InsetCaption::InsetCaption(BufferParams const & bp)
61 : InsetText(bp), textclass_(bp.getTextClass())
63 setAutoBreakRows(true);
65 setFrameColor(Color::captionframe);
69 void InsetCaption::write(Buffer const & buf, ostream & os) const
76 void InsetCaption::read(Buffer const & buf, Lexer & lex)
79 // We will enably this check again when the compability
80 // code is removed from Buffer::Read (Lgb)
81 string const token = lex.GetString();
82 if (token != "Caption") {
83 lyxerr << "InsetCaption::Read: consistency check failed."
87 InsetText::read(buf, lex);
91 docstring const InsetCaption::editMessage() const
93 return _("Opened Caption Inset");
97 void InsetCaption::cursorPos(BufferView const & bv,
98 CursorSlice const & sl, bool boundary, int & x, int & y) const
100 InsetText::cursorPos(bv, sl, boundary, x, y);
105 void InsetCaption::setCustomLabel(docstring const & label)
107 if (!support::isAscii(label) || label.empty())
108 // This must be a user defined layout. We cannot translate
109 // this, since gettext accepts only ascii keys.
110 custom_label_ = label;
112 custom_label_ = _(to_ascii(label));
116 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
121 ParConstIterator pit = par_const_iterator_begin(*this);
123 Toc & toc = toclist[type_];
124 docstring const str = full_label_ + ". " + pit->asString(buf, false);
125 toc.push_back(TocItem(pit, 0, str));
129 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
131 Font tmpfont = mi.base.font;
132 mi.base.font = mi.base.bv->buffer().params().getFont();
133 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
134 // add some space to separate the label from the inset text
135 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
136 dim.wid = labelwidth_;
138 // Correct for button and label width
139 mi.base.textwidth -= dim.wid;
140 InsetText::metrics(mi, textdim);
141 mi.base.font = tmpfont;
142 mi.base.textwidth += dim.wid;
143 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
144 dim.asc = textdim.asc;
145 dim.wid += textdim.wid;
149 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
151 // We must draw the label, we should get the label string
152 // from the enclosing float inset.
153 // The question is: Who should draw the label, the caption inset,
154 // the text inset or the paragraph?
155 // We should also draw the float number (Lgb)
157 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
159 Font tmpfont = pi.base.font;
160 pi.base.font = pi.base.bv->buffer().params().getFont();
161 pi.pain.text(x, y, full_label_, pi.base.font);
162 InsetText::draw(pi, x + labelwidth_, y);
163 pi.base.font = tmpfont;
167 void InsetCaption::edit(Cursor & cur, bool left)
170 InsetText::edit(cur, left);
174 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
177 return InsetText::editXY(cur, x, y);
181 bool InsetCaption::insetAllowed(InsetCode code) const
191 return InsetText::insetAllowed(code);
196 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
197 FuncStatus & status) const
199 switch (cmd.action) {
201 case LFUN_BREAK_PARAGRAPH:
202 case LFUN_BREAK_PARAGRAPH_SKIP:
203 status.enabled(false);
206 case LFUN_OPTIONAL_INSERT:
207 status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
211 return InsetText::getStatus(cur, cmd, status);
216 int InsetCaption::latex(Buffer const & buf, odocstream & os,
217 OutputParams const & runparams_in) const
219 // This is a bit too simplistic to take advantage of
220 // caption options we must add more later. (Lgb)
221 // This code is currently only able to handle the simple
222 // \caption{...}, later we will make it take advantage
223 // of the one of the caption packages. (Lgb)
224 OutputParams runparams = runparams_in;
225 // FIXME: actually, it is moving only when there is no
226 // optional argument.
227 runparams.moving_arg = true;
229 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
231 l += InsetText::latex(buf, os, runparams);
233 runparams_in.encoding = runparams.encoding;
238 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
239 OutputParams const & runparams) const
241 os << '[' << full_label_ << "\n";
242 InsetText::plaintext(buf, os, runparams);
245 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
249 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
250 OutputParams const & runparams) const
254 ret = InsetText::docbook(buf, os, runparams);
260 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
261 OutputParams const & runparams) const
263 return InsetText::latex(buf, os, runparams);
267 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
268 OutputParams const & runparams) const
270 return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
274 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
276 using support::bformat;
277 TextClass const & tclass = buf.params().getTextClass();
278 Counters & cnts = tclass.counters();
279 string const & type = cnts.current_float();
281 full_label_ = buf.B_("Senseless!!! ");
283 // FIXME: life would be _much_ simpler if listings was
284 // listed in Floating.
286 if (type == "listing")
287 name = buf.B_("Listing");
289 name = buf.B_(tclass.floats().getType(type).name());
290 if (cnts.hasCounter(from_utf8(type))) {
291 cnts.step(from_utf8(type));
292 full_label_ = bformat(from_ascii("%1$s %2$s:"),
294 cnts.theCounter(from_utf8(type)));
296 full_label_ = bformat(from_ascii("%1$s #:"), name);
299 // Do the real work now.
300 InsetText::updateLabels(buf, it);
304 Inset * InsetCaption::clone() const
306 return new InsetCaption(*this);