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