]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCaption.cpp
Integrate texrow with otexstream in order to perform automatic line
[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) const
108 {
109         if (type_.empty())
110                 return;
111
112         DocIterator pit = cpit;
113         pit.push_back(CursorSlice(const_cast<InsetCaption &>(*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 void InsetCaption::latex(otexstream & 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;
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         latexArgInsets(paragraphs()[0], os, runparams, 0, 1);
248         os << '{';
249         InsetText::latex(os, runparams);
250         os << "}\n";
251         runparams_in.encoding = runparams.encoding;
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 void InsetCaption::getArgument(otexstream & os,
293                         OutputParams const & runparams) const
294 {
295         InsetText::latex(os, runparams);
296 }
297
298
299 void InsetCaption::getOptArg(otexstream & os,
300                         OutputParams const & runparams) const
301 {
302         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