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