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