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