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"
27 #include "support/gettext.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"
45 using namespace lyx::support;
50 InsetCaption::InsetCaption(InsetCaption const & ic)
51 : InsetText(ic), textclass_(ic.textclass_)
53 setAutoBreakRows(true);
55 setFrameColor(Color_captionframe);
59 InsetCaption::InsetCaption(BufferParams const & bp)
60 : InsetText(bp), textclass_(bp.getTextClass())
62 setAutoBreakRows(true);
64 setFrameColor(Color_captionframe);
65 //FIXME Do we need to set all paragraphs here? or will there
67 paragraphs().back().layout(bp.getTextClass().emptyLayout());
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 (!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, ParConstIterator const &) const
123 ParConstIterator pit = par_const_iterator_begin(*this);
125 Toc & toc = toclist[type_];
126 docstring const str = full_label_ + ". " + pit->asString(buf, false);
127 toc.push_back(TocItem(pit, 0, str));
131 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
133 FontInfo tmpfont = mi.base.font;
134 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
135 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
136 // add some space to separate the label from the inset text
137 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
138 dim.wid = labelwidth_;
140 // Correct for button and label width
141 mi.base.textwidth -= dim.wid;
142 InsetText::metrics(mi, textdim);
143 mi.base.font = tmpfont;
144 mi.base.textwidth += dim.wid;
145 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
146 dim.asc = textdim.asc;
147 dim.wid += textdim.wid;
151 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
153 // We must draw the label, we should get the label string
154 // from the enclosing float inset.
155 // The question is: Who should draw the label, the caption inset,
156 // the text inset or the paragraph?
157 // We should also draw the float number (Lgb)
159 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
161 FontInfo tmpfont = pi.base.font;
162 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
163 pi.pain.text(x, y, full_label_, pi.base.font);
164 InsetText::draw(pi, x + labelwidth_, y);
165 pi.base.font = tmpfont;
169 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
172 InsetText::edit(cur, front, entry_from);
176 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
179 return InsetText::editXY(cur, x, y);
183 bool InsetCaption::insetAllowed(InsetCode code) const
194 return InsetText::insetAllowed(code);
199 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
200 FuncStatus & status) const
202 switch (cmd.action) {
204 case LFUN_BREAK_PARAGRAPH:
205 case LFUN_BREAK_PARAGRAPH_SKIP:
206 status.enabled(false);
209 case LFUN_OPTIONAL_INSERT:
210 status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
214 return InsetText::getStatus(cur, cmd, status);
219 int InsetCaption::latex(Buffer const & buf, odocstream & os,
220 OutputParams const & runparams_in) const
222 // This is a bit too simplistic to take advantage of
223 // caption options we must add more later. (Lgb)
224 // This code is currently only able to handle the simple
225 // \caption{...}, later we will make it take advantage
226 // of the one of the caption packages. (Lgb)
227 OutputParams runparams = runparams_in;
228 // FIXME: actually, it is moving only when there is no
229 // optional argument.
230 runparams.moving_arg = true;
232 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
234 l += InsetText::latex(buf, os, runparams);
236 runparams_in.encoding = runparams.encoding;
241 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
242 OutputParams const & runparams) const
244 os << '[' << full_label_ << "\n";
245 InsetText::plaintext(buf, os, runparams);
248 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
252 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
253 OutputParams const & runparams) const
257 ret = InsetText::docbook(buf, os, runparams);
263 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
264 OutputParams const & runparams) const
266 return InsetText::latex(buf, os, runparams);
270 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
271 OutputParams const & runparams) const
273 return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
277 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
279 TextClass const & tclass = buf.params().getTextClass();
280 Counters & cnts = tclass.counters();
281 string const & type = cnts.current_float();
282 // Memorize type for addToToc().
285 full_label_ = buf.B_("Senseless!!! ");
287 // FIXME: life would be _much_ simpler if listings was
288 // listed in Floating.
290 if (type == "listing")
291 name = buf.B_("Listing");
293 name = buf.B_(tclass.floats().getType(type).name());
294 if (cnts.hasCounter(from_utf8(type))) {
295 cnts.step(from_utf8(type));
296 full_label_ = bformat(from_ascii("%1$s %2$s:"),
298 cnts.theCounter(from_utf8(type)));
300 full_label_ = bformat(from_ascii("%1$s #:"), name);
303 // Do the real work now.
304 InsetText::updateLabels(buf, it);
308 Inset * InsetCaption::clone() const
310 return new InsetCaption(*this);