[go: up one dir, main page]

File: valuator.c

package info (click to toggle)
libforms1 1.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 6,648 kB
  • ctags: 14,351
  • sloc: ansic: 90,441; makefile: 9,902; sh: 8
file content (138 lines) | stat: -rw-r--r-- 3,175 bytes parent folder | download | duplicates (3)
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
/*
 *
 *  This file is part of the XForms library package.
 *
 * XForms is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1, or
 * (at your option) any later version.
 *
 * XForms is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with XForms; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 *
 */


/*
 * $Id: valuator.c,v 1.0 2002/05/08 05:16:30 zhao Release $
 *
 *  This file is part of the XForms library package.
 *  Copyright (c) 1998-2002  T.C. Zhao
 *  All rights reserved.
 *
 *
 *  Handle some common valuator tasks. 
 *   TODO: move slider and counter handler here
 */

#include "forms.h"
#include "pvaluator.h"

void *
fl_init_valuator(FL_OBJECT * ob)
{
    FL_VALUATOR_SPEC *sp = ob->spec;

    if (!sp)
	sp = ob->spec = fl_calloc(1, ob->spec_size = sizeof(*sp));

    sp->min = 0.0;
    sp->max = 1.0;
    sp->val = 0.5;
    sp->prec = 2;
    sp->step = 0.01;
    sp->draw_type = COMPLETE_DRAW;

    return sp;
}

#define CROSS_OVER(v,vmin,vmax)  ((v)<(vmin)?(vmax):((v)>(vmax)?(vmin):(v)))


double
fl_valuator_round_and_clamp(FL_OBJECT * ob, double value)
{
    FL_VALUATOR_SPEC *sp = ob->spec;
    double vmin, vmax;

    if (sp->step != 0.0)
    {
	float f = value / sp->step;
	value = sp->step * (int) (f > 0 ? (f + 0.4) : (f - 0.4));
    }

    vmin = FL_min(sp->min, sp->max);
    vmax = FL_max(sp->min, sp->max);

    if(!sp->cross_over)
       return FL_clamp(value, vmin, vmax);
    else
       return CROSS_OVER(value,vmin,vmax);
}

int
fl_valuator_handle_drag(FL_OBJECT * ob, double value)
{
    FL_VALUATOR_SPEC *sp = ob->spec;

    value = fl_valuator_round_and_clamp(ob, value);

    if (value != sp->val)
    {
	sp->val = value;
	sp->draw_type = VALUE_DRAW;
	fl_redraw_object(ob);
	return (sp->how_return == FL_RETURN_CHANGED ||
		sp->how_return == FL_RETURN_ALWAYS);
    }

    return (sp->how_return == FL_RETURN_ALWAYS);
}

int
fl_valuator_handle_release(FL_OBJECT * ob, double value)
{
    FL_VALUATOR_SPEC *sp = ob->spec;

    value = fl_valuator_round_and_clamp(ob, value);

    if (value != sp->val)
    {
	sp->val = value;
	sp->draw_type = VALUE_DRAW;
	fl_redraw_object(ob);
	if (sp->how_return == FL_RETURN_CHANGED)
	    return 1;
    }

    if (sp->start_val != sp->val && (sp->how_return == FL_RETURN_END_CHANGED))
	return 1;

    return (sp->how_return == FL_RETURN_ALWAYS ||
	    sp->how_return == FL_RETURN_END);
}

int
fl_set_valuator_return(FL_OBJECT * ob, int n)
{
    FL_VALUATOR_SPEC *sp = ob->spec;
    int old = sp->how_return;

    sp->how_return = n;

    return old;
}

double
fl_clamp(double val, double min, double max)
{
    double vmin = FL_min(min, max), vmax = FL_max(min, max);
    return FL_clamp(val, vmin, vmax);
}