]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
Handle boundary in getColumnNearX (and more)
[lyx.git] / src / Row.cpp
1 /**
2  * \file Row.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  * \author André Pönitz
9  * \author Jürgen Vigna
10  * \author Jean-Marc Lasgouttes
11  *
12  * Full author contact details are available in file CREDITS.
13  *
14  * Metrics for an on-screen text row.
15  */
16
17 #include <config.h>
18
19 #include "Row.h"
20
21 #include "DocIterator.h"
22
23 #include "frontends/FontMetrics.h"
24
25 #include "support/debug.h"
26 #include "support/lassert.h"
27
28 #include <algorithm>
29 #include <ostream>
30
31 using namespace std;
32
33 namespace lyx {
34
35 using frontend::FontMetrics;
36
37 double Row::Element::pos2x(pos_type const i) const
38 {
39         bool const rtl = font.isVisibleRightToLeft();
40
41         // handle first the two bounds of the element
42         if ((!rtl && pos >= i) || (rtl && endpos <= i))
43                 return 0;
44         if ((!rtl && endpos <= i) || (rtl && pos >= i))
45                 return width();
46
47         FontMetrics const & fm = theFontMetrics(font);
48         // FIXME Avoid caching of metrics there?
49         int const w = fm.width(str.substr(0, i - pos));
50         if (rtl)
51                 return width() - w;
52         else
53                 return w;
54 }
55
56
57         pos_type Row::Element::x2pos(double &x, bool const low) const
58 {
59         //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
60         // if element is rtl, flip x value
61         bool const rtl = font.isVisibleRightToLeft();
62         double x2 = rtl ? (width() - x) : x;
63
64         FontMetrics const & fm = theFontMetrics(font);
65         double last_w = 0;
66         double w = 0;
67         size_t i = 1;
68         // non-STRING element only contain one position
69         if (type != STRING) {
70                 i = 0;
71                 w = width();
72         } else {
73                 // FIXME: implement dichotomy search?
74                 for ( ; i <= str.size() ; ++i) {
75                         last_w = w;
76                         w = fm.width(str.substr(0,i));
77                         if (w > x2) {
78                                 --i;
79                                 break;
80                         }
81                 }
82                 // if (i == str.size())
83                 //      lyxerr << " NOT FOUND ";
84         }
85
86         // round to the closest side
87         if (!low && (x2 - last_w > w - x2)) {
88                 x2 = w;
89                 ++i;
90         } else
91                 x2 = last_w;
92
93         // is element is rtl, flip values
94         if (rtl) {
95                 x = width() - x2;
96         } else {
97                 x = x2;
98         }
99
100         //lyxerr << "=> p=" << i << " x=" << x << endl;
101         return pos + i;
102 }
103
104
105 Row::Row()
106         : separator(0), label_hfill(0), x(0), right_margin(0),
107         sel_beg(-1), sel_end(-1),
108         begin_margin_sel(false), end_margin_sel(false),
109         changed_(false), crc_(0), pos_(0), end_(0)
110 {}
111
112
113 void Row::setCrc(size_type crc) const
114 {
115         changed_ = crc != crc_;
116         crc_ = crc;
117 }
118
119
120 void Row::pos(pos_type p)
121 {
122         pos_ = p;
123 }
124
125
126 void Row::endpos(pos_type p)
127 {
128         end_ = p;
129 }
130
131
132 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
133                 DocIterator const & end) const
134 {
135         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
136         pos_type const margin_pos = left_margin ? pos_ : end_;
137
138         // Is the chosen margin selected ?
139         if (sel_pos == margin_pos) {
140                 if (beg.pos() == end.pos())
141                         // This is a special case in which the space between after
142                         // pos i-1 and before pos i is selected, i.e. the margins
143                         // (see DocIterator::boundary_).
144                         return beg.boundary() && !end.boundary();
145                 else if (end.pos() == margin_pos)
146                         // If the selection ends around the margin, it is only
147                         // drawn if the cursor is after the margin.
148                         return !end.boundary();
149                 else if (beg.pos() == margin_pos)
150                         // If the selection begins around the margin, it is
151                         // only drawn if the cursor is before the margin.
152                         return beg.boundary();
153                 else
154                         return true;
155         }
156         return false;
157 }
158
159
160 void Row::setSelectionAndMargins(DocIterator const & beg,
161                 DocIterator const & end) const
162 {
163         setSelection(beg.pos(), end.pos());
164
165         if (selection()) {
166                 end_margin_sel = isMarginSelected(false, beg, end);
167                 begin_margin_sel = isMarginSelected(true, beg, end);
168         }
169 }
170
171
172 void Row::setSelection(pos_type beg, pos_type end) const
173 {
174         if (pos_ >= beg && pos_ <= end)
175                 sel_beg = pos_;
176         else if (beg > pos_ && beg <= end_)
177                 sel_beg = beg;
178         else
179                 sel_beg = -1;
180
181         if (end_ >= beg && end_ <= end)
182                 sel_end = end_;
183         else if (end < end_ && end >= pos_)
184                 sel_end = end;
185         else
186                 sel_end = -1;
187 }
188
189
190 bool Row::selection() const
191 {
192         return sel_beg != -1 && sel_end != -1;
193 }
194
195
196 ostream & operator<<(ostream & os, Row::Element const & e)
197 {
198         if (e.font.isVisibleRightToLeft())
199                 os << e.endpos << "<<" << e.pos << " ";
200         else
201                 os << e.pos << ">>" << e.endpos << " ";
202
203         switch (e.type) {
204         case Row::STRING:
205                 os << "STRING: `" << to_utf8(e.str) << "' " << e.dim.wid;
206                 break;
207         case Row::VIRTUAL:
208                 os << "VIRTUAL: `" << to_utf8(e.str) << "'";
209                 break;
210         case Row::INSET:
211                 os << "INSET: " << to_utf8(e.inset->layoutName());
212                 break;
213         case Row::SEPARATOR:
214                 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
215                 break;
216         case Row::SPACE:
217                 os << "SPACE: " << e.dim.wid;
218                 break;
219         }
220         return os;
221 }
222
223
224 ostream & operator<<(ostream & os, Row const & row)
225 {
226         os << " pos: " << row.pos_ << " end: " << row.end_
227            << " x: " << row.x
228            << " width: " << row.dim_.wid
229            << " ascent: " << row.dim_.asc
230            << " descent: " << row.dim_.des
231            << " separator: " << row.separator
232            << " label_hfill : " << row.label_hfill << "\n";
233         Row::Elements::const_iterator it = row.elements_.begin();
234         for ( ; it != row.elements_.end() ; ++it) {
235                 os << "** " << *it << endl;
236         }
237         return os;
238 }
239
240
241 bool Row::sameString(Font const & f, Change const & ch) const
242 {
243         if (elements_.empty())
244                 return false;
245         Element const & elt = elements_.back();
246         return elt.type == STRING && !elt.final
247                    && elt.font == f && elt.change == ch;
248 }
249
250
251 void Row::finalizeLast()
252 {
253         if (elements_.empty())
254                 return;
255         Element & elt = elements_.back();
256         if (elt.final)
257                 return;
258         elt.final = true;
259
260         if (elt.type == STRING) {
261                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
262                 dim_.wid += elt.dim.wid;
263         }
264 }
265
266
267 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
268               Font const & f, Change const & ch)
269 {
270         finalizeLast();
271         Element e(INSET, pos, f, ch);
272         e.inset = ins;
273         e.dim = dim;
274         elements_.push_back(e);
275         dim_.wid += dim.wid;
276 }
277
278
279 void Row::add(pos_type const pos, char_type const c,
280               Font const & f, Change const & ch)
281 {
282         if (!sameString(f, ch)) {
283                 finalizeLast();
284                 Element e(STRING, pos, f, ch);
285                 elements_.push_back(e);
286         }
287         //lyxerr << "FONT " <<back().font.language() << endl;
288         back().str += c;
289         back().endpos = pos + 1;
290 }
291
292
293 void Row::addVirtual(pos_type const pos, docstring const & s,
294                      Font const & f, Change const & ch)
295 {
296         finalizeLast();
297         Element e(VIRTUAL, pos, f, ch);
298         e.str = s;
299         // A completion has no size
300         e.endpos = pos;
301         elements_.push_back(e);
302         finalizeLast();
303 }
304
305
306 void Row::addSeparator(pos_type const pos, char_type const c,
307                        Font const & f, Change const & ch)
308 {
309         finalizeLast();
310         Element e(SEPARATOR, pos, f, ch);
311         e.str += c;
312         e.dim.wid = theFontMetrics(f).width(c);
313         elements_.push_back(e);
314         dim_.wid += e.dim.wid;
315 }
316
317
318 void Row::addSpace(pos_type const pos, int const width,
319                    Font const & f, Change const & ch)
320 {
321         finalizeLast();
322         Element e(SPACE, pos, f, ch);
323         e.dim.wid = width;
324         elements_.push_back(e);
325         dim_.wid += e.dim.wid;
326 }
327
328
329 void Row::pop_back()
330 {
331         dim_.wid -= elements_.back().dim.wid;
332         elements_.pop_back();
333 }
334
335
336 void Row::shorten_if_needed(pos_type const keep, int const w)
337 {
338         if (empty() || width() < w)
339                 return;
340         int i = elements_.size();
341         int new_end = end_;
342         int new_wid = dim_.wid;
343         if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
344                 --i;
345                 new_end = elements_[i].pos;
346                 new_wid -= elements_[i].dim.wid;
347         }
348
349         while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
350                 --i;
351                 new_end = elements_[i].pos;
352                 new_wid -= elements_[i].dim.wid;
353         }
354         if (i == 0) {
355                 if (elements_.size() != 1) {
356                         LYXERR0("Row is too large but has more than one element. " << *this);
357                         return;
358                 }
359 #if 1
360                 return;
361 #else
362                 // does not work yet
363                 if (back().type != STRING)
364                         return;
365                 double xstr = w - x;
366                 pos_type new_pos = back().x2pos(xstr, true);
367                 back().str = back().str.substr(0, new_pos);
368                 back().endpos = new_pos;
369                 end_ = new_pos;
370                 dim_.wid = x + xstr;
371 #endif
372         }
373         end_ = new_end;
374         dim_.wid = new_wid;
375         elements_.erase(elements_.begin() + i, elements_.end());
376 }
377
378
379 void Row::reverseRTL()
380 {
381         pos_type i = 0;
382         pos_type const end = elements_.size();
383         while (i < end) {
384                 // skip LtR elements
385                 while (i < end && !elements_[i].font.isRightToLeft())
386                         ++i;
387                 if (i >= end)
388                         break;
389
390                 // look for a RTL sequence
391                 pos_type j = i;
392                 while (j < end && elements_[j].font.isRightToLeft())
393                         ++j;
394                 reverse(elements_.begin() + i, elements_.begin() + j);
395                 i = j;
396         }
397 }
398
399 } // namespace lyx