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)
53 setAutoBreakRows(true);
55 setFrameColor(Color_captionframe);
59 InsetCaption::InsetCaption(BufferParams const & bp)
62 setAutoBreakRows(true);
64 setFrameColor(Color_captionframe);
65 //FIXME Do we need to set all paragraphs here? or will there
67 paragraphs().back().setLayout(bp.documentClass().emptyLayout());
71 void InsetCaption::write(ostream & os) const
74 text_.write(buffer(), os);
78 void InsetCaption::read(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."
93 docstring 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(ParConstIterator const & cpit) const
123 ParConstIterator pit = cpit;
124 pit.push_back(*this);
126 Toc & toc = buffer().tocBackend().toc(type_);
127 docstring const str = full_label_ + ". " + pit->asString(false);
128 toc.push_back(TocItem(pit, 0, str));
132 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
134 FontInfo tmpfont = mi.base.font;
135 mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
136 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
137 // add some space to separate the label from the inset text
138 labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
139 dim.wid = labelwidth_;
141 // Correct for button and label width
142 mi.base.textwidth -= dim.wid;
143 InsetText::metrics(mi, textdim);
144 mi.base.font = tmpfont;
145 mi.base.textwidth += dim.wid;
146 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
147 dim.asc = textdim.asc;
148 dim.wid += textdim.wid;
152 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
154 // We must draw the label, we should get the label string
155 // from the enclosing float inset.
156 // The question is: Who should draw the label, the caption inset,
157 // the text inset or the paragraph?
158 // We should also draw the float number (Lgb)
160 // Answer: the text inset (in buffer_funcs.cpp: setCaption).
162 FontInfo tmpfont = pi.base.font;
163 pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
164 pi.pain.text(x, y, full_label_, pi.base.font);
165 InsetText::draw(pi, x + labelwidth_, y);
166 pi.base.font = tmpfont;
170 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
173 InsetText::edit(cur, front, entry_from);
177 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
180 return InsetText::editXY(cur, x, y);
184 bool InsetCaption::insetAllowed(InsetCode code) const
195 return InsetText::insetAllowed(code);
200 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
201 FuncStatus & status) const
203 switch (cmd.action) {
205 case LFUN_BREAK_PARAGRAPH:
206 case LFUN_BREAK_PARAGRAPH_SKIP:
207 status.enabled(false);
210 case LFUN_OPTIONAL_INSERT:
211 status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
215 return InsetText::getStatus(cur, cmd, status);
220 int InsetCaption::latex(odocstream & os,
221 OutputParams const & runparams_in) const
224 // caption is output as an optional argument
226 // This is a bit too simplistic to take advantage of
227 // caption options we must add more later. (Lgb)
228 // This code is currently only able to handle the simple
229 // \caption{...}, later we will make it take advantage
230 // of the one of the caption packages. (Lgb)
231 OutputParams runparams = runparams_in;
232 // FIXME: actually, it is moving only when there is no
233 // optional argument.
234 runparams.moving_arg = true;
236 int l = latexOptArgInsets(paragraphs()[0], os, runparams, 1);
238 l += InsetText::latex(os, runparams);
240 runparams_in.encoding = runparams.encoding;
245 int InsetCaption::plaintext(odocstream & os,
246 OutputParams const & runparams) const
248 os << '[' << full_label_ << "\n";
249 InsetText::plaintext(os, runparams);
252 return PLAINTEXT_NEWLINE + 1; // one char on a separate line
256 int InsetCaption::docbook(odocstream & os,
257 OutputParams const & runparams) const
261 ret = InsetText::docbook(os, runparams);
267 int InsetCaption::getArgument(odocstream & os,
268 OutputParams const & runparams) const
270 return InsetText::latex(os, runparams);
274 int InsetCaption::getOptArg(odocstream & os,
275 OutputParams const & runparams) const
277 return latexOptArgInsets(paragraphs()[0], os, runparams, 1);
281 void InsetCaption::updateLabels(ParIterator const & it)
283 DocumentClass const & tclass = buffer().params().documentClass();
284 Counters & cnts = tclass.counters();
285 string const & type = cnts.current_float();
286 // Memorize type for addToToc().
288 in_subfloat_ = cnts.isSubfloat();
290 full_label_ = buffer().B_("Senseless!!! ");
292 // FIXME: life would be _much_ simpler if listings was
293 // listed in Floating.
295 if (type == "listing")
296 name = buffer().B_("Listing");
298 name = buffer().B_(tclass.floats().getType(type).name());
299 docstring counter = from_utf8(type);
301 counter = "sub-" + from_utf8(type);
302 name = bformat(_("Sub-%1$s"),
303 buffer().B_(tclass.floats().getType(type).name()));
305 if (cnts.hasCounter(counter)) {
307 full_label_ = bformat(from_ascii("%1$s %2$s:"),
309 cnts.theCounter(counter));
311 full_label_ = bformat(from_ascii("%1$s #:"), name);
314 // Do the real work now.
315 InsetText::updateLabels(it);
319 Inset * InsetCaption::clone() const
321 return new InsetCaption(*this);