Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkGet.c
1810 views
1
/*
2
* tkGet.c --
3
*
4
* This file contains a number of "Tk_GetXXX" procedures, which
5
* parse text strings into useful forms for Tk. This file has
6
* the simpler procedures, like Tk_GetDirection and Tk_GetUid.
7
* The more complex procedures like Tk_GetColor are in separate
8
* files.
9
*
10
* Copyright (c) 1991-1994 The Regents of the University of California.
11
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
12
*
13
* See the file "license.terms" for information on usage and redistribution
14
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15
*
16
* SCCS: @(#) tkGet.c 1.12 96/02/15 18:53:33
17
*/
18
19
#include "tkInt.h"
20
21
/*
22
* The hash table below is used to keep track of all the Tk_Uids created
23
* so far.
24
*/
25
26
static Tcl_HashTable uidTable;
27
static int initialized = 0;
28
29
/*
30
*--------------------------------------------------------------
31
*
32
* Tk_GetAnchor --
33
*
34
* Given a string, return the corresponding Tk_Anchor.
35
*
36
* Results:
37
* The return value is a standard Tcl return result. If
38
* TCL_OK is returned, then everything went well and the
39
* position is stored at *anchorPtr; otherwise TCL_ERROR
40
* is returned and an error message is left in
41
* interp->result.
42
*
43
* Side effects:
44
* None.
45
*
46
*--------------------------------------------------------------
47
*/
48
49
int
50
Tk_GetAnchor(interp, string, anchorPtr)
51
Tcl_Interp *interp; /* Use this for error reporting. */
52
char *string; /* String describing a direction. */
53
Tk_Anchor *anchorPtr; /* Where to store Tk_Anchor corresponding
54
* to string. */
55
{
56
switch (string[0]) {
57
case 'n':
58
if (string[1] == 0) {
59
*anchorPtr = TK_ANCHOR_N;
60
return TCL_OK;
61
} else if ((string[1] == 'e') && (string[2] == 0)) {
62
*anchorPtr = TK_ANCHOR_NE;
63
return TCL_OK;
64
} else if ((string[1] == 'w') && (string[2] == 0)) {
65
*anchorPtr = TK_ANCHOR_NW;
66
return TCL_OK;
67
}
68
goto error;
69
case 's':
70
if (string[1] == 0) {
71
*anchorPtr = TK_ANCHOR_S;
72
return TCL_OK;
73
} else if ((string[1] == 'e') && (string[2] == 0)) {
74
*anchorPtr = TK_ANCHOR_SE;
75
return TCL_OK;
76
} else if ((string[1] == 'w') && (string[2] == 0)) {
77
*anchorPtr = TK_ANCHOR_SW;
78
return TCL_OK;
79
} else {
80
goto error;
81
}
82
case 'e':
83
if (string[1] == 0) {
84
*anchorPtr = TK_ANCHOR_E;
85
return TCL_OK;
86
}
87
goto error;
88
case 'w':
89
if (string[1] == 0) {
90
*anchorPtr = TK_ANCHOR_W;
91
return TCL_OK;
92
}
93
goto error;
94
case 'c':
95
if (strncmp(string, "center", strlen(string)) == 0) {
96
*anchorPtr = TK_ANCHOR_CENTER;
97
return TCL_OK;
98
}
99
goto error;
100
}
101
102
error:
103
Tcl_AppendResult(interp, "bad anchor position \"", string,
104
"\": must be n, ne, e, se, s, sw, w, nw, or center",
105
(char *) NULL);
106
return TCL_ERROR;
107
}
108
109
/*
110
*--------------------------------------------------------------
111
*
112
* Tk_NameOfAnchor --
113
*
114
* Given a Tk_Anchor, return the string that corresponds
115
* to it.
116
*
117
* Results:
118
* None.
119
*
120
* Side effects:
121
* None.
122
*
123
*--------------------------------------------------------------
124
*/
125
126
char *
127
Tk_NameOfAnchor(anchor)
128
Tk_Anchor anchor; /* Anchor for which identifying string
129
* is desired. */
130
{
131
switch (anchor) {
132
case TK_ANCHOR_N: return "n";
133
case TK_ANCHOR_NE: return "ne";
134
case TK_ANCHOR_E: return "e";
135
case TK_ANCHOR_SE: return "se";
136
case TK_ANCHOR_S: return "s";
137
case TK_ANCHOR_SW: return "sw";
138
case TK_ANCHOR_W: return "w";
139
case TK_ANCHOR_NW: return "nw";
140
case TK_ANCHOR_CENTER: return "center";
141
}
142
return "unknown anchor position";
143
}
144
145
/*
146
*--------------------------------------------------------------
147
*
148
* Tk_GetJoinStyle --
149
*
150
* Given a string, return the corresponding Tk_JoinStyle.
151
*
152
* Results:
153
* The return value is a standard Tcl return result. If
154
* TCL_OK is returned, then everything went well and the
155
* justification is stored at *joinPtr; otherwise
156
* TCL_ERROR is returned and an error message is left in
157
* interp->result.
158
*
159
* Side effects:
160
* None.
161
*
162
*--------------------------------------------------------------
163
*/
164
165
int
166
Tk_GetJoinStyle(interp, string, joinPtr)
167
Tcl_Interp *interp; /* Use this for error reporting. */
168
char *string; /* String describing a justification style. */
169
int *joinPtr; /* Where to store join style corresponding
170
* to string. */
171
{
172
int c;
173
size_t length;
174
175
c = string[0];
176
length = strlen(string);
177
178
if ((c == 'b') && (strncmp(string, "bevel", length) == 0)) {
179
*joinPtr = JoinBevel;
180
return TCL_OK;
181
}
182
if ((c == 'm') && (strncmp(string, "miter", length) == 0)) {
183
*joinPtr = JoinMiter;
184
return TCL_OK;
185
}
186
if ((c == 'r') && (strncmp(string, "round", length) == 0)) {
187
*joinPtr = JoinRound;
188
return TCL_OK;
189
}
190
191
Tcl_AppendResult(interp, "bad join style \"", string,
192
"\": must be bevel, miter, or round",
193
(char *) NULL);
194
return TCL_ERROR;
195
}
196
197
/*
198
*--------------------------------------------------------------
199
*
200
* Tk_NameOfJoinStyle --
201
*
202
* Given a Tk_JoinStyle, return the string that corresponds
203
* to it.
204
*
205
* Results:
206
* None.
207
*
208
* Side effects:
209
* None.
210
*
211
*--------------------------------------------------------------
212
*/
213
214
char *
215
Tk_NameOfJoinStyle(join)
216
int join; /* Join style for which identifying string
217
* is desired. */
218
{
219
switch (join) {
220
case JoinBevel: return "bevel";
221
case JoinMiter: return "miter";
222
case JoinRound: return "round";
223
}
224
return "unknown join style";
225
}
226
227
/*
228
*--------------------------------------------------------------
229
*
230
* Tk_GetCapStyle --
231
*
232
* Given a string, return the corresponding Tk_CapStyle.
233
*
234
* Results:
235
* The return value is a standard Tcl return result. If
236
* TCL_OK is returned, then everything went well and the
237
* justification is stored at *capPtr; otherwise
238
* TCL_ERROR is returned and an error message is left in
239
* interp->result.
240
*
241
* Side effects:
242
* None.
243
*
244
*--------------------------------------------------------------
245
*/
246
247
int
248
Tk_GetCapStyle(interp, string, capPtr)
249
Tcl_Interp *interp; /* Use this for error reporting. */
250
char *string; /* String describing a justification style. */
251
int *capPtr; /* Where to store cap style corresponding
252
* to string. */
253
{
254
int c;
255
size_t length;
256
257
c = string[0];
258
length = strlen(string);
259
260
if ((c == 'b') && (strncmp(string, "butt", length) == 0)) {
261
*capPtr = CapButt;
262
return TCL_OK;
263
}
264
if ((c == 'p') && (strncmp(string, "projecting", length) == 0)) {
265
*capPtr = CapProjecting;
266
return TCL_OK;
267
}
268
if ((c == 'r') && (strncmp(string, "round", length) == 0)) {
269
*capPtr = CapRound;
270
return TCL_OK;
271
}
272
273
Tcl_AppendResult(interp, "bad cap style \"", string,
274
"\": must be butt, projecting, or round",
275
(char *) NULL);
276
return TCL_ERROR;
277
}
278
279
/*
280
*--------------------------------------------------------------
281
*
282
* Tk_NameOfCapStyle --
283
*
284
* Given a Tk_CapStyle, return the string that corresponds
285
* to it.
286
*
287
* Results:
288
* None.
289
*
290
* Side effects:
291
* None.
292
*
293
*--------------------------------------------------------------
294
*/
295
296
char *
297
Tk_NameOfCapStyle(cap)
298
int cap; /* Cap style for which identifying string
299
* is desired. */
300
{
301
switch (cap) {
302
case CapButt: return "butt";
303
case CapProjecting: return "projecting";
304
case CapRound: return "round";
305
}
306
return "unknown cap style";
307
}
308
309
/*
310
*--------------------------------------------------------------
311
*
312
* Tk_GetJustify --
313
*
314
* Given a string, return the corresponding Tk_Justify.
315
*
316
* Results:
317
* The return value is a standard Tcl return result. If
318
* TCL_OK is returned, then everything went well and the
319
* justification is stored at *justifyPtr; otherwise
320
* TCL_ERROR is returned and an error message is left in
321
* interp->result.
322
*
323
* Side effects:
324
* None.
325
*
326
*--------------------------------------------------------------
327
*/
328
329
int
330
Tk_GetJustify(interp, string, justifyPtr)
331
Tcl_Interp *interp; /* Use this for error reporting. */
332
char *string; /* String describing a justification style. */
333
Tk_Justify *justifyPtr; /* Where to store Tk_Justify corresponding
334
* to string. */
335
{
336
int c;
337
size_t length;
338
339
c = string[0];
340
length = strlen(string);
341
342
if ((c == 'l') && (strncmp(string, "left", length) == 0)) {
343
*justifyPtr = TK_JUSTIFY_LEFT;
344
return TCL_OK;
345
}
346
if ((c == 'r') && (strncmp(string, "right", length) == 0)) {
347
*justifyPtr = TK_JUSTIFY_RIGHT;
348
return TCL_OK;
349
}
350
if ((c == 'c') && (strncmp(string, "center", length) == 0)) {
351
*justifyPtr = TK_JUSTIFY_CENTER;
352
return TCL_OK;
353
}
354
355
Tcl_AppendResult(interp, "bad justification \"", string,
356
"\": must be left, right, or center",
357
(char *) NULL);
358
return TCL_ERROR;
359
}
360
361
/*
362
*--------------------------------------------------------------
363
*
364
* Tk_NameOfJustify --
365
*
366
* Given a Tk_Justify, return the string that corresponds
367
* to it.
368
*
369
* Results:
370
* None.
371
*
372
* Side effects:
373
* None.
374
*
375
*--------------------------------------------------------------
376
*/
377
378
char *
379
Tk_NameOfJustify(justify)
380
Tk_Justify justify; /* Justification style for which
381
* identifying string is desired. */
382
{
383
switch (justify) {
384
case TK_JUSTIFY_LEFT: return "left";
385
case TK_JUSTIFY_RIGHT: return "right";
386
case TK_JUSTIFY_CENTER: return "center";
387
}
388
return "unknown justification style";
389
}
390
391
/*
392
*----------------------------------------------------------------------
393
*
394
* Tk_GetUid --
395
*
396
* Given a string, this procedure returns a unique identifier
397
* for the string.
398
*
399
* Results:
400
* This procedure returns a Tk_Uid corresponding to the "string"
401
* argument. The Tk_Uid has a string value identical to string
402
* (strcmp will return 0), but it's guaranteed that any other
403
* calls to this procedure with a string equal to "string" will
404
* return exactly the same result (i.e. can compare Tk_Uid
405
* *values* directly, without having to call strcmp on what they
406
* point to).
407
*
408
* Side effects:
409
* New information may be entered into the identifier table.
410
*
411
*----------------------------------------------------------------------
412
*/
413
414
Tk_Uid
415
Tk_GetUid(string)
416
char *string; /* String to convert. */
417
{
418
int dummy;
419
420
if (!initialized) {
421
Tcl_InitHashTable(&uidTable, TCL_STRING_KEYS);
422
initialized = 1;
423
}
424
return (Tk_Uid) Tcl_GetHashKey(&uidTable,
425
Tcl_CreateHashEntry(&uidTable, string, &dummy));
426
}
427
428
/*
429
*--------------------------------------------------------------
430
*
431
* Tk_GetScreenMM --
432
*
433
* Given a string, returns the number of screen millimeters
434
* corresponding to that string.
435
*
436
* Results:
437
* The return value is a standard Tcl return result. If
438
* TCL_OK is returned, then everything went well and the
439
* screen distance is stored at *doublePtr; otherwise
440
* TCL_ERROR is returned and an error message is left in
441
* interp->result.
442
*
443
* Side effects:
444
* None.
445
*
446
*--------------------------------------------------------------
447
*/
448
449
int
450
Tk_GetScreenMM(interp, tkwin, string, doublePtr)
451
Tcl_Interp *interp; /* Use this for error reporting. */
452
Tk_Window tkwin; /* Window whose screen determines conversion
453
* from centimeters and other absolute
454
* units. */
455
char *string; /* String describing a screen distance. */
456
double *doublePtr; /* Place to store converted result. */
457
{
458
char *end;
459
double d;
460
461
d = strtod(string, &end);
462
if (end == string) {
463
error:
464
Tcl_AppendResult(interp, "bad screen distance \"", string,
465
"\"", (char *) NULL);
466
return TCL_ERROR;
467
}
468
while ((*end != '\0') && isspace(UCHAR(*end))) {
469
end++;
470
}
471
switch (*end) {
472
case 0:
473
d /= WidthOfScreen(Tk_Screen(tkwin));
474
d *= WidthMMOfScreen(Tk_Screen(tkwin));
475
break;
476
case 'c':
477
d *= 10;
478
end++;
479
break;
480
case 'i':
481
d *= 25.4;
482
end++;
483
break;
484
case 'm':
485
end++;
486
break;
487
case 'p':
488
d *= 25.4/72.0;
489
end++;
490
break;
491
default:
492
goto error;
493
}
494
while ((*end != '\0') && isspace(UCHAR(*end))) {
495
end++;
496
}
497
if (*end != 0) {
498
goto error;
499
}
500
*doublePtr = d;
501
return TCL_OK;
502
}
503
504
/*
505
*--------------------------------------------------------------
506
*
507
* Tk_GetPixels --
508
*
509
* Given a string, returns the number of pixels corresponding
510
* to that string.
511
*
512
* Results:
513
* The return value is a standard Tcl return result. If
514
* TCL_OK is returned, then everything went well and the
515
* rounded pixel distance is stored at *intPtr; otherwise
516
* TCL_ERROR is returned and an error message is left in
517
* interp->result.
518
*
519
* Side effects:
520
* None.
521
*
522
*--------------------------------------------------------------
523
*/
524
525
int
526
Tk_GetPixels(interp, tkwin, string, intPtr)
527
Tcl_Interp *interp; /* Use this for error reporting. */
528
Tk_Window tkwin; /* Window whose screen determines conversion
529
* from centimeters and other absolute
530
* units. */
531
char *string; /* String describing a justification style. */
532
int *intPtr; /* Place to store converted result. */
533
{
534
char *end;
535
double d;
536
537
d = strtod(string, &end);
538
if (end == string) {
539
error:
540
Tcl_AppendResult(interp, "bad screen distance \"", string,
541
"\"", (char *) NULL);
542
return TCL_ERROR;
543
}
544
while ((*end != '\0') && isspace(UCHAR(*end))) {
545
end++;
546
}
547
switch (*end) {
548
case 0:
549
break;
550
case 'c':
551
d *= 10*WidthOfScreen(Tk_Screen(tkwin));
552
d /= WidthMMOfScreen(Tk_Screen(tkwin));
553
end++;
554
break;
555
case 'i':
556
d *= 25.4*WidthOfScreen(Tk_Screen(tkwin));
557
d /= WidthMMOfScreen(Tk_Screen(tkwin));
558
end++;
559
break;
560
case 'm':
561
d *= WidthOfScreen(Tk_Screen(tkwin));
562
d /= WidthMMOfScreen(Tk_Screen(tkwin));
563
end++;
564
break;
565
case 'p':
566
d *= (25.4/72.0)*WidthOfScreen(Tk_Screen(tkwin));
567
d /= WidthMMOfScreen(Tk_Screen(tkwin));
568
end++;
569
break;
570
default:
571
goto error;
572
}
573
while ((*end != '\0') && isspace(UCHAR(*end))) {
574
end++;
575
}
576
if (*end != 0) {
577
goto error;
578
}
579
if (d < 0) {
580
*intPtr = (int) (d - 0.5);
581
} else {
582
*intPtr = (int) (d + 0.5);
583
}
584
return TCL_OK;
585
}
586
587