File: tixGrUtl.c

package info (click to toggle)
tix 8.4.3-10
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • 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 (205 lines) | stat: -rw-r--r-- 4,408 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205

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

/* 
 * tixGrUtl.c --
 *
 *	Utility functions for Grid
 *
 * Copyright (c) 1996, Expert Interface Technologies
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#include <tixPort.h>
#include <tixInt.h>
#include <tixDef.h>
#include <tixGrid.h>

#ifndef UCHAR
#define UCHAR(c) ((unsigned char) (c))
#endif

/* string must be a real number plus "char". E.g, "3.0char" */
int
Tix_GetChars(interp, string, doublePtr)
    Tcl_Interp *interp;		/* Use this for error reporting. */
    CONST84 char *string;	/* String describing a justification style. */
    double *doublePtr;		/* Place to store converted result. */
{
    char *end;
    double d;

    d = strtod(string, &end);
    if (end == string) {
	goto error;
    }
    while ((*end != '\0') && isspace(*end)) {
	end++;
    }
    if (strncmp(end, "char", 4) != 0) {
	goto error;
    }
    for (end+=4; (*end != '\0') && isspace(UCHAR(*end)); end++) {
	;
    }
    if (*end != '\0') {
	goto error;
    }
    if (d < 0) {
	goto error;
    }

    *doublePtr = d;
    return TCL_OK;

  error:
    Tcl_AppendResult(interp, "bad screen distance \"", string,
	"\"", (char *) NULL);
    return TCL_ERROR;
}


int Tix_GrConfigSize(interp, wPtr, argc, argv, sizePtr, argcErrorMsg,
	changed_ret)
    Tcl_Interp *interp;
    WidgetPtr wPtr;
    int argc;
    CONST84 char **argv;
    TixGridSize *sizePtr;
    CONST84 char * argcErrorMsg;
    int *changed_ret;
{
    int pixels;
    double chars;
    int i;
    TixGridSize newSize;
    int changed = 0;

    if (argc == 0) {
	char buff[40];

	Tcl_AppendResult(interp, "-size ", NULL);

	switch (sizePtr->sizeType) {
	  case TIX_GR_AUTO:
	    Tcl_AppendResult(interp, "auto", NULL);
	    break;

	  case TIX_GR_DEFAULT:
	    Tcl_AppendResult(interp, "default", NULL);
	    break;

	  case TIX_GR_DEFINED_PIXEL:
	    sprintf(buff, "%d", sizePtr->sizeValue);
	    Tcl_AppendResult(interp, buff, NULL);
	    break;

	  case TIX_GR_DEFINED_CHAR:
	    sprintf(buff, "%fchar", sizePtr->charValue);
	    Tcl_AppendResult(interp, buff, NULL);
	    break;

	  default:
	    Tcl_AppendResult(interp, "default", NULL);
	    break;
	}

	Tcl_AppendResult(interp, " -pad0 ", NULL);
	sprintf(buff, "%d", sizePtr->pad0);
	Tcl_AppendResult(interp, buff, NULL);

	Tcl_AppendResult(interp, " -pad1 ", NULL);
	sprintf(buff, "%d", sizePtr->pad1);
	Tcl_AppendResult(interp, buff, NULL);

	return TCL_OK;
    }

    if ((argc %2) != 0) {
	Tcl_AppendResult(interp, "value missing for option \"",
	    argv[argc-1], "\"", NULL);
	return TCL_ERROR;
    }

    newSize = *sizePtr;

    for (i=0; i<argc; i+=2) {

	if (strncmp("-size", argv[i], strlen(argv[i])) == 0) {
	    if (strcmp(argv[i+1], "auto")==0) {
		newSize.sizeType  = TIX_GR_AUTO;
		newSize.sizeValue = 0;
	    }
	    else if (strcmp(argv[i+1], "default")==0) {
		newSize.sizeType  = TIX_GR_DEFAULT;
		newSize.sizeValue = 0;
	    }
	    else if (Tk_GetPixels(interp, wPtr->dispData.tkwin, argv[i+1],
		 &pixels) == TCL_OK) {

		newSize.sizeType  = TIX_GR_DEFINED_PIXEL;
		newSize.sizeValue = pixels;
	    }
	    else {
		Tcl_ResetResult(interp);
		if (Tix_GetChars(interp, argv[i+1], &chars) == TCL_OK) {
		    newSize.sizeType  = TIX_GR_DEFINED_CHAR;
		    newSize.charValue = chars;
		}
		else {
		    return TCL_ERROR;
		}
	    }
	}
	else if (strcmp("-pad0", argv[i]) == 0) {
	    if (Tk_GetPixels(interp, wPtr->dispData.tkwin, argv[i+1],
		 &pixels) == TCL_OK) {

		newSize.pad0 = pixels;
	    }
	    else {
		return TCL_ERROR;
	    }
	}
	else if (strcmp("-pad1", argv[i]) == 0) {
	    if (Tk_GetPixels(interp, wPtr->dispData.tkwin, argv[i+1],
		 &pixels) == TCL_OK) {

		newSize.pad1 = pixels;
	    }
	    else {
		return TCL_ERROR;
	    }
	}
	else {
	    Tcl_AppendResult(interp, "Unknown option \"", argv[i],
		"\"; must be -pad0, -pad1 or -size", NULL);
	    return TCL_ERROR;
	}
    }

    if (changed_ret) {
	if (sizePtr->sizeType  != newSize.sizeType) {
	    changed = 1;
	}
	if (sizePtr->sizeValue != newSize.sizeValue) {
	    changed = 1;
	}
	if (sizePtr->charValue != newSize.charValue) {
	    changed = 1;
	}
	if (sizePtr->pad1      != newSize.pad0) {
	    changed = 1;
	}
	if (sizePtr->pad1      != newSize.pad1) {
	    changed = 1;
	}
	*changed_ret = changed;
    }

    *sizePtr = newSize;
    return TCL_OK;
}