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