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