]> git.lyx.org Git - features.git/blob - src/insets/InsetCaption.cpp
* Lazy MathData to avoid unneeded interpretation of macro definitions
[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 "support/gettext.h"
28 #include "InsetList.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/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(InsetCaption const & ic)
51         : InsetText(ic), textclass_(ic.textclass_)
52 {
53         setAutoBreakRows(true);
54         setDrawFrame(true);
55         setFrameColor(Color_captionframe);
56 }
57
58 InsetCaption::InsetCaption(BufferParams const & bp)
59         : InsetText(bp), textclass_(bp.getTextClass())
60 {
61         setAutoBreakRows(true);
62         setDrawFrame(true);
63         setFrameColor(Color_captionframe);
64 }
65
66
67 void InsetCaption::write(Buffer const & buf, ostream & os) const
68 {
69         os << "Caption\n";
70         text_.write(buf, os);
71 }
72
73
74 void InsetCaption::read(Buffer const & buf, Lexer & lex)
75 {
76 #if 0
77         // We will enably this check again when the compability
78         // code is removed from Buffer::Read (Lgb)
79         string const token = lex.GetString();
80         if (token != "Caption") {
81                 lyxerr << "InsetCaption::Read: consistency check failed."
82                        << endl;
83         }
84 #endif
85         InsetText::read(buf, lex);
86 }
87
88
89 docstring const InsetCaption::editMessage() const
90 {
91         return _("Opened Caption Inset");
92 }
93
94
95 void InsetCaption::cursorPos(BufferView const & bv,
96                 CursorSlice const & sl, bool boundary, int & x, int & y) const
97 {
98         InsetText::cursorPos(bv, sl, boundary, x, y);
99         x += labelwidth_;
100 }
101
102
103 void InsetCaption::setCustomLabel(docstring const & label)
104 {
105         if (!isAscii(label) || label.empty())
106                 // This must be a user defined layout. We cannot translate
107                 // this, since gettext accepts only ascii keys.
108                 custom_label_ = label;
109         else
110                 custom_label_ = _(to_ascii(label));
111 }
112
113
114 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstIterator const &) const
115 {
116         if (type_.empty())
117                 return;
118
119         ParConstIterator pit = par_const_iterator_begin(*this);
120
121         Toc & toc = toclist[type_];
122         docstring const str = full_label_ + ". " + pit->asString(buf, false);
123         toc.push_back(TocItem(pit, 0, str));
124 }
125
126
127 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
128 {
129         FontInfo tmpfont = mi.base.font;
130         mi.base.font = mi.base.bv->buffer().params().getFont().fontInfo();
131         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
132         // add some space to separate the label from the inset text
133         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
134         dim.wid = labelwidth_;
135         Dimension textdim;
136         // Correct for button and label width
137         mi.base.textwidth -= dim.wid;
138         InsetText::metrics(mi, textdim);
139         mi.base.font = tmpfont;
140         mi.base.textwidth += dim.wid;
141         dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
142         dim.asc = textdim.asc;
143         dim.wid += textdim.wid;
144 }
145
146
147 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
148 {
149         // We must draw the label, we should get the label string
150         // from the enclosing float inset.
151         // The question is: Who should draw the label, the caption inset,
152         // the text inset or the paragraph?
153         // We should also draw the float number (Lgb)
154
155         // Answer: the text inset (in buffer_funcs.cpp: setCaption).
156
157         FontInfo tmpfont = pi.base.font;
158         pi.base.font = pi.base.bv->buffer().params().getFont().fontInfo();
159         pi.pain.text(x, y, full_label_, pi.base.font);
160         InsetText::draw(pi, x + labelwidth_, y);
161         pi.base.font = tmpfont;
162 }
163
164
165 void InsetCaption::edit(Cursor & cur, bool left)
166 {
167         cur.push(*this);
168         InsetText::edit(cur, left);
169 }
170
171
172 Inset * InsetCaption::editXY(Cursor & cur, int x, int y)
173 {
174         cur.push(*this);
175         return InsetText::editXY(cur, x, y);
176 }
177
178
179 bool InsetCaption::insetAllowed(InsetCode code) const
180 {
181         switch (code) {
182         case FLOAT_CODE:
183         case TABULAR_CODE:
184         case WRAP_CODE:
185         case CAPTION_CODE:
186         case NEWPAGE_CODE:
187         case MATHMACRO_CODE:
188                 return false;
189         default:
190                 return InsetText::insetAllowed(code);
191         }
192 }
193
194
195 bool InsetCaption::getStatus(Cursor & cur, FuncRequest const & cmd,
196         FuncStatus & status) const
197 {
198         switch (cmd.action) {
199
200         case LFUN_BREAK_PARAGRAPH:
201         case LFUN_BREAK_PARAGRAPH_SKIP:
202                 status.enabled(false);
203                 return true;
204
205         case LFUN_OPTIONAL_INSERT:
206                 status.enabled(cur.paragraph().insetList().find(OPTARG_CODE) == -1);
207                 return true;
208
209         default:
210                 return InsetText::getStatus(cur, cmd, status);
211         }
212 }
213
214
215 int InsetCaption::latex(Buffer const & buf, odocstream & os,
216                         OutputParams const & runparams_in) const
217 {
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(buf, paragraphs()[0], os, runparams, 1);
229         os << '{';
230         l += InsetText::latex(buf, os, runparams);
231         os << "}\n";
232         runparams_in.encoding = runparams.encoding;
233         return l + 1;
234 }
235
236
237 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
238                             OutputParams const & runparams) const
239 {
240         os << '[' << full_label_ << "\n";
241         InsetText::plaintext(buf, os, runparams);
242         os << "\n]";
243
244         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
245 }
246
247
248 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
249                           OutputParams const & runparams) const
250 {
251         int ret;
252         os << "<title>";
253         ret = InsetText::docbook(buf, os, runparams);
254         os << "</title>\n";
255         return ret;
256 }
257
258
259 int InsetCaption::getArgument(Buffer const & buf, odocstream & os,
260                         OutputParams const & runparams) const
261 {
262         return InsetText::latex(buf, os, runparams);
263 }
264
265
266 int InsetCaption::getOptArg(Buffer const & buf, odocstream & os,
267                         OutputParams const & runparams) const
268 {
269         return latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
270 }
271
272
273 void InsetCaption::updateLabels(Buffer const & buf, ParIterator const & it)
274 {
275         TextClass const & tclass = buf.params().getTextClass();
276         Counters & cnts = tclass.counters();
277         string const & type = cnts.current_float();
278         if (type.empty())
279                 full_label_ = buf.B_("Senseless!!! ");
280         else {
281                 // FIXME: life would be _much_ simpler if listings was
282                 // listed in Floating.
283                 docstring name;
284                 if (type == "listing")
285                         name = buf.B_("Listing");
286                 else
287                         name = buf.B_(tclass.floats().getType(type).name());
288                 if (cnts.hasCounter(from_utf8(type))) {
289                         cnts.step(from_utf8(type));
290                         full_label_ = bformat(from_ascii("%1$s %2$s:"), 
291                                               name, 
292                                               cnts.theCounter(from_utf8(type)));
293                 } else
294                         full_label_ = bformat(from_ascii("%1$s #:"), name);     
295         }
296
297         // Do the real work now.
298         InsetText::updateLabels(buf, it);
299 }
300
301
302 Inset * InsetCaption::clone() const
303 {
304         return new InsetCaption(*this);
305 }
306
307
308 } // namespace lyx