]> git.lyx.org Git - lyx.git/blob - src/lyxgluelength.C
The Gtk patch.
[lyx.git] / src / lyxgluelength.C
1 /**
2  * \file lyxgluelength.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 Matthias Ettrich
8  * \author John Levon
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "lyxgluelength.h"
16 #include "lengthcommon.h"
17
18 #include "Lsstream.h"
19
20
21 LyXGlueLength::LyXGlueLength(LyXLength const & len)
22         : len_(len)
23 {}
24
25
26 LyXGlueLength::LyXGlueLength(LyXLength const & len, LyXLength const & plus,
27                 LyXLength const & minus)
28         : len_(len), plus_(plus), minus_(minus)
29 {}
30
31
32 LyXGlueLength::LyXGlueLength(string const & data)
33 {
34         isValidGlueLength(data, this);
35 }
36
37
38 string const LyXGlueLength::asString() const
39 {
40         ostringstream buffer;
41
42         buffer << len_.value();
43
44         if (plus_.zero() && minus_.zero()) {
45                 buffer << unit_name[len_.unit()];
46                 return STRCONV(buffer.str());
47         }
48
49         // just len and plus
50         if (minus_.zero()) {
51                 if (len_.unit() != plus_.unit())
52                         buffer << unit_name[len_.unit()];
53                 buffer << '+' << plus_.value();
54                 buffer << unit_name[plus_.unit()];
55                 return STRCONV(buffer.str());
56         }
57
58         // just len and minus
59         if (plus_.zero()) {
60                 if (len_.unit() != minus_.unit())
61                         buffer << unit_name[len_.unit()];
62                 buffer << '-' << minus_.value();
63                 buffer << unit_name[minus_.unit()];
64                 return STRCONV(buffer.str());
65         }
66
67         // ok, len, plus AND minus
68
69         // len+-
70         if (minus_ == plus_) {
71                 if (len_.unit() != minus_.unit())
72                         buffer << unit_name[len_.unit()];
73                 buffer << "+-" << minus_.value();
74                 buffer << unit_name[minus_.unit()];
75                 return STRCONV(buffer.str());
76         }
77
78         // this is so rare a case, why bother minimising units ?
79
80         buffer << unit_name[len_.unit()];
81         buffer << '+' << plus_.value() << unit_name[plus_.unit()];
82         buffer << '-' << minus_.value() << unit_name[minus_.unit()];
83
84         return STRCONV(buffer.str());
85 }
86
87
88 string const LyXGlueLength::asLatexString() const
89 {
90         ostringstream buffer;
91
92         buffer << len_.value() << unit_name[len_.unit()];
93
94         if (!plus_.zero())
95                 buffer << " plus " << plus_.value() << unit_name[plus_.unit()];
96         if (!minus_.zero())
97                 buffer << " minus " << minus_.value() << unit_name[minus_.unit()];
98         return STRCONV(buffer.str());
99 }
100
101
102 LyXLength const & LyXGlueLength::len() const
103 {
104         return len_;
105 }
106
107
108 LyXLength const & LyXGlueLength::plus() const
109 {
110         return plus_;
111 }
112
113
114 LyXLength const & LyXGlueLength::minus() const
115 {
116         return minus_;
117 }
118
119
120 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
121 {
122         return l1.len() == l2.len()
123                  && l1.plus() == l2.plus()
124                  && l1.minus() == l2.minus();
125 }
126
127
128 bool operator!=(LyXGlueLength const & l1, LyXGlueLength const & l2)
129 {
130         return !(l1 == l2);
131 }