]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
The speed patch: redraw only rows that have changed
[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 <sstream>
33
34
35 using lyx::support::bformat;
36
37 using std::auto_ptr;
38 using std::endl;
39 using std::string;
40 using std::ostream;
41 using std::ostringstream;
42
43
44 InsetCaption::InsetCaption(BufferParams const & bp)
45         : InsetText(bp)
46 {
47         setAutoBreakRows(true);
48         setDrawFrame(true);
49         setFrameColor(LColor::captionframe);
50 }
51
52
53 void InsetCaption::write(Buffer const & buf, ostream & os) const
54 {
55         os << "Caption\n";
56         text_.write(buf, os);
57 }
58
59
60 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
61 {
62 #if 0
63         // We will enably this check again when the compability
64         // code is removed from Buffer::Read (Lgb)
65         string const token = lex.GetString();
66         if (token != "Caption") {
67                 lyxerr << "InsetCaption::Read: consistency check failed."
68                        << endl;
69         }
70 #endif
71         InsetText::read(buf, lex);
72 }
73
74
75 string const InsetCaption::editMessage() const
76 {
77         return _("Opened Caption Inset");
78 }
79
80
81 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
82 {
83         // We must draw the label, we should get the label string
84         // from the enclosing float inset.
85         // The question is: Who should draw the label, the caption inset,
86         // the text inset or the paragraph?
87         // We should also draw the float number (Lgb)
88
89         // See if we can find the name of the float this caption
90         // belongs to.
91 #if 0
92         InsetBase * i1 = owner();
93         InsetBase * i2 = i1 ? i1->owner() : 0;
94         string type;
95         if (i2->lyxCode() == FLOAT_CODE)
96 #ifdef WITH_WARNINGS
97 #warning Now, what happens for i2 == 0?
98 #endif
99                 type = static_cast<InsetFloat *>(i2)->params().type;
100         else if (i2->lyxCode() == WRAP_CODE)
101                 type = static_cast<InsetWrap *>(i2)->params().type;
102         else
103                 BOOST_ASSERT(false);
104
105         FloatList const & floats =
106                 pi.base.bv->buffer()->params().getLyXTextClass().floats();
107         string const fl = i2 ? floats.getType(type).name() : N_("Float");
108 #else
109         string type = "float";
110         string const fl = N_("Float");
111 #endif
112
113         // Discover the number...
114         string const num = "#";
115
116         // Generate the label
117         string const label = bformat("%1$s %2$s:", _(fl), num);
118         int const w = font_metrics::width(label, pi.base.font);
119         pi.pain.text(x, y, label, pi.base.font);
120         InsetText::draw(pi, x + w, y);
121 }
122
123
124 int InsetCaption::latex(Buffer const & buf, ostream & os,
125                         OutputParams const & runparams) const
126 {
127         // This is a bit too simplistic to take advantage of
128         // caption options we must add more later. (Lgb)
129         // This code is currently only able to handle the simple
130         // \caption{...}, later we will make it take advantage
131         // of the one of the caption packages. (Lgb)
132         ostringstream ost;
133         int const l = InsetText::latex(buf, ost, runparams);
134         os << "\\caption{" << ost.str() << "}\n";
135         return l + 1;
136 }
137
138
139 int InsetCaption::plaintext(Buffer const & /*buf*/,ostream & /*os*/,
140                         OutputParams const & /*runparams*/) const
141 {
142         // FIXME: Implement me!
143         return 0;
144 }
145
146
147 int InsetCaption::docbook(Buffer const & buf, ostream & os,
148                           OutputParams const & runparams) const
149 {
150         int ret;
151         os << "<title>";
152         ret = InsetText::docbook(buf, os, runparams);
153         os << "</title>\n";
154         return ret;
155 }
156
157
158 auto_ptr<InsetBase> InsetCaption::doClone() const
159 {
160         return auto_ptr<InsetBase>(new InsetCaption(*this));
161 }