File: tixScroll.c

package info (click to toggle)
tix 8.4.3-10
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 9,080 kB
  • ctags: 7,129
  • sloc: ansic: 28,082; tcl: 22,774; python: 7,577; makefile: 331; cs: 253; sh: 210; perl: 128
file content (191 lines) | stat: -rw-r--r-- 4,419 bytes parent folder | download | duplicates (6)
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

/*	$Id: tixScroll.c,v 1.2 2004/03/28 02:44:57 hobbs Exp $	*/

/*
 * tixScroll.c -- Handle all the mess of Tk scroll bars
 *
 *
 *
 */

#include <tixInt.h>


void Tix_InitScrollInfo(siPtr, type)
    Tix_ScrollInfo * siPtr;
    int type;
{
    Tix_IntScrollInfo*    isiPtr = (Tix_IntScrollInfo*)   siPtr;
    Tix_DoubleScrollInfo* dsiPtr = (Tix_DoubleScrollInfo*)siPtr;

    siPtr->command 	= NULL;
    siPtr->type 	= type;

    if (type == TIX_SCROLL_INT) {
	isiPtr->total	= 1;
	isiPtr->window	= 1;
	isiPtr->offset	= 0;
	isiPtr->unit	= 1;
    }
    else {
	dsiPtr->total	= 1.0;
	dsiPtr->window	= 1.0;
	dsiPtr->offset	= 0.0;
	dsiPtr->unit	= 1.0;
    }
}

/*----------------------------------------------------------------------
 * Tix_GetScrollFractions --
 *
 * Compute the fractions of a scroll-able widget.
 *
 */
void Tix_GetScrollFractions(siPtr, first_ret, last_ret)
    Tix_ScrollInfo * siPtr;
    double * first_ret;
    double * last_ret;
{
    Tix_IntScrollInfo*    isiPtr = (Tix_IntScrollInfo*)   siPtr;
    Tix_DoubleScrollInfo* dsiPtr = (Tix_DoubleScrollInfo*)siPtr;
    double total, window, first;

    if (siPtr->type == TIX_SCROLL_INT) {
	total  = isiPtr->total;
	window = isiPtr->window;
	first  = isiPtr->offset;
    } else {
	total  = dsiPtr->total;
	window = dsiPtr->window;
	first  = dsiPtr->offset;
    }

    if (total == 0 || total < window) {
	*first_ret = 0.0;
	*last_ret  = 1.0;
    } else {
	*first_ret = first / total;
	*last_ret  = (first+window) / total;
    }
}

void Tix_UpdateScrollBar(interp, siPtr)
    Tcl_Interp *interp;
    Tix_ScrollInfo * siPtr;
{
    Tix_IntScrollInfo*    isiPtr = (Tix_IntScrollInfo*)   siPtr;
    Tix_DoubleScrollInfo* dsiPtr = (Tix_DoubleScrollInfo*)siPtr;
    double d_first, d_last;
    char string[100];

    if (siPtr->type == TIX_SCROLL_INT) {
	/* Check whether the topPixel is out of bound */
	if (isiPtr->offset < 0) {
	    isiPtr->offset = 0;
	} else {
	    if (isiPtr->window > isiPtr->total) {
		isiPtr->offset = 0;
	    }
	    else if((isiPtr->offset+isiPtr->window) > isiPtr->total) {
		isiPtr->offset = isiPtr->total - isiPtr->window;
	    }
	}
    } else {
	/* Check whether the topPixel is out of bound */
	if (dsiPtr->offset < 0) {
	    dsiPtr->offset = 0;
	} else {
	    if (dsiPtr->window > dsiPtr->total) {
		dsiPtr->offset = 0;
	    }
	    else if((dsiPtr->offset+dsiPtr->window) > dsiPtr->total) {
		dsiPtr->offset = dsiPtr->total - dsiPtr->window;
	    }
	}
    }


    if (siPtr->command) {
	Tix_GetScrollFractions(siPtr, &d_first, &d_last);

	sprintf(string, " %f %f", d_first, d_last);
	if (Tcl_VarEval(interp, siPtr->command, string, 
	    (char *) NULL) != TCL_OK) {
	    Tcl_AddErrorInfo(interp,
		"\n    (scrolling command executed by tixTList)");
	    Tk_BackgroundError(interp);
	}
    }
}

int Tix_SetScrollBarView(interp, siPtr, argc, argv, compat)
    Tcl_Interp *interp;		/* Current interpreter. */
    Tix_ScrollInfo * siPtr;
    int argc;			/* Number of arguments. */
    CONST84 char **argv;	/* Argument strings. */
    int compat;			/* compatibility mode */
{
    Tix_IntScrollInfo*    isiPtr = (Tix_IntScrollInfo*)   siPtr;
    Tix_DoubleScrollInfo* dsiPtr = (Tix_DoubleScrollInfo*)siPtr;
    int offset;

    if (compat && Tcl_GetInt(interp, argv[0], &offset) == TCL_OK) {
	/* backward-compatible mode */
	if (siPtr->type == TIX_SCROLL_INT) {
	    isiPtr->offset = offset;
	}
	else {
	    dsiPtr->offset = (double)offset;
	}

	return TCL_OK;
    }
    else {
	int type, count;
	double fraction;

	Tcl_ResetResult(interp);

	/* Tk_GetScrollInfo () wants strange argc,argv combinations .. */
	type = Tk_GetScrollInfo(interp, argc+2, argv-2, &fraction, &count);

	if (siPtr->type == TIX_SCROLL_INT) {
	    switch (type) {
	      case TK_SCROLL_ERROR:
		return TCL_ERROR;

	      case TK_SCROLL_MOVETO:
		isiPtr->offset = 
		  (int)(fraction * (double)isiPtr->total);
		break;

	      case TK_SCROLL_PAGES:
		isiPtr->offset += count * isiPtr->window;
		break;

	      case TK_SCROLL_UNITS:
		isiPtr->offset += count * isiPtr->unit;
		break;
	    }
	} else {
	    switch (type) {
	      case TK_SCROLL_ERROR:
		return TCL_ERROR;

	      case TK_SCROLL_MOVETO:
		dsiPtr->offset = 
		  fraction * dsiPtr->total;
		break;

	      case TK_SCROLL_PAGES:
		dsiPtr->offset += count * dsiPtr->window;
		break;

	      case TK_SCROLL_UNITS:
		dsiPtr->offset += count * dsiPtr->unit;
		break;
	    }
	}
    }
    return TCL_OK;
}