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