]> git.lyx.org Git - lyx.git/blob - src/lyxlength.C
bug 183
[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 #include "lengthcommon.h"
19
20 #include "Lsstream.h"
21
22 #include <cstdlib>
23
24
25 LyXLength::LyXLength()
26         : val_(0), unit_(LyXLength::PT)
27 {}
28
29
30 LyXLength::LyXLength(double v, LyXLength::UNIT u)
31         : val_(v), unit_(u)
32 {}
33
34
35 LyXLength::LyXLength(string const & data)
36         : val_(0), unit_(LyXLength::PT)
37 {
38         LyXLength tmp;
39         
40         if (!isValidLength (data, &tmp))
41                 return; // should raise an exception
42
43         val_  = tmp.val_;
44         unit_ = tmp.unit_;
45 }
46
47
48 string const LyXLength::asString() const
49 {
50         ostringstream buffer;
51         buffer << val_ << unit_name[unit_]; // setw?
52         return buffer.str().c_str();
53 }
54
55
56 string const LyXLength::asLatexString() const
57 {
58         ostringstream buffer;
59         switch(unit_) {
60         case PW:
61         case PE:
62             buffer << abs(static_cast<int>(val_/100)) << "."
63                    << abs(static_cast<int>(val_)%100) << "\\columnwidth";
64             break;
65         case PP:
66             buffer << abs(static_cast<int>(val_/100)) << "."
67                    << abs(static_cast<int>(val_)%100) << "\\pagewidth";
68             break;
69         case PL:
70             buffer << abs(static_cast<int>(val_/100)) << "."
71                    << abs(static_cast<int>(val_)%100) << "\\linewidth";
72             break;
73         default:
74             buffer << val_ << unit_name[unit_]; // setw?
75             break;
76         }
77         return buffer.str().c_str();
78 }
79
80
81 double LyXLength::value() const
82 {
83         return val_;
84 }
85
86
87 LyXLength::UNIT LyXLength::unit() const
88 {
89         return unit_;
90 }
91
92
93 void LyXLength::value(double v)
94 {
95         val_ = v;
96 }
97
98
99 void LyXLength::unit(LyXLength::UNIT u)
100 {
101         unit_ = u;
102 }
103
104
105 bool LyXLength::zero() const 
106 {
107         return val_ == 0.0;
108 }
109
110
111 bool operator==(LyXLength const & l1, LyXLength const & l2)
112 {
113         return l1.value() == l2.value() && l1.unit() == l2.unit();
114 }
115
116
117 bool operator!=(LyXLength const & l1, LyXLength const & l2)
118 {
119         return !(l1 == l2);
120 }
121