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"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
29 #include "support/lstrings.h"
31 #include "support/std_sstream.h"
33 using lyx::support::bformat;
38 using std::ostringstream;
41 InsetCaption::InsetCaption(BufferParams const & bp)
44 setAutoBreakRows(true);
45 setDrawFrame(InsetText::LOCKED);
46 setFrameColor(LColor::captionframe);
50 void InsetCaption::write(Buffer const & buf, ostream & os) const
53 writeParagraphData(buf, os);
57 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
60 // We will enably this check again when the compability
61 // code is removed from Buffer::Read (Lgb)
62 string const token = lex.GetString();
63 if (token != "Caption") {
64 lyxerr << "InsetCaption::Read: consistency check failed."
68 InsetText::read(buf, lex);
72 string const InsetCaption::editMessage() const
74 return _("Opened Caption Inset");
78 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
80 // We must draw the label, we should get the label string
81 // from the enclosing float inset.
82 // The question is: Who should draw the label, the caption inset,
83 // the text inset or the paragraph?
84 // We should also draw the float number (Lgb)
86 // See if we can find the name of the float this caption
88 InsetOld * i1 = owner();
89 InsetOld * i2 = i1 ? i1->owner() : 0;
91 if (i2->lyxCode() == FLOAT_CODE)
92 type = static_cast<InsetFloat *>(i2)->params().type;
93 else if (i2->lyxCode() == WRAP_CODE)
94 type = static_cast<InsetWrap *>(i2)->params().type;
98 FloatList const & floats =
99 pi.base.bv->buffer()->params().getLyXTextClass().floats();
100 string const fl = i2 ? floats.getType(type).name() : N_("Float");
102 // Discover the number...
103 string const num = "#";
105 // Generate the label
106 string const label = bformat("%1$s %2$s:", _(fl), num);
107 int const w = font_metrics::width(label, pi.base.font);
108 pi.pain.text(x, y, label, pi.base.font);
109 InsetText::draw(pi, x + w, y);
113 int InsetCaption::latex(Buffer const & buf, ostream & os,
114 LatexRunParams const & runparams) const
116 // This is a bit too simplistic to take advantage of
117 // caption options we must add more later. (Lgb)
118 // This code is currently only able to handle the simple
119 // \caption{...}, later we will make it take advantage
120 // of the one of the caption packages. (Lgb)
122 int const l = InsetText::latex(buf, ost, runparams);
123 os << "\\caption{" << ost.str() << "}\n";
128 int InsetCaption::ascii(Buffer const & /*buf*/,
129 ostream & /*os*/, int /*linelen*/) const
131 // FIX: Implement me!
136 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
140 ret = InsetText::docbook(buf, os, mixcont);