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 "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/gettext.h"
41 #include "support/lstrings.h"
46 using namespace lyx::support;
51 InsetCaption::InsetCaption(Buffer const & buf)
52 : InsetText(buf, InsetText::PlainLayout)
54 setAutoBreakRows(true);
56 setFrameColor(Color_captionframe);
60 void InsetCaption::write(ostream & os) const
63 text().write(buffer(), os);
67 void InsetCaption::read(Lexer & lex)
70 // We will enably this check again when the compability
71 // code is removed from Buffer::Read (Lgb)
72 lex.setContext("InsetCaption::Read: consistency check");
79 docstring InsetCaption::editMessage() const
81 return _("Opened Caption Inset");
85 void InsetCaption::cursorPos(BufferView const & bv,
86 CursorSlice const & sl, bool boundary, int & x, int & y) const
88 InsetText::cursorPos(bv, sl, boundary, x, y);
93 void InsetCaption::setCustomLabel(docstring const & label)
95 if (!isAscii(label) || label.empty())
96 // This must be a user defined layout. We cannot translate
97 // this, since gettext accepts only ascii keys.
98 custom_label_ = label;
100 custom_label_ = _(to_ascii(label));
104 void InsetCaption::addToToc(DocIterator const & cpit)
109 DocIterator pit = cpit;
110 pit.push_back(CursorSlice(*this));
112 Toc & toc = buffer().tocBackend().toc(type_);
113 docstring const str = full_label_ + ". " + text().getPar(0).asString();
114 toc.push_back(TocItem(pit, 0, str));
116 // Proceed with the rest of the inset.
117 InsetText::addToToc(cpit);
121 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
123 FontInfo tmpfont = mi.base.font;
124 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
125 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
126 // add some space to separate the label from the inset text
127 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
128 dim.wid = labelwidth_;
130 // Correct for button and label width
131 mi.base.textwidth -= dim.wid;
132 InsetText::metrics(mi, textdim);
133 mi.base.font = tmpfont;
134 mi.base.textwidth += dim.wid;
135 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
136 dim.asc = textdim.asc;
137 dim.wid += textdim.wid;
141 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
143 // We must draw the label, we should get the label string
144 // from the enclosing float inset.
145 // The question is: Who should draw the label, the caption inset,
146 // the text inset or the paragraph?
147 // We should also draw the float number (Lgb)
149 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
151 FontInfo tmpfont = pi.base.font;
152 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
153 pi.pain.text(x, y, full_label_, pi.base.font);
154 InsetText::draw(pi, x + labelwidth_, y);
155 pi.base.font = tmpfont;
159 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
162 InsetText::edit(cur, front, entry_from);
166 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
169 return InsetText::editXY(cur, x, y);
173 bool InsetCaption::insetAllowed(InsetCode code) const
184 return InsetText::insetAllowed(code);
189 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
190 FuncStatus & status) const
192 switch (cmd.action) {
194 case LFUN_BREAK_PARAGRAPH:
195 status.setEnabled(false);
198 case LFUN_OPTIONAL_INSERT:
199 status.setEnabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
202 case LFUN_INSET_TOGGLE:
203 // pass back to owner
208 return InsetText::getStatus(cur, cmd, status);
213 int InsetCaption::latex(odocstream & os,
214 OutputParams const & runparams_in) const
216 if (runparams_in.inFloat == OutputParams::SUBFLOAT)
217 // caption is output as an optional argument
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(paragraphs()[0], os, runparams, 1);
231 l += InsetText::latex(os, runparams);
233 runparams_in.encoding = runparams.encoding;
238 int InsetCaption::plaintext(odocstream & os,
239 OutputParams const & runparams) const
241 os << '[' << full_label_ << "\n";
242 InsetText::plaintext(os, runparams);
245 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
249 int InsetCaption::docbook(odocstream & os,
250 OutputParams const & runparams) const
254 ret = InsetText::docbook(os, runparams);
260 docstring InsetCaption::xhtml(odocstream & os,
261 OutputParams const & rp) const
263 if (rp.disable_captions)
265 os << "<div class='float-caption'>\n";
266 docstring def = getCaptionAsHTML(os, rp);
271 int InsetCaption::getArgument(odocstream & os,
272 OutputParams const & runparams) const
274 return InsetText::latex(os, runparams);
278 int InsetCaption::getOptArg(odocstream & os,
279 OutputParams const & runparams) const
281 return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
285 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
286 OutputParams const & runparams) const
288 os << full_label_ << ' ';
289 return InsetText::plaintext(os, runparams);
293 docstring InsetCaption::getCaptionAsHTML(odocstream & os,
294 OutputParams const & runparams) const
296 os << full_label_ << ' ';
297 return InsetText::xhtml(os, runparams);
301 void InsetCaption::updateLabels(ParIterator const & it)
303 Buffer const & master = *buffer().masterBuffer();
304 DocumentClass const & tclass = master.params().documentClass();
305 string const & lang = it.paragraph().getParLanguage(master.params())->code();
306 Counters & cnts = tclass.counters();
307 string const & type = cnts.current_float();
308 // Memorize type for addToToc().
311 full_label_ = master.B_("Senseless!!! ");
313 // FIXME: life would be _much_ simpler if listings was
314 // listed in Floating.
316 if (type == "listing")
317 name = master.B_("Listing");
319 name = master.B_(tclass.floats().getType(type).name());
320 docstring counter = from_utf8(type);
321 if (cnts.isSubfloat()) {
322 counter = "sub-" + from_utf8(type);
323 name = bformat(_("Sub-%1$s"),
324 master.B_(tclass.floats().getType(type).name()));
326 if (cnts.hasCounter(counter)) {
328 full_label_ = bformat(from_ascii("%1$s %2$s:"),
330 cnts.theCounter(counter, lang));
332 full_label_ = bformat(from_ascii("%1$s #:"), name);
335 // Do the real work now.
336 InsetText::updateLabels(it);