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 "BufferView.h"
20 #include "FloatList.h"
22 #include "metricsinfo.h"
24 #include "frontends/Painter.h"
25 #include "frontends/font_metrics.h"
27 #include "support/LAssert.h"
28 #include "support/lstrings.h"
30 #include "support/std_sstream.h"
32 using namespace lyx::support;
36 using std::ostringstream;
39 InsetCaption::InsetCaption(BufferParams const & bp)
42 setAutoBreakRows(true);
43 setDrawFrame(InsetText::LOCKED);
44 setFrameColor(LColor::captionframe);
48 void InsetCaption::write(Buffer const & buf, ostream & os) const
51 writeParagraphData(buf, os);
55 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
58 // We will enably this check again when the compability
59 // code is removed from Buffer::Read (Lgb)
60 string const token = lex.GetString();
61 if (token != "Caption") {
62 lyxerr << "InsetCaption::Read: consistency check failed."
66 InsetText::read(buf, lex);
70 string const InsetCaption::editMessage() const
72 return _("Opened Caption Inset");
76 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
78 // We must draw the label, we should get the label string
79 // from the enclosing float inset.
80 // The question is: Who should draw the label, the caption inset,
81 // the text inset or the paragraph?
82 // We should also draw the float number (Lgb)
84 // See if we can find the name of the float this caption
86 InsetOld * i1 = owner();
87 InsetOld * i2 = i1 ? i1->owner() : 0;
89 if (i2->lyxCode() == FLOAT_CODE)
90 type = static_cast<InsetFloat *>(i2)->params().type;
91 else if (i2->lyxCode() == WRAP_CODE)
92 type = static_cast<InsetWrap *>(i2)->params().type;
96 FloatList const & floats =
97 pi.base.bv->buffer()->params.getLyXTextClass().floats();
98 string const fl = i2 ? floats.getType(type).name() : N_("Float");
100 // Discover the number...
101 string const num = "#";
103 // Generate the label
104 string const label = bformat("%1$s %2$s:", _(fl), num);
105 int const w = font_metrics::width(label, pi.base.font);
106 pi.pain.text(x, y, label, pi.base.font);
107 InsetText::draw(pi, x + w, y);
111 int InsetCaption::latex(Buffer const & buf, ostream & os,
112 LatexRunParams const & runparams) const
114 // This is a bit too simplistic to take advantage of
115 // caption options we must add more later. (Lgb)
116 // This code is currently only able to handle the simple
117 // \caption{...}, later we will make it take advantage
118 // of the one of the caption packages. (Lgb)
120 int const l = InsetText::latex(buf, ost, runparams);
121 os << "\\caption{" << ost.str() << "}\n";
126 int InsetCaption::ascii(Buffer const & /*buf*/,
127 ostream & /*os*/, int /*linelen*/) const
129 // FIX: Implement me!
134 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
138 ret = InsetText::docbook(buf, os, mixcont);