]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
fix display of label when inserting a new float
[lyx.git] / src / insets / insetwrap.C
1 /**
2  * \file insetwrap.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Dekel Tsur
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetwrap.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "BufferView.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "dispatchresult.h"
21 #include "Floating.h"
22 #include "FloatList.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "lyxlex.h"
28 #include "outputparams.h"
29 #include "paragraph.h"
30
31 #include "support/convert.h"
32
33 #include <sstream>
34
35 using std::string;
36 using std::endl;
37 using std::auto_ptr;
38 using std::istringstream;
39 using std::ostream;
40 using std::ostringstream;
41
42
43 namespace {
44
45 // this should not be hardcoded, but be part of the definition
46 // of the float (JMarc)
47 string const caplayout("Caption");
48
49 string floatname(string const & type, BufferParams const & bp)
50 {
51         FloatList const & floats = bp.getLyXTextClass().floats();
52         FloatList::const_iterator it = floats[type];
53         return (it == floats.end()) ? type : _(it->second.name());
54 }
55
56 } // namespace anon
57
58
59 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
60         : InsetCollapsable(bp)
61 {
62         setLabel(_("wrap: ") + floatname(type, bp));
63         LyXFont font(LyXFont::ALL_SANE);
64         font.decSize();
65         font.decSize();
66         font.setColor(LColor::collapsable);
67         setLabelFont(font);
68         params_.type = type;
69         params_.width = LyXLength(50, LyXLength::PCW);
70         setInsetName(type);
71 }
72
73
74 InsetWrap::~InsetWrap()
75 {
76         InsetWrapMailer(*this).hideDialog();
77 }
78
79
80 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
81 {
82         switch (cmd.action) {
83         case LFUN_INSET_MODIFY: {
84                 InsetWrapParams params;
85                 InsetWrapMailer::string2params(cmd.argument, params);
86                 params_.placement = params.placement;
87                 params_.width     = params.width;
88                 cur.bv().update();
89                 break;
90         }
91
92         case LFUN_INSET_DIALOG_UPDATE:
93                 InsetWrapMailer(*this).updateDialog(&cur.bv());
94                 break;
95
96         case LFUN_MOUSE_RELEASE: {
97                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
98                         InsetWrapMailer(*this).showDialog(&cur.bv());
99                         break;
100                 }
101                 InsetCollapsable::doDispatch(cur, cmd);
102                 break;
103         }
104
105         default:
106                 InsetCollapsable::doDispatch(cur, cmd);
107                 break;
108         }
109 }
110
111
112 void InsetWrapParams::write(ostream & os) const
113 {
114         os << "Wrap " << type << '\n';
115
116         if (!placement.empty())
117                 os << "placement " << placement << "\n";
118
119         os << "width \"" << width.asString() << "\"\n";
120 }
121
122
123 void InsetWrapParams::read(LyXLex & lex)
124 {
125         string token;
126         lex >> token;
127         if (token == "placement")
128                 lex >> placement;
129         else {
130                 // take countermeasures
131                 lex.pushToken(token);
132         }
133         if (!lex)
134                 return;
135         lex >> token;
136         if (token == "width") {
137                 lex.next();
138                 width = LyXLength(lex.getString());
139         } else {
140                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
141                         << endl;
142                 // take countermeasures
143                 lex.pushToken(token);
144         }
145 }
146
147
148 void InsetWrap::write(Buffer const & buf, ostream & os) const
149 {
150         params_.write(os);
151         InsetCollapsable::write(buf, os);
152 }
153
154
155 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
156 {
157         params_.read(lex);
158         InsetCollapsable::read(buf, lex);
159 }
160
161
162 void InsetWrap::validate(LaTeXFeatures & features) const
163 {
164         features.require("floatflt");
165         InsetCollapsable::validate(features);
166 }
167
168
169 auto_ptr<InsetBase> InsetWrap::doClone() const
170 {
171         return auto_ptr<InsetBase>(new InsetWrap(*this));
172 }
173
174
175 string const InsetWrap::editMessage() const
176 {
177         return _("Opened Wrap Inset");
178 }
179
180
181 int InsetWrap::latex(Buffer const & buf, ostream & os,
182                      OutputParams const & runparams) const
183 {
184         os << "\\begin{floating" << params_.type << '}';
185         if (!params_.placement.empty())
186                 os << '[' << params_.placement << ']';
187         os << '{' << params_.width.asLatexString() << "}%\n";
188         int const i = InsetText::latex(buf, os, runparams);
189         os << "\\end{floating" << params_.type << "}%\n";
190         return i + 2;
191 }
192
193
194 int InsetWrap::docbook(Buffer const & buf, ostream & os,
195                        OutputParams const & runparams) const
196 {
197         os << '<' << params_.type << '>';
198         int const i = InsetText::docbook(buf, os, runparams);
199         os << "</" << params_.type << '>';
200         return i;
201 }
202
203
204 bool InsetWrap::insetAllowed(InsetBase::Code code) const
205 {
206         switch(code) {
207         case FLOAT_CODE:
208         case FOOT_CODE:
209         case MARGIN_CODE:
210                 return false;
211         default:
212                 return InsetCollapsable::insetAllowed(code);
213         }
214 }
215
216
217 bool InsetWrap::showInsetDialog(BufferView * bv) const
218 {
219         if (!InsetText::showInsetDialog(bv))
220                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
221         return true;
222 }
223
224
225 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
226 {
227         // Now find the caption in the float...
228         ParagraphList::const_iterator tmp = paragraphs().begin();
229         ParagraphList::const_iterator end = paragraphs().end();
230
231         for (; tmp != end; ++tmp) {
232                 if (tmp->layout()->name() == caplayout) {
233                         string const name = floatname(params_.type, buf.params());
234                         string const str =
235                                 convert<string>(toclist[name].size() + 1)
236                                 + ". " + tmp->asString(buf, false);
237                         lyx::toc::TocItem const item(tmp->id(), 0 , str);
238                         toclist[name].push_back(item);
239                 }
240         }
241 }
242
243
244 string const InsetWrapMailer::name_("wrap");
245
246 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
247         : inset_(inset)
248 {}
249
250
251 string const InsetWrapMailer::inset2string(Buffer const &) const
252 {
253         return params2string(inset_.params());
254 }
255
256
257 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
258 {
259         params = InsetWrapParams();
260         if (in.empty())
261                 return;
262
263         istringstream data(in);
264         LyXLex lex(0,0);
265         lex.setStream(data);
266
267         string name;
268         lex >> name;
269         if (!lex || name != name_)
270                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
271
272         // This is part of the inset proper that is usually swallowed
273         // by LyXText::readInset
274         string id;
275         lex >> id;
276         if (!lex || id != "Wrap")
277                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
278
279         // We have to read the type here!
280         lex >> params.type;
281         params.read(lex);
282 }
283
284
285 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
286 {
287         ostringstream data;
288         data << name_ << ' ';
289         params.write(data);
290         return data.str();
291 }