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