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