]> git.lyx.org Git - lyx.git/blob - src/Row.cpp
b910edb1c7136ec732ab5d0fece6a29dd4ba9a79
[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 unknown
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
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 "support/debug.h"
24
25
26 namespace lyx {
27
28
29 Row::Row()
30         : separator(0), label_hfill(0), x(0),
31         sel_beg(-1), sel_end(-1),
32         left_margin_sel(false), right_margin_sel(false), 
33         changed_(false), crc_(0), pos_(0), end_(0)
34 {}
35
36
37 Row::Row(pos_type pos)
38         : separator(0), label_hfill(0), x(0),
39         sel_beg(-1), sel_end(-1),
40         left_margin_sel(false), right_margin_sel(false), 
41         changed_(false), crc_(0), pos_(pos), end_(0)
42 {}
43
44
45 void Row::setCrc(size_type crc) const
46 {
47         changed_ = crc != crc_;
48         crc_ = crc;
49 }
50
51
52 void Row::setDimension(Dimension const & dim)
53 {
54         dim_ = dim;
55 }
56
57
58 void Row::pos(pos_type p)
59 {
60         pos_ = p;
61 }
62
63
64 void Row::endpos(pos_type p)
65 {
66         end_ = p;
67 }
68
69
70 bool Row::isMarginSelected(bool left_margin, DocIterator const & beg,
71                 DocIterator const & end) const
72 {
73         pos_type const sel_pos = left_margin ? sel_beg : sel_end;
74         pos_type const margin_pos = left_margin ? pos_ : end_;
75
76         // Is the chosen margin selected ?
77         if (sel_pos == margin_pos) {
78                 if (beg.pos() == end.pos())
79                         // This is a special case in which the space between after 
80                         // pos i-1 and before pos i is selected, i.e. the margins
81                         // (see DocIterator::boundary_).
82                         return beg.boundary() && !end.boundary();
83                 else if (end.pos() == margin_pos)
84                         // If the selection ends around the margin, it is only
85                         // drawn if the cursor is after the margin.
86                         return !end.boundary();
87                 else if (beg.pos() == margin_pos)
88                         // If the selection begins around the margin, it is 
89                         // only drawn if the cursor is before the margin.
90                         return beg.boundary();
91                 else 
92                         return true;
93         }
94         return false;
95 }
96
97
98 void Row::setSelectionAndMargins(DocIterator const & beg, 
99                 DocIterator const & end) const
100 {
101         setSelection(beg.pos(), end.pos());
102         
103         if (selection()) {
104                 right_margin_sel = isMarginSelected(false, beg, end);
105                 left_margin_sel = isMarginSelected(true, beg, end);
106         }
107 }
108
109
110 void Row::setSelection(pos_type beg, pos_type end) const
111 {
112         if (pos_ >= beg && pos_ <= end)
113                 sel_beg = pos_;
114         else if (beg > pos_ && beg <= end_)
115                 sel_beg = beg;
116         else
117                 sel_beg = -1;
118
119         if (end_ >= beg && end_ <= end)
120                 sel_end = end_;
121         else if (end < end_ && end >= pos_)
122                 sel_end = end;
123         else
124                 sel_end = -1;
125 }
126
127
128 bool Row::selection() const
129 {
130         return sel_beg != -1 && sel_end != -1;
131 }
132
133
134 void Row::dump(char const * s) const
135 {
136         LYXERR0(s << " pos: " << pos_ << " end: " << end_
137                 << " width: " << dim_.wid
138                 << " ascent: " << dim_.asc
139                 << " descent: " << dim_.des);
140 }
141
142
143 } // namespace lyx