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(BufferParams const & bp)
55 : InsetText(bp), textclass_(bp.getLyXTextClass())
57 setAutoBreakRows(true);
59 setFrameColor(LColor::captionframe);
63 void InsetCaption::write(Buffer const & buf, ostream & os) const
70 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
73 // We will enably this check again when the compability
74 // code is removed from Buffer::Read (Lgb)
75 string const token = lex.GetString();
76 if (token != "Caption") {
77 lyxerr << "InsetCaption::Read: consistency check failed."
81 InsetText::read(buf, lex);
85 docstring const InsetCaption::editMessage() const
87 return _("Opened Caption Inset");
91 void InsetCaption::cursorPos(BufferView const & bv,
92 CursorSlice const & sl, bool boundary, int & x, int & y) const
94 InsetText::cursorPos(bv, sl, boundary, x, y);
99 void InsetCaption::setCustomLabel(docstring const & label)
101 if (!support::isAscii(label) || label.empty())
102 // This must be a user defined layout. We cannot translate
103 // this, since gettext accepts only ascii keys.
104 custom_label_ = label;
106 custom_label_ = _(to_ascii(label));
110 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf) const
115 ParConstIterator pit = par_const_iterator_begin(*this);
117 Toc & toc = toclist[type_];
118 docstring const str = convert<docstring>(counter_)
119 + ". " + pit->asString(buf, false);
120 toc.push_back(TocItem(pit, 0, str));
124 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
126 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
128 full_label_ = _("Senseless!!! ");
130 docstring const number = convert<docstring>(counter_);
131 docstring label = custom_label_.empty()? _(type_): custom_label_;
132 full_label_ = bformat(from_ascii("%1$s %2$s:"), label, number);
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 InsetText::metrics(mi, textdim);
140 // Correct for button width, and re-fit
141 mi.base.textwidth -= dim.wid;
142 InsetText::metrics(mi, textdim);
143 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
144 dim.asc = textdim.asc;
145 dim.wid += textdim.wid;
146 dim.asc += TEXT_TO_INSET_OFFSET;
147 dim.des += TEXT_TO_INSET_OFFSET;
148 dim.wid += 0.5 * TEXT_TO_INSET_OFFSET;
149 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
150 bool const changed = dim_ != dim;
156 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
158 // We must draw the label, we should get the label string
159 // from the enclosing float inset.
160 // The question is: Who should draw the label, the caption inset,
161 // the text inset or the paragraph?
162 // We should also draw the float number (Lgb)
164 // Answer: the text inset (in buffer_funcs.C: setCaption).
166 labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
167 // add some space to separate the label from the inset text
168 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
169 InsetText::draw(pi, x + labelwidth_, y);
170 setPosCache(pi, x, y);
174 void InsetCaption::drawSelection(PainterInfo & pi, int x, int y) const
176 InsetText::drawSelection(pi, x + labelwidth_, y);
180 void InsetCaption::edit(LCursor & cur, bool left)
183 InsetText::edit(cur, left);
187 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
190 return InsetText::editXY(cur, x, y);
194 bool InsetCaption::insetAllowed(InsetBase::Code code) const
204 return InsetText::insetAllowed(code);
209 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
210 FuncStatus & status) const
212 switch (cmd.action) {
214 case LFUN_BREAK_PARAGRAPH:
215 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
216 case LFUN_BREAK_PARAGRAPH_SKIP:
217 status.enabled(false);
220 case LFUN_OPTIONAL_INSERT:
221 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
225 return InsetText::getStatus(cur, cmd, status);
230 int InsetCaption::latex(Buffer const & buf, odocstream & os,
231 OutputParams const & runparams_in) const
233 // This is a bit too simplistic to take advantage of
234 // caption options we must add more later. (Lgb)
235 // This code is currently only able to handle the simple
236 // \caption{...}, later we will make it take advantage
237 // of the one of the caption packages. (Lgb)
238 OutputParams runparams = runparams_in;
239 // FIXME: actually, it is moving only when there is no
240 // optional argument.
241 runparams.moving_arg = true;
243 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
245 l += InsetText::latex(buf, os, runparams);
251 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
252 OutputParams const & runparams) const
254 os << full_label_ << ' ';
255 return InsetText::plaintext(buf, os, runparams);
259 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
260 OutputParams const & runparams) const
264 ret = InsetText::docbook(buf, os, runparams);
270 auto_ptr<InsetBase> InsetCaption::doClone() const
272 return auto_ptr<InsetBase>(new InsetCaption(*this));