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"
38 using lyx::support::bformat;
44 using std::ostringstream;
47 InsetCaption::InsetCaption(BufferParams const & bp)
48 : InsetText(bp), textclass_(bp.getLyXTextClass())
50 setAutoBreakRows(true);
52 setFrameColor(LColor::captionframe);
56 void InsetCaption::write(Buffer const & buf, ostream & os) const
63 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
66 // We will enably this check again when the compability
67 // code is removed from Buffer::Read (Lgb)
68 string const token = lex.GetString();
69 if (token != "Caption") {
70 lyxerr << "InsetCaption::Read: consistency check failed."
74 InsetText::read(buf, lex);
78 docstring const InsetCaption::editMessage() const
80 return _("Opened Caption Inset");
84 void InsetCaption::cursorPos(BufferView const & bv,
85 CursorSlice const & sl, bool boundary, int & x, int & y) const
87 InsetText::cursorPos(bv, sl, boundary, x, y);
92 void InsetCaption::setLabel(LCursor & cur) const
94 // Set caption label _only_ if the cursor is in _this_ float:
95 if (cur.top().text() == &text_) {
97 size_t i = cur.depth();
100 InsetBase * const in = &cur[i].inset();
101 if (in->lyxCode() == InsetBase::FLOAT_CODE
102 || in->lyxCode() == InsetBase::WRAP_CODE) {
103 s = in->getInsetName();
107 Floating const & fl = textclass_.floats().getType(s);
113 num = convert<string>(counter_);
115 // Generate the label
116 label = lyx::to_utf8(
117 bformat(lyx::from_ascii("%1$s %2$s:"), _(s), lyx::from_ascii(num)));
122 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
124 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
125 LCursor cur = mi.base.bv->cursor();
127 docstring dlab(label.begin(), label.end());
128 labelwidth_ = theFontMetrics(mi.base.font).width(dlab);
129 dim.wid = labelwidth_;
131 InsetText::metrics(mi, textdim);
132 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
133 dim.asc = textdim.asc;
134 dim.wid += textdim.wid;
135 dim.asc += TEXT_TO_INSET_OFFSET;
136 dim.des += TEXT_TO_INSET_OFFSET;
137 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
138 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
143 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
145 // We must draw the label, we should get the label string
146 // from the enclosing float inset.
147 // The question is: Who should draw the label, the caption inset,
148 // the text inset or the paragraph?
149 // We should also draw the float number (Lgb)
151 // See if we can find the name of the float this caption
153 LCursor cur = pi.base.bv->cursor();
155 docstring dlab(label.begin(), label.end());
156 // FXIME: instead of using the fontLoader metrics, we should make
157 // painter::text() returns the drawn text witdh.
158 labelwidth_ = theFontMetrics(pi.base.font).width(dlab);
159 pi.pain.text(x, y, dlab, pi.base.font);
160 InsetText::draw(pi, x + labelwidth_, y);
161 setPosCache(pi, x, y);
165 void InsetCaption::edit(LCursor & cur, bool left)
168 InsetText::edit(cur, left);
172 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
175 return InsetText::editXY(cur, x, y);
179 int InsetCaption::latex(Buffer const & buf, ostream & os,
180 OutputParams const & runparams) const
182 // This is a bit too simplistic to take advantage of
183 // caption options we must add more later. (Lgb)
184 // This code is currently only able to handle the simple
185 // \caption{...}, later we will make it take advantage
186 // of the one of the caption packages. (Lgb)
188 int const l = InsetText::latex(buf, ost, runparams);
189 os << "\\caption{" << ost.str() << "}\n";
194 int InsetCaption::plaintext(Buffer const & /*buf*/, lyx::odocstream & /*os*/,
195 OutputParams const & /*runparams*/) const
197 // FIXME: Implement me!
202 int InsetCaption::docbook(Buffer const & buf, ostream & os,
203 OutputParams const & runparams) const
207 ret = InsetText::docbook(buf, os, runparams);
213 auto_ptr<InsetBase> InsetCaption::doClone() const
215 return auto_ptr<InsetBase>(new InsetCaption(*this));