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"
28 #include "MetricsInfo.h"
29 #include "output_latex.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
39 #include "support/gettext.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 lex.setContext("InsetCaption::Read: consistency check");
80 docstring InsetCaption::editMessage() const
82 return _("Opened Caption Inset");
86 void InsetCaption::cursorPos(BufferView const & bv,
87 CursorSlice const & sl, bool boundary, int & x, int & y) const
89 InsetText::cursorPos(bv, sl, boundary, x, y);
94 void InsetCaption::setCustomLabel(docstring const & label)
96 if (!isAscii(label) || label.empty())
97 // This must be a user defined layout. We cannot translate
98 // this, since gettext accepts only ascii keys.
99 custom_label_ = label;
101 custom_label_ = _(to_ascii(label));
105 void InsetCaption::addToToc(DocIterator const & cpit)
110 DocIterator pit = cpit;
111 pit.push_back(CursorSlice(*this));
113 Toc & toc = buffer().tocBackend().toc(type_);
114 docstring const str = full_label_ + ". " + text().getPar(0).asString();
115 toc.push_back(TocItem(pit, 0, str));
117 // Proceed with the rest of the inset.
118 InsetText::addToToc(cpit);
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 status.setEnabled(false);
199 case LFUN_OPTIONAL_INSERT:
200 status.setEnabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
203 case LFUN_INSET_TOGGLE:
204 // pass back to owner
209 return InsetText::getStatus(cur, cmd, status);
214 int InsetCaption::latex(odocstream & os,
215 OutputParams const & runparams_in) const
218 // caption is output as an optional argument
220 // This is a bit too simplistic to take advantage of
221 // caption options we must add more later. (Lgb)
222 // This code is currently only able to handle the simple
223 // \caption{...}, later we will make it take advantage
224 // of the one of the caption packages. (Lgb)
225 OutputParams runparams = runparams_in;
226 // FIXME: actually, it is moving only when there is no
227 // optional argument.
228 runparams.moving_arg = true;
230 int l = latexOptArgInsets(paragraphs()[0], os, runparams, 1);
232 l += InsetText::latex(os, runparams);
234 runparams_in.encoding = runparams.encoding;
239 int InsetCaption::plaintext(odocstream & os,
240 OutputParams const & runparams) const
242 os << '[' << full_label_ << "\n";
243 InsetText::plaintext(os, runparams);
246 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
250 int InsetCaption::docbook(odocstream & os,
251 OutputParams const & runparams) const
255 ret = InsetText::docbook(os, runparams);
261 int InsetCaption::getArgument(odocstream & os,
262 OutputParams const & runparams) const
264 return InsetText::latex(os, runparams);
268 int InsetCaption::getOptArg(odocstream & os,
269 OutputParams const & runparams) const
271 return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
275 int InsetCaption::getCaptionText(odocstream & os,
276 OutputParams const & runparams) const
278 os << full_label_ << ' ';
279 return InsetText::plaintext(os, runparams);
283 void InsetCaption::updateLabels(ParIterator const & it)
285 Buffer const & master = *buffer().masterBuffer();
286 DocumentClass const & tclass = master.params().documentClass();
287 Counters & cnts = tclass.counters();
288 string const & type = cnts.current_float();
289 // Memorize type for addToToc().
291 in_subfloat_ = cnts.isSubfloat();
293 full_label_ = master.B_("Senseless!!! ");
295 // FIXME: life would be _much_ simpler if listings was
296 // listed in Floating.
298 if (type == "listing")
299 name = master.B_("Listing");
301 name = master.B_(tclass.floats().getType(type).name());
302 docstring counter = from_utf8(type);
304 counter = "sub-" + from_utf8(type);
305 name = bformat(_("Sub-%1$s"),
306 master.B_(tclass.floats().getType(type).name()));
308 if (cnts.hasCounter(counter)) {
310 full_label_ = bformat(from_ascii("%1$s %2$s:"),
312 cnts.theCounter(counter));
314 full_label_ = bformat(from_ascii("%1$s #:"), name);
317 // Do the real work now.
318 InsetText::updateLabels(it);