]> git.lyx.org Git - lyx.git/blob - src/lyxgluelength.C
fix to #241 and #300 from John
[lyx.git] / src / lyxgluelength.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "lyxgluelength.h"
18 #include "lengthcommon.h"
19
20 #include "Lsstream.h"
21
22
23 LyXGlueLength::LyXGlueLength(LyXLength const & len)
24         : len_(len)
25 {}
26
27
28 LyXGlueLength::LyXGlueLength(LyXLength const & len, LyXLength const & plus,
29                 LyXLength const & minus)
30         : len_(len), plus_(plus), minus_(minus)
31 {}
32
33
34 LyXGlueLength::LyXGlueLength(string const & data)
35 {
36         isValidGlueLength(data, this);
37 }
38
39
40 string const LyXGlueLength::asString() const
41 {
42         ostringstream buffer;
43
44         if (!plus_.zero())
45                 if (!minus_.zero())
46                         if (len_.unit() == plus_.unit() && len_.unit() == minus_.unit())
47                                 if (plus_.value() == minus_.value())
48                                         buffer << len_.value() << "+-"
49                                                << plus_.value() << unit_name[len_.unit()];
50                                 else
51                                         buffer << len_.value()
52                                                << '+' << plus_.value()
53                                                << '-' << minus_.value()
54                                                << unit_name[len_.unit()];
55                         else
56                                 if (plus_.unit() == minus_.unit()
57                                     && plus_.value() == minus_.value())
58                                         buffer << len_.value() << unit_name[len_.unit()]
59                                                << "+-" << plus_.value()
60                                                << unit_name[plus_.unit()];
61
62                                 else
63                                         buffer << len_.value() << unit_name[len_.unit()]
64                                                << '+' << plus_.value()
65                                                << unit_name[plus_.unit()]
66                                                << '-' << minus_.value()
67                                                << unit_name[minus_.unit()];
68                 else
69                         if (len_.unit() == plus_.unit())
70                                 buffer << len_.value() << '+' << plus_.value()
71                                        << unit_name[len_.unit()];
72                         else
73                                 buffer << len_.value() << unit_name[len_.unit()]
74                                        << '+' << plus_.value()
75                                        << unit_name[plus_.unit()];
76
77         else
78                 if (!minus_.zero())
79                         if (len_.unit() == minus_.unit())
80                                 buffer << len_.value() << '-' << minus_.value()
81                                        << unit_name[len_.unit()];
82
83                         else
84                                 buffer << len_.value() << unit_name[len_.unit()]
85                                        << '-' << minus_.value()
86                                        << unit_name[minus_.unit()];
87                 else
88                         buffer << len_.value() << unit_name[len_.unit()];
89
90         return buffer.str().c_str();
91 }
92
93
94 string const LyXGlueLength::asLatexString() const
95 {
96         ostringstream buffer;
97
98         if (!plus_.zero())
99                 if (!minus_.zero())
100                         buffer << len_.value() << unit_name[len_.unit()]
101                                << " plus "
102                                << plus_.value() << unit_name[plus_.unit()]
103                                << " minus "
104                                << minus_.value() << unit_name[minus_.unit()];
105                 else
106                         buffer << len_.value() << unit_name[len_.unit()]
107                                << " plus "
108                                << plus_.value() << unit_name[plus_.unit()];
109         else
110                 if (!minus_.zero())
111                         buffer << len_.value() << unit_name[len_.unit()]
112                                << " minus "
113                                << minus_.value() << unit_name[minus_.unit()];
114                 else
115                         buffer << len_.value() << unit_name[len_.unit()];
116
117         return buffer.str().c_str();
118 }
119
120
121 LyXLength const & LyXGlueLength::len() const
122 {
123         return len_;
124 }
125
126
127 LyXLength const & LyXGlueLength::plus() const
128 {
129         return plus_;
130 }
131
132
133 LyXLength const & LyXGlueLength::minus() const
134 {
135         return minus_;
136 }
137
138
139 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
140 {
141         return l1.len() == l2.len()
142                  && l1.plus() == l2.plus()
143                  && l1.minus() == l2.minus();
144 }
145
146
147 bool operator!=(LyXGlueLength const & l1, LyXGlueLength const & l2)
148 {
149         return !(l1 == l2);
150 }