]> git.lyx.org Git - lyx.git/blob - src/vspace.h
d11dfdc88bf2295460d77ab6e0de3cf1e62cb414
[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                 ///
56                 PE,
57                 /// Percent of pagewidth
58                 PP,
59                 /// Percent of linewidth
60                 PL,
61                 /// no unit
62                 UNIT_NONE
63         };
64
65         ///
66         LyXLength() : val(0), uni(LyXLength::PT) {}
67         ///
68         LyXLength(float v, LyXLength::UNIT u) : val(v), uni(u) {}
69
70         /** "data" must be a decimal number, followed by a unit. */
71         explicit
72         LyXLength(string const & data);
73         
74         ///
75         float value() const         { return val; }
76         ///
77         LyXLength::UNIT unit() const { return uni; }
78
79         /// conversion
80         virtual string const asString() const;
81         ///
82         virtual string const asLatexString() const;
83
84         /** If "data" is valid, the length represented by it is
85           stored into "result", if that is not 0. */
86         friend bool isValidLength(string const & data, 
87                                   LyXLength * result = 0);
88
89 protected:
90         ///
91         float           val;
92         ///
93         LyXLength::UNIT uni;
94 };
95
96 ///
97 inline
98 bool operator==(LyXLength const & l1, LyXLength const & l2)
99 {
100         return l1.value() == l2.value()
101                 && l1.unit() == l2.unit();
102 }
103         
104 ///
105 extern LyXLength::UNIT unitFromString (string const & data);
106 ///
107 extern bool isValidLength(string const & data, LyXLength * result);
108 ///
109 extern const char * stringFromUnit(int unit);
110
111 /// LyXGlueLength class
112 class LyXGlueLength : public LyXLength {
113 public:
114         ///
115         LyXGlueLength(float v,
116                       LyXLength::UNIT u, 
117                       float pv = 0.0,
118                       LyXLength::UNIT pu = LyXLength::UNIT_NONE, 
119                       float mv = 0.0,
120                       LyXLength::UNIT mu = LyXLength::UNIT_NONE) 
121                 : LyXLength (v, u), 
122                   plus_val(pv), minus_val(mv), 
123                   plus_uni(pu), minus_uni(mu) {}
124
125         /** "data" must be a decimal number, followed by a unit, and 
126           optional "glue" indicated by "+" and "-".  You may abbreviate
127           reasonably.  Examples:
128           1.2 cm  //  4mm +2pt  //  2cm -4mm +2mm  //  4+0.1-0.2cm
129           The traditional Latex format is also accepted, like  
130           4cm plus 10pt minus 10pt */
131         explicit
132         LyXGlueLength(string const & data);
133         
134         ///
135         float plusValue() const         { return plus_val; }
136         ///
137         LyXLength::UNIT plusUnit() const { return plus_uni; }
138         ///
139         float minusValue() const         { return minus_val; }
140         ///
141         LyXLength::UNIT minusUnit() const { return minus_uni; }
142
143         /// conversion
144         virtual string const asString() const;
145         ///
146         virtual string const asLatexString() const;
147
148
149         /** If "data" is valid, the length represented by it is
150           stored into "result", if that is not 0. */
151         friend bool isValidGlueLength(string const & data, 
152                                       LyXGlueLength* result = 0);
153
154 protected:
155         ///
156         float plus_val;
157         ///
158         float minus_val;
159         ///
160         LyXLength::UNIT plus_uni;
161         ///
162         LyXLength::UNIT minus_uni;
163 };
164
165 ///
166 inline
167 bool operator==(LyXGlueLength const & l1, LyXGlueLength const & l2)
168 {
169         return l1.value() == l2.value()
170                 && l1.unit() == l2.unit()
171                 && l1.plusValue() == l2.plusValue()
172                 && l1.plusUnit() == l2.plusUnit()
173                 && l1.minusValue() == l2.minusValue()
174                 && l1.minusUnit() == l2.minusUnit();
175 }
176
177
178 ///
179 extern bool isValidGlueLength(string const & data, LyXGlueLength * result);
180
181 ///  VSpace class
182 class VSpace {
183 public:
184         /// The different kinds of spaces.
185         enum vspace_kind {
186                 ///
187                 NONE,
188                 ///
189                 DEFSKIP,
190                 ///
191                 SMALLSKIP,
192                 ///
193                 MEDSKIP,
194                 ///
195                 BIGSKIP,
196                 ///
197                 VFILL,
198                 ///
199                 LENGTH
200         };
201         /// Constructor
202         VSpace() : 
203                 kin (NONE), 
204                 len(0.0, LyXLength::PT),
205                 kp (false) {}
206         /// Constructor
207         explicit
208         VSpace(vspace_kind k) :
209                 kin (k), 
210                 len (0.0, LyXLength::PT),
211                 kp (false) {}
212         /// Constructor
213         explicit
214         VSpace(LyXGlueLength l) :
215                 kin (LENGTH),
216                 len (l),
217                 kp (false) {}
218
219         /// Constructor
220         explicit
221         VSpace(float v, LyXLength::UNIT u) : 
222                 kin (LENGTH), 
223                 len (v, u),
224                 kp (false) {}
225
226         /// Constructor for reading from a .lyx file
227         explicit
228         VSpace(string const & data);
229         
230         /// access functions
231         vspace_kind kind() const { return  kin; }
232         ///
233         LyXGlueLength   length() const { return len; }
234
235         // a flag that switches between \vspace and \vspace*
236         bool keep() const      { return kp; }
237         ///
238         void setKeep(bool val) { kp = val; } 
239         ///
240         bool operator==(VSpace const &) const;
241
242         // conversion
243         ///
244         string const asLyXCommand() const;  // how it goes into the LyX file
245         ///
246         string const asLatexCommand(BufferParams const & params) const;
247         ///
248         int inPixels(BufferView * bv) const;
249         ///
250         int inPixels(int default_height, int default_skip, int default_width=0) const;
251 private:
252         /// This VSpace kind
253         vspace_kind kin;
254         ///
255         LyXGlueLength len;
256         ///
257         bool kp;
258 };
259
260 #endif