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;
40 using std::ostringstream;
43 InsetCaption::InsetCaption(BufferParams const & bp)
46 setAutoBreakRows(true);
48 setFrameColor(LColor::captionframe);
52 void InsetCaption::write(Buffer const & buf, ostream & os) const
59 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
62 // We will enably this check again when the compability
63 // code is removed from Buffer::Read (Lgb)
64 string const token = lex.GetString();
65 if (token != "Caption") {
66 lyxerr << "InsetCaption::Read: consistency check failed."
70 InsetText::read(buf, lex);
74 string const InsetCaption::editMessage() const
76 return _("Opened Caption Inset");
80 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
82 // We must draw the label, we should get the label string
83 // from the enclosing float inset.
84 // The question is: Who should draw the label, the caption inset,
85 // the text inset or the paragraph?
86 // We should also draw the float number (Lgb)
88 // See if we can find the name of the float this caption
91 InsetOld * i1 = owner();
92 InsetOld * i2 = i1 ? i1->owner() : 0;
94 if (i2->lyxCode() == FLOAT_CODE)
96 #warning Now, what happens for i2 == 0?
98 type = static_cast<InsetFloat *>(i2)->params().type;
99 else if (i2->lyxCode() == WRAP_CODE)
100 type = static_cast<InsetWrap *>(i2)->params().type;
104 FloatList const & floats =
105 pi.base.bv->buffer()->params().getLyXTextClass().floats();
106 string const fl = i2 ? floats.getType(type).name() : N_("Float");
108 string type = "float";
109 string const fl = N_("Float");
112 // Discover the number...
113 string const num = "#";
115 // Generate the label
116 string const label = bformat("%1$s %2$s:", _(fl), num);
117 int const w = font_metrics::width(label, pi.base.font);
118 pi.pain.text(x, y, label, pi.base.font);
119 InsetText::draw(pi, x + w, y);
123 int InsetCaption::latex(Buffer const & buf, ostream & os,
124 OutputParams const & runparams) const
126 // This is a bit too simplistic to take advantage of
127 // caption options we must add more later. (Lgb)
128 // This code is currently only able to handle the simple
129 // \caption{...}, later we will make it take advantage
130 // of the one of the caption packages. (Lgb)
132 int const l = InsetText::latex(buf, ost, runparams);
133 os << "\\caption{" << ost.str() << "}\n";
138 int InsetCaption::plaintext(Buffer const & /*buf*/,ostream & /*os*/,
139 OutputParams const & /*runparams*/) const
141 // FIX: Implement me!
146 int InsetCaption::docbook(Buffer const & buf, ostream & os,
147 OutputParams const & runparams) const
151 ret = InsetText::docbook(buf, os, runparams);