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
10 * Copyright (c) 1991-1994 The Regents of the University of California.
11 * Copyright (c) 1994-1995 Sun Microsystems, Inc.
13 * See the file "license.terms" for information on usage and redistribution
14 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
23 * The hash table below is used to keep track of all the Tk_Uids created
27 static Tcl_HashTable uidTable;
28 static int initialized = 0;
31 *--------------------------------------------------------------
35 * Given a string, return the corresponding Tk_Anchor.
38 * The return value is a standard Tcl return result. If
39 * TCL_OK is returned, then everything went well and the
40 * position is stored at *anchorPtr; otherwise TCL_ERROR
41 * is returned and an error message is left in
47 *--------------------------------------------------------------
51 Tk_GetAnchor(interp, string, anchorPtr)
52 Tcl_Interp *interp; /* Use this for error reporting. */
53 char *string; /* String describing a direction. */
54 Tk_Anchor *anchorPtr; /* Where to store Tk_Anchor corresponding
60 *anchorPtr = TK_ANCHOR_N;
62 } else if ((string[1] == 'e') && (string[2] == 0)) {
63 *anchorPtr = TK_ANCHOR_NE;
65 } else if ((string[1] == 'w') && (string[2] == 0)) {
66 *anchorPtr = TK_ANCHOR_NW;
72 *anchorPtr = TK_ANCHOR_S;
74 } else if ((string[1] == 'e') && (string[2] == 0)) {
75 *anchorPtr = TK_ANCHOR_SE;
77 } else if ((string[1] == 'w') && (string[2] == 0)) {
78 *anchorPtr = TK_ANCHOR_SW;
85 *anchorPtr = TK_ANCHOR_E;
91 *anchorPtr = TK_ANCHOR_W;
96 if (strncmp(string, "center", strlen(string)) == 0) {
97 *anchorPtr = TK_ANCHOR_CENTER;
104 Tcl_AppendResult(interp, "bad anchor position \"", string,
105 "\": must be n, ne, e, se, s, sw, w, nw, or center",
111 *--------------------------------------------------------------
115 * Given a Tk_Anchor, return the string that corresponds
124 *--------------------------------------------------------------
128 Tk_NameOfAnchor(anchor)
129 Tk_Anchor anchor; /* Anchor for which identifying string
133 case TK_ANCHOR_N: return "n";
134 case TK_ANCHOR_NE: return "ne";
135 case TK_ANCHOR_E: return "e";
136 case TK_ANCHOR_SE: return "se";
137 case TK_ANCHOR_S: return "s";
138 case TK_ANCHOR_SW: return "sw";
139 case TK_ANCHOR_W: return "w";
140 case TK_ANCHOR_NW: return "nw";
141 case TK_ANCHOR_CENTER: return "center";
143 return "unknown anchor position";
147 *--------------------------------------------------------------
151 * Given a string, return the corresponding Tk_JoinStyle.
154 * The return value is a standard Tcl return result. If
155 * TCL_OK is returned, then everything went well and the
156 * justification is stored at *joinPtr; otherwise
157 * TCL_ERROR is returned and an error message is left in
163 *--------------------------------------------------------------
167 Tk_GetJoinStyle(interp, string, joinPtr)
168 Tcl_Interp *interp; /* Use this for error reporting. */
169 char *string; /* String describing a justification style. */
170 int *joinPtr; /* Where to store join style corresponding
177 length = strlen(string);
179 if ((c == 'b') && (strncmp(string, "bevel", length) == 0)) {
180 *joinPtr = JoinBevel;
183 if ((c == 'm') && (strncmp(string, "miter", length) == 0)) {
184 *joinPtr = JoinMiter;
187 if ((c == 'r') && (strncmp(string, "round", length) == 0)) {
188 *joinPtr = JoinRound;
192 Tcl_AppendResult(interp, "bad join style \"", string,
193 "\": must be bevel, miter, or round",
199 *--------------------------------------------------------------
201 * Tk_NameOfJoinStyle --
203 * Given a Tk_JoinStyle, return the string that corresponds
212 *--------------------------------------------------------------
216 Tk_NameOfJoinStyle(join)
217 int join; /* Join style for which identifying string
221 case JoinBevel: return "bevel";
222 case JoinMiter: return "miter";
223 case JoinRound: return "round";
225 return "unknown join style";
229 *--------------------------------------------------------------
233 * Given a string, return the corresponding Tk_CapStyle.
236 * The return value is a standard Tcl return result. If
237 * TCL_OK is returned, then everything went well and the
238 * justification is stored at *capPtr; otherwise
239 * TCL_ERROR is returned and an error message is left in
245 *--------------------------------------------------------------
249 Tk_GetCapStyle(interp, string, capPtr)
250 Tcl_Interp *interp; /* Use this for error reporting. */
251 char *string; /* String describing a justification style. */
252 int *capPtr; /* Where to store cap style corresponding
259 length = strlen(string);
261 if ((c == 'b') && (strncmp(string, "butt", length) == 0)) {
265 if ((c == 'p') && (strncmp(string, "projecting", length) == 0)) {
266 *capPtr = CapProjecting;
269 if ((c == 'r') && (strncmp(string, "round", length) == 0)) {
274 Tcl_AppendResult(interp, "bad cap style \"", string,
275 "\": must be butt, projecting, or round",
281 *--------------------------------------------------------------
283 * Tk_NameOfCapStyle --
285 * Given a Tk_CapStyle, return the string that corresponds
294 *--------------------------------------------------------------
298 Tk_NameOfCapStyle(cap)
299 int cap; /* Cap style for which identifying string
303 case CapButt: return "butt";
304 case CapProjecting: return "projecting";
305 case CapRound: return "round";
307 return "unknown cap style";
311 *--------------------------------------------------------------
315 * Given a string, return the corresponding Tk_Justify.
318 * The return value is a standard Tcl return result. If
319 * TCL_OK is returned, then everything went well and the
320 * justification is stored at *justifyPtr; otherwise
321 * TCL_ERROR is returned and an error message is left in
327 *--------------------------------------------------------------
331 Tk_GetJustify(interp, string, justifyPtr)
332 Tcl_Interp *interp; /* Use this for error reporting. */
333 char *string; /* String describing a justification style. */
334 Tk_Justify *justifyPtr; /* Where to store Tk_Justify corresponding
341 length = strlen(string);
343 if ((c == 'l') && (strncmp(string, "left", length) == 0)) {
344 *justifyPtr = TK_JUSTIFY_LEFT;
347 if ((c == 'r') && (strncmp(string, "right", length) == 0)) {
348 *justifyPtr = TK_JUSTIFY_RIGHT;
351 if ((c == 'c') && (strncmp(string, "center", length) == 0)) {
352 *justifyPtr = TK_JUSTIFY_CENTER;
356 Tcl_AppendResult(interp, "bad justification \"", string,
357 "\": must be left, right, or center",
363 *--------------------------------------------------------------
365 * Tk_NameOfJustify --
367 * Given a Tk_Justify, return the string that corresponds
376 *--------------------------------------------------------------
380 Tk_NameOfJustify(justify)
381 Tk_Justify justify; /* Justification style for which
382 * identifying string is desired. */
385 case TK_JUSTIFY_LEFT: return "left";
386 case TK_JUSTIFY_RIGHT: return "right";
387 case TK_JUSTIFY_CENTER: return "center";
389 return "unknown justification style";
393 *----------------------------------------------------------------------
397 * Given a string, this procedure returns a unique identifier
401 * This procedure returns a Tk_Uid corresponding to the "string"
402 * argument. The Tk_Uid has a string value identical to string
403 * (strcmp will return 0), but it's guaranteed that any other
404 * calls to this procedure with a string equal to "string" will
405 * return exactly the same result (i.e. can compare Tk_Uid
406 * *values* directly, without having to call strcmp on what they
410 * New information may be entered into the identifier table.
412 *----------------------------------------------------------------------
417 CONST char *string; /* String to convert. */
422 Tcl_InitHashTable(&uidTable, TCL_STRING_KEYS);
425 return (Tk_Uid) Tcl_GetHashKey(&uidTable,
426 Tcl_CreateHashEntry(&uidTable, string, &dummy));
430 *--------------------------------------------------------------
434 * Given a string, returns the number of screen millimeters
435 * corresponding to that string.
438 * The return value is a standard Tcl return result. If
439 * TCL_OK is returned, then everything went well and the
440 * screen distance is stored at *doublePtr; otherwise
441 * TCL_ERROR is returned and an error message is left in
447 *--------------------------------------------------------------
451 Tk_GetScreenMM(interp, tkwin, string, doublePtr)
452 Tcl_Interp *interp; /* Use this for error reporting. */
453 Tk_Window tkwin; /* Window whose screen determines conversion
454 * from centimeters and other absolute
456 char *string; /* String describing a screen distance. */
457 double *doublePtr; /* Place to store converted result. */
462 d = strtod(string, &end);
465 Tcl_AppendResult(interp, "bad screen distance \"", string,
466 "\"", (char *) NULL);
469 while ((*end != '\0') && isspace(UCHAR(*end))) {
474 d /= WidthOfScreen(Tk_Screen(tkwin));
475 d *= WidthMMOfScreen(Tk_Screen(tkwin));
495 while ((*end != '\0') && isspace(UCHAR(*end))) {
506 *--------------------------------------------------------------
510 * Given a string, returns the number of pixels corresponding
514 * The return value is a standard Tcl return result. If
515 * TCL_OK is returned, then everything went well and the
516 * rounded pixel distance is stored at *intPtr; otherwise
517 * TCL_ERROR is returned and an error message is left in
523 *--------------------------------------------------------------
527 Tk_GetPixels(interp, tkwin, string, intPtr)
528 Tcl_Interp *interp; /* Use this for error reporting. */
529 Tk_Window tkwin; /* Window whose screen determines conversion
530 * from centimeters and other absolute
532 char *string; /* String describing a justification style. */
533 int *intPtr; /* Place to store converted result. */
538 d = strtod(string, &end);
541 Tcl_AppendResult(interp, "bad screen distance \"", string,
542 "\"", (char *) NULL);
545 while ((*end != '\0') && isspace(UCHAR(*end))) {
552 d *= 10*WidthOfScreen(Tk_Screen(tkwin));
553 d /= WidthMMOfScreen(Tk_Screen(tkwin));
557 d *= 25.4*WidthOfScreen(Tk_Screen(tkwin));
558 d /= WidthMMOfScreen(Tk_Screen(tkwin));
562 d *= WidthOfScreen(Tk_Screen(tkwin));
563 d /= WidthMMOfScreen(Tk_Screen(tkwin));
567 d *= (25.4/72.0)*WidthOfScreen(Tk_Screen(tkwin));
568 d /= WidthMMOfScreen(Tk_Screen(tkwin));
574 while ((*end != '\0') && isspace(UCHAR(*end))) {
581 *intPtr = (int) (d - 0.5);
583 *intPtr = (int) (d + 0.5);