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"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
32 #include "support/lstrings.h"
33 #include "support/convert.h"
40 using support::bformat;
48 InsetCaption::InsetCaption(BufferParams const & bp)
49 : InsetText(bp), textclass_(bp.getLyXTextClass())
51 setAutoBreakRows(true);
53 setFrameColor(LColor::captionframe);
57 void InsetCaption::write(Buffer const & buf, ostream & os) const
64 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
67 // We will enably this check again when the compability
68 // code is removed from Buffer::Read (Lgb)
69 string const token = lex.GetString();
70 if (token != "Caption") {
71 lyxerr << "InsetCaption::Read: consistency check failed."
75 InsetText::read(buf, lex);
79 docstring const 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::setLabel(LCursor & cur) const
95 // Set caption label _only_ if the cursor is in _this_ float:
96 if (cur.top().text() == &text_) {
98 size_t i = cur.depth();
101 InsetBase * const in = &cur[i].inset();
102 if (in->lyxCode() == InsetBase::FLOAT_CODE
103 || in->lyxCode() == InsetBase::WRAP_CODE) {
104 s = to_utf8(in->getInsetName());
108 Floating const & fl = textclass_.floats().getType(s);
114 num = convert<docstring>(counter_);
116 // Generate the label
118 bformat(from_ascii("%1$s %2$s:"), _(s), num));
123 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
125 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
126 LCursor cur = mi.base.bv->cursor();
128 docstring dlab(label.begin(), label.end());
129 labelwidth_ = theFontMetrics(mi.base.font).width(dlab);
130 dim.wid = labelwidth_;
132 InsetText::metrics(mi, textdim);
133 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
134 dim.asc = textdim.asc;
135 dim.wid += textdim.wid;
136 dim.asc += TEXT_TO_INSET_OFFSET;
137 dim.des += TEXT_TO_INSET_OFFSET;
138 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
139 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
144 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
146 // We must draw the label, we should get the label string
147 // from the enclosing float inset.
148 // The question is: Who should draw the label, the caption inset,
149 // the text inset or the paragraph?
150 // We should also draw the float number (Lgb)
152 // See if we can find the name of the float this caption
154 LCursor cur = pi.base.bv->cursor();
156 docstring dlab(label.begin(), label.end());
157 // FXIME: instead of using the fontLoader metrics, we should make
158 // painter::text() returns the drawn text witdh.
159 labelwidth_ = theFontMetrics(pi.base.font).width(dlab);
160 pi.pain.text(x, y, dlab, pi.base.font);
161 InsetText::draw(pi, x + labelwidth_, y);
162 setPosCache(pi, x, y);
166 void InsetCaption::edit(LCursor & cur, bool left)
169 InsetText::edit(cur, left);
173 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
176 return InsetText::editXY(cur, x, y);
180 int InsetCaption::latex(Buffer const & buf, odocstream & os,
181 OutputParams const & runparams) const
183 // This is a bit too simplistic to take advantage of
184 // caption options we must add more later. (Lgb)
185 // This code is currently only able to handle the simple
186 // \caption{...}, later we will make it take advantage
187 // of the one of the caption packages. (Lgb)
188 odocstringstream ost;
189 int const l = InsetText::latex(buf, ost, runparams);
190 os << "\\caption{" << ost.str() << "}\n";
195 int InsetCaption::plaintext(Buffer const & /*buf*/, odocstream & /*os*/,
196 OutputParams const & /*runparams*/) const
198 // FIXME: Implement me!
203 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
204 OutputParams const & runparams) const
208 ret = InsetText::docbook(buf, os, runparams);
214 auto_ptr<InsetBase> InsetCaption::doClone() const
216 return auto_ptr<InsetBase>(new InsetCaption(*this));