Debian Codegraph @

pTk/mTk/generic/tkUtil.c

raw symbols SCIP
1 /*
2 * tkUtil.c --
3 *
4 * This file contains miscellaneous utility procedures that
5 * are used by the rest of Tk, such as a procedure for drawing
6 * a focus highlight.
7 *
8 * Copyright (c) 1994 The Regents of the University of California.
9 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
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 * RCS: @(#) $Id: tkUtil.c,v 1.12 2002/08/05 04:30:40 dgp Exp $
15 */
16
17 #include "tkInt.h"
18 #include "tkPort.h"
19
20 /*
21 * The structure below defines the implementation of the "statekey"
22 * Tcl object, used for quickly finding a mapping in a TkStateMap.
23 */
24
25 Tcl_ObjType tkStateKeyObjType = {
26 "statekey", /* name */
27 (Tcl_FreeInternalRepProc *) NULL, /* freeIntRepProc */
28 (Tcl_DupInternalRepProc *) NULL, /* dupIntRepProc */
29 (Tcl_UpdateStringProc *) NULL, /* updateStringProc */
30 (Tcl_SetFromAnyProc *) NULL /* setFromAnyProc */
31 };
32
33
34 /*
35 *--------------------------------------------------------------
36 *
37 * TkStateParseProc --
38 *
39 * This procedure is invoked during option processing to handle
40 * the "-state" and "-default" options.
41 *
42 * Results:
43 * A standard Tcl return value.
44 *
45 * Side effects:
46 * The state for a given item gets replaced by the state
47 * indicated in the value argument.
48 *
49 *--------------------------------------------------------------
50 */
51
52 int
53 Tk_StateParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
54 ClientData clientData; /* some flags.*/
55 Tcl_Interp *interp; /* Used for reporting errors. */
56 Tk_Window tkwin; /* Window containing canvas widget. */
57 Tcl_Obj * ovalue; /* Value of option. */
58 char *widgRec; /* Pointer to record for item. */
59 int offset; /* Offset into item. */
60 {
61 int c;
62 int flags = (int)clientData;
63 size_t length;
64 char *value = Tcl_GetString(ovalue);
65
66 register Tk_State *statePtr = (Tk_State *) (widgRec + offset);
67
68 if(value == NULL || *value == 0) {
69 *statePtr = TK_STATE_NULL;
70 return TCL_OK;
71 }
72
73 c = value[0];
74 length = strlen(value);
75
76 if ((c == 'n') && (strncmp(value, "normal", length) == 0)) {
77 *statePtr = TK_STATE_NORMAL;
78 return TCL_OK;
79 }
80 if ((c == 'd') && (strncmp(value, "disabled", length) == 0)) {
81 *statePtr = TK_STATE_DISABLED;
82 return TCL_OK;
83 }
84 if ((c == 'a') && (flags&1) && (strncmp(value, "active", length) == 0)) {
85 *statePtr = TK_STATE_ACTIVE;
86 return TCL_OK;
87 }
88 if ((c == 'h') && (flags&2) && (strncmp(value, "hidden", length) == 0)) {
89 *statePtr = TK_STATE_HIDDEN;
90 return TCL_OK;
91 }
92
93 Tcl_AppendResult(interp, "bad ", (flags&4)?"-default" : "state",
94 " value \"", value, "\": must be normal",
95 (char *) NULL);
96 if (flags&1) {
97 Tcl_AppendResult(interp, ", active",(char *) NULL);
98 }
99 if (flags&2) {
100 Tcl_AppendResult(interp, ", hidden",(char *) NULL);
101 }
102 if (flags&3) {
103 Tcl_AppendResult(interp, ",",(char *) NULL);
104 }
105 Tcl_AppendResult(interp, " or disabled",(char *) NULL);
106 *statePtr = TK_STATE_NORMAL;
107 return TCL_ERROR;
108 }
109
110 /*
111 *--------------------------------------------------------------
112 *
113 * TkStatePrintProc --
114 *
115 * This procedure is invoked by the Tk configuration code
116 * to produce a printable string for the "-state"
117 * configuration option.
118 *
119 * Results:
120 * The return value is a string describing the state for
121 * the item referred to by "widgRec". In addition, *freeProcPtr
122 * is filled in with the address of a procedure to call to free
123 * the result string when it's no longer needed (or NULL to
124 * indicate that the string doesn't need to be freed).
125 *
126 * Side effects:
127 * None.
128 *
129 *--------------------------------------------------------------
130 */
131
132 Tcl_Obj *
133 Tk_StatePrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
134 ClientData clientData; /* Ignored. */
135 Tk_Window tkwin; /* Window containing canvas widget. */
136 char *widgRec; /* Pointer to record for item. */
137 int offset; /* Offset into item. */
138 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
139 * information about how to reclaim
140 * storage for return string. */
141 {
142 register Tk_State *statePtr = (Tk_State *) (widgRec + offset);
143
144 if (*statePtr==TK_STATE_NORMAL) {
145 return LangStringArg("normal");
146 } else if (*statePtr==TK_STATE_DISABLED) {
147 return LangStringArg("disabled");
148 } else if (*statePtr==TK_STATE_HIDDEN) {
149 return LangStringArg("hidden");
150 } else if (*statePtr==TK_STATE_ACTIVE) {
151 return LangStringArg("active");
152 } else {
153 return LangStringArg("");
154 }
155 }
156
157 /*
158 *--------------------------------------------------------------
159 *
160 * TkOrientParseProc --
161 *
162 * This procedure is invoked during option processing to handle
163 * the "-orient" option.
164 *
165 * Results:
166 * A standard Tcl return value.
167 *
168 * Side effects:
169 * The orientation for a given item gets replaced by the orientation
170 * indicated in the value argument.
171 *
172 *--------------------------------------------------------------
173 */
174
175 int
176 TkOrientParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
177 ClientData clientData; /* some flags.*/
178 Tcl_Interp *interp; /* Used for reporting errors. */
179 Tk_Window tkwin; /* Window containing canvas widget. */
180 Tcl_Obj * ovalue; /* Value of option. */
181 char *widgRec; /* Pointer to record for item. */
182 int offset; /* Offset into item. */
183 {
184 int c;
185 size_t length;
186 char *value = Tcl_GetString(ovalue);
187
188 register int *orientPtr = (int *) (widgRec + offset);
189
190 if(value == NULL || *value == 0) {
191 *orientPtr = 0;
192 return TCL_OK;
193 }
194
195 c = value[0];
196 length = strlen(value);
197
198 if ((c == 'h') && (strncmp(value, "horizontal", length) == 0)) {
199 *orientPtr = 0;
200 return TCL_OK;
201 }
202 if ((c == 'v') && (strncmp(value, "vertical", length) == 0)) {
203 *orientPtr = 1;
204 return TCL_OK;
205 }
206 Tcl_AppendResult(interp, "bad orientation \"", value,
207 "\": must be vertical or horizontal",
208 (char *) NULL);
209 *orientPtr = 0;
210 return TCL_ERROR;
211 }
212
213 /*
214 *--------------------------------------------------------------
215 *
216 * TkOrientPrintProc --
217 *
218 * This procedure is invoked by the Tk configuration code
219 * to produce a printable string for the "-orient"
220 * configuration option.
221 *
222 * Results:
223 * The return value is a string describing the orientation for
224 * the item referred to by "widgRec". In addition, *freeProcPtr
225 * is filled in with the address of a procedure to call to free
226 * the result string when it's no longer needed (or NULL to
227 * indicate that the string doesn't need to be freed).
228 *
229 * Side effects:
230 * None.
231 *
232 *--------------------------------------------------------------
233 */
234
235 Tcl_Obj *
236 TkOrientPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
237 ClientData clientData; /* Ignored. */
238 Tk_Window tkwin; /* Window containing canvas widget. */
239 char *widgRec; /* Pointer to record for item. */
240 int offset; /* Offset into item. */
241 Tcl_FreeProc **freeProcPtr; /* Pointer to variable to fill in with
242 * information about how to reclaim
243 * storage for return string. */
244 {
245 register int *statePtr = (int *) (widgRec + offset);
246
247 if (*statePtr) {
248 return LangStringArg("vertical");
249 } else {
250 return LangStringArg("horizontal");
251 }
252 }
253
254 /*
255 *----------------------------------------------------------------------
256 *
257 * Tk_TileParseProc --
258 *
259 * Converts the name of an image into a tile.
260 *
261 *----------------------------------------------------------------------
262 */
263
264 int
265 TkTileParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
266 ClientData clientData; /* not used */
267 Tcl_Interp *interp; /* Interpreter to send results back to */
268 Tk_Window tkwin; /* Window on same display as tile */
269 Tcl_Obj * ovalue; /* Name of image */
270 char *widgRec; /* Widget structure record */
271 int offset; /* Offset of tile in record */
272 {
273 Tk_Tile *tilePtr = (Tk_Tile *)(widgRec + offset);
274 Tk_Tile tile, lastTile;
275 char *value = Tcl_GetString(ovalue);
276
277 lastTile = *tilePtr;
278 tile = NULL;
279 if ((value != NULL) && (*value != '\0')) {
280 tile = Tk_GetTile(interp, tkwin, value);
281 if (tile == NULL) {
282 return TCL_ERROR;
283 }
284 }
285 if (lastTile != NULL) {
286 Tk_FreeTile(lastTile);
287 }
288 *tilePtr = tile;
289 return TCL_OK;
290 }
291
292
293 /*
294 *----------------------------------------------------------------------
295 *
296 * Tk_TilePrintProc --
297 *
298 * Returns the name of the tile.
299 *
300 * Results:
301 * The name of the tile is returned.
302 *
303 *----------------------------------------------------------------------
304 */
305
306 Tcl_Obj *
307 TkTilePrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
308 ClientData clientData; /* not used */
309 Tk_Window tkwin; /* not used */
310 char *widgRec; /* Widget structure record */
311 int offset; /* Offset of tile in record */
312 Tcl_FreeProc **freeProcPtr; /* not used */
313 {
314 Tk_Tile tile = *(Tk_Tile *)(widgRec + offset);
315
316 return LangStringArg(Tk_NameOfTile(tile));
317 }
318 /*
319 *----------------------------------------------------------------------
320 *
321 * TkOffsetParseProc --
322 *
323 * Converts the offset of a stipple or tile into the Tk_TSOffset structure.
324 *
325 *----------------------------------------------------------------------
326 */
327
328 int
329 TkOffsetParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
330 ClientData clientData; /* not used */
331 Tcl_Interp *interp; /* Interpreter to send results back to */
332 Tk_Window tkwin; /* Window on same display as tile */
333 Tcl_Obj * ovalue; /* Name of image */
334 char *widgRec; /* Widget structure record */
335 int offset; /* Offset of tile in record */
336 {
337 Tk_TSOffset *offsetPtr = (Tk_TSOffset *)(widgRec + offset);
338 Tk_TSOffset tsoffset;
339 CONST char *q, *p;
340 int result;
341 Tcl_Obj **args;
342 int argc;
343 char *value = NULL;
344 if (Tcl_ListObjGetElements(NULL,ovalue,&argc,&args) != TCL_OK) {
345 goto badTSOffset;
346 }
347 if (argc > 0) {
348 value = Tcl_GetString(args[0]);
349 }
350 if ((value == NULL) || (*value == 0)) {
351 tsoffset.flags = TK_OFFSET_CENTER|TK_OFFSET_MIDDLE;
352 goto goodTSOffset;
353 }
354 tsoffset.flags = 0;
355 p = value;
356
357 switch(value[0]) {
358 case '#':
359 if (((int)clientData) & TK_OFFSET_RELATIVE) {
360 tsoffset.flags = TK_OFFSET_RELATIVE;
361 argc--;
362 args++;
363 break;
364 }
365 goto badTSOffset;
366 case 'e':
367 switch(value[1]) {
368 case '\0':
369 tsoffset.flags = TK_OFFSET_RIGHT|TK_OFFSET_MIDDLE;
370 goto goodTSOffset;
371 case 'n':
372 if (value[2]!='d' || value[3]!='\0') {goto badTSOffset;}
373 tsoffset.flags = INT_MAX;
374 goto goodTSOffset;
375 }
376 case 'w':
377 if (value[1] != '\0') {goto badTSOffset;}
378 tsoffset.flags = TK_OFFSET_LEFT|TK_OFFSET_MIDDLE;
379 goto goodTSOffset;
380 case 'n':
381 if ((value[1] != '\0') && (value[2] != '\0')) {
382 goto badTSOffset;
383 }
384 switch(value[1]) {
385 case '\0': tsoffset.flags = TK_OFFSET_CENTER|TK_OFFSET_TOP;
386 goto goodTSOffset;
387 case 'w': tsoffset.flags = TK_OFFSET_LEFT|TK_OFFSET_TOP;
388 goto goodTSOffset;
389 case 'e': tsoffset.flags = TK_OFFSET_RIGHT|TK_OFFSET_TOP;
390 goto goodTSOffset;
391 }
392 goto badTSOffset;
393 case 's':
394 if ((value[1] != '\0') && (value[2] != '\0')) {
395 goto badTSOffset;
396 }
397 switch(value[1]) {
398 case '\0': tsoffset.flags = TK_OFFSET_CENTER|TK_OFFSET_BOTTOM;
399 goto goodTSOffset;
400 case 'w': tsoffset.flags = TK_OFFSET_LEFT|TK_OFFSET_BOTTOM;
401 goto goodTSOffset;
402 case 'e': tsoffset.flags = TK_OFFSET_RIGHT|TK_OFFSET_BOTTOM;
403 goto goodTSOffset;
404 }
405 goto badTSOffset;
406 case 'c':
407 if (strncmp(value, "center", strlen(value)) != 0) {
408 goto badTSOffset;
409 }
410 tsoffset.flags = TK_OFFSET_CENTER|TK_OFFSET_MIDDLE;
411 goto goodTSOffset;
412 }
413 if (argc == 1) {
414 if (((int)clientData) & TK_OFFSET_INDEX) {
415 if (Tcl_GetIntFromObj(interp, args[0], &tsoffset.flags) != TCL_OK) {
416 Tcl_ResetResult(interp);
417 goto badTSOffset;
418 }
419 tsoffset.flags |= TK_OFFSET_INDEX;
420 goto goodTSOffset;
421 }
422 goto badTSOffset;
423 } else if (argc == 2) {
424 result = Tk_GetPixels(interp, tkwin, Tcl_GetString(args[0]), &tsoffset.xoffset);
425 if (result != TCL_OK) {
426 return TCL_ERROR;
427 }
428 result = Tk_GetPixels(interp, tkwin, Tcl_GetString(args[1]), &tsoffset.yoffset);
429 if (result != TCL_OK) {
430 return TCL_ERROR;
431 }
432 } else {
433 goto badTSOffset;
434 }
435
436 goodTSOffset:
437 /* below is a hack to allow the stipple/tile offset to be stored
438 * in the internal tile structure. Most of the times, offsetPtr
439 * is a pointer to an already existing tile structure. However
440 * if this structure is not already created, we must do it
441 * with Tk_GetTile()!!!!;
442 */
443
444 memcpy(offsetPtr,&tsoffset, sizeof(Tk_TSOffset));
445 return TCL_OK;
446
447 badTSOffset:
448 Tcl_AppendResult(interp, "bad offset \"", value,
449 "\": expected \"x,y\"", (char *) NULL);
450 if (((int) clientData) & TK_OFFSET_RELATIVE) {
451 Tcl_AppendResult(interp, ", \"#x,y\"", (char *) NULL);
452 }
453 if (((int) clientData) & TK_OFFSET_INDEX) {
454 Tcl_AppendResult(interp, ", <index>", (char *) NULL);
455 }
456 Tcl_AppendResult(interp, ", n, ne, e, se, s, sw, w, nw, or center",
457 (char *) NULL);
458 return TCL_ERROR;
459 }
460
461 /*
462 *----------------------------------------------------------------------
463 *
464 * TkOffsetPrintProc --
465 *
466 * Returns the offset of the tile.
467 *
468 * Results:
469 * The offset of the tile is returned.
470 *
471 *----------------------------------------------------------------------
472 */
473
474 Tcl_Obj *
475 TkOffsetPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
476 ClientData clientData; /* not used */
477 Tk_Window tkwin; /* not used */
478 char *widgRec; /* Widget structure record */
479 int offset; /* Offset of tile in record */
480 Tcl_FreeProc **freeProcPtr; /* not used */
481 {
482 Tk_TSOffset *offsetPtr = (Tk_TSOffset *)(widgRec + offset);
483 char *p, *q;
484
485 if ((offsetPtr->flags) & TK_OFFSET_INDEX) {
486 if ((offsetPtr->flags) >= INT_MAX) {
487 return LangStringArg("end");
488 }
489 return Tcl_NewIntObj(offsetPtr->flags & (~TK_OFFSET_INDEX));
490 }
491 if ((offsetPtr->flags) & TK_OFFSET_TOP) {
492 if ((offsetPtr->flags) & TK_OFFSET_LEFT) {
493 return LangStringArg("nw");
494 } else if ((offsetPtr->flags) & TK_OFFSET_CENTER) {
495 return LangStringArg("n");
496 } else if ((offsetPtr->flags) & TK_OFFSET_RIGHT) {
497 return LangStringArg("ne");
498 }
499 } else if ((offsetPtr->flags) & TK_OFFSET_MIDDLE) {
500 if ((offsetPtr->flags) & TK_OFFSET_LEFT) {
501 return LangStringArg("w");
502 } else if ((offsetPtr->flags) & TK_OFFSET_CENTER) {
503 return LangStringArg("center");
504 } else if ((offsetPtr->flags) & TK_OFFSET_RIGHT) {
505 return LangStringArg("e");
506 }
507 } else if ((offsetPtr->flags) & TK_OFFSET_BOTTOM) {
508 if ((offsetPtr->flags) & TK_OFFSET_LEFT) {
509 return LangStringArg("sw");
510 } else if ((offsetPtr->flags) & TK_OFFSET_CENTER) {
511 return LangStringArg("s");
512 } else if ((offsetPtr->flags) & TK_OFFSET_RIGHT) {
513 return LangStringArg("se");
514 }
515 }
516 {
517 Tcl_Obj *result = Tcl_NewListObj(0,NULL);
518 if ((offsetPtr->flags) & TK_OFFSET_RELATIVE) {
519 Tcl_ListObjAppendElement(NULL,result,LangStringArg("#"));
520 }
521 Tcl_ListObjAppendElement(NULL,result,Tcl_NewIntObj(offsetPtr->xoffset));
522 Tcl_ListObjAppendElement(NULL,result,Tcl_NewIntObj(offsetPtr->yoffset));
523 return result;
524 }
525 }
526
527
528 /*
529 *----------------------------------------------------------------------
530 *
531 * TkPixelParseProc --
532 *
533 * Converts the name of an image into a tile.
534 *
535 *----------------------------------------------------------------------
536 */
537
538 int
539 TkPixelParseProc(clientData, interp, tkwin, ovalue, widgRec, offset)
540 ClientData clientData; /* if non-NULL, negative values are
541 * allowed as well */
542 Tcl_Interp *interp; /* Interpreter to send results back to */
543 Tk_Window tkwin; /* Window on same display as tile */
544 Tcl_Obj * ovalue; /* Name of image */
545 char *widgRec; /* Widget structure record */
546 int offset; /* Offset of tile in record */
547 {
548 double *doublePtr = (double *)(widgRec + offset);
549 int result;
550
551 result = TkGetDoublePixels(interp, tkwin, Tcl_GetString(ovalue), doublePtr);
552
553 if ((result == TCL_OK) && (clientData == NULL) && (*doublePtr < 0.0)) {
554 Tcl_AppendResult(interp, "bad screen distance \"", Tcl_GetString(ovalue),
555 "\"", (char *) NULL);
556 return TCL_ERROR;
557 }
558 return result;
559 }
560
561 /*
562 *----------------------------------------------------------------------
563 *
564 * TkPixelPrintProc --
565 *
566 * Returns the name of the tile.
567 *
568 * Results:
569 * The name of the tile is returned.
570 *
571 *----------------------------------------------------------------------
572 */
573
574 Tcl_Obj *
575 TkPixelPrintProc(clientData, tkwin, widgRec, offset, freeProcPtr)
576 ClientData clientData; /* not used */
577 Tk_Window tkwin; /* not used */
578 char *widgRec; /* Widget structure record */
579 int offset; /* Offset of tile in record */
580 Tcl_FreeProc **freeProcPtr; /* not used */
581 {
582 double *doublePtr = (double *)(widgRec + offset);
583 return Tcl_NewDoubleObj(*doublePtr);
584 }
585
586 /*
587 *----------------------------------------------------------------------
588 *
589 * TkDrawInsetFocusHighlight --
590 *
591 * This procedure draws a rectangular ring around the outside of
592 * a widget to indicate that it has received the input focus. It
593 * takes an additional padding argument that specifies how much
594 * padding is present outside th widget.
595 *
596 * Results:
597 * None.
598 *
599 * Side effects:
600 * A rectangle "width" pixels wide is drawn in "drawable",
601 * corresponding to the outer area of "tkwin".
602 *
603 *----------------------------------------------------------------------
604 */
605
606 void
607 TkDrawInsetFocusHighlight(tkwin, gc, width, drawable, padding)
608 Tk_Window tkwin; /* Window whose focus highlight ring is
609 * to be drawn. */
610 GC gc; /* Graphics context to use for drawing
611 * the highlight ring. */
612 int width; /* Width of the highlight ring, in pixels. */
613 Drawable drawable; /* Where to draw the ring (typically a
614 * pixmap for double buffering). */
615 int padding; /* Width of padding outside of widget. */
616 {
617 XRectangle rects[4];
618
619 rects[0].x = padding;
620 rects[0].y = padding;
621 rects[0].width = Tk_Width(tkwin) - (2 * padding);
622 rects[0].height = width;
623 rects[1].x = padding;
624 rects[1].y = Tk_Height(tkwin) - width - padding;
625 rects[1].width = Tk_Width(tkwin) - (2 * padding);
626 rects[1].height = width;
627 rects[2].x = padding;
628 rects[2].y = width + padding;
629 rects[2].width = width;
630 rects[2].height = Tk_Height(tkwin) - 2*width - 2*padding;
631 rects[3].x = Tk_Width(tkwin) - width - padding;
632 rects[3].y = rects[2].y;
633 rects[3].width = width;
634 rects[3].height = rects[2].height;
635 XFillRectangles(Tk_Display(tkwin), drawable, gc, rects, 4);
636 }
637
638 /*
639 *----------------------------------------------------------------------
640 *
641 * Tk_DrawFocusHighlight --
642 *
643 * This procedure draws a rectangular ring around the outside of
644 * a widget to indicate that it has received the input focus.
645 *
646 * This function is now deprecated. Use TkpDrawHighlightBorder instead,
647 * since this function does not handle drawing the Focus ring properly
648 * on the Macintosh - you need to know the background GC as well
649 * as the foreground since the Mac focus ring separated from the widget
650 * by a 1 pixel border.
651 *
652 * Results:
653 * None.
654 *
655 * Side effects:
656 * A rectangle "width" pixels wide is drawn in "drawable",
657 * corresponding to the outer area of "tkwin".
658 *
659 *----------------------------------------------------------------------
660 */
661
662 void
663 Tk_DrawFocusHighlight(tkwin, gc, width, drawable)
664 Tk_Window tkwin; /* Window whose focus highlight ring is
665 * to be drawn. */
666 GC gc; /* Graphics context to use for drawing
667 * the highlight ring. */
668 int width; /* Width of the highlight ring, in pixels. */
669 Drawable drawable; /* Where to draw the ring (typically a
670 * pixmap for double buffering). */
671 {
672 TkDrawInsetFocusHighlight(tkwin, gc, width, drawable, 0);
673 }
674
675 /*
676 *----------------------------------------------------------------------
677 *
678 * Tk_GetScrollInfo --
679 *
680 * This procedure is invoked to parse "xview" and "yview"
681 * scrolling commands for widgets using the new scrolling
682 * command syntax ("moveto" or "scroll" options).
683 *
684 * Results:
685 * The return value is either TK_SCROLL_MOVETO, TK_SCROLL_PAGES,
686 * TK_SCROLL_UNITS, or TK_SCROLL_ERROR. This indicates whether
687 * the command was successfully parsed and what form the command
688 * took. If TK_SCROLL_MOVETO, *dblPtr is filled in with the
689 * desired position; if TK_SCROLL_PAGES or TK_SCROLL_UNITS,
690 * *intPtr is filled in with the number of lines to move (may be
691 * negative); if TK_SCROLL_ERROR, the interp's result contains an
692 * error message.
693 *
694 * Side effects:
695 * None.
696 *
697 *----------------------------------------------------------------------
698 */
699
700 int
701 Tk_GetScrollInfo(interp, argc, argv, dblPtr, intPtr)
702 Tcl_Interp *interp; /* Used for error reporting. */
703 int argc; /* # arguments for command. */
704 CONST char **argv; /* Arguments for command. */
705 double *dblPtr; /* Filled in with argument "moveto"
706 * option, if any. */
707 int *intPtr; /* Filled in with number of pages
708 * or lines to scroll, if any. */
709 {
710 int c;
711 size_t length;
712
713 length = strlen(argv[2]);
714 c = argv[2][0];
715 if ((c == 'm') && (strncmp(argv[2], "moveto", length) == 0)) {
716 if (argc != 4) {
717 Tcl_AppendResult(interp, "wrong # args: should be \"",
718 argv[0], " ", argv[1], " moveto fraction\"",
719 (char *) NULL);
720 return TK_SCROLL_ERROR;
721 }
722 if (Tcl_GetDoubleFromObj(interp, argv[3], dblPtr) != TCL_OK) {
723 return TK_SCROLL_ERROR;
724 }
725 return TK_SCROLL_MOVETO;
726 } else if ((c == 's')
727 && (strncmp(argv[2], "scroll", length) == 0)) {
728 if (argc != 5) {
729 Tcl_AppendResult(interp, "wrong # args: should be \"",
730 argv[0], " ", argv[1], " scroll number units|pages\"",
731 (char *) NULL);
732 return TK_SCROLL_ERROR;
733 }
734 if (Tcl_GetIntFromObj(interp, argv[3], intPtr) != TCL_OK) {
735 return TK_SCROLL_ERROR;
736 }
737 length = strlen(argv[4]);
738 c = argv[4][0];
739 if ((c == 'p') && (strncmp(argv[4], "pages", length) == 0)) {
740 return TK_SCROLL_PAGES;
741 } else if ((c == 'u')
742 && (strncmp(argv[4], "units", length) == 0)) {
743 return TK_SCROLL_UNITS;
744 } else {
745 Tcl_AppendResult(interp, "bad argument \"", argv[4],
746 "\": must be units or pages", (char *) NULL);
747 return TK_SCROLL_ERROR;
748 }
749 }
750 Tcl_AppendResult(interp, "unknown option \"", argv[2],
751 "\": must be moveto or scroll", (char *) NULL);
752 return TK_SCROLL_ERROR;
753 }
754
755 /*
756 *----------------------------------------------------------------------
757 *
758 * Tk_GetScrollInfoObj --
759 *
760 * This procedure is invoked to parse "xview" and "yview"
761 * scrolling commands for widgets using the new scrolling
762 * command syntax ("moveto" or "scroll" options).
763 *
764 * Results:
765 * The return value is either TK_SCROLL_MOVETO, TK_SCROLL_PAGES,
766 * TK_SCROLL_UNITS, or TK_SCROLL_ERROR. This indicates whether
767 * the command was successfully parsed and what form the command
768 * took. If TK_SCROLL_MOVETO, *dblPtr is filled in with the
769 * desired position; if TK_SCROLL_PAGES or TK_SCROLL_UNITS,
770 * *intPtr is filled in with the number of lines to move (may be
771 * negative); if TK_SCROLL_ERROR, the interp's result contains an
772 * error message.
773 *
774 * Side effects:
775 * None.
776 *
777 *----------------------------------------------------------------------
778 */
779
780 int
781 Tk_GetScrollInfoObj(interp, objc, objv, dblPtr, intPtr)
782 Tcl_Interp *interp; /* Used for error reporting. */
783 int objc; /* # arguments for command. */
784 Tcl_Obj *CONST objv[]; /* Arguments for command. */
785 double *dblPtr; /* Filled in with argument "moveto"
786 * option, if any. */
787 int *intPtr; /* Filled in with number of pages
788 * or lines to scroll, if any. */
789 {
790 int c;
791 size_t length;
792 char *arg2, *arg4;
793
794 arg2 = Tcl_GetString(objv[2]);
795 length = strlen(arg2);
796 c = arg2[0];
797 if ((c == 'm') && (strncmp(arg2, "moveto", length) == 0)) {
798 if (objc != 4) {
799 Tcl_WrongNumArgs(interp, 2, objv, "moveto fraction");
800 return TK_SCROLL_ERROR;
801 }
802 if (Tcl_GetDoubleFromObj(interp, objv[3], dblPtr) != TCL_OK) {
803 return TK_SCROLL_ERROR;
804 }
805 return TK_SCROLL_MOVETO;
806 } else if ((c == 's')
807 && (strncmp(arg2, "scroll", length) == 0)) {
808 if (objc != 5) {
809 Tcl_WrongNumArgs(interp, 2, objv, "scroll number units|pages");
810 return TK_SCROLL_ERROR;
811 }
812 if (Tcl_GetIntFromObj(interp, objv[3], intPtr) != TCL_OK) {
813 return TK_SCROLL_ERROR;
814 }
815 arg4 = Tcl_GetString(objv[4]);
816 length = (strlen(arg4));
817 c = arg4[0];
818 if ((c == 'p') && (strncmp(arg4, "pages", length) == 0)) {
819 return TK_SCROLL_PAGES;
820 } else if ((c == 'u')
821 && (strncmp(arg4, "units", length) == 0)) {
822 return TK_SCROLL_UNITS;
823 } else {
824 Tcl_AppendResult(interp, "bad argument \"", arg4,
825 "\": must be units or pages", (char *) NULL);
826 return TK_SCROLL_ERROR;
827 }
828 }
829 Tcl_AppendResult(interp, "unknown option \"", arg2,
830 "\": must be moveto or scroll", (char *) NULL);
831 return TK_SCROLL_ERROR;
832 }
833
834 /*
835 *---------------------------------------------------------------------------
836 *
837 * TkComputeAnchor --
838 *
839 * Determine where to place a rectangle so that it will be properly
840 * anchored with respect to the given window. Used by widgets
841 * to align a box of text inside a window. When anchoring with
842 * respect to one of the sides, the rectangle be placed inside of
843 * the internal border of the window.
844 *
845 * Results:
846 * *xPtr and *yPtr set to the upper-left corner of the rectangle
847 * anchored in the window.
848 *
849 * Side effects:
850 * None.
851 *
852 *---------------------------------------------------------------------------
853 */
854 void
855 TkComputeAnchor(anchor, tkwin, padX, padY, innerWidth, innerHeight, xPtr, yPtr)
856 Tk_Anchor anchor; /* Desired anchor. */
857 Tk_Window tkwin; /* Anchored with respect to this window. */
858 int padX, padY; /* Use this extra padding inside window, in
859 * addition to the internal border. */
860 int innerWidth, innerHeight;/* Size of rectangle to anchor in window. */
861 int *xPtr, *yPtr; /* Returns upper-left corner of anchored
862 * rectangle. */
863 {
864 switch (anchor) {
865 case TK_ANCHOR_NW:
866 case TK_ANCHOR_W:
867 case TK_ANCHOR_SW:
868 *xPtr = Tk_InternalBorderLeft(tkwin) + padX;
869 break;
870
871 case TK_ANCHOR_N:
872 case TK_ANCHOR_CENTER:
873 case TK_ANCHOR_S:
874 *xPtr = (Tk_Width(tkwin) - innerWidth) / 2;
875 break;
876
877 default:
878 *xPtr = Tk_Width(tkwin) - (Tk_InternalBorderRight(tkwin) + padX)
879 - innerWidth;
880 break;
881 }
882
883 switch (anchor) {
884 case TK_ANCHOR_NW:
885 case TK_ANCHOR_N:
886 case TK_ANCHOR_NE:
887 *yPtr = Tk_InternalBorderTop(tkwin) + padY;
888 break;
889
890 case TK_ANCHOR_W:
891 case TK_ANCHOR_CENTER:
892 case TK_ANCHOR_E:
893 *yPtr = (Tk_Height(tkwin) - innerHeight) / 2;
894 break;
895
896 default:
897 *yPtr = Tk_Height(tkwin) - Tk_InternalBorderBottom(tkwin) - padY
898 - innerHeight;
899 break;
900 }
901 }
902
903 /*
904 *---------------------------------------------------------------------------
905 *
906 * TkFindStateString --
907 *
908 * Given a lookup table, map a number to a string in the table.
909 *
910 * Results:
911 * If numKey was equal to the numeric key of one of the elements
912 * in the table, returns the string key of that element.
913 * Returns NULL if numKey was not equal to any of the numeric keys
914 * in the table.
915 *
916 * Side effects.
917 * None.
918 *
919 *---------------------------------------------------------------------------
920 */
921
922 char *
923 TkFindStateString(mapPtr, numKey)
924 CONST TkStateMap *mapPtr; /* The state table. */
925 int numKey; /* The key to try to find in the table. */
926 {
927 for ( ; mapPtr->strKey != NULL; mapPtr++) {
928 if (numKey == mapPtr->numKey) {
929 return mapPtr->strKey;
930 }
931 }
932 return NULL;
933 }
934
935 /*
936 *---------------------------------------------------------------------------
937 *
938 * TkFindStateNum --
939 *
940 * Given a lookup table, map a string to a number in the table.
941 *
942 * Results:
943 * If strKey was equal to the string keys of one of the elements
944 * in the table, returns the numeric key of that element.
945 * Returns the numKey associated with the last element (the NULL
946 * string one) in the table if strKey was not equal to any of the
947 * string keys in the table. In that case, an error message is
948 * also left in the interp's result (if interp is not NULL).
949 *
950 * Side effects.
951 * None.
952 *
953 *---------------------------------------------------------------------------
954 */
955
956 int
957 TkFindStateNum(interp, option, mapPtr, strKey)
958 Tcl_Interp *interp; /* Interp for error reporting. */
959 CONST char *option; /* String to use when constructing error. */
960 CONST TkStateMap *mapPtr; /* Lookup table. */
961 CONST char *strKey; /* String to try to find in lookup table. */
962 {
963 CONST TkStateMap *mPtr;
964
965 for (mPtr = mapPtr; mPtr->strKey != NULL; mPtr++) {
966 if (strcmp(strKey, mPtr->strKey) == 0) {
967 return mPtr->numKey;
968 }
969 }
970 if (interp != NULL) {
971 mPtr = mapPtr;
972 Tcl_AppendResult(interp, "bad ", option, " value \"", strKey,
973 "\": must be ", mPtr->strKey, (char *) NULL);
974 for (mPtr++; mPtr->strKey != NULL; mPtr++) {
975 Tcl_AppendResult(interp,
976 ((mPtr[1].strKey != NULL) ? ", " : ", or "),
977 mPtr->strKey, (char *) NULL);
978 }
979 }
980 return mPtr->numKey;
981 }
982
983 int
984 TkFindStateNumObj(interp, optionPtr, mapPtr, keyObjPtr)
985 Tcl_Interp *interp; /* Interp for error reporting. */
986 Tcl_Obj *optionPtr; /* String to use when constructing error. */
987 CONST TkStateMap *mapPtr; /* Lookup table. */
988 Tcl_Obj *keyObjPtr; /* String key to find in lookup table. */
989 {
990 CONST TkStateMap *mPtr;
991 CONST char *key;
992 CONST Tcl_ObjType *typePtr;
993
994 if ((TclObjGetType(keyObjPtr) == &tkStateKeyObjType)
995 && (keyObjPtr->internalRep.twoPtrValue.ptr1 == (VOID *) mapPtr)) {
996 return (int) keyObjPtr->internalRep.twoPtrValue.ptr2;
997 }
998
999 key = Tcl_GetStringFromObj(keyObjPtr, NULL);
1000 for (mPtr = mapPtr; mPtr->strKey != NULL; mPtr++) {
1001 if (strcmp(key, mPtr->strKey) == 0) {
1002 typePtr = TclObjGetType(keyObjPtr);
1003 if ((typePtr != NULL) && (typePtr->freeIntRepProc != NULL)) {
1004 (*typePtr->freeIntRepProc)(keyObjPtr);
1005 }
1006 keyObjPtr->internalRep.twoPtrValue.ptr1 = (VOID *) mapPtr;
1007 keyObjPtr->internalRep.twoPtrValue.ptr2 = (VOID *) mPtr->numKey;
1008 keyObjPtr->typePtr = &tkStateKeyObjType;
1009 return mPtr->numKey;
1010 }
1011 }
1012 if (interp != NULL) {
1013 mPtr = mapPtr;
1014 Tcl_AppendResult(interp, "bad ",
1015 Tcl_GetStringFromObj(optionPtr, NULL), " value \"", key,
1016 "\": must be ", mPtr->strKey, (char *) NULL);
1017 for (mPtr++; mPtr->strKey != NULL; mPtr++) {
1018 Tcl_AppendResult(interp,
1019 ((mPtr[1].strKey != NULL) ? ", " : ", or "),
1020 mPtr->strKey, (char *) NULL);
1021 }
1022 }
1023 return mPtr->numKey;
1024 }
1025
1026
1027
1028
1029