]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
Add In nsetOld * argument to updateInset to rebreak the correct par.
[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 using std::auto_ptr;
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()
79 {
80         InsetWrapMailer(*this).hideDialog();
81 }
82
83
84 dispatch_result InsetWrap::localDispatch(FuncRequest const & cmd)
85 {
86         switch (cmd.action) {
87         case LFUN_INSET_MODIFY: {
88                 InsetWrapParams params;
89                 InsetWrapMailer::string2params(cmd.argument, params);
90
91                 params_.placement = params.placement;
92                 params_.width     = params.width;
93
94                 cmd.view()->updateInset(this);
95                 return DISPATCHED;
96         }
97
98         case LFUN_INSET_DIALOG_UPDATE:
99                 InsetWrapMailer(*this).updateDialog(cmd.view());
100                 return DISPATCHED;
101
102         default:
103                 return InsetCollapsable::localDispatch(cmd);
104         }
105 }
106
107
108 void InsetWrapParams::write(ostream & os) const
109 {
110         os << "Wrap " << type << '\n';
111
112         if (!placement.empty())
113                 os << "placement " << placement << "\n";
114
115         os << "width \"" << width.asString() << "\"\n";
116 }
117
118
119 void InsetWrapParams::read(LyXLex & lex)
120 {
121         if (lex.isOK()) {
122                 lex.next();
123                 string token = lex.getString();
124                 if (token == "placement") {
125                         lex.next();
126                         placement = lex.getString();
127                 } else {
128                         // take countermeasures
129                         lex.pushToken(token);
130                 }
131         }
132         if (lex.isOK()) {
133                 lex.next();
134                 string token = lex.getString();
135                 if (token == "width") {
136                         lex.next();
137                         width = LyXLength(lex.getString());
138                 } else {
139                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
140                                << endl;
141                         // take countermeasures
142                         lex.pushToken(token);
143                 }
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::clone() 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                      LatexRunParams const & runparams) const
183 {
184         os << "\\begin{floating" << params_.type << '}';
185         if (!params_.placement.empty()) {
186                 os << '[' << params_.placement << ']';
187         }
188         os  << '{' << params_.width.asLatexString() << "}%\n";
189
190         int const i = inset.latex(buf, os, runparams);
191
192         os << "\\end{floating" << params_.type << "}%\n";
193         return i + 2;
194 }
195
196
197 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
198 {
199         os << '<' << params_.type << '>';
200         int const i = inset.docbook(buf, os, mixcont);
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 int InsetWrap::latexTextWidth(BufferView * bv) const
221 {
222         return params_.width.inPixels(InsetCollapsable::latexTextWidth(bv));
223 }
224
225
226 bool InsetWrap::showInsetDialog(BufferView * bv) const
227 {
228         if (!inset.showInsetDialog(bv)) {
229                 InsetWrap * tmp = const_cast<InsetWrap *>(this);
230                 InsetWrapMailer(*tmp).showDialog(bv);
231         }
232         return true;
233 }
234
235
236 void InsetWrap::addToToc(lyx::toc::TocList & toclist, Buffer const * buf) const
237 {
238         // Now find the caption in the float...
239         ParagraphList::iterator tmp = inset.paragraphs.begin();
240         ParagraphList::iterator end = inset.paragraphs.end();
241
242         for (; tmp != end; ++tmp) {
243                 if (tmp->layout()->name() == caplayout) {
244                         string const name = floatname(params_.type, buf->params);
245                         string const str =
246                                 tostr(toclist[name].size() + 1)
247                                 + ". " + tmp->asString(buf, false);
248                         lyx::toc::TocItem const item(tmp->id(), 0 , str);
249                         toclist[name].push_back(item);
250                 }
251         }
252 }
253
254
255 string const InsetWrapMailer::name_("wrap");
256
257 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
258         : inset_(inset)
259 {}
260
261
262 string const InsetWrapMailer::inset2string(Buffer const &) const
263 {
264         return params2string(inset_.params());
265 }
266
267
268 void InsetWrapMailer::string2params(string const & in,
269                                     InsetWrapParams & params)
270 {
271         params = InsetWrapParams();
272
273         if (in.empty())
274                 return;
275
276         istringstream data(STRCONV(in));
277         LyXLex lex(0,0);
278         lex.setStream(data);
279
280         if (lex.isOK()) {
281                 lex.next();
282                 string const token = lex.getString();
283                 if (token != name_)
284                         return;
285         }
286
287         // This is part of the inset proper that is usually swallowed
288         // by Buffer::readInset
289         if (lex.isOK()) {
290                 lex.next();
291                 string const token = lex.getString();
292                 if (token != "Wrap" || !lex.eatLine())
293                         return;
294         }
295
296         if (lex.isOK()) {
297                 params.read(lex);
298         }
299 }
300
301
302 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
303 {
304         ostringstream data;
305         data << name_ << ' ';
306         params.write(data);
307         return STRCONV(data.str());
308 }