(canvasPtr, itemPtr, beforeThis, string)
| 810 | * |
| 811 | * Side effects: |
| 812 | * The text in the given item is modified. The cursor and |
| 813 | * selection positions are also modified to reflect the |
| 814 | * insertion. |
| 815 | * |
| 816 | *-------------------------------------------------------------- |
| 817 | */ |
| 818 | |
| 819 | static int |
| 820 | TextInsert(canvasPtr, itemPtr, beforeThis, string) |
| 821 | Tk_Canvas *canvasPtr; /* Canvas containing text item. */ |
| 822 | Tk_Item *itemPtr; /* Text item to be modified. */ |
| 823 | int beforeThis; /* Index of character before which text is |
| 824 | * to be inserted. */ |
| 825 | char *string; /* New characters to be inserted. */ |
| 826 | { |
| 827 | register TextItem *textPtr = (TextItem *) itemPtr; |
| 828 | int length; |
| 829 | char *new; |
| 830 | |
| 831 | length = strlen(string); |
| 832 | if (length == 0) { |
| 833 | return TCL_OK; |
| 834 | } |
| 835 | if (beforeThis < 0) { |
| 836 | beforeThis = 0; |
| 837 | } |
| 838 | if (beforeThis > textPtr->numChars) { |
| 839 | beforeThis = textPtr->numChars; |
| 840 | } |
| 841 | |
| 842 | new = (char *) ckalloc((unsigned) (textPtr->numChars + length + 1)); |
| 843 | strncpy(new, textPtr->text, beforeThis); |
| 844 | strcpy(new+beforeThis, string); |
| 845 | strcpy(new+beforeThis+length, textPtr->text+beforeThis); |
| 846 | ckfree(textPtr->text); |
| 847 | textPtr->text = new; |
| 848 | textPtr->numChars += length; |
| 849 | |
| 850 | /* |
| 851 | * Inserting characters invalidates indices such as those for the |
| 852 | * selection and cursor. Update the indices appropriately. |
| 853 | */ |
| 854 | |
| 855 | if (canvasPtr->selItemPtr == itemPtr) { |
| 856 | if (canvasPtr->selectFirst >= beforeThis) { |
| 857 | canvasPtr->selectFirst += length; |
| 858 | } |
| 859 | if (canvasPtr->selectLast >= beforeThis) { |
| 860 | canvasPtr->selectLast += length; |
| 861 | } |
| 862 | if ((canvasPtr->anchorItemPtr == itemPtr) |
| 863 | && (canvasPtr->selectAnchor >= beforeThis)) { |
| 864 | canvasPtr->selectAnchor += length; |
| 865 | } |
| 866 | } |
| 867 | if (textPtr->cursorPos >= beforeThis) { |
| 868 | textPtr->cursorPos += length; |
nothing calls this directly
no test coverage detected