]> git.lyx.org Git - lyx.git/blob - src/vspace.h
Fix paragraph spacing
[lyx.git] / src / vspace.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2001 The LyX Team.
9  *
10  * ====================================================== */
11
12 #ifndef VSPACE_H
13 #define VSPACE_H
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "LString.h"
20
21 class BufferParams;
22 class BufferView;
23
24 ///  LyXLength Class
25 class LyXLength {
26 public:
27         /// length units
28         enum UNIT {
29                 /// Scaled point (65536sp = 1pt) TeX's smallest unit.
30                 SP,
31                 /// Point = 1/72.27in = 0.351mm
32                 PT,
33                 /// Big point (72bp = 1in), also PostScript point
34                 BP,
35                 /// Didot point = 1/72 of a French inch, = 0.376mm
36                 DD,
37                 /// Millimeter = 2.845pt
38                 MM,
39                 /// Pica = 12pt = 4.218mm
40                 PC,
41                 /// Cicero = 12dd = 4.531mm
42                 CC,
43                 /// Centimeter = 10mm = 2.371pc
44                 CM,
45                 /// Inch = 25.4mm = 72.27pt = 6.022pc
46                 IN,
47                 /// Height of a small "x" for the current font.
48                 EX,
49                 /// Width of capital "M" in current font.
50                 EM,
51                 /// Math unit (18mu = 1em) for positioning in math mode
52                 MU,
53                 /// Percent of columnwidth both "%" or "%c"
54                 PW,
55                 PE,
56                 /// Percent of pagewidth
57                 PP,
58                 /// Percent of linewidth
59                 PL,
60                 /// no unit
61                 UNIT_NONE
62         };
63
64         ///
65         LyXLength() : val(0), uni(LyXLength::PT) {}
66         ///
67         LyXLength(float v, LyXLength::UNIT u) : val(v), uni(u) {}
68
69         /** "data" must be a decimal number, followed by a unit. */
70         explicit
71         LyXLength(string const & data);
72         
73         ///
74         float value() const         { return val; }
75         ///
76         LyXLength::UNIT unit() const { return uni; }
77
78         /// conversion
79         virtual string const asString() const;
80         ///
81         virtual string const asLatexString() const;
82
83         /** If "data" is valid, the length represented by it is
84           stored into "result", if that is not 0. */
85         friend bool isValidLength(string const & data, 
86                                   LyXLength * result = 0);
87
88 protected:
89         ///
90         float           val;
91         ///
92         LyXLength::UNIT uni;
93 };
94
95 ///
96 inline
97 bool operator==(LyXLength const & l1, LyXLength const & l2)
98 {
99         return l1.value() == l2.value()
100                 && l1.unit() == l2.unit();
101 }
102         
103 ///
104 extern LyXLength::UNIT unitFromString (string const & data);
105 ///
106 extern bool isValidLength(string const & data, LyXLength * result);
107 ///
108 extern const char * stringFromUnit(int unit);
109
110 /// LyXGlueLength class
111 class LyXGlueLength : public LyXLength {
112 public:
113         ///
114         LyXGlueLength(float v,
115                       LyXLength::UNIT u, 
116                       float pv = 0.0,
117                       LyXLength::UNIT pu = LyXLength::UNIT_NONE, 
118                       float mv = 0.0,
119                       LyXLength::UNIT mu = LyXLength::UNIT_NONE) 
120                 : LyXLength (v, u), 
121                   plus_val(pv), minus_val(mv), 
122                   plus_uni(pu), minus_uni(mu) {}
123
124         /** "data" must be a decimal number, followed by a unit, and 
125           optional "glue" indicated by "+" and "-".  You may abbreviate
126           reasonably.  Examples:
127           1.2 cm  //  4mm +2pt  //  2cm -4mm +2mm  //  4+0.1-0.2cm
128           The traditional Latex format is also accepted, like  
129           4cm plus 10pt minus 10pt */
130         explicit
131         LyXGlueLength(string const & data);
132         
133         ///
134         float plusValue() const         { return plus_val; }
135         ///
136         LyXLength::UNIT plusUnit() const { return plus_uni; }
137         ///
138         float minusValue() const         { return minus_val; }
139         ///
140         LyXLength::UNIT minusUnit() const { return minus_uni; }
141
142         /// conversion
143         virtual string const asString() const;
144         ///
145         virtual string const asLatexString() const;
146
147
148         /** If "data" is valid, the length represented by it is
149           stored into "result", if that is not 0. */
150         friend bool isValidGlueLength(string const & data, 
151                                       LyXGlueLength* result = 0);
152
153 protected:
154         ///
155         float plus_val;
156         ///
157         float minus_val;
158         ///
159         LyXLength::UNIT plus_uni;
160         ///
161         LyXLength::UNIT minus_uni;
162 };
163
164 ///
165 inline
166 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
167 {
168         return l1.value() == l2.value()
169                 && l1.unit() == l2.unit()
170                 && l1.plusValue() == l2.plusValue()
171                 && l1.plusUnit() == l2.plusUnit()
172                 && l1.minusValue() == l2.minusValue()
173                 && l1.minusUnit() == l2.minusUnit();
174 }
175
176
177 ///
178 extern bool isValidGlueLength(string const & data, LyXGlueLength * result);
179
180 ///  VSpace class
181 class VSpace {
182 public:
183         /// The different kinds of spaces.
184         enum vspace_kind {
185                 ///
186                 NONE,
187                 ///
188                 DEFSKIP,
189                 ///
190                 SMALLSKIP,
191                 ///
192                 MEDSKIP,
193                 ///
194                 BIGSKIP,
195                 ///
196                 VFILL,
197                 ///
198                 LENGTH
199         };
200         /// Constructor
201         VSpace() : 
202                 kin (NONE), 
203                 len(0.0, LyXLength::PT),
204                 kp (false) {}
205         /// Constructor
206         explicit
207         VSpace(vspace_kind k) :
208                 kin (k), 
209                 len (0.0, LyXLength::PT),
210                 kp (false) {}
211         /// Constructor
212         explicit
213         VSpace(LyXGlueLength l) :
214                 kin (LENGTH),
215                 len (l),
216                 kp (false) {}
217
218         /// Constructor
219         explicit
220         VSpace(float v, LyXLength::UNIT u) : 
221                 kin (LENGTH), 
222                 len (v, u),
223                 kp (false) {}
224
225         /// Constructor for reading from a .lyx file
226         explicit
227         VSpace(string const & data);
228         
229         /// access functions
230         vspace_kind kind() const { return  kin; }
231         ///
232         LyXGlueLength   length() const { return len; }
233
234         // a flag that switches between \vspace and \vspace*
235         bool keep() const      { return kp; }
236         ///
237         void setKeep(bool val) { kp = val; } 
238         ///
239         bool operator==(VSpace const &) const;
240
241         // conversion
242         ///
243         string const asLyXCommand() const;  // how it goes into the LyX file
244         ///
245         string const asLatexCommand(BufferParams const & params) const;
246         ///
247         int inPixels(BufferView * bv) const;
248         ///
249         int inPixels(int default_height, int default_skip, int default_width=0) const;
250 private:
251         /// This VSpace kind
252         vspace_kind kin;
253         ///
254         LyXGlueLength len;
255         ///
256         bool kp;
257 };
258
259 #endif