My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
#include "RichEdit.h"

NAMESPACE_UPP

double UnitMultiplier(int unit) {
static double m[] =
{
1,
72.0 / 600,
1.0 / 600,
25.4 / 600,
2.54 / 600,
};
ASSERT(unit >= UNIT_DOT && unit <= UNIT_CM);
return m[unit];
}

const char *UnitText(int unit) {
static const char *txt[] =
{
"dt",
"pt",
"\"",
"mm",
"cm"
};
return txt[unit];
}

void UnitEdit::Read(double& d, int& u) const
{
String txt = GetText().ToString();
const char *s = txt;
u = unit;
d = Null;
if(s && *s) {
char *e;
int sign = 1;
for(;;) {
if(*s == '-' && sgn)
sign = -1;
else
if(*s != ' ')
break;
s++;
}
d = sign * strtod(s, &e);
if(e == s) {
d = Null;
return;
}
while(*e == ' ') e++;
if(*e == '\"' || *e == 'i')
u = UNIT_INCH;
if(*e == 'm')
u = UNIT_MM;
if(*e == 'p' || *e == 'b')
u = UNIT_POINT;
if(*e == 'c')
u = UNIT_CM;
if(*e == 'd')
u = UNIT_DOT;
}
}

Value UnitEdit::GetData() const
{
double q;
int u;
Read(q, u);
return IsNull(q) ? Null : int(q / UnitMultiplier(u) + 0.5);
}

String UnitEdit::AsText(double d, int unit)
{
if(IsNull(d))
return Null;
String utxt = UnitText(unit);
if(unit == UNIT_POINT)
d = floor(4 * d + 0.5) / 4;
return AsString(d, unit == UNIT_DOT ? 0 : unit == UNIT_MM ? 1 : 2) + ' ' + utxt;
}

String UnitEdit::DotAsText(int dot, int unit)
{
if(IsNull(dot)) return Null;
return AsText(dot * UnitMultiplier(unit), unit);
}

Value UnitEdit::Format(const Value& v) const
{
return DotAsText(v, unit);
}

void UnitEdit::SetData(const Value& v)
{
SetText(DotAsText(v, unit).ToWString());
}

bool UnitEdit::Key(dword key, int repcnt)
{
if(key == K_UP) { Spin(+1); return true; }
if(key == K_DOWN) { Spin(-1); return true; }
return EditField::Key(key, repcnt);
}

void UnitEdit::Spin(int delta)
{
double q;
int u;
Read(q, u);
if(IsNull(q))
q = 0;
else {
double h = 10;
switch(u) {
case UNIT_DOT: h = 10; break;
case UNIT_POINT: h = 0.5; break;
case UNIT_MM: h = 0.5; break;
case UNIT_CM:
case UNIT_INCH: h = 0.05; break;
default: NEVER();
}
h *= delta;
q = ceil(q / h + 1e-2) * h;
if(!sgn && q < 0)
q = 0;
}
SetText(AsText(q, unit).ToWString());
UpdateAction();
}

int CharFilterUnitEdit(int c)
{
return IsAlpha(c) ? ToLower(c) : IsDigit(c) || c == ' ' || c == '\"' || c == '.' ? c : 0;
}

int CharFilterUnitEditSgn(int c)
{
return c == '-' ? c : CharFilterUnitEdit(c);
}

UnitEdit& UnitEdit::WithSgn(bool b)
{
sgn = b;
SetFilter(b ? CharFilterUnitEditSgn : CharFilterUnitEdit);
return *this;
}

UnitEdit::UnitEdit()
{
AddFrame(spin);
spin.inc.WhenRepeat = spin.inc.WhenPush = THISBACK1(Spin, 1);
spin.dec.WhenRepeat = spin.dec.WhenPush = THISBACK1(Spin, -1);
unit = UNIT_DOT;
WithSgn(false);
}

END_UPP_NAMESPACE

Change log

r4457 by cxl on Jan 21, 2012   Diff
*uppsrc: Fixed many GCC warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r281 by mdelfede on Jun 7, 2008   Diff
changed svn layout
r48 by unodgs on Dec 25, 2006   Diff
[No log message]
r3 by unodgs on Feb 26, 2006   Diff
[No log message]
All revisions of this file

File info

Size: 2775 bytes, 159 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting