]> git.lyx.org Git - features.git/blob - src/Row.cpp
Fix various selection-related problems
[features.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         LASSERT(i >= pos && i <= endpos, return 0);
40
41         bool const rtl = font.isVisibleRightToLeft();
42
43         int w = 0;
44         //handle first the two bounds of the element
45         if (i == pos)
46                 w = 0;
47         else if (i == endpos)
48                 w = width();
49         else {
50                 LASSERT(type == STRING, return 0);
51                 FontMetrics const & fm = theFontMetrics(font);
52                 // FIXME Avoid caching of metrics there?
53                 w = fm.width(str.substr(0, i - pos));
54         }
55
56         if (rtl)
57                 return width() - w;
58         else
59                 return w;
60 }
61
62
63 pos_type Row::Element::x2pos(double &x, bool const low) const
64 {
65         //lyxerr << "x2pos: x=" << x << " w=" << width() << " " << *this;
66         // If element is rtl, flip x value
67         bool const rtl = font.isVisibleRightToLeft();
68         double x2 = rtl ? (width() - x) : x;
69
70         double last_w = 0;
71         double w = 0;
72         size_t i = 0;
73         switch (type) {
74         case VIRTUAL:
75                 // those elements are actually empty (but they have a width)
76                 break;
77         case STRING: {
78                 FontMetrics const & fm = theFontMetrics(font);
79                 // FIXME: implement dichotomy search?
80                 for ( ; i < str.size() ; ++i) {
81                         last_w = w;
82                         w = fm.width(str.substr(0, i + 1));
83                         if (w > x2)
84                                 break;
85                 }
86                 break;
87         }
88         case SEPARATOR:
89         case INSET:
90         case SPACE:
91                 // those elements contain only one position
92                 w = width();
93         }
94
95         if (type == STRING && i == str.size())
96                 x2 = w;
97         // round to the closest side. The !rtl is here to obtain the
98         // same rounding as with the old code (this is cosmetic and
99         // can be eventually removed).
100         else if (type != VIRTUAL && !low && (x2 - last_w + !rtl > w - x2)) {
101                 x2 = w;
102                 ++i;
103         } else
104                 x2 = last_w;
105
106         // is element is rtl, flip values back
107         x = rtl ? width() - x2 : x2;
108
109         //lyxerr << "=> p=" << pos + i << " x=" << x << endl;
110         return pos + i;
111 }
112
113
114 pos_type Row::Element::left_pos() const
115 {
116         return font.isVisibleRightToLeft() ? endpos : pos;
117 }
118
119
120 pos_type Row::Element::right_pos() const
121 {
122         return font.isVisibleRightToLeft() ? pos : endpos;
123 }
124
125
126
127 Row::Row()
128         : separator(0), label_hfill(0), x(0), right_margin(0),
129           sel_beg(-1), sel_end(-1),
130           begin_margin_sel(false), end_margin_sel(false),
131           changed_(false), crc_(0), pos_(0), end_(0), right_boundary_(false)
132 {}
133
134
135 void Row::setCrc(size_type crc) const
136 {
137         changed_ = crc != crc_;
138         crc_ = crc;
139 }
140
141
142 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
143                 DocIterator const & end) const
144 {
145         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
146         pos_type const margin_pos = left_margin ? pos_ : end_;
147
148         // Is the chosen margin selected ?
149         if (sel_pos == margin_pos) {
150                 if (beg.pos() == end.pos())
151                         // This is a special case in which the space between after
152                         // pos i-1 and before pos i is selected, i.e. the margins
153                         // (see DocIterator::boundary_).
154                         return beg.boundary() && !end.boundary();
155                 else if (end.pos() == margin_pos)
156                         // If the selection ends around the margin, it is only
157                         // drawn if the cursor is after the margin.
158                         return !end.boundary();
159                 else if (beg.pos() == margin_pos)
160                         // If the selection begins around the margin, it is
161                         // only drawn if the cursor is before the margin.
162                         return beg.boundary();
163                 else
164                         return true;
165         }
166         return false;
167 }
168
169
170 void Row::setSelectionAndMargins(DocIterator const & beg,
171                 DocIterator const & end) const
172 {
173         setSelection(beg.pos(), end.pos());
174
175         if (selection()) {
176                 end_margin_sel = isMarginSelected(false, beg, end);
177                 begin_margin_sel = isMarginSelected(true, beg, end);
178         }
179 }
180
181
182 void Row::setSelection(pos_type beg, pos_type end) const
183 {
184         if (pos_ >= beg && pos_ <= end)
185                 sel_beg = pos_;
186         else if (beg > pos_ && beg <= end_)
187                 sel_beg = beg;
188         else
189                 sel_beg = -1;
190
191         if (end_ >= beg && end_ <= end)
192                 sel_end = end_;
193         else if (end < end_ && end >= pos_)
194                 sel_end = end;
195         else
196                 sel_end = -1;
197 }
198
199
200 bool Row::selection() const
201 {
202         return sel_beg != -1 && sel_end != -1;
203 }
204
205
206 ostream & operator<<(ostream & os, Row::Element const & e)
207 {
208         if (e.font.isVisibleRightToLeft())
209                 os << e.endpos << "<<" << e.pos << " ";
210         else
211                 os << e.pos << ">>" << e.endpos << " ";
212
213         switch (e.type) {
214         case Row::STRING:
215                 os << "STRING: `" << to_utf8(e.str) << "', ";
216                 break;
217         case Row::VIRTUAL:
218                 os << "VIRTUAL: `" << to_utf8(e.str) << "', ";
219                 break;
220         case Row::INSET:
221                 os << "INSET: " << to_utf8(e.inset->layoutName()) << ", ";
222                 break;
223         case Row::SEPARATOR:
224                 os << "SEPARATOR: extra=" << e.extra << ", ";
225                 break;
226         case Row::SPACE:
227                 os << "SPACE: ";
228                 break;
229         }
230         os << "width=" << e.width();
231         return os;
232 }
233
234
235 ostream & operator<<(ostream & os, Row const & row)
236 {
237         os << " pos: " << row.pos_ << " end: " << row.end_
238            << " x: " << row.x
239            << " width: " << row.dim_.wid
240            << " right_margin: " << row.right_margin
241            << " ascent: " << row.dim_.asc
242            << " descent: " << row.dim_.des
243            << " separator: " << row.separator
244            << " label_hfill : " << row.label_hfill << "\n";
245         double x = row.x;
246         Row::Elements::const_iterator it = row.elements_.begin();
247         for ( ; it != row.elements_.end() ; ++it) {
248                 os << "x=" << x << " => " << *it << endl;
249                 x += it->width();
250         }
251         return os;
252 }
253
254
255 bool Row::sameString(Font const & f, Change const & ch) const
256 {
257         if (elements_.empty())
258                 return false;
259         Element const & elt = elements_.back();
260         return elt.type == STRING && !elt.final
261                    && elt.font == f && elt.change == ch;
262 }
263
264
265 void Row::finalizeLast()
266 {
267         if (elements_.empty())
268                 return;
269         Element & elt = elements_.back();
270         if (elt.final)
271                 return;
272         elt.final = true;
273
274         if (elt.type == STRING) {
275                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
276                 dim_.wid += elt.dim.wid;
277         }
278 }
279
280
281 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
282               Font const & f, Change const & ch)
283 {
284         finalizeLast();
285         Element e(INSET, pos, f, ch);
286         e.inset = ins;
287         e.dim = dim;
288         elements_.push_back(e);
289         dim_.wid += dim.wid;
290 }
291
292
293 void Row::add(pos_type const pos, char_type const c,
294               Font const & f, Change const & ch)
295 {
296         if (!sameString(f, ch)) {
297                 finalizeLast();
298                 Element e(STRING, pos, f, ch);
299                 elements_.push_back(e);
300         }
301         back().str += c;
302         back().endpos = pos + 1;
303 }
304
305
306 void Row::addVirtual(pos_type const pos, docstring const & s,
307                      Font const & f, Change const & ch)
308 {
309         finalizeLast();
310         Element e(VIRTUAL, pos, f, ch);
311         e.str = s;
312         e.dim.wid = theFontMetrics(f).width(s);
313         dim_.wid += e.dim.wid;
314         e.endpos = pos;
315         elements_.push_back(e);
316         finalizeLast();
317 }
318
319
320 void Row::addSeparator(pos_type const pos, char_type const c,
321                        Font const & f, Change const & ch)
322 {
323         finalizeLast();
324         Element e(SEPARATOR, pos, f, ch);
325         e.str += c;
326         e.dim.wid = theFontMetrics(f).width(c);
327         elements_.push_back(e);
328         dim_.wid += e.dim.wid;
329 }
330
331
332 void Row::addSpace(pos_type const pos, int const width,
333                    Font const & f, Change const & ch)
334 {
335         finalizeLast();
336         Element e(SPACE, pos, f, ch);
337         e.dim.wid = width;
338         elements_.push_back(e);
339         dim_.wid += e.dim.wid;
340 }
341
342
343 void Row::pop_back()
344 {
345         dim_.wid -= elements_.back().dim.wid;
346         elements_.pop_back();
347 }
348
349
350 void Row::shorten_if_needed(pos_type const keep, int const w)
351 {
352         if (empty() || width() < w)
353                 return;
354
355         /** First, we try to remove elements one by one from the end
356          * until a separator is found.
357          */
358         int i = elements_.size();
359         int new_end = end_;
360         int new_wid = dim_.wid;
361         if (i > 0 && elements_[i - 1].type == SEPARATOR && new_end > keep) {
362                 --i;
363                 new_end = elements_[i].pos;
364                 new_wid -= elements_[i].dim.wid;
365         }
366
367         while (i > 0 && elements_[i - 1].type != SEPARATOR && new_end > keep) {
368                 --i;
369                 new_end = elements_[i].pos;
370                 new_wid -= elements_[i].dim.wid;
371         }
372         if (i == 0) {
373                 /* If we are here, it means that we have not found a
374                  * separator to shorten the row. There is one case
375                  * where we can do something: when we have one big
376                  * string, maybe with a paragraph marker after it.
377                  */
378                 Element & front = elements_.front();
379                 if (!(front.type == STRING
380                       && (elements_.size() == 1
381                           || (elements_.size() == 2
382                               && back().type == VIRTUAL))))
383                         return;
384
385                 // If this is a string element, we can try to split it.
386                 if (front.type != STRING)
387                         return;
388                 double xstr = w - x;
389                 // If there is a paragraph marker, it should be taken in account
390                 if (elements_.size() == 2)
391                         xstr -= back().width();
392                 pos_type new_pos = front.x2pos(xstr, true);
393                 front.str = front.str.substr(0, new_pos - pos_);
394                 front.dim.wid = xstr;
395                 front.endpos = new_pos;
396                 end_ = new_pos;
397                 dim_.wid = x + xstr;
398                 // If there is a paragraph marker, it should be removed.
399                 if (elements_.size() == 2)
400                         elements_.pop_back();
401                 return;
402         }
403         end_ = new_end;
404         dim_.wid = new_wid;
405         elements_.erase(elements_.begin() + i, elements_.end());
406 }
407
408
409 void Row::reverseRTL(bool const rtl_par)
410 {
411         pos_type i = 0;
412         pos_type const end = elements_.size();
413         while (i < end) {
414                 // gather a sequence of elements with the same direction
415                 bool const rtl = elements_[i].font.isVisibleRightToLeft();
416                 pos_type j = i;
417                 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
418                         ++j;
419                 // if the direction is not the same as the paragraph
420                 // direction, the sequence has to be reverted.
421                 if (rtl != rtl_par)
422                         reverse(elements_.begin() + i, elements_.begin() + j);
423                 i = j;
424         }
425         // If the paragraph itself is RTL, reverse everything
426         if (rtl_par)
427                 reverse(elements_.begin(), elements_.end());
428 }
429
430 } // namespace lyx