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