]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
Reintroduce the code related to InsetEnvSeparator
[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         int w = 0;
49         //handle first the two bounds of the element
50         if (i == pos || type != STRING)
51                 w = rtl ? width() : 0;
52         else if (i == endpos)
53                 w = rtl ? 0 : width();
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 = 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 -= w;
117         else
118                 dim.wid = 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 << "\n";
254         double x = row.x;
255         Row::Elements::const_iterator it = row.elements_.begin();
256         for ( ; it != row.elements_.end() ; ++it) {
257                 os << "x=" << x << " => " << *it << endl;
258                 x += it->width();
259         }
260         return os;
261 }
262
263
264 bool Row::sameString(Font const & f, Change const & ch) const
265 {
266         if (elements_.empty())
267                 return false;
268         Element const & elt = elements_.back();
269         return elt.type == STRING && !elt.final
270                    && elt.font == f && elt.change == ch;
271 }
272
273
274 void Row::finalizeLast()
275 {
276         if (elements_.empty())
277                 return;
278         Element & elt = elements_.back();
279         if (elt.final)
280                 return;
281         elt.final = true;
282
283         if (elt.type == STRING) {
284                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
285                 dim_.wid += elt.dim.wid;
286         }
287 }
288
289
290 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
291               Font const & f, Change const & ch)
292 {
293         finalizeLast();
294         Element e(INSET, pos, f, ch);
295         e.inset = ins;
296         e.dim = dim;
297         elements_.push_back(e);
298         dim_.wid += dim.wid;
299 }
300
301
302 void Row::add(pos_type const pos, char_type const c,
303               Font const & f, Change const & ch)
304 {
305         if (!sameString(f, ch)) {
306                 finalizeLast();
307                 Element e(STRING, pos, f, ch);
308                 elements_.push_back(e);
309         }
310         back().str += c;
311         back().endpos = pos + 1;
312 }
313
314
315 void Row::addVirtual(pos_type const pos, docstring const & s,
316                      Font const & f, Change const & ch)
317 {
318         finalizeLast();
319         Element e(VIRTUAL, pos, f, ch);
320         e.str = s;
321         e.dim.wid = theFontMetrics(f).width(s);
322         dim_.wid += e.dim.wid;
323         e.endpos = pos;
324         elements_.push_back(e);
325         finalizeLast();
326 }
327
328
329 void Row::addSeparator(pos_type const pos, char_type const c,
330                        Font const & f, Change const & ch)
331 {
332         finalizeLast();
333         Element e(SEPARATOR, pos, f, ch);
334         e.str += c;
335         e.dim.wid = theFontMetrics(f).width(c);
336         elements_.push_back(e);
337         dim_.wid += e.dim.wid;
338 }
339
340
341 void Row::addSpace(pos_type const pos, int const width,
342                    Font const & f, Change const & ch)
343 {
344         finalizeLast();
345         Element e(SPACE, pos, f, ch);
346         e.dim.wid = width;
347         elements_.push_back(e);
348         dim_.wid += e.dim.wid;
349 }
350
351
352 void Row::pop_back()
353 {
354         dim_.wid -= elements_.back().dim.wid;
355         elements_.pop_back();
356 }
357
358
359 void Row::shortenIfNeeded(pos_type const keep, int const w)
360 {
361         if (empty() || width() <= w)
362                 return;
363
364         /** First, we try to remove elements one by one from the end
365          * until a separator is found. cit points to the first element
366          * we want to remove from the row.
367          */
368         Elements::iterator const beg = elements_.begin();
369         Elements::iterator const end = elements_.end();
370         Elements::iterator cit = end;
371         Elements::iterator first_below = end;
372         int new_end = end_;
373         int new_wid = dim_.wid;
374         // if the row ends with a separator, skip it.
375         if (cit != beg && boost::prior(cit)->type == SEPARATOR && new_end > keep) {
376                 --cit;
377                 new_end = cit->pos;
378                 new_wid -= cit->dim.wid;
379         }
380
381         // Search for a separator where the row can be broken.
382         while (cit != beg && boost::prior(cit)->type != SEPARATOR && new_end > keep) {
383                 --cit;
384                 new_end = cit->pos;
385                 new_wid -= cit->dim.wid;
386                 if (new_wid < w && first_below == end)
387                         first_below = cit;
388         }
389
390         if (cit != beg) {
391                 // We have found a suitable separator. This is the
392                 // common case.
393                 end_ = new_end;
394                 dim_.wid = new_wid;
395                 elements_.erase(cit, end);
396                 return;
397         }
398
399         /* If we are here, it means that we have not found a separator
400          * to shorten the row. There is one case where we can do
401          * something: when we have one big string, maybe with some
402          * other things after it.
403          */
404         double max_w = w - x;
405         if (first_below->breakAt(max_w)) {
406                 end_ = first_below->endpos;
407                 dim_.wid = x + first_below->width();
408                 // If there are other elements, they should be removed.
409                 elements_.erase(boost::next(first_below), end);
410         } else if (first_below->pos > pos_) {
411                 end_ = first_below->pos;
412                 dim_.wid = new_wid;
413                 // Remove all elements from first_below.
414                 elements_.erase(first_below, end);
415         }
416 }
417
418
419 void Row::reverseRTL(bool const rtl_par)
420 {
421         pos_type i = 0;
422         pos_type const end = elements_.size();
423         while (i < end) {
424                 // gather a sequence of elements with the same direction
425                 bool const rtl = elements_[i].font.isVisibleRightToLeft();
426                 pos_type j = i;
427                 while (j < end && elements_[j].font.isVisibleRightToLeft() == rtl)
428                         ++j;
429                 // if the direction is not the same as the paragraph
430                 // direction, the sequence has to be reverted.
431                 if (rtl != rtl_par)
432                         reverse(elements_.begin() + i, elements_.begin() + j);
433                 i = j;
434         }
435         // If the paragraph itself is RTL, reverse everything
436         if (rtl_par)
437                 reverse(elements_.begin(), elements_.end());
438 }
439
440 } // namespace lyx