]> git.lyx.org Git - lyx.git/blob - src/mathed/math_textinset.C
fix #1073
[lyx.git] / src / mathed / math_textinset.C
1
2 #include "math_textinset.h"
3 #include "metricsinfo.h"
4 #include "debug.h"
5
6
7 MathTextInset::MathTextInset()
8         : MathNestInset(1)
9 {}
10
11
12 MathInset * MathTextInset::clone() const
13 {
14         return new MathTextInset(*this);
15 }
16
17
18 MathInset::idx_type MathTextInset::pos2row(pos_type pos) const
19 {
20         for (pos_type r = 0, n = cache_.nargs(); r < n; ++r)
21                 if (pos >= cache_.cellinfo_[r].begin_ && pos <= cache_.cellinfo_[r].end_)
22                         return r;
23         lyxerr << "illegal row for pos " << pos << "\n";
24         return 0;
25 }
26
27
28 void MathTextInset::getPos(idx_type /*idx*/, pos_type pos, int & x, int & y) const
29 {
30         idx_type const i = pos2row(pos);
31         pos_type const p = pos - cache_.cellinfo_[i].begin_;
32         cache_.getPos(i, p, x, y);
33         y = cache_.cell(i).yo();
34 }
35
36
37 bool MathTextInset::idxUpDown2(idx_type &, pos_type & pos, bool up,
38         int /*targetx*/) const
39 {
40         // try to move only one screen row up or down if possible
41         idx_type i = pos2row(pos);
42         //lyxerr << "\nMathTextInset::idxUpDown()  i: " << i << endl;
43         MathGridInset::CellInfo const & cell1 = cache_.cellinfo_[i];
44         int const x = cache_.cell(i).pos2x(pos - cell1.begin_, cell1.glue_);
45         if (up) {
46                 if (i == 0)
47                         return false;
48                 --i;
49         } else {
50                 ++i;
51                 if (i == cache_.nargs())
52                         return false;
53         }
54         MathGridInset::CellInfo const & cell2 = cache_.cellinfo_[i];
55         pos = cell2.begin_ + cache_.cell(i).x2pos(x, cell2.glue_);
56         return true;
57 }
58
59
60 void MathTextInset::metrics(MetricsInfo & mi) const
61 {
62         cell(0).metrics(mi);
63
64         // we do our own metrics fiddling
65         // save old positional information
66         int const old_xo = cache_.cell(0).xo();
67         int const old_yo = cache_.cell(0).yo();
68
69         // delete old cache
70         cache_ = MathGridInset(1, 0);
71
72         int spaces  = 0;
73         int safe    = 0;
74         int curr    = 0;
75         int begin   = 0;
76         int safepos = 0;
77         for (size_type i = 0, n = cell(0).size(); i < n; ++i) {
78                 //lyxerr << "at pos: " << i << " of " << n << " safepos: " << safepos
79                 //      << " curr: " << curr << " safe: " << safe
80                 //      << " spaces: " << spaces << endl;
81
82                 //   0      1      2      3       4      5      6
83                 // <char> <char> <char> <space> <char> <char> <char>
84                 // ................... <safe>
85                 //                      ..........................<curr>
86                 // ....................<safepos>
87
88                 // Special handling of spaces. We reached a safe position for breaking.
89                 char const c = cell(0)[i]->getChar();
90                 if (c == ' ') {
91                         //lyxerr << "reached safe pos\n";
92                         // we don't count the space into the safe pos
93                         safe += curr;
94                         // we reset to this safepos if the next chunk does not fit
95                         safepos = i;
96                         ++spaces;
97                         // restart chunk with size of the space
98                         curr = cell(0)[i]->width();
99                         continue;
100                 }
101
102                 if (c != '\n') {
103                         // This is a regular char. Go on if we either don't care for
104                         // the width limit or have not reached that limit.
105                         curr += cell(0)[i]->width();
106                         if (!mi.base.restrictwidth || curr + safe <= mi.base.textwidth)
107                                 continue;
108                 }
109
110                 // We passed the limit. Create a row entry.
111                 //lyxerr << "passed limit\n";
112                 cache_.appendRow();
113                 MathArray & ar = cache_.cell(cache_.nargs() - 1);
114                 MathGridInset::CellInfo & row = cache_.cellinfo_.back();
115                 if (c == '\n') {
116                         // we are here because we hit a hard newline
117                         row.begin_ = begin;
118                         row.end_   = i + 1;
119                         begin      = i + 1;    // next chunk starts after the newline
120                         spaces     = 0;
121                 } else if (spaces) {
122                         // but we had a space break before this position.
123                         // so retreat to this position
124                         //lyxerr << "... but had safe pos.\n";
125                         row.begin_ = begin;
126                         row.end_   = safepos;  // this is position of the safe space
127                         i          = safepos;  // i gets incremented at end of loop
128                         begin      = i + 1;    // next chunk starts after the space
129                         spaces     = 0;
130                 } else {
131                         // This item is too large and it is the only one.
132                         // We have no choice but to produce an overfull box.
133                         lyxerr << "... without safe pos\n";
134                         row.begin_ = begin;
135                         row.end_   = i + 1;
136                         begin      = i + 1;
137                 }
138                 ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
139                 //lyxerr << "line: " << ar << "\n";
140                 // in any case, start the new row with empty boxes
141                 curr = 0;
142                 safe = 0;
143         }
144         // last row: put in everything else
145         cache_.appendRow();
146         MathArray & ar = cache_.cell(cache_.nargs() - 1);
147         MathGridInset::CellInfo & row = cache_.cellinfo_.back();
148         row.begin_ = begin;
149         row.end_   = cell(0).size();
150         ar = MathArray(cell(0).begin() + row.begin_, cell(0).begin() + row.end_);
151         //lyxerr << "last line: " << ar.data() << "\n";
152
153         // what to report?
154         cache_.metrics(mi);
155         dim_ = cache_.dimensions();
156         //lyxerr << "outer dim: " << dim_ << endl;
157
158         // reset position cache
159         for (idx_type i = 0; i < cache_.nargs(); ++i)
160                 cache_.cell(i).setXY(old_xo, old_yo);
161
162 }
163
164
165 void MathTextInset::draw(PainterInfo & pi, int x, int y) const
166 {
167         cache_.draw(pi, x + 1, y);
168 }
169
170
171 void MathTextInset::drawSelection(PainterInfo & pi,
172                 idx_type idx1, pos_type pos1, idx_type idx2, pos_type pos2) const
173 {
174         cache_.drawSelection(pi, idx1, pos1, idx2, pos2);
175 }