]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
Fix the painting of the labels of InsetCaption insets.
[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 "BufferView.h"
20 #include "Counters.h"
21 #include "Cursor.h"
22 #include "Dimension.h"
23 #include "Floating.h"
24 #include "FloatList.h"
25 #include "FuncRequest.h"
26 #include "FuncStatus.h"
27 #include "InsetList.h"
28 #include "Language.h"
29 #include "MetricsInfo.h"
30 #include "output_latex.h"
31 #include "output_xhtml.h"
32 #include "OutputParams.h"
33 #include "Paragraph.h"
34 #include "TextClass.h"
35 #include "TextMetrics.h"
36 #include "TocBackend.h"
37
38 #include "frontends/FontMetrics.h"
39 #include "frontends/Painter.h"
40
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43
44 #include <sstream>
45
46 using namespace std;
47 using namespace lyx::support;
48
49 namespace lyx {
50
51
52 InsetCaption::InsetCaption(Buffer * buf)
53         : InsetText(buf, InsetText::PlainLayout)
54 {
55         setAutoBreakRows(true);
56         setDrawFrame(true);
57         setFrameColor(Color_collapsableframe);
58 }
59
60
61 void InsetCaption::write(ostream & os) const
62 {
63         os << "Caption\n";
64         text().write(os);
65 }
66
67
68 docstring InsetCaption::name() const
69 {
70         if (type_.empty())
71                 return from_ascii("Caption");
72         return from_utf8("Caption:" + type_);
73 }
74
75
76 void InsetCaption::read(Lexer & lex)
77 {
78 #if 0
79         // We will enably this check again when the compability
80         // code is removed from Buffer::Read (Lgb)
81         lex.setContext("InsetCaption::Read: consistency check");
82         lex >> "Caption";
83 #endif
84         InsetText::read(lex);
85 }
86
87
88 void InsetCaption::cursorPos(BufferView const & bv,
89                 CursorSlice const & sl, bool boundary, int & x, int & y) const
90 {
91         InsetText::cursorPos(bv, sl, boundary, x, y);
92         x += labelwidth_;
93 }
94
95
96 void InsetCaption::setCustomLabel(docstring const & label)
97 {
98         if (!isAscii(label) || label.empty())
99                 // This must be a user defined layout. We cannot translate
100                 // this, since gettext accepts only ascii keys.
101                 custom_label_ = label;
102         else
103                 custom_label_ = _(to_ascii(label));
104 }
105
106
107 void InsetCaption::addToToc(DocIterator const & cpit)
108 {
109         if (type_.empty())
110                 return;
111
112         DocIterator pit = cpit;
113         pit.push_back(CursorSlice(*this));
114
115         Toc & toc = buffer().tocBackend().toc(type_);
116         docstring const str = full_label_ + ". " + text().getPar(0).asString();
117         toc.push_back(TocItem(pit, 0, str));
118
119         // Proceed with the rest of the inset.
120         InsetText::addToToc(cpit);
121 }
122
123
124 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
125 {
126         FontInfo tmpfont = mi.base.font;
127         mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
128         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
129         // add some space to separate the label from the inset text
130         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
131         dim.wid = labelwidth_;
132         Dimension textdim;
133         // Correct for button and label width
134         mi.base.textwidth -= dim.wid;
135         InsetText::metrics(mi, textdim);
136         mi.base.font = tmpfont;
137         mi.base.textwidth += dim.wid;
138         dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
139         dim.asc = textdim.asc;
140         dim.wid += textdim.wid;
141 }
142
143
144 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
145 {
146         TextMetrics & tm = pi.base.bv->textMetrics(&text());
147         int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
148         int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
149         pi.pain.fillRectangle(x, yy, labelwidth_, h, Color_red);
150 }
151
152
153 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
154 {
155         // We must draw the label, we should get the label string
156         // from the enclosing float inset.
157         // The question is: Who should draw the label, the caption inset,
158         // the text inset or the paragraph?
159         // We should also draw the float number (Lgb)
160
161         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
162
163         FontInfo tmpfont = pi.base.font;
164         pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
165         pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
166         int const xx = x + TEXT_TO_INSET_OFFSET;
167         pi.pain.text(xx, y, full_label_, pi.base.font);
168         InsetText::draw(pi, x + labelwidth_, y);
169         pi.base.font = tmpfont;
170 }
171
172
173 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
174 {
175         cur.push(*this);
176         InsetText::edit(cur, front, entry_from);
177 }
178
179
180 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
181 {
182         cur.push(*this);
183         return InsetText::editXY(cur, x, y);
184 }
185
186
187 bool InsetCaption::insetAllowed(InsetCode code) const
188 {
189         switch (code) {
190         // code that is not allowed in a caption
191         case CAPTION_CODE:
192         case FLOAT_CODE:
193         case FOOT_CODE:
194         case NEWPAGE_CODE:
195         case MARGIN_CODE:
196         case MATHMACRO_CODE:
197         case TABULAR_CODE:
198         case WRAP_CODE:
199                 return false;
200         default:
201                 return InsetText::insetAllowed(code);
202         }
203 }
204
205
206 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
207         FuncStatus & status) const
208 {
209         switch (cmd.action()) {
210
211         case LFUN_BREAK_PARAGRAPH:
212                 status.setEnabled(false);
213                 return true;
214
215         case LFUN_ARGUMENT_INSERT:
216                 status.setEnabled(cur.paragraph().insetList().find(ARG_CODE) == -1);
217                 return true;
218
219         case LFUN_INSET_TOGGLE:
220                 // pass back to owner
221                 cur.undispatched();
222                 return false;
223
224         default:
225                 return InsetText::getStatus(cur, cmd, status);
226         }
227 }
228
229
230 int InsetCaption::latex(odocstream & os,
231                         OutputParams const & runparams_in) const
232 {
233         if (runparams_in.inFloat == OutputParams::SUBFLOAT)
234                 // caption is output as an optional argument
235                 return 0;
236         // This is a bit too simplistic to take advantage of
237         // caption options we must add more later. (Lgb)
238         // This code is currently only able to handle the simple
239         // \caption{...}, later we will make it take advantage
240         // of the one of the caption packages. (Lgb)
241         OutputParams runparams = runparams_in;
242         // FIXME: actually, it is moving only when there is no
243         // optional argument.
244         runparams.moving_arg = true;
245         os << "\\caption";
246         int l = latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
247         os << '{';
248         l += InsetText::latex(os, runparams);
249         os << "}\n";
250         runparams_in.encoding = runparams.encoding;
251         return l + 1;
252 }
253
254
255 int InsetCaption::plaintext(odocstream & os,
256                             OutputParams const & runparams) const
257 {
258         os << '[' << full_label_ << "\n";
259         InsetText::plaintext(os, runparams);
260         os << "\n]";
261
262         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
263 }
264
265
266 int InsetCaption::docbook(odocstream & os,
267                           OutputParams const & runparams) const
268 {
269         int ret;
270         os << "<title>";
271         ret = InsetText::docbook(os, runparams);
272         os << "</title>\n";
273         return ret;
274 }
275
276
277 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
278 {
279         if (rp.html_disable_captions)
280                 return docstring();
281         string attr = "class='float-caption";
282         if (!type_.empty())
283                 attr += " float-caption-" + type_;
284         attr += "'";
285         xs << html::StartTag("div", attr);
286         docstring def = getCaptionAsHTML(xs, rp);
287         xs << html::EndTag("div");
288         return def;
289 }
290
291
292 int InsetCaption::getArgument(odocstream & os,
293                         OutputParams const & runparams) const
294 {
295         return InsetText::latex(os, runparams);
296 }
297
298
299 int InsetCaption::getOptArg(odocstream & os,
300                         OutputParams const & runparams) const
301 {
302         return latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
303 }
304
305
306 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
307                         OutputParams const & runparams) const
308 {
309         os << full_label_ << ' ';
310         return InsetText::plaintext(os, runparams);
311 }
312
313
314 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
315                         OutputParams const & runparams) const
316 {
317         xs << full_label_ << ' ';
318         InsetText::XHTMLOptions const opts = 
319                 InsetText::WriteLabel | InsetText::WriteInnerTag;
320         return InsetText::insetAsXHTML(xs, runparams, opts);
321 }
322
323
324 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
325 {
326         Buffer const & master = *buffer().masterBuffer();
327         DocumentClass const & tclass = master.params().documentClass();
328         string const & lang = it.paragraph().getParLanguage(master.params())->code();
329         Counters & cnts = tclass.counters();
330         string const & type = cnts.current_float();
331         if (utype == OutputUpdate) {
332                 // counters are local to the caption
333                 cnts.saveLastCounter();
334         }
335         // Memorize type for addToToc().
336         type_ = type;
337         if (type.empty())
338                 full_label_ = master.B_("Senseless!!! ");
339         else {
340                 // FIXME: life would be _much_ simpler if listings was
341                 // listed in Floating.
342                 docstring name;
343                 if (type == "listing")
344                         name = master.B_("Listing");
345                 else
346                         name = master.B_(tclass.floats().getType(type).name());
347                 docstring counter = from_utf8(type);
348                 if (cnts.isSubfloat()) {
349                         counter = "sub-" + from_utf8(type);
350                         name = bformat(_("Sub-%1$s"),
351                                        master.B_(tclass.floats().getType(type).name()));
352                 }
353                 if (cnts.hasCounter(counter)) {
354                         cnts.step(counter, utype);
355                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
356                                               name,
357                                               cnts.theCounter(counter, lang));
358                 } else
359                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
360         }
361
362         // Do the real work now.
363         InsetText::updateBuffer(it, utype);
364         if (utype == OutputUpdate)
365                 cnts.restoreLastCounter();
366 }
367
368
369 } // namespace lyx