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