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"
21 #include "BufferView.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
28 #include "metricsinfo.h"
29 #include "output_latex.h"
30 #include "paragraph.h"
31 #include "TocBackend.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
36 #include "support/lstrings.h"
37 #include "support/convert.h"
50 using support::bformat;
52 InsetCaption::InsetCaption(BufferParams const & bp)
53 : InsetText(bp), textclass_(bp.getLyXTextClass())
55 setAutoBreakRows(true);
57 setFrameColor(LColor::captionframe);
61 void InsetCaption::write(Buffer const & buf, ostream & os) const
68 void InsetCaption::read(Buffer const & buf, LyXLex & 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."
79 InsetText::read(buf, lex);
83 docstring const 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 (!support::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(TocList & toclist, Buffer const & buf) const
113 ParConstIterator pit = par_const_iterator_begin(*this);
115 Toc & toc = toclist[type_];
116 docstring const str = convert<docstring>(counter_)
117 + ". " + pit->asString(buf, false);
118 toc.push_back(TocItem(pit, 0, str));
122 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
124 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
126 full_label_ = _("Senseless!!! ");
128 docstring const number = convert<docstring>(counter_);
129 docstring label = custom_label_.empty()? _(type_): custom_label_;
130 full_label_ = bformat(from_ascii("%1$s %2$s:"), label, number);
132 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
133 dim.wid = labelwidth_;
135 InsetText::metrics(mi, textdim);
136 // Correct for button width, and re-fit
137 mi.base.textwidth -= dim.wid;
138 InsetText::metrics(mi, textdim);
139 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
140 dim.asc = textdim.asc;
141 dim.wid += textdim.wid;
142 dim.asc += TEXT_TO_INSET_OFFSET;
143 dim.des += TEXT_TO_INSET_OFFSET;
144 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
145 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
146 bool const changed = dim_ != dim;
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.C: setCaption).
162 labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
163 InsetText::draw(pi, x + labelwidth_, y);
164 setPosCache(pi, x, y);
168 void InsetCaption::edit(LCursor & cur, bool left)
171 InsetText::edit(cur, left);
175 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
178 return InsetText::editXY(cur, x, y);
182 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
183 FuncStatus & status) const
185 switch (cmd.action) {
187 case LFUN_CAPTION_INSERT:
188 case LFUN_FLOAT_INSERT:
189 case LFUN_FLOAT_WIDE_INSERT:
190 case LFUN_WRAP_INSERT:
191 case LFUN_PARAGRAPH_MOVE_UP:
192 case LFUN_PARAGRAPH_MOVE_DOWN:
193 case LFUN_BREAK_PARAGRAPH:
194 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
195 case LFUN_BREAK_PARAGRAPH_SKIP:
196 case LFUN_PARAGRAPH_SPACING:
197 case LFUN_PAGEBREAK_INSERT:
198 case LFUN_TABULAR_INSERT:
199 status.enabled(false);
202 case LFUN_DIALOG_SHOW_NEW_INSET:
203 case LFUN_INSET_INSERT: {
204 string const name = cmd.getArg(0);
206 || name == "graphics"
210 status.enabled(false);
215 return InsetText::getStatus(cur, cmd, status);
220 int InsetCaption::latex(Buffer const & buf, odocstream & os,
221 OutputParams const & runparams) const
223 // This is a bit too simplistic to take advantage of
224 // caption options we must add more later. (Lgb)
225 // This code is currently only able to handle the simple
226 // \caption{...}, later we will make it take advantage
227 // of the one of the caption packages. (Lgb)
229 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
231 l += InsetText::latex(buf, os, runparams);
237 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
238 OutputParams const & runparams) const
240 os << full_label_ << ' ';
241 return InsetText::plaintext(buf, os, runparams);
245 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
246 OutputParams const & runparams) const
250 ret = InsetText::docbook(buf, os, runparams);
256 auto_ptr<InsetBase> InsetCaption::doClone() const
258 return auto_ptr<InsetBase>(new InsetCaption(*this));