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