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