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 "frontends/Painter.h"
15 #include "frontends/font_metrics.h"
16 #include "BufferView.h"
18 #include "FloatList.h"
19 #include "insets/insetfloat.h"
20 #include "insets/insetwrap.h"
24 #include "metricsinfo.h"
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "support/BoostFormat.h"
29 using namespace lyx::support;
35 InsetCaption::InsetCaption(BufferParams const & bp)
38 setAutoBreakRows(true);
39 setDrawFrame(InsetText::LOCKED);
40 setFrameColor(LColor::captionframe);
44 void InsetCaption::write(Buffer const * buf, ostream & os) const
47 writeParagraphData(buf, os);
51 void InsetCaption::read(Buffer const * buf, LyXLex & lex)
54 // We will enably this check again when the compability
55 // code is removed from Buffer::Read (Lgb)
56 string const token = lex.GetString();
57 if (token != "Caption") {
58 lyxerr << "InsetCaption::Read: consistency check failed."
62 InsetText::read(buf, lex);
66 string const InsetCaption::editMessage() const
68 return _("Opened Caption Inset");
72 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
74 // We must draw the label, we should get the label string
75 // from the enclosing float inset.
76 // The question is: Who should draw the label, the caption inset,
77 // the text inset or the paragraph?
78 // We should also draw the float number (Lgb)
80 // See if we can find the name of the float this caption
82 InsetOld * i1 = owner();
83 InsetOld * i2 = i1 ? i1->owner() : 0;
85 if (i2->lyxCode() == FLOAT_CODE)
86 type = static_cast<InsetFloat *>(i2)->params().type;
87 else if (i2->lyxCode() == WRAP_CODE)
88 type = static_cast<InsetWrap *>(i2)->params().type;
92 FloatList const & floats =
93 pi.base.bv->buffer()->params.getLyXTextClass().floats();
94 string const fl = i2 ? floats.getType(type).name() : N_("Float");
96 // Discover the number...
97 string const num = "#";
100 string const label = bformat("%1$s %2$s:", _(fl), num);
101 int const w = font_metrics::width(label, pi.base.font);
102 pi.pain.text(x, y, label, pi.base.font);
103 InsetText::draw(pi, x + w, y);
107 int InsetCaption::latex(Buffer const * buf, ostream & os,
108 LatexRunParams const & runparams) const
110 // This is a bit too simplistic to take advantage of
111 // caption options we must add more later. (Lgb)
112 // This code is currently only able to handle the simple
113 // \caption{...}, later we will make it take advantage
114 // of the one of the caption packages. (Lgb)
116 int const l = InsetText::latex(buf, ost, runparams);
117 os << "\\caption{" << ost.str() << "}\n";
122 int InsetCaption::ascii(Buffer const * /*buf*/,
123 ostream & /*os*/, int /*linelen*/) const
125 // FIX: Implement me!
130 int InsetCaption::docbook(Buffer const * buf, ostream & os, bool mixcont) const
134 ret = InsetText::docbook(buf, os, mixcont);