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