]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
Fix Bug 3947: BibTeX allows parentheses in the key (even if the whole entry is delimi...
[lyx.git] / src / insets / InsetCaption.cpp
1 /**
2  * \file InsetCaption.cpp
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 "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "Color.h"
28 #include "MetricsInfo.h"
29 #include "output_latex.h"
30 #include "OutputParams.h"
31 #include "Paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "TocBackend.h"
34
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
37
38 #include "support/lstrings.h"
39 #include "support/convert.h"
40
41 #include <sstream>
42
43
44 using std::auto_ptr;
45 using std::endl;
46 using std::string;
47 using std::ostream;
48
49
50 namespace lyx {
51
52 using support::bformat;
53
54 InsetCaption::InsetCaption(InsetCaption const & ic)
55         : InsetText(ic), textclass_(ic.textclass_)
56 {
57         setAutoBreakRows(true);
58         setDrawFrame(true);
59         setFrameColor(Color::captionframe);
60 }
61
62 InsetCaption::InsetCaption(BufferParams const & bp)
63         : InsetText(bp), textclass_(bp.getTextClass())
64 {
65         setAutoBreakRows(true);
66         setDrawFrame(true);
67         setFrameColor(Color::captionframe);
68 }
69
70
71 void InsetCaption::write(Buffer const & buf, ostream & os) const
72 {
73         os << "Caption\n";
74         text_.write(buf, os);
75 }
76
77
78 void InsetCaption::read(Buffer const & buf, Lexer & lex)
79 {
80 #if 0
81         // We will enably this check again when the compability
82         // code is removed from Buffer::Read (Lgb)
83         string const token = lex.GetString();
84         if (token != "Caption") {
85                 lyxerr << "InsetCaption::Read: consistency check failed."
86                        << endl;
87         }
88 #endif
89         InsetText::read(buf, lex);
90 }
91
92
93 docstring const InsetCaption::editMessage() const
94 {
95         return _("Opened Caption Inset");
96 }
97
98
99 void InsetCaption::cursorPos(BufferView const & bv,
100                 CursorSlice const & sl, bool boundary, int & x, int & y) const
101 {
102         InsetText::cursorPos(bv, sl, boundary, x, y);
103         x += labelwidth_;
104 }
105
106
107 void InsetCaption::setCustomLabel(docstring const & label)
108 {
109         if (!support::isAscii(label) || label.empty())
110                 // This must be a user defined layout. We cannot translate
111                 // this, since gettext accepts only ascii keys.
112                 custom_label_ = label;
113         else
114                 custom_label_ = _(to_ascii(label));
115 }
116
117
118 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
119 {
120         if (type_.empty())
121                 return;
122
123         ParConstIterator pit = par_const_iterator_begin(*this);
124
125         Toc & toc = toclist[type_];
126         docstring const str = convert<docstring>(counter_)
127                 + ". " + pit->asString(buf, false);
128         toc.push_back(TocItem(pit, 0, str));
129 }
130
131
132 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
133 {
134         int const width_offset = TEXT_TO_INSET_OFFSET / 2;
135         mi.base.textwidth -= width_offset;
136
137         computeFullLabel(*mi.base.bv->buffer());
138
139         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
140         // add some space to separate the label from the inset text
141         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
142         dim.wid = labelwidth_;
143         Dimension textdim;
144         InsetText::metrics(mi, textdim);
145         // Correct for button width, and re-fit
146         mi.base.textwidth -= dim.wid;
147         InsetText::metrics(mi, textdim);
148         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
149         dim.asc = textdim.asc;
150         dim.wid += textdim.wid;
151         dim.asc += TEXT_TO_INSET_OFFSET;
152         dim.des += TEXT_TO_INSET_OFFSET;
153         dim.wid += width_offset;
154         mi.base.textwidth += width_offset;
155         bool const changed = dim_ != dim;
156         dim_ = dim;
157         return changed;
158 }
159
160
161 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
162 {
163         // We must draw the label, we should get the label string
164         // from the enclosing float inset.
165         // The question is: Who should draw the label, the caption inset,
166         // the text inset or the paragraph?
167         // We should also draw the float number (Lgb)
168
169         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
170
171         labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
172         // add some space to separate the label from the inset text
173         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
174         InsetText::draw(pi, x + labelwidth_, y);
175         setPosCache(pi, x, y);
176 }
177
178
179 void InsetCaption::drawSelection(PainterInfo & pi, int x, int y) const
180 {
181         InsetText::drawSelection(pi, x + labelwidth_, y);
182 }
183
184
185 void InsetCaption::edit(Cursor & cur, bool left)
186 {
187         cur.push(*this);
188         InsetText::edit(cur, left);
189 }
190
191
192 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
193 {
194         cur.push(*this);
195         return InsetText::editXY(cur, x, y);
196 }
197
198
199 bool InsetCaption::insetAllowed(Inset::Code code) const
200 {
201         switch (code) {
202         case FLOAT_CODE:
203         case TABULAR_CODE:
204         case WRAP_CODE:
205         case CAPTION_CODE:
206         case PAGEBREAK_CODE:
207                 return false;
208         default:
209                 return InsetText::insetAllowed(code);
210         }
211 }
212
213
214 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
215         FuncStatus & status) const
216 {
217         switch (cmd.action) {
218
219         case LFUN_BREAK_PARAGRAPH:
220         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
221         case LFUN_BREAK_PARAGRAPH_SKIP:
222                 status.enabled(false);
223                 return true;
224
225         case LFUN_OPTIONAL_INSERT:
226                 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
227                 return true;
228
229         default:
230                 return InsetText::getStatus(cur, cmd, status);
231         }
232 }
233
234
235 int InsetCaption::latex(Buffer const & buf, odocstream & os,
236                         OutputParams const & runparams_in) const
237 {
238         // This is a bit too simplistic to take advantage of
239         // caption options we must add more later. (Lgb)
240         // This code is currently only able to handle the simple
241         // \caption{...}, later we will make it take advantage
242         // of the one of the caption packages. (Lgb)
243         OutputParams runparams = runparams_in;
244         // FIXME: actually, it is moving only when there is no
245         // optional argument.
246         runparams.moving_arg = true;
247         os << "\\caption";
248         int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
249         os << '{';
250         l += InsetText::latex(buf, os, runparams);
251         os << "}\n";
252         runparams_in.encoding = runparams.encoding;
253         return l + 1;
254 }
255
256
257 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
258                             OutputParams const & runparams) const
259 {
260         computeFullLabel(buf);
261
262         os << '[' << full_label_ << "\n";
263         InsetText::plaintext(buf, os, runparams);
264         os << "\n]";
265
266         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
267 }
268
269
270 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
271                           OutputParams const & runparams) const
272 {
273         int ret;
274         os << "<title>";
275         ret = InsetText::docbook(buf, os, runparams);
276         os << "</title>\n";
277         return ret;
278 }
279
280
281 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
282                         OutputParams const & runparams) const
283 {
284         return InsetText::latex(buf, os, runparams);
285 }
286
287
288 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
289                         OutputParams const & runparams) const
290 {
291         return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
292 }
293
294
295 void InsetCaption::computeFullLabel(Buffer const & buf) const
296 {
297         if (type_.empty())
298                 full_label_ = buf.B_("Senseless!!! ");
299         else {
300                 docstring const number = convert<docstring>(counter_);
301                 docstring label = custom_label_.empty()? buf.B_(type_): custom_label_;
302                 full_label_ = bformat(from_ascii("%1$s %2$s:"), label, number);
303         }
304 }
305
306
307 auto_ptr<Inset> InsetCaption::doClone() const
308 {
309         return auto_ptr<Inset>(new InsetCaption(*this));
310 }
311
312
313 } // namespace lyx