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