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