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