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