]> git.lyx.org Git - lyx.git/blob - src/VSpace.cpp
Update fr.po
[lyx.git] / src / VSpace.cpp
1 /**
2  * \file VSpace.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Matthias Ettrich
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "VSpace.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "BufferView.h"
18 #include "support/gettext.h"
19 #include "Length.h"
20 #include "Text.h"
21 #include "TextMetrics.h" // for defaultRowHeight()
22
23 #include "support/convert.h"
24 #include "support/lstrings.h"
25
26 #include "support/lassert.h"
27
28 using namespace std;
29 using namespace lyx::support;
30
31
32 namespace lyx {
33
34 //
35 //  VSpace class
36 //
37
38 VSpace::VSpace()
39         : kind_(DEFSKIP), len_(), keep_(false)
40 {}
41
42
43 VSpace::VSpace(VSpaceKind k)
44         : kind_(k), len_(), keep_(false)
45 {}
46
47
48 VSpace::VSpace(Length const & l)
49         : kind_(LENGTH), len_(l), keep_(false)
50 {}
51
52
53 VSpace::VSpace(GlueLength const & l)
54         : kind_(LENGTH), len_(l), keep_(false)
55 {}
56
57
58 VSpace::VSpace(string const & data)
59         : kind_(DEFSKIP), len_(), keep_(false)
60 {
61         if (data.empty())
62                 return;
63
64         string input = rtrim(data);
65
66         size_t const length = input.length();
67
68         if (length > 1 && input[length - 1] == '*') {
69                 keep_ = true;
70                 input.erase(length - 1);
71         }
72
73         if (prefixIs(input, "defskip"))
74                 kind_ = DEFSKIP;
75         else if (prefixIs(input, "smallskip"))
76                 kind_ = SMALLSKIP;
77         else if (prefixIs(input, "medskip"))
78                 kind_ = MEDSKIP;
79         else if (prefixIs(input, "bigskip"))
80                 kind_ = BIGSKIP;
81         else if (prefixIs(input, "vfill"))
82                 kind_ = VFILL;
83         else if (isValidGlueLength(input, &len_))
84                 kind_ = LENGTH;
85         else if (isStrDbl(input)) {
86                 // This last one is for reading old .lyx files
87                 // without units in added_space_top/bottom.
88                 // Let unit default to centimeters here.
89                 kind_ = LENGTH;
90                 len_  = GlueLength(Length(convert<double>(input), Length::CM));
91         }
92 }
93
94
95 bool VSpace::operator==(VSpace const & other) const
96 {
97         if (kind_ != other.kind_)
98                 return false;
99
100         if (kind_ != LENGTH)
101                 return this->keep_ == other.keep_;
102
103         if (len_ != other.len_)
104                 return false;
105
106         return keep_ == other.keep_;
107 }
108
109
110 string const VSpace::asLyXCommand() const
111 {
112         string result;
113         switch (kind_) {
114         case DEFSKIP:   result = "defskip";      break;
115         case SMALLSKIP: result = "smallskip";    break;
116         case MEDSKIP:   result = "medskip";      break;
117         case BIGSKIP:   result = "bigskip";      break;
118         case VFILL:     result = "vfill";        break;
119         case LENGTH:    result = len_.asString(); break;
120         }
121         if (keep_)
122                 result += '*';
123         return result;
124 }
125
126
127 string const VSpace::asLatexCommand(BufferParams const & params) const
128 {
129         switch (kind_) {
130         case DEFSKIP:
131                 return params.getDefSkip().asLatexCommand(params);
132
133         case SMALLSKIP:
134                 return keep_ ? "\\vspace*{\\smallskipamount}" : "\\smallskip{}";
135
136         case MEDSKIP:
137                 return keep_ ? "\\vspace*{\\medskipamount}" : "\\medskip{}";
138
139         case BIGSKIP:
140                 return keep_ ? "\\vspace*{\\bigskipamount}" : "\\bigskip{}";
141
142         case VFILL:
143                 return keep_ ? "\\vspace*{\\fill}" : "\\vfill{}";
144
145         case LENGTH:
146                 return keep_ ? "\\vspace*{" + len_.asLatexString() + '}'
147                         : "\\vspace{" + len_.asLatexString() + '}';
148
149         default:
150                 LATTEST(false);
151                 // fall through in release mode
152         }
153         return string();
154 }
155
156
157 docstring const VSpace::asGUIName() const
158 {
159         docstring result;
160         switch (kind_) {
161         case DEFSKIP:
162                 result = _("Default skip");
163                 break;
164         case SMALLSKIP:
165                 result = _("Small skip");
166                 break;
167         case MEDSKIP:
168                 result = _("Medium skip");
169                 break;
170         case BIGSKIP:
171                 result = _("Big skip");
172                 break;
173         case VFILL:
174                 result = _("Vertical fill");
175                 break;
176         case LENGTH:
177                 result = from_ascii(len_.asString());
178                 break;
179         }
180         if (keep_)
181                 result += ", " + _("protected");
182         return result;
183 }
184
185
186 string VSpace::asHTMLLength() const
187 {
188         string result;
189         switch (kind_) {
190                 case DEFSKIP:   result = "2ex"; break;
191                 case SMALLSKIP: result = "1ex"; break;
192                 case MEDSKIP:   result = "3ex"; break;
193                 case BIGSKIP:   result = "5ex"; break;
194                 case LENGTH: {
195                         Length tmp = len_.len();
196                         if (tmp.value() > 0)
197                                 result = tmp.asHTMLString();
198                 }
199                 case VFILL:     break;
200         }
201         return result;
202 }
203
204
205 int VSpace::inPixels(BufferView const & bv) const
206 {
207         // Height of a normal line in pixels (zoom factor considered)
208         int const default_height = defaultRowHeight();
209
210         switch (kind_) {
211
212         case DEFSKIP:
213                 return bv.buffer().params().getDefSkip().inPixels(bv);
214
215         // This is how the skips are normally defined by LaTeX.
216         // But there should be some way to change this per document.
217         case SMALLSKIP:
218                 return int(default_height / 4);
219
220         case MEDSKIP:
221                 return int(default_height / 2);
222
223         case BIGSKIP:
224                 return default_height;
225
226         case VFILL:
227                 // leave space for the vfill symbol
228                 return 3 * default_height;
229
230         case LENGTH:
231                 return bv.inPixels(len_.len());
232
233         default:
234                 LATTEST(false);
235                 // fall through in release mode
236         }
237         return 0;
238 }
239
240
241 } // namespace lyx