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