]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
b205fbe123318295f1170076936134ed641abd48
[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 "counters.h"
20 #include "cursor.h"
21 #include "BufferView.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "gettext.h"
25 #include "LColor.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
28
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
31
32 #include "support/lstrings.h"
33 #include "support/convert.h"
34
35 #include <sstream>
36
37
38 using lyx::support::bformat;
39
40 using std::auto_ptr;
41 using std::endl;
42 using std::string;
43 using std::ostream;
44 using std::ostringstream;
45
46
47 InsetCaption::InsetCaption(BufferParams const & bp)
48         : InsetText(bp), textclass_(bp.getLyXTextClass())
49 {
50         setAutoBreakRows(true);
51         setDrawFrame(true);
52         setFrameColor(LColor::captionframe);
53 }
54
55
56 void InsetCaption::write(Buffer const & buf, ostream & os) const
57 {
58         os << "Caption\n";
59         text_.write(buf, os);
60 }
61
62
63 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
64 {
65 #if 0
66         // We will enably this check again when the compability
67         // code is removed from Buffer::Read (Lgb)
68         string const token = lex.GetString();
69         if (token != "Caption") {
70                 lyxerr << "InsetCaption::Read: consistency check failed."
71                        << endl;
72         }
73 #endif
74         InsetText::read(buf, lex);
75 }
76
77
78 string const InsetCaption::editMessage() const
79 {
80         return _("Opened Caption Inset");
81 }
82
83
84 void InsetCaption::cursorPos
85         (CursorSlice const & sl, bool boundary, int & x, int & y) const
86 {
87         InsetText::cursorPos(sl, boundary, x, y);
88         x += labelwidth_;
89 }
90
91
92 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
93 {
94         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
95         labelwidth_ = font_metrics::width(label, mi.base.font);
96         dim.wid = labelwidth_;
97         Dimension textdim;
98         InsetText::metrics(mi, textdim);
99         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
100         dim.asc = textdim.asc;
101         dim.wid += textdim.wid;
102         dim.asc += TEXT_TO_INSET_OFFSET;
103         dim.des += TEXT_TO_INSET_OFFSET;
104         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
105         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
106         dim_ = dim;
107 }
108
109
110 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
111 {
112         // We must draw the label, we should get the label string
113         // from the enclosing float inset.
114         // The question is: Who should draw the label, the caption inset,
115         // the text inset or the paragraph?
116         // We should also draw the float number (Lgb)
117
118         // See if we can find the name of the float this caption
119         // belongs to.
120         LCursor cur = pi.base.bv->cursor();
121         // Set caption label _only_ if the cursor is in _this_ float:
122         if (cur.top().text() == &text_) {
123                 string s; 
124                 size_t i = cur.depth();
125                         while (i > 0) {
126                                 --i;
127                                 InsetBase * const in = &cur[i].inset();
128                                 if (in->lyxCode() == InsetBase::FLOAT_CODE
129                                     || in->lyxCode() == InsetBase::WRAP_CODE) {
130                                         s = in->getInsetName();
131                                         break;
132                                 }
133                         }
134                 Floating const & fl = textclass_.floats().getType(s);
135                 s = fl.name();
136                 string num;
137                 if (s.empty())
138                         s = "Senseless";
139                 else
140                         num = convert<string>(textclass_.counters().value(fl.type()));
141
142                 // Generate the label
143                 label = bformat("%1$s %2$s:", _(s), num);
144         }
145
146         labelwidth_ = font_metrics::width(label, pi.base.font);
147         pi.pain.text(x, y, label, pi.base.font);
148         InsetText::draw(pi, x + labelwidth_, y);
149         setPosCache(pi, x, y);
150 }
151
152
153 void InsetCaption::edit(LCursor & cur, bool left)
154 {
155         cur.push(*this);
156         InsetText::edit(cur, left);
157 }
158
159
160 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
161 {
162         cur.push(*this);
163         return InsetText::editXY(cur, x, y);
164 }
165
166
167 int InsetCaption::latex(Buffer const & buf, ostream & os,
168                         OutputParams const & runparams) const
169 {
170         // This is a bit too simplistic to take advantage of
171         // caption options we must add more later. (Lgb)
172         // This code is currently only able to handle the simple
173         // \caption{...}, later we will make it take advantage
174         // of the one of the caption packages. (Lgb)
175         ostringstream ost;
176         int const l = InsetText::latex(buf, ost, runparams);
177         os << "\\caption{" << ost.str() << "}\n";
178         return l + 1;
179 }
180
181
182 int InsetCaption::plaintext(Buffer const & /*buf*/,ostream & /*os*/,
183                         OutputParams const & /*runparams*/) const
184 {
185         // FIXME: Implement me!
186         return 0;
187 }
188
189
190 int InsetCaption::docbook(Buffer const & buf, ostream & os,
191                           OutputParams const & runparams) const
192 {
193         int ret;
194         os << "<title>";
195         ret = InsetText::docbook(buf, os, runparams);
196         os << "</title>\n";
197         return ret;
198 }
199
200
201 auto_ptr<InsetBase> InsetCaption::doClone() const
202 {
203         return auto_ptr<InsetBase>(new InsetCaption(*this));
204 }