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