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"
19 #include "BufferView.h"
21 #include "FloatList.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lstrings.h"
35 using lyx::support::bformat;
41 using std::ostringstream;
44 InsetCaption::InsetCaption(BufferParams const & bp)
47 setAutoBreakRows(true);
49 setFrameColor(LColor::captionframe);
53 void InsetCaption::write(Buffer const & buf, ostream & os) const
60 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
63 // We will enably this check again when the compability
64 // code is removed from Buffer::Read (Lgb)
65 string const token = lex.GetString();
66 if (token != "Caption") {
67 lyxerr << "InsetCaption::Read: consistency check failed."
71 InsetText::read(buf, lex);
75 string const InsetCaption::editMessage() const
77 return _("Opened Caption Inset");
81 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
83 // We must draw the label, we should get the label string
84 // from the enclosing float inset.
85 // The question is: Who should draw the label, the caption inset,
86 // the text inset or the paragraph?
87 // We should also draw the float number (Lgb)
89 // See if we can find the name of the float this caption
92 InsetBase * i1 = owner();
93 InsetBase * i2 = i1 ? i1->owner() : 0;
95 if (i2->lyxCode() == FLOAT_CODE)
97 #warning Now, what happens for i2 == 0?
99 type = static_cast<InsetFloat *>(i2)->params().type;
100 else if (i2->lyxCode() == WRAP_CODE)
101 type = static_cast<InsetWrap *>(i2)->params().type;
105 FloatList const & floats =
106 pi.base.bv->buffer()->params().getLyXTextClass().floats();
107 string const fl = i2 ? floats.getType(type).name() : N_("Float");
109 string type = "float";
110 string const fl = N_("Float");
113 // Discover the number...
114 string const num = "#";
116 // Generate the label
117 string const label = bformat("%1$s %2$s:", _(fl), num);
118 int const w = font_metrics::width(label, pi.base.font);
119 pi.pain.text(x, y, label, pi.base.font);
120 InsetText::draw(pi, x + w, y);
124 int InsetCaption::latex(Buffer const & buf, ostream & os,
125 OutputParams const & runparams) const
127 // This is a bit too simplistic to take advantage of
128 // caption options we must add more later. (Lgb)
129 // This code is currently only able to handle the simple
130 // \caption{...}, later we will make it take advantage
131 // of the one of the caption packages. (Lgb)
133 int const l = InsetText::latex(buf, ost, runparams);
134 os << "\\caption{" << ost.str() << "}\n";
139 int InsetCaption::plaintext(Buffer const & /*buf*/,ostream & /*os*/,
140 OutputParams const & /*runparams*/) const
142 // FIXME: Implement me!
147 int InsetCaption::docbook(Buffer const & buf, ostream & os,
148 OutputParams const & runparams) const
152 ret = InsetText::docbook(buf, os, runparams);
158 auto_ptr<InsetBase> InsetCaption::doClone() const
160 return auto_ptr<InsetBase>(new InsetCaption(*this));