]> git.lyx.org Git - lyx.git/blob - src/lyxrow_funcs.C
some more getPar removals
[lyx.git] / src / lyxrow_funcs.C
1 #include <config.h>
2
3 #include "lyxrow_funcs.h"
4 #include "lyxtext.h"
5 #include "paragraph.h"
6 #include "lyxlayout.h"
7
8 #include <boost/next_prior.hpp>
9 #include <algorithm>
10
11 using lyx::pos_type;
12
13 using std::max;
14 using std::min;
15
16
17 bool isParEnd(LyXText const & lt,
18         ParagraphList::iterator pit, RowList::iterator rit)
19 {
20         return boost::next(rit) == lt.endRow(pit);
21 }
22
23
24 pos_type lastPos(LyXText const & lt,
25         ParagraphList::iterator pit, RowList::iterator rit)
26 {
27         if (pit->empty())
28                 return 0;
29
30         if (isParEnd(lt, pit, rit))
31                 return pit->size() - 1;
32
33         return boost::next(rit)->pos() - 1;
34 }
35
36
37 namespace {
38
39 bool nextRowIsAllInset(
40         ParagraphList::iterator pit, RowList::iterator rit, pos_type last)
41 {
42         if (last + 1 >= pit->size())
43                 return false;
44
45         if (!pit->isInset(last + 1))
46                 return false;
47
48         InsetOld const * i = pit->getInset(last + 1);
49         return i->needFullRow() || i->display();
50 }
51
52 } // anon namespace
53
54
55 pos_type lastPrintablePos(LyXText const & lt,
56         ParagraphList::iterator pit, RowList::iterator rit)
57 {
58         pos_type const last = lastPos(lt, pit, rit);
59
60         // if this row is an end of par, just act like lastPos()
61         if (isParEnd(lt, pit, rit))
62                 return last;
63
64         if (!nextRowIsAllInset(pit, rit, last) && pit->isSeparator(last))
65                 return last - 1;
66
67         return last;
68 }
69
70
71 int numberOfSeparators(LyXText const & lt,
72         ParagraphList::iterator pit, RowList::iterator rit)
73 {
74         pos_type const last = lastPrintablePos(lt, pit, rit);
75         int n = 0;
76         pos_type p = max(rit->pos(), pit->beginningOfBody());
77         for ( ; p < last; ++p)
78                 if (pit->isSeparator(p))
79                         ++n;
80         return n;
81 }
82
83
84 // This is called _once_ from LyXText and should at least be moved into
85 // an anonymous namespace there. (Lgb)
86 int numberOfHfills(LyXText const & lt,
87         ParagraphList::iterator pit, RowList::iterator rit)
88 {
89         pos_type const last = lastPos(lt, pit, rit);
90         pos_type first = rit->pos();
91
92         // hfill *DO* count at the beginning of paragraphs!
93         if (first) {
94                 while (first < last && pit->isHfill(first))
95                         ++first;
96         }
97
98         first = max(first, pit->beginningOfBody());
99
100         int n = 0;
101
102         // last, because the end is ignored!
103         for (pos_type p = first; p < last; ++p) {
104                 if (pit->isHfill(p))
105                         ++n;
106         }
107         return n;
108 }
109
110
111 // This is called _once_ from LyXText and should at least be moved into
112 // an anonymous namespace there. (Lgb)
113 int numberOfLabelHfills(LyXText const & lt,
114         ParagraphList::iterator pit, RowList::iterator rit)
115 {
116         pos_type last = lastPos(lt, pit, rit);
117         pos_type first = rit->pos();
118
119         // hfill *DO* count at the beginning of paragraphs!
120         if (first) {
121                 while (first < last && pit->isHfill(first))
122                         ++first;
123         }
124
125         last = min(last, pit->beginningOfBody());
126         int n = 0;
127
128         // last, because the end is ignored!
129         for (pos_type p = first; p < last; ++p) {
130                 if (pit->isHfill(p))
131                         ++n;
132         }
133         return n;
134 }
135
136
137 bool hfillExpansion(LyXText const & lt,
138         ParagraphList::iterator pit, RowList::iterator rit, pos_type pos)
139 {
140         if (!pit->isHfill(pos))
141                 return false;
142
143         // at the end of a row it does not count
144         // unless another hfill exists on the line
145         if (pos >= lastPos(lt, pit, rit))
146                 for (pos_type i = rit->pos(); i < pos && !pit->isHfill(i); ++i)
147                         return false;
148
149         // at the beginning of a row it does not count, if it is not
150         // the first row of a paragaph
151         if (rit->isParStart())
152                 return true;
153
154         // in some labels it does not count
155         if (pit->layout()->margintype != MARGIN_MANUAL
156             && pos < pit->beginningOfBody())
157                 return false;
158
159         // if there is anything between the first char of the row and
160         // the specified position that is not a newline and not a hfill,
161         // the hfill will count, otherwise not
162         pos_type i = rit->pos();
163         while (i < pos && (pit->isNewline(i) || pit->isHfill(i)))
164                 ++i;
165
166         return i != pos;
167 }