]> git.lyx.org Git - lyx.git/blob - src/insets/insetcaption.C
Add "Senseless!!! " label to non supported caption.
[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 "paragraph.h"
31
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
34
35 #include "support/lstrings.h"
36 #include "support/convert.h"
37
38 #include <sstream>
39
40
41 namespace lyx {
42
43 using support::bformat;
44
45 using std::auto_ptr;
46 using std::endl;
47 using std::string;
48 using std::ostream;
49
50
51 InsetCaption::InsetCaption(BufferParams const & bp)
52         : InsetText(bp), textclass_(bp.getLyXTextClass()), counter_(-1)
53 {
54         setAutoBreakRows(true);
55         setDrawFrame(true);
56         setFrameColor(LColor::captionframe);
57 }
58
59
60 void InsetCaption::write(Buffer const & buf, ostream & os) const
61 {
62         os << "Caption\n";
63         text_.write(buf, os);
64 }
65
66
67 void InsetCaption::read(Buffer const & buf, LyXLex & lex)
68 {
69 #if 0
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."
75                        << endl;
76         }
77 #endif
78         InsetText::read(buf, lex);
79 }
80
81
82 docstring const InsetCaption::editMessage() const
83 {
84         return _("Opened Caption Inset");
85 }
86
87
88 void InsetCaption::cursorPos(BufferView const & bv,
89                 CursorSlice const & sl, bool boundary, int & x, int & y) const
90 {
91         InsetText::cursorPos(bv, sl, boundary, x, y);
92         x += labelwidth_;
93 }
94
95
96 void InsetCaption::setLabel(docstring const & label)
97 {
98         label_ = _(to_ascii(label));
99 }
100
101
102 bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
103 {
104         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
105         if (counter_ < 0)
106                 full_label_ = _("Senseless!!! ");
107         else {
108                 docstring const number = convert<docstring>(counter_);
109                 full_label_ = bformat(from_ascii("%1$s %2$s:"), label_, number);
110         }
111         labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
112         dim.wid = labelwidth_;
113         Dimension textdim;
114         InsetText::metrics(mi, textdim);
115         // Correct for button width, and re-fit
116         mi.base.textwidth -= dim.wid;
117         InsetText::metrics(mi, textdim);
118         dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
119         dim.asc = textdim.asc;
120         dim.wid += textdim.wid;
121         dim.asc += TEXT_TO_INSET_OFFSET;
122         dim.des += TEXT_TO_INSET_OFFSET;
123         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
124         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
125         bool const changed = dim_ != dim;
126         dim_ = dim;
127         return changed;
128 }
129
130
131 void InsetCaption::draw(PainterInfo & pi, int x, int y) const
132 {
133         // We must draw the label, we should get the label string
134         // from the enclosing float inset.
135         // The question is: Who should draw the label, the caption inset,
136         // the text inset or the paragraph?
137         // We should also draw the float number (Lgb)
138
139         // Answer: the text inset (in buffer_funcs.C: setCaption).
140
141         labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
142         InsetText::draw(pi, x + labelwidth_, y);
143         setPosCache(pi, x, y);
144 }
145
146
147 void InsetCaption::edit(LCursor & cur, bool left)
148 {
149         cur.push(*this);
150         InsetText::edit(cur, left);
151 }
152
153
154 InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y)
155 {
156         cur.push(*this);
157         return InsetText::editXY(cur, x, y);
158 }
159
160
161 bool InsetCaption::getStatus(LCursor & cur, FuncRequest const & cmd,
162         FuncStatus & status) const
163 {
164         switch (cmd.action) {
165
166         case LFUN_CAPTION_INSERT:
167         case LFUN_FLOAT_INSERT:
168         case LFUN_FLOAT_WIDE_INSERT:
169         case LFUN_WRAP_INSERT:
170         case LFUN_PARAGRAPH_MOVE_UP:
171         case LFUN_PARAGRAPH_MOVE_DOWN:
172         case LFUN_BREAK_PARAGRAPH:
173         case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
174         case LFUN_BREAK_PARAGRAPH_SKIP:
175         case LFUN_PARAGRAPH_SPACING:
176         case LFUN_PAGEBREAK_INSERT:
177                 status.enabled(false);
178                 return true;
179
180         default:
181                 return InsetText::getStatus(cur, cmd, status);
182         }
183 }
184
185
186 int InsetCaption::latex(Buffer const & buf, odocstream & os,
187                         OutputParams const & runparams) const
188 {
189         // This is a bit too simplistic to take advantage of
190         // caption options we must add more later. (Lgb)
191         // This code is currently only able to handle the simple
192         // \caption{...}, later we will make it take advantage
193         // of the one of the caption packages. (Lgb)
194         os << "\\caption";
195         int l = latexOptArgInsets(buf, paragraphs()[0], os, runparams, 1);
196         os << '{';
197         l += InsetText::latex(buf, os, runparams);
198         os << "}\n";
199         return l + 1;
200 }
201
202
203 int InsetCaption::plaintext(Buffer const & buf, odocstream & os,
204                 OutputParams const & runparams) const
205 {
206         os << full_label_ << ' ';
207         return InsetText::plaintext(buf, os, runparams);
208 }
209
210
211 int InsetCaption::docbook(Buffer const & buf, odocstream & os,
212                           OutputParams const & runparams) const
213 {
214         int ret;
215         os << "<title>";
216         ret = InsetText::docbook(buf, os, runparams);
217         os << "</title>\n";
218         return ret;
219 }
220
221
222 auto_ptr<InsetBase> InsetCaption::doClone() const
223 {
224         return auto_ptr<InsetBase>(new InsetCaption(*this));
225 }
226
227
228 } // namespace lyx