]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parboxinset.C
some code shuffling. New 'Dimension' class instead of passing around three
[lyx.git] / src / mathed / math_parboxinset.C
1
2 #include "math_parboxinset.h"
3 #include "math_mathmlstream.h"
4 #include "math_streamstr.h"
5 #include "lyxlength.h"
6 #include "debug.h"
7
8
9 MathParboxInset::MathParboxInset()
10         : MathNestInset(1), lyx_width_(0), tex_width_("0mm"),
11           position_('c')
12 {
13         lyxerr << "constructing MathParboxInset\n";
14 }
15
16
17 MathInset * MathParboxInset::clone() const
18 {
19         return new MathParboxInset(*this);
20 }
21
22
23 void MathParboxInset::setPosition(string const & p)
24 {
25         position_ = p.size() > 0 ? p[0] : 'c';
26 }
27
28
29 void MathParboxInset::setWidth(string const & w)
30 {
31         tex_width_ = w;
32         lyx_width_ = LyXLength(w).inBP();
33         lyxerr << "setting " << w << " to " << lyx_width_ << " pixel\n";
34 }
35
36
37 void MathParboxInset::metrics(MathMetricsInfo & mi) const
38 {
39         MathFontSetChanger dummy(mi.base, "textnormal");
40
41         // we do our own metrics fiddling
42         // delete old cache
43         rows_.clear();
44
45 #if 1
46
47         xcell(0).metrics(mi);
48         width_ = xcell(0).width();
49         ascent_ = xcell(0).ascent();
50         descent_ = xcell(0).descent();
51
52 #else
53
54         xcell(0).metricsExternal(mi, rows_);
55
56         int spaces = 0;
57         Dimension safe(0, 0, 0);
58         Dimension curr(0, 0, 0);
59         int safepos = 0;
60         int yo = 0;
61         for (size_type i = 0, n = cell(0).size(); i != n; ++i) {
62                 // Special handling of spaces. We reached a safe position for breaking.
63                 if (cell(0)[i]->getChar() == ' ') {
64                         safe += curr;
65                         safepos = i + 1;
66                         ++spaces;
67                         // restart chunk
68                         curr = Dimension(0, 0, 0);
69                         continue;
70                 }
71
72                 // This is a regular item. Go on if we either don't care for
73                 // the width limit or have not reached that limit.
74                 curr += rows_[i].dim;
75                 if (curr.w + safe.w <= lyx_width_) 
76                         continue;
77
78                 // We passed the limit. Create a row entry.
79                 MathXArray::Row row;
80                 if (spaces) {
81                         // but we had a space break before this position.
82                         row.dim  = safe;
83                         row.glue = (lyx_width_ - safe.w) / spaces;
84                         row.end  = safepos;
85                         i        = safepos;
86                         spaces   = 0;
87                 } else {
88                         // This item is too large and it is the only one.
89                         // We have no choice but to produce an overfull box.
90                         row.dim  = curr;   // safe should be 0.
91                         row.glue = 0;      // does not matter
92                         row.end  = i + 1;
93                 }
94                 yo      += rows_[i].dim.height();
95                 row.yo   = yo;
96                 rows_.push_back(row);
97         }
98         // last row:
99         MathXArray::Row row;
100         row.dim  = safe;
101         row.dim += curr;
102         row.end  = cell(0).size();
103         row.glue = spaces ? (lyx_width_ - row.dim.w) / spaces : 0;
104         yo      += row.dim.height();
105         row.yo   = yo;
106         rows_.push_back(row);
107
108         // what to report?
109         ascent_  = xcell(0).ascent();
110         descent_ = xcell(0).descent() + 1;
111         width_   = xcell(0).width()   + 2;
112 #endif
113 }
114
115
116 void MathParboxInset::draw(MathPainterInfo & pi, int x, int y) const
117 {
118         MathFontSetChanger dummy(pi.base, "textnormal");
119 #if 1
120         xcell(0).draw(pi, x + 1, y);
121 #else
122         xcell(0).drawExternal(pi, x + 1, y, rows_);
123 #endif
124         drawMarkers(pi, x, y);
125 }
126
127
128 void MathParboxInset::write(WriteStream & os) const
129 {
130         os << "\\parbox";
131         if (position_ != 'c')
132                 os << '[' << position_ << ']';
133         os << '{' << tex_width_ << "}{" << cell(0) << '}';
134 }
135
136
137 void MathParboxInset::infoize(std::ostream & os) const
138 {
139         os << "Box: Parbox " << tex_width_ << ' ';
140 }
141