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"
23 #include "paragraph.h"
25 #include "frontends/Painter.h"
26 #include "frontends/font_metrics.h"
28 #include "support/LAssert.h"
29 #include "support/lstrings.h"
31 #include "support/std_sstream.h"
33 using namespace lyx::support;
37 using std::ostringstream;
40 InsetCaption::InsetCaption(BufferParams const & bp)
43 setAutoBreakRows(true);
44 setDrawFrame(InsetText::LOCKED);
45 setFrameColor(LColor::captionframe);
49 void InsetCaption::write(Buffer const & buf, ostream & os) const
52 writeParagraphData(buf, os);
56 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
59 // We will enably this check again when the compability
60 // code is removed from Buffer::Read (Lgb)
61 string const token = lex.GetString();
62 if (token != "Caption") {
63 lyxerr << "InsetCaption::Read: consistency check failed."
67 InsetText::read(buf, lex);
71 string const InsetCaption::editMessage() const
73 return _("Opened Caption Inset");
77 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
79 // We must draw the label, we should get the label string
80 // from the enclosing float inset.
81 // The question is: Who should draw the label, the caption inset,
82 // the text inset or the paragraph?
83 // We should also draw the float number (Lgb)
85 // See if we can find the name of the float this caption
87 InsetOld * i1 = owner();
88 InsetOld * i2 = i1 ? i1->owner() : 0;
90 if (i2->lyxCode() == FLOAT_CODE)
91 type = static_cast<InsetFloat *>(i2)->params().type;
92 else if (i2->lyxCode() == WRAP_CODE)
93 type = static_cast<InsetWrap *>(i2)->params().type;
97 FloatList const & floats =
98 pi.base.bv->buffer()->params.getLyXTextClass().floats();
99 string const fl = i2 ? floats.getType(type).name() : N_("Float");
101 // Discover the number...
102 string const num = "#";
104 // Generate the label
105 string const label = bformat("%1$s %2$s:", _(fl), num);
106 int const w = font_metrics::width(label, pi.base.font);
107 pi.pain.text(x, y, label, pi.base.font);
108 InsetText::draw(pi, x + w, y);
112 int InsetCaption::latex(Buffer const & buf, ostream & os,
113 LatexRunParams const & runparams) const
115 // This is a bit too simplistic to take advantage of
116 // caption options we must add more later. (Lgb)
117 // This code is currently only able to handle the simple
118 // \caption{...}, later we will make it take advantage
119 // of the one of the caption packages. (Lgb)
121 int const l = InsetText::latex(buf, ost, runparams);
122 os << "\\caption{" << ost.str() << "}\n";
127 int InsetCaption::ascii(Buffer const & /*buf*/,
128 ostream & /*os*/, int /*linelen*/) const
130 // FIX: Implement me!
135 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
139 ret = InsetText::docbook(buf, os, mixcont);