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