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