]> git.lyx.org Git - lyx.git/blob - src/insets/insetwrap.C
Make it compile when USE_BOOST_FORMAT is unset
[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 #include "FloatList.h"
32
33 using std::ostream;
34 using std::endl;
35
36 namespace {
37
38 // this should not be hardcoded, but be part of the definition
39 // of the float (JMarc)
40 string const caplayout("Caption");
41 string floatname(string const & type, BufferParams const & bp)
42 {
43         FloatList const & floats = bp.getLyXTextClass().floats();
44         FloatList::const_iterator it = floats[type];
45         if (it == floats.end())
46                 return type;
47
48         return _(it->second.name());
49 }
50
51 } // namespace anon
52
53
54 InsetWrap::InsetWrap(BufferParams const & bp, string const & type)
55         : InsetCollapsable(bp), width_(50, LyXLength::PCW)
56 {
57         string lab(_("wrap: "));
58         lab += floatname(type, bp);
59         setLabel(lab);
60         LyXFont font(LyXFont::ALL_SANE);
61         font.decSize();
62         font.decSize();
63         font.setColor(LColor::collapsable);
64         setLabelFont(font);
65         Type_ = type;
66         setInsetName(type);
67         LyXTextClass const & tclass = bp.getLyXTextClass();
68         if (tclass.hasLayout(caplayout))
69                 inset.paragraph()->layout(tclass[caplayout]);
70 }
71
72
73 InsetWrap::InsetWrap(InsetWrap const & in, bool same_id)
74         : InsetCollapsable(in, same_id), Type_(in.Type_),
75           Placement_(in.Placement_), width_(in.width_)
76 {}
77
78
79 InsetWrap::~InsetWrap()
80 {
81         hideDialog();
82 }
83
84
85 void InsetWrap::write(Buffer const * buf, ostream & os) const
86 {
87         os << "Wrap " // getInsetName()
88            << Type_ << '\n';
89
90         if (!Placement_.empty()) {
91                 os << "placement " << Placement_ << "\n";
92         }
93         os << "width \"" << width_.asString() << "\"\n";
94
95         InsetCollapsable::write(buf, os);
96 }
97
98
99 void InsetWrap::read(Buffer const * buf, LyXLex & lex)
100 {
101         if (lex.isOK()) {
102                 lex.next();
103                 string token = lex.getString();
104                 if (token == "placement") {
105                         lex.next();
106                         Placement_ = lex.getString();
107                 } else {
108                         // take countermeasures
109                         lex.pushToken(token);
110                 }
111         }
112         if (lex.isOK()) {
113                 lex.next();
114                 string token = lex.getString();
115                 if (token == "width") {
116                         lex.next();
117                         width_ = LyXLength(lex.getString());
118                 } else {
119                         lyxerr << "InsetWrap::Read:: Missing 'width'-tag!"
120                                << endl;
121                         // take countermeasures
122                         lex.pushToken(token);
123                 }
124         }
125         InsetCollapsable::read(buf, lex);
126 }
127
128
129 void InsetWrap::validate(LaTeXFeatures & features) const
130 {
131         features.require("floatflt");
132         InsetCollapsable::validate(features);
133 }
134
135
136 Inset * InsetWrap::clone(Buffer const &, bool same_id) const
137 {
138         return new InsetWrap(*const_cast<InsetWrap *>(this), same_id);
139 }
140
141
142 string const InsetWrap::editMessage() const
143 {
144         return _("Opened Wrap Inset");
145 }
146
147
148 int InsetWrap::latex(Buffer const * buf,
149                       ostream & os, bool fragile, bool fp) const
150 {
151         os << "\\begin{floating" << Type_ << "}";
152         if (!Placement_.empty()) {
153                 os << "[" << Placement_ << "]";
154         }
155         os  << "{" << width_.asLatexString() << "}%\n";
156
157         int const i = inset.latex(buf, os, fragile, fp);
158
159         os << "\\end{floating" << Type_ << "}%\n";
160         return i + 2;
161 }
162
163
164 int InsetWrap::docbook(Buffer const * buf, ostream & os, bool mixcont) const
165 {
166         os << "<" << Type_ << ">";
167         int const i = inset.docbook(buf, os, mixcont);
168         os << "</" << Type_ << ">";
169
170         return i;
171 }
172
173
174 bool InsetWrap::insetAllowed(Inset::Code code) const
175 {
176         switch(code) {
177         case FLOAT_CODE:
178         case FOOT_CODE:
179         case MARGIN_CODE:
180                 return false;
181         default:
182                 return InsetCollapsable::insetAllowed(code);
183         }
184 }
185
186 int InsetWrap::getMaxWidth(BufferView * bv, UpdatableInset const * inset)
187         const
188 {
189         if (owner() &&
190             static_cast<UpdatableInset*>(owner())->getMaxWidth(bv, inset) < 0) {
191                 return -1;
192         }
193         if (!width_.zero()) {
194                 int ww1 = latexTextWidth(bv);
195                 int ww2 = InsetCollapsable::getMaxWidth(bv, inset);
196                 if (ww2 > 0 && ww2 < ww1) {
197                         return ww2;
198                 }
199                 return ww1;
200         }
201         // this should not happen!
202         return InsetCollapsable::getMaxWidth(bv, inset);
203 }
204
205
206 int InsetWrap::latexTextWidth(BufferView * bv) const
207 {
208         return width_.inPixels(InsetCollapsable::latexTextWidth(bv));
209 }
210
211
212 string const & InsetWrap::type() const
213 {
214         return Type_;
215 }
216
217
218 LyXLength const & InsetWrap::pageWidth() const
219 {
220         return width_;
221 }
222
223
224 void InsetWrap::pageWidth(LyXLength const & ll)
225 {
226         if (ll != width_) {
227                 width_ = ll;
228                 need_update = FULL;
229         }
230 }
231
232
233 void InsetWrap::placement(string const & p)
234 {
235         Placement_ = p;
236 }
237
238
239 string const & InsetWrap::placement() const
240 {
241         return Placement_;
242 }
243
244
245 bool InsetWrap::showInsetDialog(BufferView * bv) const
246 {
247         if (!inset.showInsetDialog(bv)) {
248                 bv->owner()->getDialogs().showWrap(const_cast<InsetWrap *>(this));
249         }
250         return true;
251 }
252
253
254 void InsetWrap::addToToc(toc::TocList & toclist, Buffer const * buf) const
255 {
256         // Now find the caption in the float...
257         // We now tranverse the paragraphs of
258         // the inset...
259         Paragraph * tmp = inset.paragraph();
260         while (tmp) {
261                 if (tmp->layout()->name() == caplayout) {
262                         string const str =
263                                 tostr(toclist[type()].size() + 1)
264                                 + ". " + tmp->asString(buf, false);
265                         toc::TocItem const item(tmp, 0 , str);
266                         toclist[type()].push_back(item);
267                 }
268                 tmp = tmp->next();
269         }
270 }