1
2 /* $Id: tixGrUtl.c,v 1.1.1.1 2000/05/17 11:08:42 idiscovery Exp $ */
3
4 /*
5 * tixGrUtl.c --
6 *
7 * Utility functions for Grid
8 *
9 * Copyright (c) 1996, Expert Interface Technologies
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 */
15
16 #include "tixPort.h"
17 #include "tixInt.h"
18 #include "tixDef.h"
19
20 #include "tixGrid.h"
21
22 #ifndef UCHAR
23 #define UCHAR(c) ((unsigned char) (c))
24 #endif
25
26 /* string must be a real number plus "char". E.g, "3.0char" */
27 int
Tix_GetChars(interp,string,doublePtr)28 Tix_GetChars(interp, string, doublePtr)
29 Tcl_Interp *interp; /* Use this for error reporting. */
30 char *string; /* String describing a justification style. */
31 double *doublePtr; /* Place to store converted result. */
32 {
33 char *end;
34 double d;
35
36 d = strtod(string, &end);
37 if (end == string) {
38 goto error;
39 }
40 while ((*end != '\0') && isspace(UCHAR(*end))) {
41 end++;
42 }
43 if (strncmp(end, "char", 4) != 0) {
44 goto error;
45 }
46 for (end+=4; (*end != '\0') && isspace(UCHAR(*end)); end++) {
47 ;
48 }
49 if (*end != '\0') {
50 goto error;
51 }
52 if (d < 0) {
53 goto error;
54 }
55
56 *doublePtr = d;
57 return TCL_OK;
58
59 error:
60 Tcl_AppendResult(interp, "bad screen distance \"", string,
61 "\"", (char *) NULL);
62 return TCL_ERROR;
63 }
64
65
Tix_GrConfigSize(interp,wPtr,argc,argv,sizePtr,argcErrorMsg,changed_ret)66 int Tix_GrConfigSize(interp, wPtr, argc, argv, sizePtr, argcErrorMsg,
67 changed_ret)
68 Tcl_Interp *interp;
69 WidgetPtr wPtr;
70 int argc;
71 char **argv;
72 TixGridSize *sizePtr;
73 char * argcErrorMsg;
74 int *changed_ret;
75 {
76 int pixels;
77 double chars;
78 int i;
79 TixGridSize newSize;
80 int changed = 0;
81
82 if (argc == 0) {
83 char buff[40];
84
85 Tcl_AppendResult(interp, "-size ", NULL);
86
87 switch (sizePtr->sizeType) {
88 case TIX_GR_AUTO:
89 Tcl_AppendResult(interp, "auto", NULL);
90 break;
91
92 case TIX_GR_DEFAULT:
93 Tcl_AppendResult(interp, "default", NULL);
94 break;
95
96 case TIX_GR_DEFINED_PIXEL:
97 sprintf(buff, "%d", sizePtr->sizeValue);
98 Tcl_AppendResult(interp, buff, NULL);
99 break;
100
101 case TIX_GR_DEFINED_CHAR:
102 sprintf(buff, "%fchar", sizePtr->charValue);
103 Tcl_AppendResult(interp, buff, NULL);
104 break;
105
106 default:
107 Tcl_AppendResult(interp, "default", NULL);
108 break;
109 }
110
111 Tcl_AppendResult(interp, " -pad0 ", NULL);
112 sprintf(buff, "%d", sizePtr->pad0);
113 Tcl_AppendResult(interp, buff, NULL);
114
115 Tcl_AppendResult(interp, " -pad1 ", NULL);
116 sprintf(buff, "%d", sizePtr->pad1);
117 Tcl_AppendResult(interp, buff, NULL);
118
119 return TCL_OK;
120 }
121
122 if ((argc %2) != 0) {
123 Tcl_AppendResult(interp, "value missing for option \"",
124 argv[argc-1], "\"", NULL);
125 return TCL_ERROR;
126 }
127
128 newSize = *sizePtr;
129
130 for (i=0; i<argc; i+=2) {
131
132 if (strncmp("-size", argv[i], strlen(argv[i])) == 0) {
133 if (strcmp(argv[i+1], "auto")==0) {
134 newSize.sizeType = TIX_GR_AUTO;
135 newSize.sizeValue = 0;
136 }
137 else if (strcmp(argv[i+1], "default")==0) {
138 newSize.sizeType = TIX_GR_DEFAULT;
139 newSize.sizeValue = 0;
140 }
141 else if (Tk_GetPixels(interp, wPtr->dispData.tkwin, argv[i+1],
142 &pixels) == TCL_OK) {
143
144 newSize.sizeType = TIX_GR_DEFINED_PIXEL;
145 newSize.sizeValue = pixels;
146 }
147 else {
148 Tcl_ResetResult(interp);
149 if (Tix_GetChars(interp, argv[i+1], &chars) == TCL_OK) {
150 newSize.sizeType = TIX_GR_DEFINED_CHAR;
151 newSize.charValue = chars;
152 }
153 else {
154 return TCL_ERROR;
155 }
156 }
157 }
158 else if (strcmp("-pad0", argv[i]) == 0) {
159 if (Tk_GetPixels(interp, wPtr->dispData.tkwin, argv[i+1],
160 &pixels) == TCL_OK) {
161
162 newSize.pad0 = pixels;
163 }
164 else {
165 return TCL_ERROR;
166 }
167 }
168 else if (strcmp("-pad1", argv[i]) == 0) {
169 if (Tk_GetPixels(interp, wPtr->dispData.tkwin, argv[i+1],
170 &pixels) == TCL_OK) {
171
172 newSize.pad1 = pixels;
173 }
174 else {
175 return TCL_ERROR;
176 }
177 }
178 else {
179 Tcl_AppendResult(interp, "Unknown option \"", argv[i],
180 "\"; must be -pad0, -pad1 or -size", NULL);
181 return TCL_ERROR;
182 }
183 }
184
185 if (changed_ret) {
186 if (sizePtr->sizeType != newSize.sizeType) {
187 changed = 1;
188 }
189 if (sizePtr->sizeValue != newSize.sizeValue) {
190 changed = 1;
191 }
192 if (sizePtr->charValue != newSize.charValue) {
193 changed = 1;
194 }
195 if (sizePtr->pad1 != newSize.pad0) {
196 changed = 1;
197 }
198 if (sizePtr->pad1 != newSize.pad1) {
199 changed = 1;
200 }
201 *changed_ret = changed;
202 }
203
204 *sizePtr = newSize;
205 return TCL_OK;
206 }
207