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