blob: 86e8c9e5835fe4df31837b5e9e6bcf994acac556 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
* Author:
* Bryce Harrington <bryce@bryceharrington.org>
*
* Copyright (C) 2004 Bryce Harrington
*
* Released under GNU GPL. Read the file 'COPYING' for more information.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <cmath>
#include "unit-menu.h"
namespace Inkscape {
namespace UI {
namespace Widget {
UnitMenu::UnitMenu() : _type(UNIT_TYPE_NONE)
{
set_active(0);
}
UnitMenu::~UnitMenu() {
}
bool UnitMenu::setUnitType(UnitType unit_type)
{
// Expand the unit widget with unit entries from the unit table
UnitTable::UnitMap m = _unit_table.units(unit_type);
UnitTable::UnitMap::iterator iter = m.begin();
while(iter != m.end()) {
Glib::ustring text = (*iter).first;
#if WITH_GTKMM_2_24
append(text);
#else
append_text(text);
#endif
++iter;
}
_type = unit_type;
set_active_text(_unit_table.primary(unit_type));
return true;
}
bool UnitMenu::resetUnitType(UnitType unit_type)
{
#if WITH_GTKMM_2_24
remove_all();
#else
clear_items();
#endif
return setUnitType(unit_type);
}
void UnitMenu::addUnit(Unit const& u)
{
_unit_table.addUnit(u, false);
#if WITH_GTKMM_2_24
append(u.abbr);
#else
append_text(u.abbr);
#endif
}
Unit UnitMenu::getUnit() const
{
if (get_active_text() == "") {
g_assert(_type != UNIT_TYPE_NONE);
return _unit_table.getUnit(_unit_table.primary(_type));
}
return _unit_table.getUnit(get_active_text());
}
bool UnitMenu::setUnit(Glib::ustring const & unit)
{
// TODO: Determine if 'unit' is available in the dropdown.
// If not, return false
set_active_text(unit);
return true;
}
Glib::ustring UnitMenu::getUnitAbbr() const
{
if (get_active_text() == "") {
return "";
}
return getUnit().abbr;
}
UnitType UnitMenu::getUnitType() const
{
return getUnit().type;
}
double UnitMenu::getUnitFactor() const
{
return getUnit().factor;
}
int UnitMenu::getDefaultDigits() const
{
return getUnit().defaultDigits();
}
double UnitMenu::getDefaultStep() const
{
int factor_digits = -1*int(log10(getUnit().factor));
return pow(10.0, factor_digits);
}
double UnitMenu::getDefaultPage() const
{
return 10 * getDefaultStep();
}
double UnitMenu::getConversion(Glib::ustring const &new_unit_abbr, Glib::ustring const &old_unit_abbr) const
{
double old_factor = getUnit().factor;
if (old_unit_abbr != "no_unit")
old_factor = _unit_table.getUnit(old_unit_abbr).factor;
Unit new_unit = _unit_table.getUnit(new_unit_abbr);
// Catch the case of zero or negative unit factors (error!)
if (old_factor < 0.0000001 ||
new_unit.factor < 0.0000001) {
// TODO: Should we assert here?
return 0.00;
}
return old_factor / new_unit.factor;
}
bool UnitMenu::isAbsolute() const
{
return getUnitType() != UNIT_TYPE_DIMENSIONLESS;
}
bool UnitMenu::isRadial() const
{
return getUnitType() == UNIT_TYPE_RADIAL;
}
} // namespace Widget
} // namespace UI
} // namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
|