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(Buffer const & buf)
53 setAutoBreakRows(true);
55 setFrameColor(Color_captionframe);
56 // There will always be only one
57 paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
61 void InsetCaption::write(ostream & os) const
64 text_.write(buffer(), os);
68 void InsetCaption::read(Lexer & lex)
71 // We will enably this check again when the compability
72 // code is removed from Buffer::Read (Lgb)
73 string const token = lex.GetString();
74 if (token != "Caption") {
75 lyxerr << "InsetCaption::Read: consistency check failed."
83 docstring InsetCaption::editMessage() const
85 return _("Opened Caption Inset");
89 void InsetCaption::cursorPos(BufferView const & bv,
90 CursorSlice const & sl, bool boundary, int & x, int & y) const
92 InsetText::cursorPos(bv, sl, boundary, x, y);
97 void InsetCaption::setCustomLabel(docstring const & label)
99 if (!isAscii(label) || label.empty())
100 // This must be a user defined layout. We cannot translate
101 // this, since gettext accepts only ascii keys.
102 custom_label_ = label;
104 custom_label_ = _(to_ascii(label));
108 void InsetCaption::addToToc(ParConstIterator const & cpit) const
113 ParConstIterator pit = cpit;
114 pit.push_back(*this);
116 Toc & toc = buffer().tocBackend().toc(type_);
117 docstring const str = full_label_ + ". " + pit->asString(false);
118 toc.push_back(TocItem(pit, 0, str));
122 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
124 FontInfo tmpfont = mi.base.font;
125 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
126 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
127 // add some space to separate the label from the inset text
128 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
129 dim.wid = labelwidth_;
131 // Correct for button and label width
132 mi.base.textwidth -= dim.wid;
133 InsetText::metrics(mi, textdim);
134 mi.base.font = tmpfont;
135 mi.base.textwidth += dim.wid;
136 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
137 dim.asc = textdim.asc;
138 dim.wid += textdim.wid;
142 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
144 // We must draw the label, we should get the label string
145 // from the enclosing float inset.
146 // The question is: Who should draw the label, the caption inset,
147 // the text inset or the paragraph?
148 // We should also draw the float number (Lgb)
150 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
152 FontInfo tmpfont = pi.base.font;
153 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
154 pi.pain.text(x, y, full_label_, pi.base.font);
155 InsetText::draw(pi, x + labelwidth_, y);
156 pi.base.font = tmpfont;
160 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
163 InsetText::edit(cur, front, entry_from);
167 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
170 return InsetText::editXY(cur, x, y);
174 bool InsetCaption::insetAllowed(InsetCode code) const
185 return InsetText::insetAllowed(code);
190 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
191 FuncStatus & status) const
193 switch (cmd.action) {
195 case LFUN_BREAK_PARAGRAPH:
196 case LFUN_BREAK_PARAGRAPH_SKIP:
197 status.enabled(false);
200 case LFUN_OPTIONAL_INSERT:
201 status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
205 return InsetText::getStatus(cur, cmd, status);
210 int InsetCaption::latex(odocstream & os,
211 OutputParams const & runparams_in) const
214 // caption is output as an optional argument
216 // This is a bit too simplistic to take advantage of
217 // caption options we must add more later. (Lgb)
218 // This code is currently only able to handle the simple
219 // \caption{...}, later we will make it take advantage
220 // of the one of the caption packages. (Lgb)
221 OutputParams runparams = runparams_in;
222 // FIXME: actually, it is moving only when there is no
223 // optional argument.
224 runparams.moving_arg = true;
226 int l = latexOptArgInsets(paragraphs()[0], os, runparams, 1);
228 l += InsetText::latex(os, runparams);
230 runparams_in.encoding = runparams.encoding;
235 int InsetCaption::plaintext(odocstream & os,
236 OutputParams const & runparams) const
238 os << '[' << full_label_ << "\n";
239 InsetText::plaintext(os, runparams);
242 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
246 int InsetCaption::docbook(odocstream & os,
247 OutputParams const & runparams) const
251 ret = InsetText::docbook(os, runparams);
257 int InsetCaption::getArgument(odocstream & os,
258 OutputParams const & runparams) const
260 return InsetText::latex(os, runparams);
264 int InsetCaption::getOptArg(odocstream & os,
265 OutputParams const & runparams) const
267 return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
271 void InsetCaption::updateLabels(ParIterator const & it)
273 DocumentClass const & tclass = buffer().params().documentClass();
274 Counters & cnts = tclass.counters();
275 string const & type = cnts.current_float();
276 // Memorize type for addToToc().
278 in_subfloat_ = cnts.isSubfloat();
280 full_label_ = buffer().B_("Senseless!!! ");
282 // FIXME: life would be _much_ simpler if listings was
283 // listed in Floating.
285 if (type == "listing")
286 name = buffer().B_("Listing");
288 name = buffer().B_(tclass.floats().getType(type).name());
289 docstring counter = from_utf8(type);
291 counter = "sub-" + from_utf8(type);
292 name = bformat(_("Sub-%1$s"),
293 buffer().B_(tclass.floats().getType(type).name()));
295 if (cnts.hasCounter(counter)) {
297 full_label_ = bformat(from_ascii("%1$s %2$s:"),
299 cnts.theCounter(counter));
301 full_label_ = bformat(from_ascii("%1$s #:"), name);
304 // Do the real work now.
305 InsetText::updateLabels(it);