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