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