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