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