Debian Codegraph @

pTk/mTk/generic/tkCanvText.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * tkCanvText.c --
3 *
4 * This file implements text items for canvas widgets.
5 *
6 * Copyright (c) 1991-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkCanvText.c,v 1.15 2003/02/09 07:48:22 hobbs Exp $
13 */
14
15 #include "tkPort.h"
16 #include "tkInt.h"
17 #include "tkCanvases.h"
18 #include "default.h"
19
20 /*
21 * The structure below defines the record for each text item.
22 */
23
24 typedef struct TextItem {
25 Tk_Item header; /* Generic stuff that's the same for all
26 * types. MUST BE FIRST IN STRUCTURE. */
27 Tk_CanvasTextInfo *textInfoPtr;
28 /* Pointer to a structure containing
29 * information about the selection and
30 * insertion cursor. The structure is owned
31 * by (and shared with) the generic canvas
32 * code. */
33 /*
34 * Fields that are set by widget commands other than "configure".
35 */
36
37 double x, y; /* Positioning point for text. */
38 int insertPos; /* Character index of character just before
39 * which the insertion cursor is displayed. */
40
41 /*
42 * Configuration settings that are updated by Tk_ConfigureWidget.
43 */
44
45 Tk_Anchor anchor; /* Where to anchor text relative to (x,y). */
46 Tk_TSOffset tsoffset;
47 XColor *color; /* Color for text. */
48 XColor *activeColor; /* Color for text. */
49 XColor *disabledColor; /* Color for text. */
50 Tk_Font tkfont; /* Font for drawing text. */
51 Tk_Justify justify; /* Justification mode for text. */
52 Pixmap stipple; /* Stipple bitmap for text, or None. */
53 Pixmap activeStipple; /* Stipple bitmap for text, or None. */
54 Pixmap disabledStipple; /* Stipple bitmap for text, or None. */
55 char *text; /* Text for item (malloc-ed). */
56 int width; /* Width of lines for word-wrap, pixels.
57 * Zero means no word-wrap. */
58
59 /*
60 * Fields whose values are derived from the current values of the
61 * configuration settings above.
62 */
63
64 int numChars; /* Length of text in characters. */
65 int numBytes; /* Length of text in bytes. */
66 Tk_TextLayout textLayout; /* Cached text layout information. */
67 int leftEdge; /* Pixel location of the left edge of the
68 * text item; where the left border of the
69 * text layout is drawn. */
70 int rightEdge; /* Pixel just to right of right edge of
71 * area of text item. Used for selecting up
72 * to end of line. */
73 GC gc; /* Graphics context for drawing text. */
74 GC selTextGC; /* Graphics context for selected text. */
75 GC cursorOffGC; /* If not None, this gives a graphics context
76 * to use to draw the insertion cursor when
77 * it's off. Used if the selection and
78 * insertion cursor colors are the same. */
79 Tk_Tile tile;
80 } TextItem;
81
82 /*
83 * Information used for parsing configuration specs:
84 */
85
86 static Tk_CustomOption stateOption = {
87 TkStateParseProc,
88 TkStatePrintProc, (ClientData) 2
89 };
90 static Tk_CustomOption tagsOption = {
91 Tk_CanvasTagsParseProc,
92 Tk_CanvasTagsPrintProc, (ClientData) NULL
93 };
94 static Tk_CustomOption tileOption = {
95 Tk_TileParseProc,
96 Tk_TilePrintProc, (ClientData) NULL
97 };
98 static Tk_CustomOption offsetOption = {
99 Tk_OffsetParseProc,
100 Tk_OffsetPrintProc, (ClientData) (TK_OFFSET_RELATIVE)
101 };
102
103 static Tk_ConfigSpec configSpecs[] = {
104 {TK_CONFIG_COLOR, "-activefill", (char *) NULL, (char *) NULL,
105 (char *) NULL, Tk_Offset(TextItem, activeColor), TK_CONFIG_NULL_OK},
106 {TK_CONFIG_BITMAP, "-activestipple", (char *) NULL, (char *) NULL,
107 (char *) NULL, Tk_Offset(TextItem, activeStipple), TK_CONFIG_NULL_OK},
108 {TK_CONFIG_ANCHOR, "-anchor", (char *) NULL, (char *) NULL,
109 "center", Tk_Offset(TextItem, anchor),
110 TK_CONFIG_DONT_SET_DEFAULT},
111 {TK_CONFIG_COLOR, "-disabledfill", (char *) NULL, (char *) NULL,
112 (char *) NULL, Tk_Offset(TextItem, disabledColor), TK_CONFIG_NULL_OK},
113 {TK_CONFIG_BITMAP, "-disabledstipple", (char *) NULL, (char *) NULL,
114 (char *) NULL, Tk_Offset(TextItem, disabledStipple), TK_CONFIG_NULL_OK},
115 {TK_CONFIG_COLOR, "-fill", (char *) NULL, (char *) NULL,
116 "black", Tk_Offset(TextItem, color), TK_CONFIG_NULL_OK},
117 {TK_CONFIG_FONT, "-font", (char *) NULL, (char *) NULL,
118 DEF_CANVTEXT_FONT, Tk_Offset(TextItem, tkfont), 0},
119 {TK_CONFIG_JUSTIFY, "-justify", (char *) NULL, (char *) NULL,
120 "left", Tk_Offset(TextItem, justify),
121 TK_CONFIG_DONT_SET_DEFAULT},
122 {TK_CONFIG_CUSTOM, "-offset", (char *) NULL, (char *) NULL,
123 "0 0", Tk_Offset(TextItem, tsoffset),
124 TK_CONFIG_DONT_SET_DEFAULT, &offsetOption},
125 {TK_CONFIG_CUSTOM, "-state", (char *) NULL, (char *) NULL,
126 (char *) NULL, Tk_Offset(Tk_Item, state), TK_CONFIG_NULL_OK,
127 &stateOption},
128 {TK_CONFIG_BITMAP, "-stipple", (char *) NULL, (char *) NULL,
129 (char *) NULL, Tk_Offset(TextItem, stipple), TK_CONFIG_NULL_OK},
130 {TK_CONFIG_CUSTOM, "-tags", (char *) NULL, (char *) NULL,
131 (char *) NULL, 0, TK_CONFIG_NULL_OK, &tagsOption},
132 {TK_CONFIG_STRING, "-text", (char *) NULL, (char *) NULL,
133 "", Tk_Offset(TextItem, text), 0},
134 {TK_CONFIG_CALLBACK, "-updatecommand", (char *) NULL, (char *) NULL,
135 (char *) NULL, Tk_Offset(Tk_Item, updateCmd), TK_CONFIG_NULL_OK},
136 {TK_CONFIG_PIXELS, "-width", (char *) NULL, (char *) NULL,
137 "0", Tk_Offset(TextItem, width), TK_CONFIG_DONT_SET_DEFAULT},
138 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
139 (char *) NULL, 0, 0}
140 };
141
142 /*
143 * Prototypes for procedures defined in this file:
144 */
145
146 static void ComputeTextBbox _ANSI_ARGS_((Tk_Canvas canvas,
147 TextItem *textPtr));
148 static int ConfigureText _ANSI_ARGS_((Tcl_Interp *interp,
149 Tk_Canvas canvas, Tk_Item *itemPtr, int argc,
150 Tcl_Obj *CONST objv[], int flags));
151 static int CreateText _ANSI_ARGS_((Tcl_Interp *interp,
152 Tk_Canvas canvas, struct Tk_Item *itemPtr,
153 int argc, Tcl_Obj *CONST objv[]));
154 static void DeleteText _ANSI_ARGS_((Tk_Canvas canvas,
155 Tk_Item *itemPtr, Display *display));
156 static void DisplayCanvText _ANSI_ARGS_((Tk_Canvas canvas,
157 Tk_Item *itemPtr, Display *display, Drawable dst,
158 int x, int y, int width, int height));
159 static int GetSelText _ANSI_ARGS_((Tk_Canvas canvas,
160 Tk_Item *itemPtr, int offset, char *buffer,
161 int maxBytes));
162 static int GetTextIndex _ANSI_ARGS_((Tcl_Interp *interp,
163 Tk_Canvas canvas, Tk_Item *itemPtr,
164 Tcl_Obj *obj, int *indexPtr));
165 static void ScaleText _ANSI_ARGS_((Tk_Canvas canvas,
166 Tk_Item *itemPtr, double originX, double originY,
167 double scaleX, double scaleY));
168 static void SetTextCursor _ANSI_ARGS_((Tk_Canvas canvas,
169 Tk_Item *itemPtr, int index));
170 static int TextCoords _ANSI_ARGS_((Tcl_Interp *interp,
171 Tk_Canvas canvas, Tk_Item *itemPtr,
172 int argc, Tcl_Obj *CONST objv[]));
173 static void TextDeleteChars _ANSI_ARGS_((Tk_Canvas canvas,
174 Tk_Item *itemPtr, int first, int last));
175 static void TextInsert _ANSI_ARGS_((Tk_Canvas canvas,
176 Tk_Item *itemPtr, int beforeThis, Tcl_Obj *string));
177 static int TextToArea _ANSI_ARGS_((Tk_Canvas canvas,
178 Tk_Item *itemPtr, double *rectPtr));
179 static double TextToPoint _ANSI_ARGS_((Tk_Canvas canvas,
180 Tk_Item *itemPtr, double *pointPtr));
181 static int TextToPostscript _ANSI_ARGS_((Tcl_Interp *interp,
182 Tk_Canvas canvas, Tk_Item *itemPtr, int prepass));
183 static void TranslateText _ANSI_ARGS_((Tk_Canvas canvas,
184 Tk_Item *itemPtr, double deltaX, double deltaY));
185
186 /*
187 * The structures below defines the rectangle and oval item types
188 * by means of procedures that can be invoked by generic item code.
189 */
190
191 Tk_ItemType tkTextType = {
192 "text", /* name */
193 sizeof(TextItem), /* itemSize */
194 CreateText, /* createProc */
195 configSpecs, /* configSpecs */
196 ConfigureText, /* configureProc */
197 TextCoords, /* coordProc */
198 DeleteText, /* deleteProc */
199 DisplayCanvText, /* displayProc */
200 TK_ITEM_VISITOR_SUPPORT|TK_CONFIG_OBJS,/* flags */
201 TextToPoint, /* pointProc */
202 TextToArea, /* areaProc */
203 TextToPostscript, /* postscriptProc */
204 ScaleText, /* scaleProc */
205 TranslateText, /* translateProc */
206 GetTextIndex, /* indexProc */
207 SetTextCursor, /* icursorProc */
208 GetSelText, /* selectionProc */
209 TextInsert, /* insertProc */
210 TextDeleteChars, /* dTextProc */
211 (Tk_ItemType *) NULL, /* nextPtr */
212 (Tk_ItemBboxProc *) ComputeTextBbox,/* bboxProc */
213 Tk_Offset(Tk_VisitorType, visitText), /* acceptProc */
214 NULL, /* getCoordProc */
215 NULL /* setCoordProc */
216 };
217
218 /*
219 *--------------------------------------------------------------
220 *
221 * CreateText --
222 *
223 * This procedure is invoked to create a new text item
224 * in a canvas.
225 *
226 * Results:
227 * A standard Tcl return value. If an error occurred in
228 * creating the item then an error message is left in
229 * the interp's result; in this case itemPtr is left uninitialized
230 * so it can be safely freed by the caller.
231 *
232 * Side effects:
233 * A new text item is created.
234 *
235 *--------------------------------------------------------------
236 */
237
238 static int
239 CreateText(interp, canvas, itemPtr, objc, objv)
240 Tcl_Interp *interp; /* Interpreter for error reporting. */
241 Tk_Canvas canvas; /* Canvas to hold new item. */
242 Tk_Item *itemPtr; /* Record to hold new item; header has been
243 * initialized by caller. */
244 int objc; /* Number of arguments in objv. */
245 Tcl_Obj *CONST objv[]; /* Arguments describing rectangle. */
246 {
247 TextItem *textPtr = (TextItem *) itemPtr;
248 int i;
249
250 if (objc == 0) {
251 panic("canvas did not pass any coords\n");
252 }
253
254 /*
255 * Carry out initialization that is needed in order to clean up after
256 * errors during the the remainder of this procedure.
257 */
258
259 textPtr->textInfoPtr = Tk_CanvasGetTextInfo(canvas);
260
261 textPtr->insertPos = 0;
262
263 textPtr->anchor = TK_ANCHOR_CENTER;
264 textPtr->tsoffset.flags = 0;
265 textPtr->tsoffset.xoffset = 0;
266 textPtr->tsoffset.yoffset = 0;
267 textPtr->color = NULL;
268 textPtr->activeColor = NULL;
269 textPtr->disabledColor = NULL;
270 textPtr->tkfont = NULL;
271 textPtr->justify = TK_JUSTIFY_LEFT;
272 textPtr->stipple = None;
273 textPtr->activeStipple = None;
274 textPtr->disabledStipple = None;
275 textPtr->text = NULL;
276 textPtr->width = 0;
277
278 textPtr->numChars = 0;
279 textPtr->numBytes = 0;
280 textPtr->textLayout = NULL;
281 textPtr->leftEdge = 0;
282 textPtr->rightEdge = 0;
283 textPtr->gc = None;
284 textPtr->selTextGC = None;
285 textPtr->cursorOffGC = None;
286 textPtr->tile = NULL;
287
288 /*
289 * Process the arguments to fill in the item record.
290 * Only 1 (list) or 2 (x y) coords are allowed.
291 */
292
293 if (objc == 1) {
294 i = 1;
295 } else {
296 char *arg = Tcl_GetString(objv[1]);
297 i = 2;
298 if ((arg[0] == '-') && (arg[1] >= 'a') && (arg[1] <= 'z')) {
299 i = 1;
300 }
301 }
302 if ((TextCoords(interp, canvas, itemPtr, i, objv) != TCL_OK)) {
303 goto error;
304 }
305 if (ConfigureText(interp, canvas, itemPtr, objc-i, objv+i, 0) == TCL_OK) {
306 return TCL_OK;
307 }
308
309 error:
310 DeleteText(canvas, itemPtr, Tk_Display(Tk_CanvasTkwin(canvas)));
311 return TCL_ERROR;
312 }
313
314 /*
315 *--------------------------------------------------------------
316 *
317 * TextCoords --
318 *
319 * This procedure is invoked to process the "coords" widget
320 * command on text items. See the user documentation for
321 * details on what it does.
322 *
323 * Results:
324 * Returns TCL_OK or TCL_ERROR, and sets the interp's result.
325 *
326 * Side effects:
327 * The coordinates for the given item may be changed.
328 *
329 *--------------------------------------------------------------
330 */
331
332 static int
333 TextCoords(interp, canvas, itemPtr, objc, objv)
334 Tcl_Interp *interp; /* Used for error reporting. */
335 Tk_Canvas canvas; /* Canvas containing item. */
336 Tk_Item *itemPtr; /* Item whose coordinates are to be read or
337 * modified. */
338 int objc; /* Number of coordinates supplied in objv. */
339 Tcl_Obj *CONST objv[]; /* Array of coordinates: x1, y1, x2, y2, ... */
340 {
341 TextItem *textPtr = (TextItem *) itemPtr;
342
343 if (objc == 0) {
344 Tcl_Obj *obj = Tcl_NewObj();
345 Tcl_Obj *subobj = Tcl_NewDoubleObj(textPtr->x);
346 Tcl_ListObjAppendElement(interp, obj, subobj);
347 subobj = Tcl_NewDoubleObj(textPtr->y);
348 Tcl_ListObjAppendElement(interp, obj, subobj);
349 Tcl_SetObjResult(interp, obj);
350 } else if (objc < 3) {
351 if (objc==1) {
352 if (Tcl_ListObjGetElements(interp, objv[0], &objc,
353 (Tcl_Obj ***) &objv) != TCL_OK) {
354 return TCL_ERROR;
355 } else if (objc != 2) {
356 char buf[64 + TCL_INTEGER_SPACE];
357
358 sprintf(buf, "wrong # coordinates: expected 2, got %d", objc);
359 Tcl_SetResult(interp, buf, TCL_VOLATILE);
360 return TCL_ERROR;
361 }
362 }
363 if ((Tk_CanvasGetCoordFromObj(interp, canvas, objv[0], &textPtr->x) != TCL_OK)
364 || (Tk_CanvasGetCoordFromObj(interp, canvas, objv[1],
365 &textPtr->y) != TCL_OK)) {
366 return TCL_ERROR;
367 }
368 ComputeTextBbox(canvas, textPtr);
369 } else {
370 char buf[64 + TCL_INTEGER_SPACE];
371
372 sprintf(buf, "wrong # coordinates: expected 0 or 2, got %d", objc);
373 Tcl_SetResult(interp, buf, TCL_VOLATILE);
374 return TCL_ERROR;
375 }
376 return TCL_OK;
377 }
378
379 /*
380 *--------------------------------------------------------------
381 *
382 * ConfigureText --
383 *
384 * This procedure is invoked to configure various aspects
385 * of a text item, such as its border and background colors.
386 *
387 * Results:
388 * A standard Tcl result code. If an error occurs, then
389 * an error message is left in the interp's result.
390 *
391 * Side effects:
392 * Configuration information, such as colors and stipple
393 * patterns, may be set for itemPtr.
394 *
395 *--------------------------------------------------------------
396 */
397
398 static int
399 ConfigureText(interp, canvas, itemPtr, objc, objv, flags)
400 Tcl_Interp *interp; /* Interpreter for error reporting. */
401 Tk_Canvas canvas; /* Canvas containing itemPtr. */
402 Tk_Item *itemPtr; /* Rectangle item to reconfigure. */
403 int objc; /* Number of elements in objv. */
404 Tcl_Obj *CONST objv[]; /* Arguments describing things to configure. */
405 int flags; /* Flags to pass to Tk_ConfigureWidget. */
406 {
407 TextItem *textPtr = (TextItem *) itemPtr;
408 XGCValues gcValues;
409 GC newGC, newSelGC;
410 unsigned long mask;
411 Tk_Window tkwin;
412 Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
413 XColor *selBgColorPtr;
414 XColor *color;
415 Pixmap stipple;
416 Tk_State state;
417 Tk_Tile tile;
418
419 tkwin = Tk_CanvasTkwin(canvas);
420 if (TCL_OK != Tk_ConfigureWidget(interp, tkwin, configSpecs, objc,
421 (CONST84 Tcl_Obj *CONST*) objv, (char *) textPtr, flags|TK_CONFIG_OBJS)) {
422 return TCL_ERROR;
423 }
424
425 /*
426 * A few of the options require additional processing, such as
427 * graphics contexts.
428 */
429
430 state = Tk_GetItemState(canvas, itemPtr);
431
432 if (textPtr->activeColor != NULL ||
433 textPtr->activeStipple != None) {
434 itemPtr->redraw_flags |= TK_ITEM_STATE_DEPENDANT;
435 } else {
436 itemPtr->redraw_flags &= ~TK_ITEM_STATE_DEPENDANT;
437 }
438
439 tile = textPtr->tile;
440 color = textPtr->color;
441 stipple = textPtr->stipple;
442 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
443 if (textPtr->activeColor!=NULL) {
444 color = textPtr->activeColor;
445 }
446 if (textPtr->activeStipple!=None) {
447 stipple = textPtr->activeStipple;
448 }
449 } else if (state==TK_STATE_DISABLED) {
450 if (textPtr->disabledColor!=NULL) {
451 color = textPtr->disabledColor;
452 }
453 if (textPtr->disabledStipple!=None) {
454 stipple = textPtr->disabledStipple;
455 }
456 }
457
458 newGC = newSelGC = None;
459 if (textPtr->tkfont != NULL) {
460 gcValues.font = Tk_FontId(textPtr->tkfont);
461 mask = GCFont;
462 if (color != NULL) {
463 gcValues.foreground = color->pixel;
464 mask |= GCForeground;
465 if (stipple != None) {
466 gcValues.stipple = stipple;
467 gcValues.fill_style = FillStippled;
468 mask |= GCStipple|GCFillStyle;
469 }
470 newGC = Tk_GetGC(tkwin, mask, &gcValues);
471 }
472 mask &= ~(GCTile|GCFillStyle|GCStipple);
473 if (stipple != None) {
474 gcValues.stipple = stipple;
475 gcValues.fill_style = FillStippled;
476 mask |= GCStipple|GCFillStyle;
477 }
478 gcValues.foreground = textInfoPtr->selFgColorPtr->pixel;
479 newSelGC = Tk_GetGC(tkwin, mask|GCForeground, &gcValues);
480 }
481 if (textPtr->gc != None) {
482 Tk_FreeGC(Tk_Display(tkwin), textPtr->gc);
483 }
484 textPtr->gc = newGC;
485 if (textPtr->selTextGC != None) {
486 Tk_FreeGC(Tk_Display(tkwin), textPtr->selTextGC);
487 }
488 textPtr->selTextGC = newSelGC;
489
490 selBgColorPtr = Tk_3DBorderColor(textInfoPtr->selBorder);
491 if (Tk_3DBorderColor(textInfoPtr->insertBorder)->pixel
492 == selBgColorPtr->pixel) {
493 if (selBgColorPtr->pixel == BlackPixelOfScreen(Tk_Screen(tkwin))) {
494 gcValues.foreground = WhitePixelOfScreen(Tk_Screen(tkwin));
495 } else {
496 gcValues.foreground = BlackPixelOfScreen(Tk_Screen(tkwin));
497 }
498 newGC = Tk_GetGC(tkwin, GCForeground, &gcValues);
499 } else {
500 newGC = None;
501 }
502 if (textPtr->cursorOffGC != None) {
503 Tk_FreeGC(Tk_Display(tkwin), textPtr->cursorOffGC);
504 }
505 textPtr->cursorOffGC = newGC;
506
507
508 /*
509 * If the text was changed, move the selection and insertion indices
510 * to keep them inside the item.
511 */
512
513 textPtr->numBytes = strlen(textPtr->text);
514 textPtr->numChars = Tcl_NumUtfChars(textPtr->text, textPtr->numBytes);
515 if (textInfoPtr->selItemPtr == itemPtr) {
516
517 if (textInfoPtr->selectFirst >= textPtr->numChars) {
518 textInfoPtr->selItemPtr = NULL;
519 } else {
520 if (textInfoPtr->selectLast >= textPtr->numChars) {
521 textInfoPtr->selectLast = textPtr->numChars - 1;
522 }
523 if ((textInfoPtr->anchorItemPtr == itemPtr)
524 && (textInfoPtr->selectAnchor >= textPtr->numChars)) {
525 textInfoPtr->selectAnchor = textPtr->numChars - 1;
526 }
527 }
528 }
529 if (textPtr->insertPos >= textPtr->numChars) {
530 textPtr->insertPos = textPtr->numChars;
531 }
532
533 ComputeTextBbox(canvas, textPtr);
534 return TCL_OK;
535 }
536
537 /*
538 *--------------------------------------------------------------
539 *
540 * DeleteText --
541 *
542 * This procedure is called to clean up the data structure
543 * associated with a text item.
544 *
545 * Results:
546 * None.
547 *
548 * Side effects:
549 * Resources associated with itemPtr are released.
550 *
551 *--------------------------------------------------------------
552 */
553
554 static void
555 DeleteText(canvas, itemPtr, display)
556 Tk_Canvas canvas; /* Info about overall canvas widget. */
557 Tk_Item *itemPtr; /* Item that is being deleted. */
558 Display *display; /* Display containing window for canvas. */
559 {
560 TextItem *textPtr = (TextItem *) itemPtr;
561
562 if (textPtr->color != NULL) {
563 Tk_FreeColor(textPtr->color);
564 }
565 if (textPtr->activeColor != NULL) {
566 Tk_FreeColor(textPtr->activeColor);
567 }
568 if (textPtr->disabledColor != NULL) {
569 Tk_FreeColor(textPtr->disabledColor);
570 }
571 Tk_FreeFont(textPtr->tkfont);
572 if (textPtr->stipple != None) {
573 Tk_FreeBitmap(display, textPtr->stipple);
574 }
575 if (textPtr->activeStipple != None) {
576 Tk_FreeBitmap(display, textPtr->activeStipple);
577 }
578 if (textPtr->disabledStipple != None) {
579 Tk_FreeBitmap(display, textPtr->disabledStipple);
580 }
581 if (textPtr->text != NULL) {
582 ckfree(textPtr->text);
583 }
584
585 Tk_FreeTextLayout(textPtr->textLayout);
586 if (textPtr->gc != None) {
587 Tk_FreeGC(display, textPtr->gc);
588 }
589 if (textPtr->selTextGC != None) {
590 Tk_FreeGC(display, textPtr->selTextGC);
591 }
592 if (textPtr->cursorOffGC != None) {
593 Tk_FreeGC(display, textPtr->cursorOffGC);
594 }
595 }
596
597 /*
598 *--------------------------------------------------------------
599 *
600 * ComputeTextBbox --
601 *
602 * This procedure is invoked to compute the bounding box of
603 * all the pixels that may be drawn as part of a text item.
604 * In addition, it recomputes all of the geometry information
605 * used to display a text item or check for mouse hits.
606 *
607 * Results:
608 * None.
609 *
610 * Side effects:
611 * The fields x1, y1, x2, and y2 are updated in the header
612 * for itemPtr, and the linePtr structure is regenerated
613 * for itemPtr.
614 *
615 *--------------------------------------------------------------
616 */
617
618 static void
619 ComputeTextBbox(canvas, textPtr)
620 Tk_Canvas canvas; /* Canvas that contains item. */
621 TextItem *textPtr; /* Item whose bbox is to be recomputed. */
622 {
623 Tk_CanvasTextInfo *textInfoPtr;
624 int leftX, topY, width, height, fudge;
625 Tk_State state = Tk_GetItemState(canvas, &textPtr->header);
626
627 Tk_FreeTextLayout(textPtr->textLayout);
628 textPtr->textLayout = Tk_ComputeTextLayout(textPtr->tkfont,
629 textPtr->text, textPtr->numChars, textPtr->width,
630 textPtr->justify, 0, &width, &height);
631
632 if (state == TK_STATE_HIDDEN || textPtr->color == NULL) {
633 width = height = 0;
634 }
635
636 /*
637 * Use overall geometry information to compute the top-left corner
638 * of the bounding box for the text item.
639 */
640
641 leftX = (int) floor(textPtr->x + 0.5);
642 topY = (int) floor(textPtr->y + 0.5);
643 switch (textPtr->anchor) {
644 case TK_ANCHOR_NW:
645 case TK_ANCHOR_N:
646 case TK_ANCHOR_NE:
647 break;
648
649 case TK_ANCHOR_W:
650 case TK_ANCHOR_CENTER:
651 case TK_ANCHOR_E:
652 topY -= height / 2;
653 break;
654
655 case TK_ANCHOR_SW:
656 case TK_ANCHOR_S:
657 case TK_ANCHOR_SE:
658 topY -= height;
659 break;
660 }
661 switch (textPtr->anchor) {
662 case TK_ANCHOR_NW:
663 case TK_ANCHOR_W:
664 case TK_ANCHOR_SW:
665 break;
666
667 case TK_ANCHOR_N:
668 case TK_ANCHOR_CENTER:
669 case TK_ANCHOR_S:
670 leftX -= width / 2;
671 break;
672
673 case TK_ANCHOR_NE:
674 case TK_ANCHOR_E:
675 case TK_ANCHOR_SE:
676 leftX -= width;
677 break;
678 }
679
680 textPtr->leftEdge = leftX;
681 textPtr->rightEdge = leftX + width;
682
683 /*
684 * Last of all, update the bounding box for the item. The item's
685 * bounding box includes the bounding box of all its lines, plus
686 * an extra fudge factor for the cursor border (which could
687 * potentially be quite large).
688 */
689
690 textInfoPtr = textPtr->textInfoPtr;
691 fudge = (textInfoPtr->insertWidth + 1) / 2;
692 if (textInfoPtr->selBorderWidth > fudge) {
693 fudge = textInfoPtr->selBorderWidth;
694 }
695 textPtr->header.x1 = leftX - fudge;
696 textPtr->header.y1 = topY;
697 textPtr->header.x2 = leftX + width + fudge;
698 textPtr->header.y2 = topY + height;
699 }
700
701 /*
702 *--------------------------------------------------------------
703 *
704 * DisplayCanvText --
705 *
706 * This procedure is invoked to draw a text item in a given
707 * drawable.
708 *
709 * Results:
710 * None.
711 *
712 * Side effects:
713 * ItemPtr is drawn in drawable using the transformation
714 * information in canvas.
715 *
716 *--------------------------------------------------------------
717 */
718
719 static void
720 DisplayCanvText(canvas, itemPtr, display, drawable, x, y, width, height)
721 Tk_Canvas canvas; /* Canvas that contains item. */
722 Tk_Item *itemPtr; /* Item to be displayed. */
723 Display *display; /* Display on which to draw item. */
724 Drawable drawable; /* Pixmap or window in which to draw item. */
725 int x, y, width, height; /* Describes region of canvas that must be
726 * redisplayed (not used). */
727 {
728 TextItem *textPtr;
729 Tk_CanvasTextInfo *textInfoPtr;
730 int selFirstChar, selLastChar;
731 short drawableX, drawableY;
732 Pixmap stipple;
733 Tk_Tile tile;
734 Tk_State state = Tk_GetItemState(canvas, itemPtr);
735
736 textPtr = (TextItem *) itemPtr;
737 textInfoPtr = textPtr->textInfoPtr;
738
739 stipple = textPtr->stipple;
740 if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
741 if (textPtr->activeStipple!=None) {
742 stipple = textPtr->activeStipple;
743 }
744 } else if (state==TK_STATE_DISABLED) {
745 if (textPtr->disabledStipple!=None) {
746 stipple = textPtr->disabledStipple;
747 }
748 }
749
750 if (textPtr->gc == None) {
751 return;
752 }
753
754 /*
755 * If we're stippling, then modify the stipple offset in the GC. Be
756 * sure to reset the offset when done, since the GC is supposed to be
757 * read-only.
758 */
759
760 if (stipple != None) {
761 Tk_CanvasSetOffset(canvas, textPtr->gc, &textPtr->tsoffset);
762 }
763
764 selFirstChar = -1;
765 selLastChar = 0; /* lint. */
766
767 if (textInfoPtr->selItemPtr == itemPtr) {
768 selFirstChar = textInfoPtr->selectFirst;
769 selLastChar = textInfoPtr->selectLast;
770 if (selLastChar > textPtr->numChars) {
771 selLastChar = textPtr->numChars - 1;
772 }
773 if ((selFirstChar >= 0) && (selFirstChar <= selLastChar)) {
774 int xFirst, yFirst, hFirst;
775 int xLast, yLast, wLast;
776
777 /*
778 * Draw a special background under the selection.
779 */
780
781 Tk_CharBbox(textPtr->textLayout, selFirstChar, &xFirst, &yFirst,
782 NULL, &hFirst);
783 Tk_CharBbox(textPtr->textLayout, selLastChar, &xLast, &yLast,
784 &wLast, NULL);
785
786 /*
787 * If the selection spans the end of this line, then display
788 * selection background all the way to the end of the line.
789 * However, for the last line we only want to display up to the
790 * last character, not the end of the line.
791 */
792
793 x = xFirst;
794 height = hFirst;
795 for (y = yFirst ; y <= yLast; y += height) {
796 if (y == yLast) {
797 width = xLast + wLast - x;
798 } else {
799 width = textPtr->rightEdge - textPtr->leftEdge - x;
800 }
801 Tk_CanvasDrawableCoords(canvas,
802 (double) (textPtr->leftEdge + x
803 - textInfoPtr->selBorderWidth),
804 (double) (textPtr->header.y1 + y),
805 &drawableX, &drawableY);
806 Tk_Fill3DRectangle(Tk_CanvasTkwin(canvas), drawable,
807 textInfoPtr->selBorder, drawableX, drawableY,
808 width + 2 * textInfoPtr->selBorderWidth,
809 height, textInfoPtr->selBorderWidth, TK_RELIEF_RAISED);
810 x = 0;
811 }
812 }
813 }
814
815 /*
816 * If the insertion point should be displayed, then draw a special
817 * background for the cursor before drawing the text. Note: if
818 * we're the cursor item but the cursor is turned off, then redraw
819 * background over the area of the cursor. This guarantees that
820 * the selection won't make the cursor invisible on mono displays,
821 * where both are drawn in the same color.
822 */
823
824 if ((textInfoPtr->focusItemPtr == itemPtr) && (textInfoPtr->gotFocus)) {
825 if (Tk_CharBbox(textPtr->textLayout, textPtr->insertPos,
826 &x, &y, NULL, &height)) {
827 Tk_CanvasDrawableCoords(canvas,
828 (double) (textPtr->leftEdge + x
829 - (textInfoPtr->insertWidth / 2)),
830 (double) (textPtr->header.y1 + y),
831 &drawableX, &drawableY);
832 Tk_SetCaretPos(Tk_CanvasTkwin(canvas), drawableX, drawableY,
833 height);
834 if (textInfoPtr->cursorOn) {
835 Tk_Fill3DRectangle(Tk_CanvasTkwin(canvas), drawable,
836 textInfoPtr->insertBorder,
837 drawableX, drawableY,
838 textInfoPtr->insertWidth, height,
839 textInfoPtr->insertBorderWidth, TK_RELIEF_RAISED);
840 } else if (textPtr->cursorOffGC != None) {
841 /*
842 * Redraw the background over the area of the cursor,
843 * even though the cursor is turned off. This
844 * guarantees that the selection won't make the cursor
845 * invisible on mono displays, where both may be drawn
846 * in the same color.
847 */
848
849 XFillRectangle(display, drawable, textPtr->cursorOffGC,
850 drawableX, drawableY,
851 (unsigned) textInfoPtr->insertWidth,
852 (unsigned) height);
853 }
854 }
855 }
856
857
858 /*
859 * Display the text in two pieces: draw the entire text item, then
860 * draw the selected text on top of it. The selected text then
861 * will only need to be drawn if it has different attributes (such
862 * as foreground color) than regular text.
863 */
864
865 Tk_CanvasDrawableCoords(canvas, (double) textPtr->leftEdge,
866 (double) textPtr->header.y1, &drawableX, &drawableY);
867 Tk_DrawTextLayout(display, drawable, textPtr->gc, textPtr->textLayout,
868 drawableX, drawableY, 0, -1);
869
870 if ((selFirstChar >= 0) && (textPtr->selTextGC != textPtr->gc)) {
871 Tk_DrawTextLayout(display, drawable, textPtr->selTextGC,
872 textPtr->textLayout, drawableX, drawableY, selFirstChar,
873 selLastChar + 1);
874 }
875
876 if (stipple != None) {
877 XSetTSOrigin(display, textPtr->gc, 0, 0);
878 }
879 }
880
881 /*
882 *--------------------------------------------------------------
883 *
884 * TextInsert --
885 *
886 * Insert characters into a text item at a given position.
887 *
888 * Results:
889 * None.
890 *
891 * Side effects:
892 * The text in the given item is modified. The cursor and
893 * selection positions are also modified to reflect the
894 * insertion.
895 *
896 *--------------------------------------------------------------
897 */
898
899 static void
900 TextInsert(canvas, itemPtr, index, ostring)
901 Tk_Canvas canvas; /* Canvas containing text item. */
902 Tk_Item *itemPtr; /* Text item to be modified. */
903 int index; /* Character index before which string is
904 * to be inserted. */
905 Tcl_Obj *ostring; /* New characters to be inserted. */
906 {
907 TextItem *textPtr = (TextItem *) itemPtr;
908 int byteIndex, byteCount, charsAdded;
909 char *new, *text;
910 Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
911
912 char * string = Tcl_GetString(ostring);
913
914 text = textPtr->text;
915
916 if (index < 0) {
917 index = 0;
918 }
919 if (index > textPtr->numChars) {
920 index = textPtr->numChars;
921 }
922 byteIndex = Tcl_UtfAtIndex(text, index) - text;
923 byteCount = strlen(string);
924 if (byteCount == 0) {
925 return;
926 }
927
928 new = (char *) ckalloc((unsigned) textPtr->numBytes + byteCount + 1);
929 memcpy(new, text, (size_t) byteIndex);
930 strcpy(new + byteIndex, string);
931 strcpy(new + byteIndex + byteCount, text + byteIndex);
932
933 ckfree(text);
934 textPtr->text = new;
935 charsAdded = Tcl_NumUtfChars(string, byteCount);
936 textPtr->numChars += charsAdded;
937 textPtr->numBytes += byteCount;
938
939 /*
940 * Inserting characters invalidates indices such as those for the
941 * selection and cursor. Update the indices appropriately.
942 */
943
944 if (textInfoPtr->selItemPtr == itemPtr) {
945 if (textInfoPtr->selectFirst >= index) {
946 textInfoPtr->selectFirst += charsAdded;
947 }
948 if (textInfoPtr->selectLast >= index) {
949 textInfoPtr->selectLast += charsAdded;
950 }
951 if ((textInfoPtr->anchorItemPtr == itemPtr)
952 && (textInfoPtr->selectAnchor >= index)) {
953 textInfoPtr->selectAnchor += charsAdded;
954 }
955 }
956 if (textPtr->insertPos >= index) {
957 textPtr->insertPos += charsAdded;
958 }
959 ComputeTextBbox(canvas, textPtr);
960 }
961
962 /*
963 *--------------------------------------------------------------
964 *
965 * TextDeleteChars --
966 *
967 * Delete one or more characters from a text item.
968 *
969 * Results:
970 * None.
971 *
972 * Side effects:
973 * Characters between "first" and "last", inclusive, get
974 * deleted from itemPtr, and things like the selection
975 * position get updated.
976 *
977 *--------------------------------------------------------------
978 */
979
980 static void
981 TextDeleteChars(canvas, itemPtr, first, last)
982 Tk_Canvas canvas; /* Canvas containing itemPtr. */
983 Tk_Item *itemPtr; /* Item in which to delete characters. */
984 int first; /* Character index of first character to
985 * delete. */
986 int last; /* Character index of last character to
987 * delete (inclusive). */
988 {
989 TextItem *textPtr = (TextItem *) itemPtr;
990 int byteIndex, byteCount, charsRemoved;
991 char *new, *text;
992 Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
993
994 text = textPtr->text;
995 if (first < 0) {
996 first = 0;
997 }
998 if (last >= textPtr->numChars) {
999 last = textPtr->numChars - 1;
1000 }
1001 if (first > last) {
1002 return;
1003 }
1004 charsRemoved = last + 1 - first;
1005
1006 byteIndex = Tcl_UtfAtIndex(text, first) - text;
1007 byteCount = Tcl_UtfAtIndex(text + byteIndex, charsRemoved)
1008 - (text + byteIndex);
1009
1010 new = (char *) ckalloc((unsigned) (textPtr->numBytes + 1 - byteCount));
1011 memcpy(new, text, (size_t) byteIndex);
1012 strcpy(new + byteIndex, text + byteIndex + byteCount);
1013
1014 ckfree(text);
1015 textPtr->text = new;
1016 textPtr->numChars -= charsRemoved;
1017 textPtr->numBytes -= byteCount;
1018
1019 /*
1020 * Update indexes for the selection and cursor to reflect the
1021 * renumbering of the remaining characters.
1022 */
1023
1024 if (textInfoPtr->selItemPtr == itemPtr) {
1025 if (textInfoPtr->selectFirst > first) {
1026 textInfoPtr->selectFirst -= charsRemoved;
1027 if (textInfoPtr->selectFirst < first) {
1028 textInfoPtr->selectFirst = first;
1029 }
1030 }
1031 if (textInfoPtr->selectLast >= first) {
1032 textInfoPtr->selectLast -= charsRemoved;
1033 if (textInfoPtr->selectLast < first - 1) {
1034 textInfoPtr->selectLast = first - 1;
1035 }
1036 }
1037 if (textInfoPtr->selectFirst > textInfoPtr->selectLast) {
1038 textInfoPtr->selItemPtr = NULL;
1039 }
1040 if ((textInfoPtr->anchorItemPtr == itemPtr)
1041 && (textInfoPtr->selectAnchor > first)) {
1042 textInfoPtr->selectAnchor -= charsRemoved;
1043 if (textInfoPtr->selectAnchor < first) {
1044 textInfoPtr->selectAnchor = first;
1045 }
1046 }
1047 }
1048 if (textPtr->insertPos > first) {
1049 textPtr->insertPos -= charsRemoved;
1050 if (textPtr->insertPos < first) {
1051 textPtr->insertPos = first;
1052 }
1053 }
1054 ComputeTextBbox(canvas, textPtr);
1055 return;
1056 }
1057
1058 /*
1059 *--------------------------------------------------------------
1060 *
1061 * TextToPoint --
1062 *
1063 * Computes the distance from a given point to a given
1064 * text item, in canvas units.
1065 *
1066 * Results:
1067 * The return value is 0 if the point whose x and y coordinates
1068 * are pointPtr[0] and pointPtr[1] is inside the text item. If
1069 * the point isn't inside the text item then the return value
1070 * is the distance from the point to the text item.
1071 *
1072 * Side effects:
1073 * None.
1074 *
1075 *--------------------------------------------------------------
1076 */
1077
1078 static double
1079 TextToPoint(canvas, itemPtr, pointPtr)
1080 Tk_Canvas canvas; /* Canvas containing itemPtr. */
1081 Tk_Item *itemPtr; /* Item to check against point. */
1082 double *pointPtr; /* Pointer to x and y coordinates. */
1083 {
1084 TextItem *textPtr;
1085 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1086 double value;
1087
1088 textPtr = (TextItem *) itemPtr;
1089 value = (double) Tk_DistanceToTextLayout(textPtr->textLayout,
1090 (int) pointPtr[0] - textPtr->leftEdge,
1091 (int) pointPtr[1] - textPtr->header.y1);
1092
1093 if ((state == TK_STATE_HIDDEN) || (textPtr->color == NULL) ||
1094 (textPtr->text == NULL) || (*textPtr->text == 0)) {
1095 value = 1.0e36;
1096 }
1097 return value;
1098 }
1099
1100 /*
1101 *--------------------------------------------------------------
1102 *
1103 * TextToArea --
1104 *
1105 * This procedure is called to determine whether an item
1106 * lies entirely inside, entirely outside, or overlapping
1107 * a given rectangle.
1108 *
1109 * Results:
1110 * -1 is returned if the item is entirely outside the area
1111 * given by rectPtr, 0 if it overlaps, and 1 if it is entirely
1112 * inside the given area.
1113 *
1114 * Side effects:
1115 * None.
1116 *
1117 *--------------------------------------------------------------
1118 */
1119
1120 static int
1121 TextToArea(canvas, itemPtr, rectPtr)
1122 Tk_Canvas canvas; /* Canvas containing itemPtr. */
1123 Tk_Item *itemPtr; /* Item to check against rectangle. */
1124 double *rectPtr; /* Pointer to array of four coordinates
1125 * (x1, y1, x2, y2) describing rectangular
1126 * area. */
1127 {
1128 TextItem *textPtr;
1129 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1130
1131 textPtr = (TextItem *) itemPtr;
1132 return Tk_IntersectTextLayout(textPtr->textLayout,
1133 (int) (rectPtr[0] + 0.5) - textPtr->leftEdge,
1134 (int) (rectPtr[1] + 0.5) - textPtr->header.y1,
1135 (int) (rectPtr[2] - rectPtr[0] + 0.5),
1136 (int) (rectPtr[3] - rectPtr[1] + 0.5));
1137 }
1138
1139 /*
1140 *--------------------------------------------------------------
1141 *
1142 * ScaleText --
1143 *
1144 * This procedure is invoked to rescale a text item.
1145 *
1146 * Results:
1147 * None.
1148 *
1149 * Side effects:
1150 * Scales the position of the text, but not the size
1151 * of the font for the text.
1152 *
1153 *--------------------------------------------------------------
1154 */
1155
1156 /* ARGSUSED */
1157 static void
1158 ScaleText(canvas, itemPtr, originX, originY, scaleX, scaleY)
1159 Tk_Canvas canvas; /* Canvas containing rectangle. */
1160 Tk_Item *itemPtr; /* Rectangle to be scaled. */
1161 double originX, originY; /* Origin about which to scale rect. */
1162 double scaleX; /* Amount to scale in X direction. */
1163 double scaleY; /* Amount to scale in Y direction. */
1164 {
1165 TextItem *textPtr = (TextItem *) itemPtr;
1166
1167 textPtr->x = originX + scaleX*(textPtr->x - originX);
1168 textPtr->y = originY + scaleY*(textPtr->y - originY);
1169 ComputeTextBbox(canvas, textPtr);
1170 return;
1171 }
1172
1173 /*
1174 *--------------------------------------------------------------
1175 *
1176 * TranslateText --
1177 *
1178 * This procedure is called to move a text item by a
1179 * given amount.
1180 *
1181 * Results:
1182 * None.
1183 *
1184 * Side effects:
1185 * The position of the text item is offset by (xDelta, yDelta),
1186 * and the bounding box is updated in the generic part of the
1187 * item structure.
1188 *
1189 *--------------------------------------------------------------
1190 */
1191
1192 static void
1193 TranslateText(canvas, itemPtr, deltaX, deltaY)
1194 Tk_Canvas canvas; /* Canvas containing item. */
1195 Tk_Item *itemPtr; /* Item that is being moved. */
1196 double deltaX, deltaY; /* Amount by which item is to be moved. */
1197 {
1198 TextItem *textPtr = (TextItem *) itemPtr;
1199
1200 textPtr->x += deltaX;
1201 textPtr->y += deltaY;
1202 ComputeTextBbox(canvas, textPtr);
1203 }
1204
1205 /*
1206 *--------------------------------------------------------------
1207 *
1208 * GetTextIndex --
1209 *
1210 * Parse an index into a text item and return either its value
1211 * or an error.
1212 *
1213 * Results:
1214 * A standard Tcl result. If all went well, then *indexPtr is
1215 * filled in with the index (into itemPtr) corresponding to
1216 * string. Otherwise an error message is left in
1217 * the interp's result.
1218 *
1219 * Side effects:
1220 * None.
1221 *
1222 *--------------------------------------------------------------
1223 */
1224
1225 static int
1226 GetTextIndex(interp, canvas, itemPtr, obj, indexPtr)
1227 Tcl_Interp *interp; /* Used for error reporting. */
1228 Tk_Canvas canvas; /* Canvas containing item. */
1229 Tk_Item *itemPtr; /* Item for which the index is being
1230 * specified. */
1231 Tcl_Obj *obj; /* Specification of a particular character
1232 * in itemPtr's text. */
1233 int *indexPtr; /* Where to store converted character
1234 * index. */
1235 {
1236 TextItem *textPtr = (TextItem *) itemPtr;
•1237 int length;
1238 int c;
1239 TkCanvas *canvasPtr = (TkCanvas *) canvas;
1240 Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
1241 char *string;
1242 int x, y;
1243 double dx,dy;
1244 char *end, *p;
1245 Tcl_Obj **objv;
1246
1247 if (Tcl_ListObjGetElements(interp, obj, &c, &objv) == TCL_OK && c == 2
1248 && Tcl_GetDoubleFromObj(interp, objv[0], &dx) == TCL_OK
1249 && Tcl_GetDoubleFromObj(interp, objv[1], &dy) == TCL_OK) {
1250 goto doxy;
1251 }
1252
1253 string = Tcl_GetStringFromObj(obj, &length);
1254 c = string[0];
1255 length = strlen(string);
1256
1257 if ((c == 'e') && (strncmp(string, "end", length) == 0)) {
1258 *indexPtr = textPtr->numChars;
1259 } else if ((c == 'i') && (strncmp(string, "insert", length) == 0)) {
1260 *indexPtr = textPtr->insertPos;
1261 } else if ((c == 's') && (strncmp(string, "sel.first", length) == 0)
1262 && (length >= 5)) {
1263 if (textInfoPtr->selItemPtr != itemPtr) {
1264 Tcl_SetResult(interp, "selection isn't in item", TCL_STATIC);
1265 return TCL_ERROR;
1266 }
1267 *indexPtr = textInfoPtr->selectFirst;
1268 } else if ((c == 's') && (strncmp(string, "sel.last", length) == 0)
1269 && (length >= 5)) {
1270 if (textInfoPtr->selItemPtr != itemPtr) {
1271 Tcl_SetResult(interp, "selection isn't in item", TCL_STATIC);
1272 return TCL_ERROR;
1273 }
1274 *indexPtr = textInfoPtr->selectLast;
1275 } else if (c == '@') {
1276 p = string+1;
1277 dx = strtod(p, &end);
1278 if ((end == p) || (*end != ',')) {
1279 goto badIndex;
1280 }
1281 p = end+1;
1282 dy = strtod(p, &end);
1283 if ((end == p) || (*end != 0)) {
1284 goto badIndex;
1285 }
1286 doxy:
1287 x = (int) ((dx < 0) ? dx - 0.5 : dx + 0.5);
1288 y = (int) ((dy < 0) ? dy - 0.5 : dy + 0.5);
1289 *indexPtr = Tk_PointToChar(textPtr->textLayout,
1290 x + canvasPtr->scrollX1 - textPtr->leftEdge,
1291 y + canvasPtr->scrollY1 - textPtr->header.y1);
1292 } else if (Tcl_GetIntFromObj((Tcl_Interp *)NULL, obj, indexPtr) == TCL_OK) {
1293 if (*indexPtr < 0){
1294 *indexPtr = 0;
1295 } else if (*indexPtr > textPtr->numChars) {
1296 *indexPtr = textPtr->numChars;
1297 }
1298 } else {
1299 /*
1300 * Some of the paths here leave messages in the interp's result,
1301 * so we have to clear it out before storing our own message.
1302 */
1303
1304 badIndex:
1305 Tcl_SetResult(interp, (char *) NULL, TCL_STATIC);
1306 Tcl_AppendResult(interp, "bad index \"", string, "\"",
1307 (char *) NULL);
1308 return TCL_ERROR;
1309 }
1310 return TCL_OK;
1311 }
1312
1313 /*
1314 *--------------------------------------------------------------
1315 *
1316 * SetTextCursor --
1317 *
1318 * Set the position of the insertion cursor in this item.
1319 *
1320 * Results:
1321 * None.
1322 *
1323 * Side effects:
1324 * The cursor position will change.
1325 *
1326 *--------------------------------------------------------------
1327 */
1328
1329 /* ARGSUSED */
1330 static void
1331 SetTextCursor(canvas, itemPtr, index)
1332 Tk_Canvas canvas; /* Record describing canvas widget. */
1333 Tk_Item *itemPtr; /* Text item in which cursor position is to
1334 * be set. */
1335 int index; /* Character index of character just before
1336 * which cursor is to be positioned. */
1337 {
1338 TextItem *textPtr = (TextItem *) itemPtr;
1339
1340 if (index < 0) {
1341 textPtr->insertPos = 0;
1342 } else if (index > textPtr->numChars) {
1343 textPtr->insertPos = textPtr->numChars;
1344 } else {
1345 textPtr->insertPos = index;
1346 }
1347 }
1348
1349 /*
1350 *--------------------------------------------------------------
1351 *
1352 * GetSelText --
1353 *
1354 * This procedure is invoked to return the selected portion
1355 * of a text item. It is only called when this item has
1356 * the selection.
1357 *
1358 * Results:
1359 * The return value is the number of non-NULL bytes stored
1360 * at buffer. Buffer is filled (or partially filled) with a
1361 * NULL-terminated string containing part or all of the selection,
1362 * as given by offset and maxBytes.
1363 *
1364 * Side effects:
1365 * None.
1366 *
1367 *--------------------------------------------------------------
1368 */
1369
1370 static int
1371 GetSelText(canvas, itemPtr, offset, buffer, maxBytes)
1372 Tk_Canvas canvas; /* Canvas containing selection. */
1373 Tk_Item *itemPtr; /* Text item containing selection. */
1374 int offset; /* Byte offset within selection of first
1375 * character to be returned. */
1376 char *buffer; /* Location in which to place selection. */
1377 int maxBytes; /* Maximum number of bytes to place at
1378 * buffer, not including terminating NULL
1379 * character. */
1380 {
1381 TextItem *textPtr = (TextItem *) itemPtr;
1382 int byteCount;
1383 char *text;
1384 CONST char *selStart, *selEnd;
1385 Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
1386
1387 if ((textInfoPtr->selectFirst < 0) ||
1388 (textInfoPtr->selectFirst > textInfoPtr->selectLast)) {
1389 return 0;
1390 }
1391 text = textPtr->text;
1392 selStart = Tcl_UtfAtIndex(text, textInfoPtr->selectFirst);
1393 selEnd = Tcl_UtfAtIndex(selStart,
1394 textInfoPtr->selectLast + 1 - textInfoPtr->selectFirst);
1395 byteCount = selEnd - selStart - offset;
1396 if (byteCount > maxBytes) {
1397 byteCount = maxBytes;
1398 }
1399 if (byteCount <= 0) {
1400 return 0;
1401 }
1402 memcpy(buffer, selStart + offset, (size_t) byteCount);
1403 buffer[byteCount] = '\0';
1404 return byteCount;
1405 }
1406
1407 /*
1408 *--------------------------------------------------------------
1409 *
1410 * TextToPostscript --
1411 *
1412 * This procedure is called to generate Postscript for
1413 * text items.
1414 *
1415 * Results:
1416 * The return value is a standard Tcl result. If an error
1417 * occurs in generating Postscript then an error message is
1418 * left in the interp's result, replacing whatever used
1419 * to be there. If no error occurs, then Postscript for the
1420 * item is appended to the result.
1421 *
1422 * Side effects:
1423 * None.
1424 *
1425 *--------------------------------------------------------------
1426 */
1427
1428 static int
1429 TextToPostscript(interp, canvas, itemPtr, prepass)
1430 Tcl_Interp *interp; /* Leave Postscript or error message here. */
1431 Tk_Canvas canvas; /* Information about overall canvas. */
1432 Tk_Item *itemPtr; /* Item for which Postscript is wanted. */
1433 int prepass; /* 1 means this is a prepass to collect
1434 * font information; 0 means final Postscript
1435 * is being created. */
1436 {
1437 TextItem *textPtr = (TextItem *) itemPtr;
1438 int x, y;
1439 Tk_FontMetrics fm;
1440 char *justify;
1441 char buffer[500];
1442 XColor *color;
1443 Pixmap stipple;
1444 Tk_State state = Tk_GetItemState(canvas, itemPtr);
1445
1446 color = textPtr->color;
1447 stipple = textPtr->stipple;
1448 if (state == TK_STATE_HIDDEN || textPtr->color == NULL ||
1449 textPtr->text == NULL || *textPtr->text == 0) {
1450 return TCL_OK;
1451 } else if (((TkCanvas *)canvas)->currentItemPtr == itemPtr) {
1452 if (textPtr->activeColor!=NULL) {
1453 color = textPtr->activeColor;
1454 }
1455 if (textPtr->activeStipple!=None) {
1456 stipple = textPtr->activeStipple;
1457 }
1458 } else if (state==TK_STATE_DISABLED) {
1459 if (textPtr->disabledColor!=NULL) {
1460 color = textPtr->disabledColor;
1461 }
1462 if (textPtr->disabledStipple!=None) {
1463 stipple = textPtr->disabledStipple;
1464 }
1465 }
1466
1467 if (Tk_CanvasPsFont(interp, canvas, textPtr->tkfont) != TCL_OK) {
1468 return TCL_ERROR;
1469 }
1470 if (prepass != 0) {
1471 return TCL_OK;
1472 }
1473 if (Tk_CanvasPsColor(interp, canvas, color) != TCL_OK) {
1474 return TCL_ERROR;
1475 }
1476 if (stipple != None) {
1477 Tcl_AppendResult(interp, "/StippleText {\n ",
1478 (char *) NULL);
1479 Tk_CanvasPsStipple(interp, canvas, stipple);
1480 Tcl_AppendResult(interp, "} bind def\n", (char *) NULL);
1481 }
1482
1483 sprintf(buffer, "%.15g %.15g [\n", textPtr->x,
1484 Tk_CanvasPsY(canvas, textPtr->y));
1485 Tcl_AppendResult(interp, buffer, (char *) NULL);
1486
1487 Tk_TextLayoutToPostscript(interp, textPtr->textLayout);
1488
1489 x = 0; y = 0; justify = NULL; /* lint. */
1490 switch (textPtr->anchor) {
1491 case TK_ANCHOR_NW: x = 0; y = 0; break;
1492 case TK_ANCHOR_N: x = 1; y = 0; break;
1493 case TK_ANCHOR_NE: x = 2; y = 0; break;
1494 case TK_ANCHOR_E: x = 2; y = 1; break;
1495 case TK_ANCHOR_SE: x = 2; y = 2; break;
1496 case TK_ANCHOR_S: x = 1; y = 2; break;
1497 case TK_ANCHOR_SW: x = 0; y = 2; break;
1498 case TK_ANCHOR_W: x = 0; y = 1; break;
1499 case TK_ANCHOR_CENTER: x = 1; y = 1; break;
1500 }
1501 switch (textPtr->justify) {
1502 case TK_JUSTIFY_LEFT: justify = "0"; break;
1503 case TK_JUSTIFY_CENTER: justify = "0.5";break;
1504 case TK_JUSTIFY_RIGHT: justify = "1"; break;
1505 }
1506
1507 Tk_GetFontMetrics(textPtr->tkfont, &fm);
1508 sprintf(buffer, "] %d %g %g %s %s DrawText\n",
1509 fm.linespace, x / -2.0, y / 2.0, justify,
1510 ((stipple == None) ? "false" : "true"));
1511 Tcl_AppendResult(interp, buffer, (char *) NULL);
1512
1513 return TCL_OK;
1514 }
1515
1516