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