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;
38 InsetCaption::InsetCaption(BufferParams const & bp)
41 setAutoBreakRows(true);
42 setDrawFrame(InsetText::LOCKED);
43 setFrameColor(LColor::captionframe);
47 void InsetCaption::write(Buffer const & buf, ostream & os) const
50 writeParagraphData(buf, os);
54 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
57 // We will enably this check again when the compability
58 // code is removed from Buffer::Read (Lgb)
59 string const token = lex.GetString();
60 if (token != "Caption") {
61 lyxerr << "InsetCaption::Read: consistency check failed."
65 InsetText::read(buf, lex);
69 string const InsetCaption::editMessage() const
71 return _("Opened Caption Inset");
75 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
77 // We must draw the label, we should get the label string
78 // from the enclosing float inset.
79 // The question is: Who should draw the label, the caption inset,
80 // the text inset or the paragraph?
81 // We should also draw the float number (Lgb)
83 // See if we can find the name of the float this caption
85 InsetOld * i1 = owner();
86 InsetOld * i2 = i1 ? i1->owner() : 0;
88 if (i2->lyxCode() == FLOAT_CODE)
89 type = static_cast<InsetFloat *>(i2)->params().type;
90 else if (i2->lyxCode() == WRAP_CODE)
91 type = static_cast<InsetWrap *>(i2)->params().type;
95 FloatList const & floats =
96 pi.base.bv->buffer()->params.getLyXTextClass().floats();
97 string const fl = i2 ? floats.getType(type).name() : N_("Float");
99 // Discover the number...
100 string const num = "#";
102 // Generate the label
103 string const label = bformat("%1$s %2$s:", _(fl), num);
104 int const w = font_metrics::width(label, pi.base.font);
105 pi.pain.text(x, y, label, pi.base.font);
106 InsetText::draw(pi, x + w, y);
110 int InsetCaption::latex(Buffer const & buf, ostream & os,
111 LatexRunParams const & runparams) const
113 // This is a bit too simplistic to take advantage of
114 // caption options we must add more later. (Lgb)
115 // This code is currently only able to handle the simple
116 // \caption{...}, later we will make it take advantage
117 // of the one of the caption packages. (Lgb)
119 int const l = InsetText::latex(buf, ost, runparams);
120 os << "\\caption{" << ost.str() << "}\n";
125 int InsetCaption::ascii(Buffer const & /*buf*/,
126 ostream & /*os*/, int /*linelen*/) const
128 // FIX: Implement me!
133 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
137 ret = InsetText::docbook(buf, os, mixcont);