]> git.lyx.org Git - features.git/blob - src/lyxlength.C
split vspace into smaller files.
[features.git] / src / lyxlength.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 "lyxlength.h"
18
19 #include "Lsstream.h"
20
21 namespace {
22 // this is now here and in lyxgluelength.C
23
24 int const num_units = LyXLength::UNIT_NONE;
25
26 // I am not sure if "mu" should be possible to select (Lgb)
27 char const * unit_name[num_units] = { "sp", "pt", "bp", "dd",
28                                       "mm", "pc", "cc", "cm",
29                                       "in", "ex", "em", "mu",
30                                       "%",  "c%", "p%", "l%" };
31
32
33 LyXLength::UNIT unitFromString(string const & data)
34 {
35         int i = 0;
36         while (i < num_units && data != unit_name[i])
37                 ++i;
38         return static_cast<LyXLength::UNIT>(i);
39 }
40
41 }
42
43
44 LyXLength::LyXLength()
45         : val_(0), unit_(LyXLength::PT)
46 {}
47
48
49 LyXLength::LyXLength(double v, LyXLength::UNIT u)
50         : val_(v), unit_(u)
51 {}
52
53
54 LyXLength::LyXLength(string const & data)
55 {
56         LyXLength tmp;
57         
58         if (!isValidLength (data, &tmp))
59                 return; // should raise an exception
60
61         val_  = tmp.val_;
62         unit_ = tmp.unit_;
63 }
64
65
66 string const LyXLength::asString() const
67 {
68         ostringstream buffer;
69         buffer << val_ << unit_name[unit_]; // setw?
70         return buffer.str().c_str();
71 }
72
73
74 string const LyXLength::asLatexString() const
75 {
76         ostringstream buffer;
77         switch(unit_) {
78         case PW:
79         case PE:
80             buffer << abs(static_cast<int>(val_/100)) << "."
81                                 << abs(static_cast<int>(val_)%100) << "\\columnwidth";
82             break;
83         case PP:
84             buffer << abs(static_cast<int>(val_/100)) << "."
85                                 << abs(static_cast<int>(val_)%100) << "\\pagewidth";
86             break;
87         case PL:
88             buffer << abs(static_cast<int>(val_/100)) << "."
89                                 << abs(static_cast<int>(val_)%100) << "\\linewidth";
90             break;
91         default:
92             buffer << val_ << unit_name[unit_]; // setw?
93             break;
94         }
95         return buffer.str().c_str();
96 }
97
98
99 double LyXLength::value() const
100 {
101         return val_;
102 }
103
104
105 LyXLength::UNIT LyXLength::unit() const
106 {
107         return unit_;
108 }
109
110
111 void LyXLength::value(double v)
112 {
113         val_ = v;
114 }
115
116
117 void LyXLength::unit(LyXLength::UNIT u)
118 {
119         unit_ = u;
120 }
121
122
123 bool operator==(LyXLength const & l1, LyXLength const & l2)
124 {
125         return l1.value() == l2.value() && l1.unit() == l2.unit();
126 }