File: scale.cc

package info (click to toggle)
clxclient 3.9.0-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 304 kB
  • sloc: cpp: 3,707; makefile: 141
file content (196 lines) | stat: -rw-r--r-- 4,073 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// ---------------------------------------------------------------------------------
//
//  Copyright (C) 2003-2013 Fons Adriaensen <fons@linuxaudio.org>
//    
//  This program 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 of the License, or
//  (at your option) any later version.
//
//  This program 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 this program; if not, write to the Free Software
//  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ---------------------------------------------------------------------------------


#include "clxclient.h"


int X_scale_style::calcpix (float v)
{
    int  k, *P, p0, p1;
    float *V, v0, v1;

    k = nseg;
    P = pix;  
    V = val;

    p0 = *P++;
    v0 = *V++; 
    if (v < v0) return p0;
    while (k--)
    {
	p1 = *P++;
	v1 = *V++;
	if (v > v1)
	{
	    p0 = p1;
	    v0 = v1;   
	}
	else return (int)(p0 + (p1 - p0) * (v - v0) / (v1 - v0) + 0.5);
    }
    return p0;
}


float X_scale_style::calcval (int p)
{
    int  k, *P, p0, p1;
    float *V, v0, v1;

    k = nseg;
    P = pix;  
    V = val;

    p0 = *P++;
    v0 = *V++; 
    if (p < p0) return v0;
    while (k--)
    {
	p1 = *P++;
	v1 = *V++;
	if (p > p1)
	{
	    p0 = p1;
	    v0 = v1;   
	}
	else return v0 + (v1 - v0) * (p - p0) / (p1 - p0);
    }
    return v0; 
}


int X_scale_style::limit (int i)
{
    if (i < pix [0]) return pix [0]; 
    if (i > pix [nseg]) return pix [nseg]; 
    return i;
}



X_hscale::X_hscale (X_window  *parent, X_scale_style *style, int xp, int yp, int h, int d) :
    X_window (parent, xp, yp, style->length (), h, style->bg), 
    _style (style),
    _h (h),
    _d (d)
{
    x_add_events (ExposureMask);
}


void X_hscale::handle_event (XEvent *E)
{
    switch (E->type)
    {
    case Expose:
	expose ((XExposeEvent *) E);
	break;  
    }
}


void X_hscale::expose (XExposeEvent *)
{
    int            k, l, m, x, y, *p;
    const char   **t;
    XftColor      *C = _style->fg;
    XftFont       *F = _style->font;
    XGlyphInfo     K;

    XClearWindow (dpy (), win ());
    XftDrawChange (xft (), win ());

    k = _style->nseg + 1;
    p = _style->pix;  
    t = _style->text;
    m = _style->marg;
    y = (_h + F->ascent - F->descent) / 2;
    while (k--)
    {
	if (*t)
	{
	    l = strlen (*t);  
            XftTextExtentsUtf8 (dpy (), F, (const FcChar8 *) *t, l, &K);
            x = m + *p - K.width / 2;
            XftDrawStringUtf8 (xft (), C, F, x, y, (const FcChar8 *) *t, l);
	}
	p++;
	t++;
    }
}



X_vscale::X_vscale (X_window *parent, X_scale_style *style, int xp, int yp, int w, int d) :
    X_window (parent, xp, yp, w, style->length (), style->bg), 
    _style (style),
    _w (w),
    _d (d)
{
    x_add_events (ExposureMask);
}


void X_vscale::handle_event (XEvent *E)
{
    switch (E->type)
    {
    case Expose:
	expose ((XExposeEvent *) E);
	break;  
    }
}


void X_vscale::expose (XExposeEvent *)
{
    int            k, l, m, x, y, *p;
    const char   **t;
    XftColor      *C = _style->fg;
    XftFont       *F = _style->font;
    XGlyphInfo     K;

    XClearWindow (dpy (), win ());
    XftDrawChange (xft (), win ());

    k = _style->nseg + 1;
    p = _style->pix;  
    t = _style->text;
    m = _style->marg + _style->pixlen () - 1 + F->ascent / 2;
    while (k--)
    {
	if (*t)
	{
	    l = strlen (*t);  
	    y = m - *p;
	    if (_d <= 0)
	    {
                XftTextExtentsUtf8 (dpy (), F, (const FcChar8 *) *t, l, &K);
		if (_d) x = _w  + _d - K.width;
		else    x = (_w - K.width) / 2;
	    }
	    else x = _d; 
            XftDrawStringUtf8 (xft (), C, F, x, y, (const FcChar8 *) *t, l);
	}
	p++;
	t++;
    }
}