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