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