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