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