]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Pimpl Buffer.
[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 "insetfloat.h"
15 #include "insetwrap.h"
16
17 #include "buffer.h"
18 #include "bufferparams.h"
19 #include "BufferView.h"
20 #include "Floating.h"
21 #include "FloatList.h"
22 #include "gettext.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/LAssert.h"
30 #include "support/lstrings.h"
31
32 #include "support/std_sstream.h"
33
34 using namespace lyx::support;
35
36 using std::endl;
37 using std::ostream;
38 using std::ostringstream;
39
40
41 InsetCaption::InsetCaption(BufferParams const & bp)
42         : InsetText(bp)
43 {
44         setAutoBreakRows(true);
45         setDrawFrame(InsetText::LOCKED);
46         setFrameColor(LColor::captionframe);
47 }
48
49
50 void InsetCaption::write(Buffer const & buf, ostream & os) const
51 {
52         os << "Caption\n";
53         writeParagraphData(buf, os);
54 }
55
56
57 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
58 {
59 #if 0
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."
65                        << endl;
66         }
67 #endif
68         InsetText::read(buf, lex);
69 }
70
71
72 string const InsetCaption::editMessage() const
73 {
74         return _("Opened Caption Inset");
75 }
76
77
78 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
79 {
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)
85
86         // See if we can find the name of the float this caption
87         // belongs to.
88         InsetOld * i1 = owner();
89         InsetOld * i2 = i1 ? i1->owner() : 0;
90         string type;
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;
95         else
96                 Assert(0);
97
98         FloatList const & floats =
99                 pi.base.bv->buffer()->params().getLyXTextClass().floats();
100         string const fl = i2 ? floats.getType(type).name() : N_("Float");
101
102         // Discover the number...
103         string const num = "#";
104
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);
110 }
111
112
113 int InsetCaption::latex(Buffer const & buf, ostream & os,
114                         LatexRunParams const & runparams) const
115 {
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)
121         ostringstream ost;
122         int const l = InsetText::latex(buf, ost, runparams);
123         os << "\\caption{" << ost.str() << "}\n";
124         return l + 1;
125 }
126
127
128 int InsetCaption::ascii(Buffer const & /*buf*/,
129                         ostream & /*os*/, int /*linelen*/) const
130 {
131         // FIX: Implement me!
132         return 0;
133 }
134
135
136 int InsetCaption::docbook(Buffer const & buf, ostream & os, bool mixcont) const
137 {
138         int ret;
139         os << "<title>";
140         ret = InsetText::docbook(buf, os, mixcont);
141         os << "</title>\n";
142         return ret;
143 }