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/Application.h"
30 #include "frontends/FontLoader.h"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
34 #include "support/lstrings.h"
35 #include "support/convert.h"
40 using lyx::support::bformat;
46 using std::ostringstream;
49 InsetCaption::InsetCaption(BufferParams const & bp)
50 : InsetText(bp), textclass_(bp.getLyXTextClass())
52 setAutoBreakRows(true);
54 setFrameColor(LColor::captionframe);
58 void InsetCaption::write(Buffer const & buf, ostream & os) const
65 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
68 // We will enably this check again when the compability
69 // code is removed from Buffer::Read (Lgb)
70 string const token = lex.GetString();
71 if (token != "Caption") {
72 lyxerr << "InsetCaption::Read: consistency check failed."
76 InsetText::read(buf, lex);
80 docstring const InsetCaption::editMessage() const
82 return _("Opened Caption Inset");
86 void InsetCaption::cursorPos
87 (CursorSlice const & sl, bool boundary, int & x, int & y) const
89 InsetText::cursorPos(sl, boundary, x, y);
94 void InsetCaption::setLabel(LCursor & cur) const
96 // Set caption label _only_ if the cursor is in _this_ float:
97 if (cur.top().text() == &text_) {
99 size_t i = cur.depth();
102 InsetBase * const in = &cur[i].inset();
103 if (in->lyxCode() == InsetBase::FLOAT_CODE
104 || in->lyxCode() == InsetBase::WRAP_CODE) {
105 s = in->getInsetName();
109 Floating const & fl = textclass_.floats().getType(s);
115 num = convert<string>(counter_);
117 // Generate the label
118 label = lyx::to_utf8(
119 bformat(lyx::from_ascii("%1$s %2$s:"), _(s), lyx::from_ascii(num)));
124 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
126 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
127 LCursor cur = mi.base.bv->cursor();
129 docstring dlab(label.begin(), label.end());
130 labelwidth_ = theApp->fontLoader().metrics(mi.base.font).width(dlab);
131 dim.wid = labelwidth_;
133 InsetText::metrics(mi, textdim);
134 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
135 dim.asc = textdim.asc;
136 dim.wid += textdim.wid;
137 dim.asc += TEXT_TO_INSET_OFFSET;
138 dim.des += TEXT_TO_INSET_OFFSET;
139 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
140 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
145 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
147 // We must draw the label, we should get the label string
148 // from the enclosing float inset.
149 // The question is: Who should draw the label, the caption inset,
150 // the text inset or the paragraph?
151 // We should also draw the float number (Lgb)
153 // See if we can find the name of the float this caption
155 LCursor cur = pi.base.bv->cursor();
157 docstring dlab(label.begin(), label.end());
158 // FXIME: instead of using the fontLoader metrics, we should make
159 // painter::text() returns the drawn text witdh.
160 labelwidth_ = theApp->fontLoader().metrics(pi.base.font).width(dlab);
161 pi.pain.text(x, y, dlab, pi.base.font);
162 InsetText::draw(pi, x + labelwidth_, y);
163 setPosCache(pi, x, y);
167 void InsetCaption::edit(LCursor & cur, bool left)
170 InsetText::edit(cur, left);
174 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
177 return InsetText::editXY(cur, x, y);
181 int InsetCaption::latex(Buffer const & buf, ostream & os,
182 OutputParams const & runparams) const
184 // This is a bit too simplistic to take advantage of
185 // caption options we must add more later. (Lgb)
186 // This code is currently only able to handle the simple
187 // \caption{...}, later we will make it take advantage
188 // of the one of the caption packages. (Lgb)
190 int const l = InsetText::latex(buf, ost, runparams);
191 os << "\\caption{" << ost.str() << "}\n";
196 int InsetCaption::plaintext(Buffer const & /*buf*/,ostream & /*os*/,
197 OutputParams const & /*runparams*/) const
199 // FIXME: Implement me!
204 int InsetCaption::docbook(Buffer const & buf, ostream & os,
205 OutputParams const & runparams) const
209 ret = InsetText::docbook(buf, os, runparams);
215 auto_ptr<InsetBase> InsetCaption::doClone() const
217 return auto_ptr<InsetBase>(new InsetCaption(*this));