3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "insetcaption.h"
14 #include "insetfloat.h"
15 #include "insetwrap.h"
18 #include "bufferparams.h"
21 #include "BufferView.h"
23 #include "FloatList.h"
24 #include "funcrequest.h"
25 #include "FuncStatus.h"
28 #include "metricsinfo.h"
29 #include "output_latex.h"
30 #include "paragraph.h"
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
35 #include "support/lstrings.h"
36 #include "support/convert.h"
43 using support::bformat;
51 InsetCaption::InsetCaption(BufferParams const & bp)
52 : InsetText(bp), textclass_(bp.getLyXTextClass())
54 setAutoBreakRows(true);
56 setFrameColor(LColor::captionframe);
60 void InsetCaption::write(Buffer const & buf, ostream & os) const
67 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
70 // We will enably this check again when the compability
71 // code is removed from Buffer::Read (Lgb)
72 string const token = lex.GetString();
73 if (token != "Caption") {
74 lyxerr << "InsetCaption::Read: consistency check failed."
78 InsetText::read(buf, lex);
82 docstring const InsetCaption::editMessage() const
84 return _("Opened Caption Inset");
88 void InsetCaption::cursorPos(BufferView const & bv,
89 CursorSlice const & sl, bool boundary, int & x, int & y) const
91 InsetText::cursorPos(bv, sl, boundary, x, y);
96 void InsetCaption::setLabel(docstring const & label)
98 label_ = _(to_ascii(label));
102 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
104 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
105 docstring const number = convert<docstring>(counter_);
106 full_label_ = bformat(from_ascii("%1$s %2$s:"), label_, number);
107 labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
108 dim.wid = labelwidth_;
110 InsetText::metrics(mi, textdim);
111 // Correct for button width, and re-fit
112 mi.base.textwidth -= dim.wid;
113 InsetText::metrics(mi, textdim);
114 dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
115 dim.asc = textdim.asc;
116 dim.wid += textdim.wid;
117 dim.asc += TEXT_TO_INSET_OFFSET;
118 dim.des += TEXT_TO_INSET_OFFSET;
119 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
120 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
121 bool const changed = dim_ != dim;
127 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
129 // We must draw the label, we should get the label string
130 // from the enclosing float inset.
131 // The question is: Who should draw the label, the caption inset,
132 // the text inset or the paragraph?
133 // We should also draw the float number (Lgb)
135 // Answer: the text inset (in buffer_funcs.C: setCaption).
137 labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
138 InsetText::draw(pi, x + labelwidth_, y);
139 setPosCache(pi, x, y);
143 void InsetCaption::edit(LCursor & cur, bool left)
146 InsetText::edit(cur, left);
150 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
153 return InsetText::editXY(cur, x, y);
157 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
158 FuncStatus & status) const
160 switch (cmd.action) {
162 case LFUN_CAPTION_INSERT:
163 case LFUN_FLOAT_INSERT:
164 case LFUN_FLOAT_WIDE_INSERT:
165 case LFUN_WRAP_INSERT:
166 case LFUN_PARAGRAPH_MOVE_UP:
167 case LFUN_PARAGRAPH_MOVE_DOWN:
168 case LFUN_BREAK_PARAGRAPH:
169 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
170 case LFUN_BREAK_PARAGRAPH_SKIP:
171 case LFUN_PARAGRAPH_SPACING:
172 case LFUN_PAGEBREAK_INSERT:
173 status.enabled(false);
177 return InsetText::getStatus(cur, cmd, status);
182 int InsetCaption::latex(Buffer const & buf, odocstream & os,
183 OutputParams const & runparams) const
185 // This is a bit too simplistic to take advantage of
186 // caption options we must add more later. (Lgb)
187 // This code is currently only able to handle the simple
188 // \caption{...}, later we will make it take advantage
189 // of the one of the caption packages. (Lgb)
191 int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
193 l += InsetText::latex(buf, os, runparams);
199 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
200 OutputParams const & runparams) const
202 os << full_label_ << ' ';
203 return InsetText::plaintext(buf, os, runparams);
207 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
208 OutputParams const & runparams) const
212 ret = InsetText::docbook(buf, os, runparams);
218 auto_ptr<InsetBase> InsetCaption::doClone() const
220 return auto_ptr<InsetBase>(new InsetCaption(*this));