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