]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Move #includes out of header files.
[lyx.git] / src / insets / insetcaption.C
1 /**
2  * \file insetcaption.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetcaption.h"
14 #include "frontends/Painter.h"
15 #include "frontends/font_metrics.h"
16 #include "BufferView.h"
17 #include "buffer.h"
18 #include "Floating.h"
19 #include "FloatList.h"
20 #include "insets/insetfloat.h"
21 #include "insets/insetwrap.h"
22 #include "debug.h"
23 #include "gettext.h"
24 #include "Lsstream.h"
25 #include "metricsinfo.h"
26 #include "support/lstrings.h"
27 #include "support/LAssert.h"
28 #include "support/BoostFormat.h"
29
30 using namespace lyx::support;
31
32 using std::ostream;
33 using std::endl;
34
35
36 InsetCaption::InsetCaption(BufferParams const & bp)
37         : InsetText(bp)
38 {
39         setAutoBreakRows(true);
40         setDrawFrame(InsetText::LOCKED);
41         setFrameColor(LColor::captionframe);
42 }
43
44
45 void InsetCaption::write(Buffer const & buf, ostream & os) const
46 {
47         os << "Caption\n";
48         writeParagraphData(buf, os);
49 }
50
51
52 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
53 {
54 #if 0
55         // We will enably this check again when the compability
56         // code is removed from Buffer::Read (Lgb)
57         string const token = lex.GetString();
58         if (token != "Caption") {
59                 lyxerr << "InsetCaption::Read: consistency check failed."
60                        << endl;
61         }
62 #endif
63         InsetText::read(buf, lex);
64 }
65
66
67 string const InsetCaption::editMessage() const
68 {
69         return _("Opened Caption Inset");
70 }
71
72
73 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
74 {
75         // We must draw the label, we should get the label string
76         // from the enclosing float inset.
77         // The question is: Who should draw the label, the caption inset,
78         // the text inset or the paragraph?
79         // We should also draw the float number (Lgb)
80
81         // See if we can find the name of the float this caption
82         // belongs to.
83         InsetOld * i1 = owner();
84         InsetOld * i2 = i1 ? i1->owner() : 0;
85         string type;
86         if (i2->lyxCode() == FLOAT_CODE)
87                 type = static_cast<InsetFloat *>(i2)->params().type;
88         else if (i2->lyxCode() == WRAP_CODE)
89                 type = static_cast<InsetWrap *>(i2)->params().type;
90         else
91                 Assert(0);
92
93         FloatList const & floats =
94                 pi.base.bv->buffer()->params.getLyXTextClass().floats();
95         string const fl = i2 ? floats.getType(type).name() : N_("Float");
96
97         // Discover the number...
98         string const num = "#";
99
100         // Generate the label
101         string const label = bformat("%1$s %2$s:", _(fl), num);
102         int const w = font_metrics::width(label, pi.base.font);
103         pi.pain.text(x, y, label, pi.base.font);
104         InsetText::draw(pi, x + w, y);
105 }
106
107
108 int InsetCaption::latex(Buffer const & buf, ostream & os,
109                         LatexRunParams const & runparams) const
110 {
111         // This is a bit too simplistic to take advantage of
112         // caption options we must add more later. (Lgb)
113         // This code is currently only able to handle the simple
114         // \caption{...}, later we will make it take advantage
115         // of the one of the caption packages. (Lgb)
116         ostringstream ost;
117         int const l = InsetText::latex(buf, ost, runparams);
118         os << "\\caption{" << ost.str() << "}\n";
119         return l + 1;
120 }
121
122
123 int InsetCaption::ascii(Buffer const & /*buf*/,
124                         ostream & /*os*/, int /*linelen*/) const
125 {
126         // FIX: Implement me!
127         return 0;
128 }
129
130
131 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
132 {
133         int ret;
134         os << "<title>";
135         ret = InsetText::docbook(buf, os, mixcont);
136         os << "</title>\n";
137         return ret;
138 }