]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
ae39aac22b5df21c5b6c355cc4e0af3140eed660
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetwrap.h"
18 #include "gettext.h"
19 #include "lyxfont.h"
20 #include "BufferView.h"
21 #include "lyxtext.h"
22 #include "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "support/lstrings.h"
25 #include "LaTeXFeatures.h"
26 #include "debug.h"
27 #include "buffer.h"
28 #include "frontends/LyXView.h"
29 #include "frontends/Dialogs.h"
30 #include "lyxlex.h"
31
32 using std::ostream;
33 using std::endl;
34
35
36 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
37         : InsetCollapsable(bp), width_(50, LyXLength::PCW)
38 {
39         string lab(_("wrap"));
40         lab += type;
41         setLabel(lab);
42         LyXFont font(LyXFont::ALL_SANE);
43         font.decSize();
44         font.decSize();
45         font.setColor(LColor::collapsable);
46         setLabelFont(font);
47         Type_ = type;
48         setInsetName(type);
49 }
50
51
52 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
53         : InsetCollapsable(in, same_id), Type_(in.Type_),
54           Placement_(in.Placement_), width_(in.width_)
55 {}
56
57
58 InsetWrap::~InsetWrap()
59 {
60         hideDialog();
61 }
62
63
64 void InsetWrap::write(Buffer const * buf, ostream & os) const
65 {
66         os << "Wrap " // getInsetName()
67            << Type_ << '\n';
68
69         if (!Placement_.empty()) {
70                 os << "placement " << Placement_ << "\n";
71         }
72         os << "width \"" << width_.asString() << "\"\n";
73
74         InsetCollapsable::write(buf, os);
75 }
76
77
78 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
79 {
80         if (lex.isOK()) {
81                 lex.next();
82                 string token = lex.getString();
83                 if (token == "placement") {
84                         lex.next();
85                         Placement_ = lex.getString();
86                 } else {
87                         // take countermeasures
88                         lex.pushToken(token);
89                 }
90         }
91         if (lex.isOK()) {
92                 lex.next();
93                 string token = lex.getString();
94                 if (token == "width") {
95                         lex.next();
96                         width_ = LyXLength(lex.getString());
97                 } else {
98                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
99                                << endl;
100                         // take countermeasures
101                         lex.pushToken(token);
102                 }
103         }
104         InsetCollapsable::read(buf, lex);
105 }
106
107
108 void InsetWrap::validate(LaTeXFeatures & features) const
109 {
110         features.require("floatflt");
111         InsetCollapsable::validate(features);
112 }
113
114
115 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
116 {
117         return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
118 }
119
120
121 string const InsetWrap::editMessage() const
122 {
123         return _("Opened Wrap Inset");
124 }
125
126
127 int InsetWrap::latex(Buffer const * buf,
128                       ostream & os, bool fragile, bool fp) const
129 {
130         os << "\\begin{floating" << Type_ << "}";
131         if (!Placement_.empty()) {
132                 os << "[" << Placement_ << "]";
133         }
134         os  << "{" << width_.asLatexString() << "}%\n";
135
136         int const i = inset.latex(buf, os, fragile, fp);
137
138         os << "\\end{floating" << Type_ << "}%\n";
139         return i + 2;
140 }
141
142
143 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
144 {
145         os << "<" << Type_ << ">";
146         int const i = inset.docbook(buf, os, mixcont);
147         os << "</" << Type_ << ">";
148
149         return i;
150 }
151
152
153 bool InsetWrap::insetAllowed(Inset::Code code) const
154 {
155         switch(code) {
156         case FLOAT_CODE:
157         case FOOT_CODE:
158         case MARGIN_CODE:
159                 return false;
160         default:
161                 return InsetCollapsable::insetAllowed(code);
162         }
163 }
164
165 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
166         const
167 {
168         if (owner() &&
169             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
170                 return -1;
171         }
172         if (!width_.zero()) {
173                 int ww1 = latexTextWidth(bv);
174                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
175                 if (ww2 > 0 && ww2 < ww1) {
176                         return ww2;
177                 }
178                 return ww1;
179         }
180         // this should not happen!
181         return InsetCollapsable::getMaxWidth(bv, inset);
182 }
183
184
185 int InsetWrap::latexTextWidth(BufferView * bv) const
186 {
187         return width_.inPixels(InsetCollapsable::latexTextWidth(bv),
188                                bv->text->defaultHeight());
189 }
190
191
192 string const & InsetWrap::type() const
193 {
194         return Type_;
195 }
196
197
198 LyXLength const & InsetWrap::pageWidth() const
199 {
200         return width_;
201 }
202
203
204 void InsetWrap::pageWidth(LyXLength const & ll)
205 {
206         if (ll != width_) {
207                 width_ = ll;
208                 need_update = FULL;
209         }
210 }
211
212
213 void InsetWrap::placement(string const & p)
214 {
215         Placement_ = p;
216 }
217
218
219 string const & InsetWrap::placement() const
220 {
221         return Placement_;
222 }
223
224
225 bool InsetWrap::showInsetDialog(BufferView * bv) const
226 {
227         if (!inset.showInsetDialog(bv)) {
228                 bv->owner()->getDialogs().showWrap(const_cast<InsetWrap *>(this));
229         }
230         return true;
231 }