]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
Fix linking issue with MacOSX.
[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         // FIXME UNICODE
51         setLabel(lyx::to_utf8(_("wrap: ")) + floatName(type, bp));
52         LyXFont font(LyXFont::ALL_SANE);
53         font.decSize();
54         font.decSize();
55         font.setColor(LColor::collapsable);
56         setLabelFont(font);
57         params_.type = type;
58         params_.width = LyXLength(50, LyXLength::PCW);
59         setInsetName(type);
60 }
61
62
63 InsetWrap::~InsetWrap()
64 {
65         InsetWrapMailer(*this).hideDialog();
66 }
67
68
69 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
70 {
71         switch (cmd.action) {
72         case LFUN_INSET_MODIFY: {
73                 InsetWrapParams params;
74                 InsetWrapMailer::string2params(lyx::to_utf8(cmd.argument()), params);
75                 params_.placement = params.placement;
76                 params_.width     = params.width;
77                 break;
78         }
79
80         case LFUN_INSET_DIALOG_UPDATE:
81                 InsetWrapMailer(*this).updateDialog(&cur.bv());
82                 break;
83
84         case LFUN_MOUSE_RELEASE: {
85                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
86                         InsetWrapMailer(*this).showDialog(&cur.bv());
87                         break;
88                 }
89                 InsetCollapsable::doDispatch(cur, cmd);
90                 break;
91         }
92
93         default:
94                 InsetCollapsable::doDispatch(cur, cmd);
95                 break;
96         }
97 }
98
99
100 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
101                 FuncStatus & flag) const
102 {
103         switch (cmd.action) {
104         case LFUN_INSET_MODIFY:
105         case LFUN_INSET_DIALOG_UPDATE:
106                 flag.enabled(true);
107                 return true;
108
109         default:
110                 return InsetCollapsable::getStatus(cur, cmd, flag);
111         }
112 }
113
114
115 void InsetWrapParams::write(ostream & os) const
116 {
117         os << "Wrap " << type << '\n';
118
119         if (!placement.empty())
120                 os << "placement " << placement << "\n";
121
122         os << "width \"" << width.asString() << "\"\n";
123 }
124
125
126 void InsetWrapParams::read(LyXLex & lex)
127 {
128         string token;
129         lex >> token;
130         if (token == "placement")
131                 lex >> placement;
132         else {
133                 // take countermeasures
134                 lex.pushToken(token);
135         }
136         if (!lex)
137                 return;
138         lex >> token;
139         if (token == "width") {
140                 lex.next();
141                 width = LyXLength(lex.getString());
142         } else {
143                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
144                         << endl;
145                 // take countermeasures
146                 lex.pushToken(token);
147         }
148 }
149
150
151 void InsetWrap::write(Buffer const & buf, ostream & os) const
152 {
153         params_.write(os);
154         InsetCollapsable::write(buf, os);
155 }
156
157
158 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
159 {
160         params_.read(lex);
161         InsetCollapsable::read(buf, lex);
162 }
163
164
165 void InsetWrap::validate(LaTeXFeatures & features) const
166 {
167         features.require("floatflt");
168         InsetCollapsable::validate(features);
169 }
170
171
172 auto_ptr<InsetBase> InsetWrap::doClone() const
173 {
174         return auto_ptr<InsetBase>(new InsetWrap(*this));
175 }
176
177
178 docstring const InsetWrap::editMessage() const
179 {
180         return _("Opened Wrap Inset");
181 }
182
183
184 int InsetWrap::latex(Buffer const & buf, ostream & os,
185                      OutputParams const & runparams) const
186 {
187         os << "\\begin{floating" << params_.type << '}';
188         if (!params_.placement.empty())
189                 os << '[' << params_.placement << ']';
190         os << '{' << params_.width.asLatexString() << "}%\n";
191         int const i = InsetText::latex(buf, os, runparams);
192         os << "\\end{floating" << params_.type << "}%\n";
193         return i + 2;
194 }
195
196
197 int InsetWrap::docbook(Buffer const & buf, ostream & os,
198                        OutputParams const & runparams) const
199 {
200         os << '<' << params_.type << '>';
201         int const i = InsetText::docbook(buf, os, runparams);
202         os << "</" << params_.type << '>';
203         return i;
204 }
205
206
207 bool InsetWrap::insetAllowed(InsetBase::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 (!InsetText::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         ParConstIterator pit = par_const_iterator_begin(*this);
231         ParConstIterator end = par_const_iterator_end(*this);
232
233         // Find a caption layout in one of the (child inset's) pars
234         for (; pit != end; ++pit) {
235                 if (pit->layout()->labeltype == LABEL_SENSITIVE) {
236                         string const type = params_.type;
237                         string const str =
238                                 convert<string>(toclist[type].size() + 1)
239                                 + ". " + pit->asString(buf, false);
240                         lyx::toc::TocItem const item(pit, 0 , str);
241                         toclist[type].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         // We have to read the type here!
283         lex >> params.type;
284         params.read(lex);
285 }
286
287
288 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
289 {
290         ostringstream data;
291         data << name_ << ' ';
292         params.write(data);
293         return data.str();
294 }