]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
The markDirty() and fitCursor() changes
[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 "debug.h"
22 #include "gettext.h"
23 #include "support/lstrings.h"
24
25 #include "BoostFormat.h"
26
27 using std::ostream;
28 using std::endl;
29
30 InsetCaption::InsetCaption(BufferParams const & bp)
31         : InsetText(bp)
32 {
33         setAutoBreakRows(true);
34         setDrawFrame(0, InsetText::LOCKED);
35         setFrameColor(0, LColor::captionframe);
36 }
37
38
39 void InsetCaption::write(Buffer const * buf, ostream & os) const
40 {
41         os << "Caption\n";
42         writeParagraphData(buf, os);
43 }
44
45
46
47 void InsetCaption::read(Buffer const * buf, LyXLex & lex)
48 {
49 #if 0
50         // We will enably this check again when the compability
51         // code is removed from Buffer::Read (Lgb)
52         string const token = lex.GetString();
53         if (token != "Caption") {
54                 lyxerr << "InsetCaption::Read: consistency check failed."
55                        << endl;
56         }
57 #endif
58         InsetText::read(buf, lex);
59 }
60
61
62 string const InsetCaption::editMessage() const
63 {
64         return _("Opened Caption Inset");
65 }
66
67
68 void InsetCaption::draw(BufferView * bv, LyXFont const & f,
69                         int baseline, float & x) const
70 {
71         // We must draw the label, we should get the label string
72         // from the enclosing float inset.
73         // The question is: Who should draw the label, the caption inset,
74         // the text inset or the paragraph?
75         // We should also draw the float number (Lgb)
76
77         // See if we can find the name of the float this caption
78         // belongs to.
79         Inset * i1 = owner();
80         Inset * i2 = i1 ? i1->owner() : 0;
81         string const type = static_cast<InsetFloat *>(i2)->type();
82         FloatList const & floats =
83                 bv->buffer()->params.getLyXTextClass().floats();
84         string const fl = i2 ? floats.getType(type).name() : N_("Float");
85
86         // Discover the number...
87         // ...
88         string const num("#");
89
90 #if USE_BOOST_FORMAT
91         // Generate the label
92         boost::format frm("%1$s %2$s:");
93         frm % _(fl) % num;
94         string const label = frm.str();
95 #else
96         // Generate the label
97         string const label = _(fl) + ' ' + num + ':';
98 #endif
99         Painter & pain = bv->painter();
100         int const w = font_metrics::width(label, f);
101         pain.text(int(x), baseline, label, f);
102         x += w;
103
104         InsetText::draw(bv, f, baseline, x);
105 }
106
107
108 int InsetCaption::latex(Buffer const * buf, ostream & os,
109                         bool fragile, bool free_spc) 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, fragile, free_spc);
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 }