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