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