]> git.lyx.org Git - features.git/blob - src/Row.cpp
Convert cursorX() and computeRowMetrics() to the new scheme
[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 Row::Row()
58         : separator(0), label_hfill(0), x(0),
59         sel_beg(-1), sel_end(-1),
60         begin_margin_sel(false), end_margin_sel(false),
61         changed_(false), crc_(0), pos_(0), end_(0)
62 {}
63
64
65 void Row::setCrc(size_type crc) const
66 {
67         changed_ = crc != crc_;
68         crc_ = crc;
69 }
70
71
72 void Row::pos(pos_type p)
73 {
74         pos_ = p;
75 }
76
77
78 void Row::endpos(pos_type p)
79 {
80         end_ = p;
81 }
82
83
84 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
85                 DocIterator const & end) const
86 {
87         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
88         pos_type const margin_pos = left_margin ? pos_ : end_;
89
90         // Is the chosen margin selected ?
91         if (sel_pos == margin_pos) {
92                 if (beg.pos() == end.pos())
93                         // This is a special case in which the space between after
94                         // pos i-1 and before pos i is selected, i.e. the margins
95                         // (see DocIterator::boundary_).
96                         return beg.boundary() && !end.boundary();
97                 else if (end.pos() == margin_pos)
98                         // If the selection ends around the margin, it is only
99                         // drawn if the cursor is after the margin.
100                         return !end.boundary();
101                 else if (beg.pos() == margin_pos)
102                         // If the selection begins around the margin, it is
103                         // only drawn if the cursor is before the margin.
104                         return beg.boundary();
105                 else
106                         return true;
107         }
108         return false;
109 }
110
111
112 void Row::setSelectionAndMargins(DocIterator const & beg,
113                 DocIterator const & end) const
114 {
115         setSelection(beg.pos(), end.pos());
116
117         if (selection()) {
118                 end_margin_sel = isMarginSelected(false, beg, end);
119                 begin_margin_sel = isMarginSelected(true, beg, end);
120         }
121 }
122
123
124 void Row::setSelection(pos_type beg, pos_type end) const
125 {
126         if (pos_ >= beg && pos_ <= end)
127                 sel_beg = pos_;
128         else if (beg > pos_ && beg <= end_)
129                 sel_beg = beg;
130         else
131                 sel_beg = -1;
132
133         if (end_ >= beg && end_ <= end)
134                 sel_end = end_;
135         else if (end < end_ && end >= pos_)
136                 sel_end = end;
137         else
138                 sel_end = -1;
139 }
140
141
142 bool Row::selection() const
143 {
144         return sel_beg != -1 && sel_end != -1;
145 }
146
147
148 ostream & operator<<(ostream & os, Row::Element const & e)
149 {
150         if (e.font.isVisibleRightToLeft())
151                 os << e.endpos << "<<" << e.pos << " ";
152         else
153                 os << e.pos << ">>" << e.endpos << " ";
154
155         switch (e.type) {
156         case Row::Element::STRING:
157                 os << "STRING: `" << to_utf8(e.str) << "'";
158                 break;
159         case Row::Element::COMPLETION:
160                 os << "COMPLETION: `" << to_utf8(e.str) << "'";
161                 break;
162         case Row::Element::INSET:
163                 os << "INSET: " << to_utf8(e.inset->layoutName());
164                 break;
165         case Row::Element::SEPARATOR:
166                 os << "SEPARATOR: " << e.dim.wid << "+" << e.extra;
167                 break;
168         case Row::Element::SPACE:
169                 os << "SPACE: " << e.dim.wid;
170                 break;
171         }
172         return os;
173 }
174
175
176 ostream & operator<<(ostream & os, Row const & row)
177 {
178         os << " pos: " << row.pos_ << " end: " << row.end_
179            << " width: " << row.dim_.wid
180            << " ascent: " << row.dim_.asc
181            << " descent: " << row.dim_.des
182            << " separator: " << row.separator
183            << " label_hfill : " << row.label_hfill << "\n";
184         Row::Elements::const_iterator it = row.elements_.begin();
185         for ( ; it != row.elements_.end() ; ++it) {
186                 os << "** " << *it << endl;
187         }
188         return os;
189 }
190
191
192 bool Row::sameString(Font const & f, Change const & ch) const
193 {
194         if (elements_.empty())
195                 return false;
196         Element const & elt = elements_.back();
197         return elt.type == Element::STRING && !elt.final
198                    && elt.font == f && elt.change == ch;
199 }
200
201
202 void Row::finalizeLast()
203 {
204         if (elements_.empty())
205                 return;
206         Element & elt = elements_.back();
207         if (elt.final)
208                 return;
209         elt.final = true;
210
211         if (elt.type == Element::STRING) {
212                 elt.dim.wid = theFontMetrics(elt.font).width(elt.str);
213                 dim_.wid += elt.dim.wid;
214         }
215 }
216
217
218 void Row::add(pos_type const pos, Inset const * ins, Dimension const & dim,
219               Font const & f, Change const & ch)
220 {
221         finalizeLast();
222         Element e(Element::INSET, pos, f, ch);
223         e.inset = ins;
224         e.dim = dim;
225         elements_.push_back(e);
226         dim_.wid += dim.wid;
227 }
228
229
230 void Row::add(pos_type const pos, char_type const c,
231               Font const & f, Change const & ch)
232 {
233         if (!sameString(f, ch)) {
234                 finalizeLast();
235                 Element e(Element::STRING, pos, f, ch);
236                 elements_.push_back(e);
237         }
238         //lyxerr << "FONT " <<back().font.language() << endl;
239         back().str += c;
240         back().endpos = pos + 1;
241 }
242
243
244 void Row::addCompletion(pos_type const pos, docstring const & s,
245                         Font const & f, Change const & ch)
246 {
247         finalizeLast();
248         Element e(Element::COMPLETION, pos, f, ch);
249         e.str = s;
250         // A completion has no size
251         e.endpos = pos;
252         elements_.push_back(e);
253         finalizeLast();
254 }
255
256
257 void Row::addSeparator(pos_type const pos, char_type const c,
258                        Font const & f, Change const & ch)
259 {
260         finalizeLast();
261         Element e(Element::SEPARATOR, pos, f, ch);
262         e.str += c;
263         e.dim.wid = theFontMetrics(f).width(c);
264         elements_.push_back(e);
265         dim_.wid += e.dim.wid;
266 }
267
268
269 void Row::addSpace(pos_type const pos, int const width,
270                    Font const & f, Change const & ch)
271 {
272         finalizeLast();
273         Element e(Element::SPACE, pos, f, ch);
274         e.dim.wid = width;
275         elements_.push_back(e);
276         dim_.wid += e.dim.wid;
277 }
278
279
280 void Row::pop_back()
281 {
282         dim_.wid -= elements_.back().dim.wid;
283         elements_.pop_back();
284 }
285
286
287 void Row::separate_back(pos_type const keep)
288 {
289         if (empty())
290                 return;
291         int i = elements_.size();
292         int new_end = end_;
293         int new_wid = dim_.wid;
294         if (i > 0 && elements_[i - 1].isSeparator() && new_end > keep) {
295                 --i;
296                 new_end = elements_[i].pos;
297                 new_wid -= elements_[i].dim.wid;
298         }
299
300         while (i > 0 && !elements_[i - 1].isSeparator() && new_end > keep) {
301                 --i;
302                 new_end = elements_[i].pos;
303                 new_wid -= elements_[i].dim.wid;
304         }
305         if (i == 0)
306                 return;
307         end_ = new_end;
308         dim_.wid = new_wid;
309         elements_.erase(elements_.begin() + i, elements_.end());
310 }
311
312
313 void Row::reverseRtL()
314 {
315         pos_type i = 0;
316         pos_type const end = elements_.size();
317         while (i < end) {
318                 // skip LtR elements
319                 while (i < end && !elements_[i].font.isRightToLeft())
320                         ++i;
321                 if (i >= end)
322                         break;
323
324                 // look for a RtL sequence
325                 pos_type j = i;
326                 while (j < end && elements_[j].font.isRightToLeft())
327                         ++j;
328                 reverse(elements_.begin() + i, elements_.begin() + j);
329                 i = j;
330         }
331 }
332
333 } // namespace lyx