]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
59e8f57aa69dc7aeaee13715a0b423c238e7b859
[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/std_sstream.h"
32 #include "support/tostr.h"
33
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         LyXTextClass const & tclass = bp.getLyXTextClass();
72         if (tclass.hasLayout(caplayout))
73                 paragraphs().begin()->layout(tclass[caplayout]);
74 }
75
76
77 InsetWrap::~InsetWrap()
78 {
79         InsetWrapMailer(*this).hideDialog();
80 }
81
82
83 void InsetWrap::priv_dispatch(LCursor & cur, FuncRequest & cmd)
84 {
85         switch (cmd.action) {
86         case LFUN_INSET_MODIFY: {
87                 InsetWrapParams params;
88                 InsetWrapMailer::string2params(cmd.argument, params);
89                 params_.placement = params.placement;
90                 params_.width     = params.width;
91                 cur.bv().update();
92                 break;
93         }
94
95         case LFUN_INSET_DIALOG_UPDATE:
96                 InsetWrapMailer(*this).updateDialog(&cur.bv());
97                 break;
98
99         default:
100                 InsetCollapsable::priv_dispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 void InsetWrapParams::write(ostream & os) const
107 {
108         os << "Wrap " << type << '\n';
109
110         if (!placement.empty())
111                 os << "placement " << placement << "\n";
112
113         os << "width \"" << width.asString() << "\"\n";
114 }
115
116
117 void InsetWrapParams::read(LyXLex & lex)
118 {
119         if (lex.isOK()) {
120                 lex.next();
121                 string token = lex.getString();
122                 if (token == "placement") {
123                         lex.next();
124                         placement = lex.getString();
125                 } else {
126                         // take countermeasures
127                         lex.pushToken(token);
128                 }
129         }
130         if (lex.isOK()) {
131                 lex.next();
132                 string token = lex.getString();
133                 if (token == "width") {
134                         lex.next();
135                         width = LyXLength(lex.getString());
136                 } else {
137                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
138                                << endl;
139                         // take countermeasures
140                         lex.pushToken(token);
141                 }
142         }
143 }
144
145
146 void InsetWrap::write(Buffer const & buf, ostream & os) const
147 {
148         params_.write(os);
149         InsetCollapsable::write(buf, os);
150 }
151
152
153 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
154 {
155         params_.read(lex);
156         InsetCollapsable::read(buf, lex);
157 }
158
159
160 void InsetWrap::validate(LaTeXFeatures & features) const
161 {
162         features.require("floatflt");
163         InsetCollapsable::validate(features);
164 }
165
166
167 auto_ptr<InsetBase> InsetWrap::clone() const
168 {
169         return auto_ptr<InsetBase>(new InsetWrap(*this));
170 }
171
172
173 string const InsetWrap::editMessage() const
174 {
175         return _("Opened Wrap Inset");
176 }
177
178
179 int InsetWrap::latex(Buffer const & buf, ostream & os,
180                      OutputParams const & runparams) const
181 {
182         os << "\\begin{floating" << params_.type << '}';
183         if (!params_.placement.empty())
184                 os << '[' << params_.placement << ']';
185         os << '{' << params_.width.asLatexString() << "}%\n";
186         int const i = InsetText::latex(buf, os, runparams);
187         os << "\\end{floating" << params_.type << "}%\n";
188         return i + 2;
189 }
190
191
192 int InsetWrap::docbook(Buffer const & buf, ostream & os,
193                        OutputParams const & runparams) const
194 {
195         os << '<' << params_.type << '>';
196         int const i = InsetText::docbook(buf, os, runparams);
197         os << "</" << params_.type << '>';
198         return i;
199 }
200
201
202 bool InsetWrap::insetAllowed(InsetOld::Code code) const
203 {
204         switch(code) {
205         case FLOAT_CODE:
206         case FOOT_CODE:
207         case MARGIN_CODE:
208                 return false;
209         default:
210                 return InsetCollapsable::insetAllowed(code);
211         }
212 }
213
214
215 bool InsetWrap::showInsetDialog(BufferView * bv) const
216 {
217         if (!InsetText::showInsetDialog(bv))
218                 InsetWrapMailer(const_cast<InsetWrap &>(*this)).showDialog(bv);
219         return true;
220 }
221
222
223 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const & buf) const
224 {
225         // Now find the caption in the float...
226         ParagraphList::const_iterator tmp = paragraphs().begin();
227         ParagraphList::const_iterator end = paragraphs().end();
228
229         for (; tmp != end; ++tmp) {
230                 if (tmp->layout()->name() == caplayout) {
231                         string const name = floatname(params_.type, buf.params());
232                         string const str =
233                                 tostr(toclist[name].size() + 1)
234                                 + ". " + tmp->asString(buf, false);
235                         lyx::toc::TocItem const item(tmp->id(), 0 , str);
236                         toclist[name].push_back(item);
237                 }
238         }
239 }
240
241
242 string const InsetWrapMailer::name_("wrap");
243
244 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
245         : inset_(inset)
246 {}
247
248
249 string const InsetWrapMailer::inset2string(Buffer const &) const
250 {
251         return params2string(inset_.params());
252 }
253
254
255 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
256 {
257         params = InsetWrapParams();
258         if (in.empty())
259                 return;
260
261         istringstream data(in);
262         LyXLex lex(0,0);
263         lex.setStream(data);
264
265         string name;
266         lex >> name;
267         if (!lex || name != name_)
268                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
269
270         // This is part of the inset proper that is usually swallowed
271         // by LyXText::readInset
272         string id;
273         lex >> id;
274         if (!lex || id != "Wrap")
275                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
276
277         params.read(lex);
278 }
279
280
281 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
282 {
283         ostringstream data;
284         data << name_ << ' ';
285         params.write(data);
286         return data.str();
287 }