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