]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
Finish disentangling tocString(). We introduce a new method, forToc(),
[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 str = full_label_ + ". ";
117         text().forToc(str, TOC_ENTRY_LENGTH);
118         toc.push_back(TocItem(pit, 0, str));
119
120         // Proceed with the rest of the inset.
121         InsetText::addToToc(cpit);
122 }
123
124
125 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
126 {
127         FontInfo tmpfont = mi.base.font;
128         mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
129         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
130         // add some space to separate the label from the inset text
131         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
132         dim.wid = labelwidth_;
133         Dimension textdim;
134         // Correct for button and label width
135         mi.base.textwidth -= dim.wid;
136         InsetText::metrics(mi, textdim);
137         mi.base.font = tmpfont;
138         mi.base.textwidth += dim.wid;
139         dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
140         dim.asc = textdim.asc;
141         dim.wid += textdim.wid;
142 }
143
144
145 void InsetCaption::drawBackground(PainterInfo & pi, int x, int y) const
146 {
147         TextMetrics & tm = pi.base.bv->textMetrics(&text());
148         int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
149         int const yy = y - TEXT_TO_INSET_OFFSET - tm.ascent();
150         pi.pain.fillRectangle(x, yy, labelwidth_, h, pi.backgroundColor(this));
151 }
152
153
154 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
155 {
156         // We must draw the label, we should get the label string
157         // from the enclosing float inset.
158         // The question is: Who should draw the label, the caption inset,
159         // the text inset or the paragraph?
160         // We should also draw the float number (Lgb)
161
162         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
163
164         FontInfo tmpfont = pi.base.font;
165         pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
166         pi.base.font.setColor(pi.textColor(pi.base.font.color()).baseColor);
167         int const xx = x + TEXT_TO_INSET_OFFSET;
168         pi.pain.text(xx, y, full_label_, pi.base.font);
169         InsetText::draw(pi, x + labelwidth_, y);
170         pi.base.font = tmpfont;
171 }
172
173
174 void InsetCaption::edit(Cursor & cur, bool front, EntryDirection entry_from)
175 {
176         cur.push(*this);
177         InsetText::edit(cur, front, entry_from);
178 }
179
180
181 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
182 {
183         cur.push(*this);
184         return InsetText::editXY(cur, x, y);
185 }
186
187
188 bool InsetCaption::insetAllowed(InsetCode code) const
189 {
190         switch (code) {
191         // code that is not allowed in a caption
192         case CAPTION_CODE:
193         case FLOAT_CODE:
194         case FOOT_CODE:
195         case NEWPAGE_CODE:
196         case MARGIN_CODE:
197         case MATHMACRO_CODE:
198         case TABULAR_CODE:
199         case WRAP_CODE:
200                 return false;
201         default:
202                 return InsetText::insetAllowed(code);
203         }
204 }
205
206
207 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
208         FuncStatus & status) const
209 {
210         switch (cmd.action()) {
211
212         case LFUN_BREAK_PARAGRAPH:
213                 status.setEnabled(false);
214                 return true;
215
216         case LFUN_ARGUMENT_INSERT:
217                 status.setEnabled(cur.paragraph().insetList().find(ARG_CODE) == -1);
218                 return true;
219
220         case LFUN_INSET_TOGGLE:
221                 // pass back to owner
222                 cur.undispatched();
223                 return false;
224
225         default:
226                 return InsetText::getStatus(cur, cmd, status);
227         }
228 }
229
230
231 int InsetCaption::latex(odocstream & os,
232                         OutputParams const & runparams_in) const
233 {
234         if (runparams_in.inFloat == OutputParams::SUBFLOAT)
235                 // caption is output as an optional argument
236                 return 0;
237         // This is a bit too simplistic to take advantage of
238         // caption options we must add more later. (Lgb)
239         // This code is currently only able to handle the simple
240         // \caption{...}, later we will make it take advantage
241         // of the one of the caption packages. (Lgb)
242         OutputParams runparams = runparams_in;
243         // FIXME: actually, it is moving only when there is no
244         // optional argument.
245         runparams.moving_arg = true;
246         os << "\\caption";
247         int l = latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
248         os << '{';
249         l += InsetText::latex(os, runparams);
250         os << "}\n";
251         runparams_in.encoding = runparams.encoding;
252         return l + 1;
253 }
254
255
256 int InsetCaption::plaintext(odocstream & os,
257                             OutputParams const & runparams) const
258 {
259         os << '[' << full_label_ << "\n";
260         InsetText::plaintext(os, runparams);
261         os << "\n]";
262
263         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
264 }
265
266
267 int InsetCaption::docbook(odocstream & os,
268                           OutputParams const & runparams) const
269 {
270         int ret;
271         os << "<title>";
272         ret = InsetText::docbook(os, runparams);
273         os << "</title>\n";
274         return ret;
275 }
276
277
278 docstring InsetCaption::xhtml(XHTMLStream & xs, OutputParams const & rp) const
279 {
280         if (rp.html_disable_captions)
281                 return docstring();
282         string attr = "class='float-caption";
283         if (!type_.empty())
284                 attr += " float-caption-" + type_;
285         attr += "'";
286         xs << html::StartTag("div", attr);
287         docstring def = getCaptionAsHTML(xs, rp);
288         xs << html::EndTag("div");
289         return def;
290 }
291
292
293 int InsetCaption::getArgument(odocstream & os,
294                         OutputParams const & runparams) const
295 {
296         return InsetText::latex(os, runparams);
297 }
298
299
300 int InsetCaption::getOptArg(odocstream & os,
301                         OutputParams const & runparams) const
302 {
303         return latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
304 }
305
306
307 int InsetCaption::getCaptionAsPlaintext(odocstream & os,
308                         OutputParams const & runparams) const
309 {
310         os << full_label_ << ' ';
311         return InsetText::plaintext(os, runparams);
312 }
313
314
315 docstring InsetCaption::getCaptionAsHTML(XHTMLStream & xs,
316                         OutputParams const & runparams) const
317 {
318         xs << full_label_ << ' ';
319         InsetText::XHTMLOptions const opts = 
320                 InsetText::WriteLabel | InsetText::WriteInnerTag;
321         return InsetText::insetAsXHTML(xs, runparams, opts);
322 }
323
324
325 void InsetCaption::updateBuffer(ParIterator const & it, UpdateType utype)
326 {
327         Buffer const & master = *buffer().masterBuffer();
328         DocumentClass const & tclass = master.params().documentClass();
329         string const & lang = it.paragraph().getParLanguage(master.params())->code();
330         Counters & cnts = tclass.counters();
331         string const & type = cnts.current_float();
332         if (utype == OutputUpdate) {
333                 // counters are local to the caption
334                 cnts.saveLastCounter();
335         }
336         // Memorize type for addToToc().
337         type_ = type;
338         if (type.empty())
339                 full_label_ = master.B_("Senseless!!! ");
340         else {
341                 // FIXME: life would be _much_ simpler if listings was
342                 // listed in Floating.
343                 docstring name;
344                 if (type == "listing")
345                         name = master.B_("Listing");
346                 else
347                         name = master.B_(tclass.floats().getType(type).name());
348                 docstring counter = from_utf8(type);
349                 if (cnts.isSubfloat()) {
350                         counter = "sub-" + from_utf8(type);
351                         name = bformat(_("Sub-%1$s"),
352                                        master.B_(tclass.floats().getType(type).name()));
353                 }
354                 if (cnts.hasCounter(counter)) {
355                         cnts.step(counter, utype);
356                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
357                                               name,
358                                               cnts.theCounter(counter, lang));
359                 } else
360                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
361         }
362
363         // Do the real work now.
364         InsetText::updateBuffer(it, utype);
365         if (utype == OutputUpdate)
366                 cnts.restoreLastCounter();
367 }
368
369
370 } // namespace lyx