]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[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 "TocBackend.h"
32
33 #include "support/convert.h"
34
35
36 namespace lyx {
37
38 using std::string;
39 using std::endl;
40 using std::auto_ptr;
41 using std::istringstream;
42 using std::ostream;
43 using std::ostringstream;
44
45
46 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
47         : InsetCollapsable(bp)
48 {
49         setLabel(_("wrap: ") + floatName(type, bp));
50         LyXFont font(LyXFont::ALL_SANE);
51         font.decSize();
52         font.decSize();
53         font.setColor(LColor::collapsable);
54         setLabelFont(font);
55         params_.type = type;
56         params_.width = LyXLength(50, LyXLength::PCW);
57         setInsetName(from_utf8(type));
58 }
59
60
61 InsetWrap::~InsetWrap()
62 {
63         InsetWrapMailer(*this).hideDialog();
64 }
65
66
67 void InsetWrap::doDispatch(LCursor & cur, FuncRequest & cmd)
68 {
69         switch (cmd.action) {
70         case LFUN_INSET_MODIFY: {
71                 InsetWrapParams params;
72                 InsetWrapMailer::string2params(to_utf8(cmd.argument()), params);
73                 params_.placement = params.placement;
74                 params_.width     = params.width;
75                 break;
76         }
77
78         case LFUN_INSET_DIALOG_UPDATE:
79                 InsetWrapMailer(*this).updateDialog(&cur.bv());
80                 break;
81
82         case LFUN_MOUSE_RELEASE: {
83                 if (cmd.button() == mouse_button::button3 && hitButton(cmd)) {
84                         InsetWrapMailer(*this).showDialog(&cur.bv());
85                         break;
86                 }
87                 InsetCollapsable::doDispatch(cur, cmd);
88                 break;
89         }
90
91         default:
92                 InsetCollapsable::doDispatch(cur, cmd);
93                 break;
94         }
95 }
96
97
98 bool InsetWrap::getStatus(LCursor & cur, FuncRequest const & cmd,
99                 FuncStatus & flag) const
100 {
101         switch (cmd.action) {
102         case LFUN_INSET_MODIFY:
103         case LFUN_INSET_DIALOG_UPDATE:
104                 flag.enabled(true);
105                 return true;
106
107         default:
108                 return InsetCollapsable::getStatus(cur, cmd, flag);
109         }
110 }
111
112
113 void InsetWrapParams::write(ostream & os) const
114 {
115         os << "Wrap " << type << '\n';
116
117         if (!placement.empty())
118                 os << "placement " << placement << "\n";
119
120         os << "width \"" << width.asString() << "\"\n";
121 }
122
123
124 void InsetWrapParams::read(LyXLex & lex)
125 {
126         string token;
127         lex >> token;
128         if (token == "placement")
129                 lex >> placement;
130         else {
131                 // take countermeasures
132                 lex.pushToken(token);
133         }
134         if (!lex)
135                 return;
136         lex >> token;
137         if (token == "width") {
138                 lex.next();
139                 width = LyXLength(lex.getString());
140         } else {
141                 lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
142                         << endl;
143                 // take countermeasures
144                 lex.pushToken(token);
145         }
146 }
147
148
149 void InsetWrap::write(Buffer const & buf, ostream & os) const
150 {
151         params_.write(os);
152         InsetCollapsable::write(buf, os);
153 }
154
155
156 void InsetWrap::read(Buffer const & buf, LyXLex & lex)
157 {
158         params_.read(lex);
159         InsetCollapsable::read(buf, lex);
160 }
161
162
163 void InsetWrap::validate(LaTeXFeatures & features) const
164 {
165         features.require("floatflt");
166         InsetCollapsable::validate(features);
167 }
168
169
170 auto_ptr<InsetBase> InsetWrap::doClone() const
171 {
172         return auto_ptr<InsetBase>(new InsetWrap(*this));
173 }
174
175
176 docstring const InsetWrap::editMessage() const
177 {
178         return _("Opened Wrap Inset");
179 }
180
181
182 int InsetWrap::latex(Buffer const & buf, odocstream & os,
183                      OutputParams const & runparams) const
184 {
185         os << "\\begin{floating" << from_ascii(params_.type) << '}';
186         if (!params_.placement.empty())
187                 os << '[' << from_ascii(params_.placement) << ']';
188         os << '{' << from_ascii(params_.width.asLatexString()) << "}%\n";
189         int const i = InsetText::latex(buf, os, runparams);
190         os << "\\end{floating" << from_ascii(params_.type) << "}%\n";
191         return i + 2;
192 }
193
194
195 int InsetWrap::docbook(Buffer const & buf, odocstream & os,
196                        OutputParams const & runparams) const
197 {
198         // FIXME UNICODE
199         os << '<' << from_ascii(params_.type) << '>';
200         int const i = InsetText::docbook(buf, os, runparams);
201         os << "</" << from_ascii(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(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                         Toc & toc = toclist[params_.type];
236                         docstring const str =
237                                 convert<docstring>(toc.size() + 1)
238                                 + ". " + pit->asString(buf, false);
239                         toc.push_back(TocItem(pit, 0, str));
240                 }
241         }
242 }
243
244
245 string const InsetWrapMailer::name_("wrap");
246
247 InsetWrapMailer::InsetWrapMailer(InsetWrap & inset)
248         : inset_(inset)
249 {}
250
251
252 string const InsetWrapMailer::inset2string(Buffer const &) const
253 {
254         return params2string(inset_.params());
255 }
256
257
258 void InsetWrapMailer::string2params(string const & in, InsetWrapParams & params)
259 {
260         params = InsetWrapParams();
261         if (in.empty())
262                 return;
263
264         istringstream data(in);
265         LyXLex lex(0,0);
266         lex.setStream(data);
267
268         string name;
269         lex >> name;
270         if (!lex || name != name_)
271                 return print_mailer_error("InsetWrapMailer", in, 1, name_);
272
273         // This is part of the inset proper that is usually swallowed
274         // by LyXText::readInset
275         string id;
276         lex >> id;
277         if (!lex || id != "Wrap")
278                 return print_mailer_error("InsetBoxMailer", in, 2, "Wrap");
279
280         // We have to read the type here!
281         lex >> params.type;
282         params.read(lex);
283 }
284
285
286 string const InsetWrapMailer::params2string(InsetWrapParams const & params)
287 {
288         ostringstream data;
289         data << name_ << ' ';
290         params.write(data);
291         return data.str();
292 }
293
294
295 } // namespace lyx