]> git.lyx.org Git - lyx.git/blob - src/mathed/math_parboxinset.C
replace (ascent, descent, width) triples by a structure 'dimension'
[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         dim_ = xcell(0).metrics(mi);
48
49 #else
50
51         xcell(0).metricsExternal(mi, rows_);
52
53         int spaces = 0;
54         Dimension safe(0, 0, 0);
55         Dimension curr(0, 0, 0);
56         int safepos = 0;
57         int yo = 0;
58         for (size_type i = 0, n = cell(0).size(); i != n; ++i) {
59                 // Special handling of spaces. We reached a safe position for breaking.
60                 if (cell(0)[i]->getChar() == ' ') {
61                         safe += curr;
62                         safepos = i + 1;
63                         ++spaces;
64                         // restart chunk
65                         curr = Dimension(0, 0, 0);
66                         continue;
67                 }
68
69                 // This is a regular item. Go on if we either don't care for
70                 // the width limit or have not reached that limit.
71                 curr += rows_[i].dim;
72                 if (curr.w + safe.w <= lyx_width_) 
73                         continue;
74
75                 // We passed the limit. Create a row entry.
76                 MathXArray::Row row;
77                 if (spaces) {
78                         // but we had a space break before this position.
79                         row.dim  = safe;
80                         row.glue = (lyx_width_ - safe.w) / spaces;
81                         row.end  = safepos;
82                         i        = safepos;
83                         spaces   = 0;
84                 } else {
85                         // This item is too large and it is the only one.
86                         // We have no choice but to produce an overfull box.
87                         row.dim  = curr;   // safe should be 0.
88                         row.glue = 0;      // does not matter
89                         row.end  = i + 1;
90                 }
91                 yo      += rows_[i].dim.height();
92                 row.yo   = yo;
93                 rows_.push_back(row);
94         }
95         // last row:
96         MathXArray::Row row;
97         row.dim  = safe;
98         row.dim += curr;
99         row.end  = cell(0).size();
100         row.glue = spaces ? (lyx_width_ - row.dim.w) / spaces : 0;
101         yo      += row.dim.height();
102         row.yo   = yo;
103         rows_.push_back(row);
104
105         // what to report?
106         dim_  = xcell(0).dim();
107         metricsMarkers();
108 #endif
109 }
110
111
112 void MathParboxInset::draw(MathPainterInfo & pi, int x, int y) const
113 {
114         MathFontSetChanger dummy(pi.base, "textnormal");
115 #if 1
116         xcell(0).draw(pi, x + 1, y);
117 #else
118         xcell(0).drawExternal(pi, x + 1, y, rows_);
119 #endif
120         drawMarkers(pi, x, y);
121 }
122
123
124 void MathParboxInset::write(WriteStream & os) const
125 {
126         os << "\\parbox";
127         if (position_ != 'c')
128                 os << '[' << position_ << ']';
129         os << '{' << tex_width_ << "}{" << cell(0) << '}';
130 }
131
132
133 void MathParboxInset::infoize(std::ostream & os) const
134 {
135         os << "Box: Parbox " << tex_width_;
136 }
137