]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[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 using lyx::docstring;
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         // FIXME UNICODE
81         return lyx::to_utf8(_("Opened Caption Inset"));
82 }
83
84
85 void InsetCaption::cursorPos
86         (CursorSlice const & sl, bool boundary, int & x, int & y) const
87 {
88         InsetText::cursorPos(sl, boundary, x, y);
89         x += labelwidth_;
90 }
91
92
93 void InsetCaption::setLabel(LCursor & cur) const
94 {
95         // Set caption label _only_ if the cursor is in _this_ float:
96         if (cur.top().text() == &text_) {
97                 string s;
98                 size_t i = cur.depth();
99                         while (i > 0) {
100                                 --i;
101                                 InsetBase * const in = &cur[i].inset();
102                                 if (in->lyxCode() == InsetBase::FLOAT_CODE
103                                     || in->lyxCode() == InsetBase::WRAP_CODE) {
104                                         s = in->getInsetName();
105                                         break;
106                                 }
107                         }
108                 Floating const & fl = textclass_.floats().getType(s);
109                 s = fl.name();
110                 string num;
111                 if (s.empty())
112                         s = "Senseless";
113                 else
114                         num = convert<string>(counter_);
115
116                 // Generate the label
117                 label = bformat("%1$s %2$s:", lyx::to_utf8(_(s)), num);
118         }
119 }
120
121
122 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
123 {
124         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
125         LCursor cur = mi.base.bv->cursor();
126         setLabel(cur);
127         docstring dlab(label.begin(), label.end());
128         labelwidth_ = font_metrics::width(dlab, mi.base.font);
129         dim.wid = labelwidth_;
130         Dimension textdim;
131         InsetText::metrics(mi, textdim);
132         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
133         dim.asc = textdim.asc;
134         dim.wid += textdim.wid;
135         dim.asc += TEXT_TO_INSET_OFFSET;
136         dim.des += TEXT_TO_INSET_OFFSET;
137         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
138         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
139         dim_ = dim;
140 }
141
142
143 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
144 {
145         // We must draw the label, we should get the label string
146         // from the enclosing float inset.
147         // The question is: Who should draw the label, the caption inset,
148         // the text inset or the paragraph?
149         // We should also draw the float number (Lgb)
150
151         // See if we can find the name of the float this caption
152         // belongs to.
153         LCursor cur = pi.base.bv->cursor();
154         setLabel(cur);
155         docstring dlab(label.begin(), label.end());
156         labelwidth_ = font_metrics::width(dlab, pi.base.font);
157         pi.pain.text(x, y, dlab, pi.base.font);
158         InsetText::draw(pi, x + labelwidth_, y);
159         setPosCache(pi, x, y);
160 }
161
162
163 void InsetCaption::edit(LCursor & cur, bool left)
164 {
165         cur.push(*this);
166         InsetText::edit(cur, left);
167 }
168
169
170 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
171 {
172         cur.push(*this);
173         return InsetText::editXY(cur, x, y);
174 }
175
176
177 int InsetCaption::latex(Buffer const & buf, ostream & os,
178                         OutputParams const & runparams) const
179 {
180         // This is a bit too simplistic to take advantage of
181         // caption options we must add more later. (Lgb)
182         // This code is currently only able to handle the simple
183         // \caption{...}, later we will make it take advantage
184         // of the one of the caption packages. (Lgb)
185         ostringstream ost;
186         int const l = InsetText::latex(buf, ost, runparams);
187         os << "\\caption{" << ost.str() << "}\n";
188         return l + 1;
189 }
190
191
192 int InsetCaption::plaintext(Buffer const & /*buf*/,ostream & /*os*/,
193                         OutputParams const & /*runparams*/) const
194 {
195         // FIXME: Implement me!
196         return 0;
197 }
198
199
200 int InsetCaption::docbook(Buffer const & buf, ostream & os,
201                           OutputParams const & runparams) const
202 {
203         int ret;
204         os << "<title>";
205         ret = InsetText::docbook(buf, os, runparams);
206         os << "</title>\n";
207         return ret;
208 }
209
210
211 auto_ptr<InsetBase> InsetCaption::doClone() const
212 {
213         return auto_ptr<InsetBase>(new InsetCaption(*this));
214 }