]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
* BufferParams:
[lyx.git] / src / insets / insetcaption.C
1 /**
2  * \file insetcaption.C
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 "counters.h"
20 #include "cursor.h"
21 #include "BufferView.h"
22 #include "Floating.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
26 #include "gettext.h"
27 #include "LColor.h"
28 #include "metricsinfo.h"
29 #include "output_latex.h"
30 #include "outputparams.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "TocBackend.h"
34
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
37
38 #include "support/lstrings.h"
39 #include "support/convert.h"
40
41 #include <sstream>
42
43
44 using std::auto_ptr;
45 using std::endl;
46 using std::string;
47 using std::ostream;
48
49
50 namespace lyx {
51
52 using support::bformat;
53
54 InsetCaption::InsetCaption(BufferParams const & bp)
55         : InsetText(bp), textclass_(bp.getLyXTextClass())
56 {
57         setAutoBreakRows(true);
58         setDrawFrame(true);
59         setFrameColor(LColor::captionframe);
60 }
61
62
63 void InsetCaption::write(Buffer const & buf, ostream & os) const
64 {
65         os << "Caption\n";
66         text_.write(buf, os);
67 }
68
69
70 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
71 {
72 #if 0
73         // We will enably this check again when the compability
74         // code is removed from Buffer::Read (Lgb)
75         string const token = lex.GetString();
76         if (token != "Caption") {
77                 lyxerr << "InsetCaption::Read: consistency check failed."
78                        << endl;
79         }
80 #endif
81         InsetText::read(buf, lex);
82 }
83
84
85 docstring const InsetCaption::editMessage() const
86 {
87         return _("Opened Caption Inset");
88 }
89
90
91 void InsetCaption::cursorPos(BufferView const & bv,
92                 CursorSlice const & sl, bool boundary, int & x, int & y) const
93 {
94         InsetText::cursorPos(bv, sl, boundary, x, y);
95         x += labelwidth_;
96 }
97
98
99 void InsetCaption::setCustomLabel(docstring const & label)
100 {
101         if (!support::isAscii(label) || label.empty())
102                 // This must be a user defined layout. We cannot translate
103                 // this, since gettext accepts only ascii keys.
104                 custom_label_ = label;
105         else
106                 custom_label_ = _(to_ascii(label));
107 }
108
109
110 void InsetCaption::addToToc(TocList & toclist, Buffer const & buf) const
111 {
112         if (type_.empty())
113                 return;
114
115         ParConstIterator pit = par_const_iterator_begin(*this);
116
117         Toc & toc = toclist[type_];
118         docstring const str = convert<docstring>(counter_)
119                 + ". " + pit->asString(buf, false);
120         toc.push_back(TocItem(pit, 0, str));
121 }
122
123
124 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
125 {
126         int const width_offset = TEXT_TO_INSET_OFFSET / 2;
127         mi.base.textwidth -= width_offset;
128
129         computeFullLabel();
130
131         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
132         // add some space to separate the label from the inset text
133         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
134         dim.wid = labelwidth_;
135         Dimension textdim;
136         InsetText::metrics(mi, textdim);
137         // Correct for button width, and re-fit
138         mi.base.textwidth -= dim.wid;
139         InsetText::metrics(mi, textdim);
140         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
141         dim.asc = textdim.asc;
142         dim.wid += textdim.wid;
143         dim.asc += TEXT_TO_INSET_OFFSET;
144         dim.des += TEXT_TO_INSET_OFFSET;
145         dim.wid += width_offset;
146         mi.base.textwidth += width_offset;
147         bool const changed = dim_ != dim;
148         dim_ = dim;
149         return changed;
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.C: setCaption).
162
163         labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
164         // add some space to separate the label from the inset text
165         labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
166         InsetText::draw(pi, x + labelwidth_, y);
167         setPosCache(pi, x, y);
168 }
169
170
171 void InsetCaption::drawSelection(PainterInfo & pi, int x, int y) const
172 {
173         InsetText::drawSelection(pi, x + labelwidth_, y);
174 }
175
176
177 void InsetCaption::edit(LCursor & cur, bool left)
178 {
179         cur.push(*this);
180         InsetText::edit(cur, left);
181 }
182
183
184 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
185 {
186         cur.push(*this);
187         return InsetText::editXY(cur, x, y);
188 }
189
190
191 bool InsetCaption::insetAllowed(InsetBase::Code code) const
192 {
193         switch (code) {
194         case FLOAT_CODE:
195         case TABULAR_CODE:
196         case WRAP_CODE:
197         case CAPTION_CODE:
198         case PAGEBREAK_CODE:
199                 return false;
200         default:
201                 return InsetText::insetAllowed(code);
202         }
203 }
204
205
206 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
207         FuncStatus & status) const
208 {
209         switch (cmd.action) {
210
211         case LFUN_BREAK_PARAGRAPH:
212         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
213         case LFUN_BREAK_PARAGRAPH_SKIP:
214                 status.enabled(false);
215                 return true;
216
217         case LFUN_OPTIONAL_INSERT:
218                 status.enabled(numberOfOptArgs(cur.paragraph()) == 0);
219                 return true;
220
221         default:
222                 return InsetText::getStatus(cur, cmd, status);
223         }
224 }
225
226
227 int InsetCaption::latex(Buffer const & buf, odocstream & os,
228                         OutputParams const & runparams_in) const
229 {
230         // This is a bit too simplistic to take advantage of
231         // caption options we must add more later. (Lgb)
232         // This code is currently only able to handle the simple
233         // \caption{...}, later we will make it take advantage
234         // of the one of the caption packages. (Lgb)
235         OutputParams runparams = runparams_in;
236         // FIXME: actually, it is moving only when there is no
237         // optional argument.
238         runparams.moving_arg = true;
239         os << "\\caption";
240         int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
241         os << '{';
242         l += InsetText::latex(buf, os, runparams);
243         os << "}\n";
244         runparams_in.encoding = runparams.encoding;
245         return l + 1;
246 }
247
248
249 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
250                             OutputParams const & runparams) const
251 {
252         computeFullLabel();
253
254         os << '[' << full_label_ << "\n";
255         InsetText::plaintext(buf, os, runparams);
256         os << "\n]";
257
258         return PLAINTEXT_NEWLINE + 1; // one char on a separate line
259 }
260
261
262 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
263                           OutputParams const & runparams) const
264 {
265         int ret;
266         os << "<title>";
267         ret = InsetText::docbook(buf, os, runparams);
268         os << "</title>\n";
269         return ret;
270 }
271
272
273 void InsetCaption::computeFullLabel() const
274 {
275         if (type_.empty())
276                 full_label_ = _("Senseless!!! ");
277         else {
278                 docstring const number = convert<docstring>(counter_);
279                 docstring label = custom_label_.empty()? _(type_): custom_label_;
280                 full_label_ = bformat(from_ascii("%1$s %2$s:"), label, number);
281         }
282 }
283
284
285 auto_ptr<InsetBase> InsetCaption::doClone() const
286 {
287         return auto_ptr<InsetBase>(new InsetCaption(*this));
288 }
289
290
291 } // namespace lyx