]> git.lyx.org Git - lyx.git/blob - src/lyxgluelength.C
update build instructions (Qt 4.2.2 etc.)
[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 <sstream>
19
20
21 namespace lyx {
22
23 using std::ostringstream;
24 using std::string;
25
26
27 LyXGlueLength::LyXGlueLength(LyXLength const & len)
28         : len_(len)
29 {}
30
31
32 LyXGlueLength::LyXGlueLength(LyXLength const & len, LyXLength const & plus,
33                 LyXLength const & minus)
34         : len_(len), plus_(plus), minus_(minus)
35 {}
36
37
38 LyXGlueLength::LyXGlueLength(string const & data)
39 {
40         isValidGlueLength(data, this);
41 }
42
43
44 string const LyXGlueLength::asString() const
45 {
46         ostringstream buffer;
47
48         buffer << len_.value();
49
50         if (plus_.zero() && minus_.zero()) {
51                 buffer << unit_name[len_.unit()];
52                 return buffer.str();
53         }
54
55         // just len and plus
56         if (minus_.zero()) {
57                 if (len_.unit() != plus_.unit())
58                         buffer << unit_name[len_.unit()];
59                 buffer << '+' << plus_.value();
60                 buffer << unit_name[plus_.unit()];
61                 return buffer.str();
62         }
63
64         // just len and minus
65         if (plus_.zero()) {
66                 if (len_.unit() != minus_.unit())
67                         buffer << unit_name[len_.unit()];
68                 buffer << '-' << minus_.value();
69                 buffer << unit_name[minus_.unit()];
70                 return buffer.str();
71         }
72
73         // ok, len, plus AND minus
74
75         // len+-
76         if (minus_ == plus_) {
77                 if (len_.unit() != minus_.unit())
78                         buffer << unit_name[len_.unit()];
79                 buffer << "+-" << minus_.value();
80                 buffer << unit_name[minus_.unit()];
81                 return buffer.str();
82         }
83
84         // this is so rare a case, why bother minimising units ?
85
86         buffer << unit_name[len_.unit()];
87         buffer << '+' << plus_.value() << unit_name[plus_.unit()];
88         buffer << '-' << minus_.value() << unit_name[minus_.unit()];
89
90         return buffer.str();
91 }
92
93
94 string const LyXGlueLength::asLatexString() const
95 {
96         ostringstream buffer;
97
98         buffer << len_.value() << unit_name[len_.unit()];
99
100         if (!plus_.zero())
101                 buffer << " plus " << plus_.value() << unit_name[plus_.unit()];
102         if (!minus_.zero())
103                 buffer << " minus " << minus_.value() << unit_name[minus_.unit()];
104         return buffer.str();
105 }
106
107
108 LyXLength const & LyXGlueLength::len() const
109 {
110         return len_;
111 }
112
113
114 LyXLength const & LyXGlueLength::plus() const
115 {
116         return plus_;
117 }
118
119
120 LyXLength const & LyXGlueLength::minus() const
121 {
122         return minus_;
123 }
124
125
126 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
127 {
128         return l1.len() == l2.len()
129                  && l1.plus() == l2.plus()
130                  && l1.minus() == l2.minus();
131 }
132
133
134 bool operator!=(LyXGlueLength const & l1, LyXGlueLength const & l2)
135 {
136         return !(l1 == l2);
137 }
138
139
140 } // namespace lyx