]> git.lyx.org Git - lyx.git/blob - src/lyxrow_funcs.C
Replace ParagraphList.h with ParagraphList_fwd.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 "debug.h"
16 #include "lyxlayout.h"
17 #include "lyxrow.h"
18 #include "lyxtext.h"
19 #include "paragraph.h"
20
21 #include "support/LAssert.h"
22
23 #include <boost/next_prior.hpp>
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 #if 1
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 #else
66 pos_type lastPos(Paragraph const &, RowList::iterator rit)
67 {
68         return rit->end() - 1;
69 }
70 #endif
71
72
73 int numberOfSeparators(Paragraph const & par, RowList::iterator rit)
74 {
75         pos_type const last = lastPos(par, rit);
76         int n = 0;
77         pos_type p = max(rit->pos(), par.beginningOfBody());
78         for ( ; p < last; ++p)
79                 if (par.isSeparator(p))
80                         ++n;
81         return n;
82 }
83
84
85 // This is called _once_ from LyXText and should at least be moved into
86 // an anonymous namespace there. (Lgb)
87 int numberOfHfills(Paragraph const & par, RowList::iterator rit)
88 {
89         pos_type const last = lastPos(par, rit);
90         pos_type first = rit->pos();
91
92         // hfill *DO* count at the beginning of paragraphs!
93         if (first) {
94                 while (first < last && par.isHfill(first))
95                         ++first;
96         }
97
98         first = max(first, par.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 (par.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(Paragraph const & par, RowList::iterator rit)
114 {
115         pos_type last = lastPos(par, rit);
116         pos_type first = rit->pos();
117
118         // hfill *DO* count at the beginning of paragraphs!
119         if (first) {
120                 while (first < last && par.isHfill(first))
121                         ++first;
122         }
123
124         last = min(last, par.beginningOfBody());
125         int n = 0;
126
127         // last, because the end is ignored!
128         for (pos_type p = first; p < last; ++p) {
129                 if (par.isHfill(p))
130                         ++n;
131         }
132         return n;
133 }
134
135
136 bool hfillExpansion(Paragraph const & par, RowList::iterator rit, pos_type pos)
137 {
138         if (!par.isHfill(pos))
139                 return false;
140
141         // at the end of a row it does not count
142         // unless another hfill exists on the line
143         if (pos >= lastPos(par, rit))
144                 for (pos_type i = rit->pos(); i < pos && !par.isHfill(i); ++i)
145                         return false;
146
147         // at the beginning of a row it does not count, if it is not
148         // the first row of a paragaph
149         if (rit->isParStart())
150                 return true;
151
152         // in some labels it does not count
153         if (par.layout()->margintype != MARGIN_MANUAL
154             && pos < par.beginningOfBody())
155                 return false;
156
157         // if there is anything between the first char of the row and
158         // the specified position that is not a newline and not a hfill,
159         // the hfill will count, otherwise not
160         pos_type i = rit->pos();
161         while (i < pos && (par.isNewline(i) || par.isHfill(i)))
162                 ++i;
163
164         return i != pos;
165 }