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