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