Tk Source Code

Check-in [0c9ad396]
Login
Bounty program for improvements to Tcl and certain Tcl packages.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Proof of concept for glyph-based indexing using a platform-specific TextManager - only implemented for macOS tk entry widgets so far.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | glyph_indexing
Files: files | file ages | folders
SHA3-256: 0c9ad396546ccb6b74374531d1842c47fb037d9ecab1ead7ce6ae023e46220fd
User & Date: marc_culler 2020-05-23 16:51:27
Context
2020-05-25
20:06
Add support for validation of Entry widgets. check-in: f37e7875 user: marc_culler tags: glyph_indexing
2020-05-23
16:51
Proof of concept for glyph-based indexing using a platform-specific TextManager - only implemented for macOS tk entry widgets so far. check-in: 0c9ad396 user: marc_culler tags: glyph_indexing
2020-05-22
13:40
Merge 8.6 check-in: 905e6388 user: jan.nijtmans tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tkEntry.c.

480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
...
527
528
529
530
531
532
533


534
535
536




537
538
539
540
541
542
543
...
731
732
733
734
735
736
737





738
739
740
741
742
743
744
....
1037
1038
1039
1040
1041
1042
1043

1044




1045
1046
1047
1048
1049
1050
1051
....
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
....
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
....
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149






2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
....
2173
2174
2175
2176
2177
2178
2179


2180





2181
2182
2183
2184

2185
2186
2187
2188
2189
2190
2191
....
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
....
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235




2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
....
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273





2274
2275
2276
2277
2278
2279
2280
....
2459
2460
2461
2462
2463
2464
2465


2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477




2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
....
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656

2657
2658
2659
2660
2661
2662
2663
2664
2665



















2666
2667
2668
2669
2670
2671
2672
 *	See the user documentation.
 *
 *--------------------------------------------------------------
 */

int
Tk_EntryObjCmd(
    ClientData dummy,	/* NULL. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Entry *entryPtr;
    Tk_OptionTable optionTable;
    Tk_Window tkwin;
    char *tmp;
    (void)dummy;

    if (objc < 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "pathName ?-option value ...?");
	return TCL_ERROR;
    }

................................................................................
    entryPtr->display		= Tk_Display(tkwin);
    entryPtr->interp		= interp;
    entryPtr->widgetCmd		= Tcl_CreateObjCommand(interp,
	    Tk_PathName(entryPtr->tkwin), EntryWidgetObjCmd, entryPtr,
	    EntryCmdDeletedProc);
    entryPtr->optionTable	= optionTable;
    entryPtr->type		= TK_ENTRY;


    tmp				= (char *)ckalloc(1);
    tmp[0]			= '\0';
    entryPtr->string		= tmp;




    entryPtr->selectFirst	= -1;
    entryPtr->selectLast	= -1;

    entryPtr->cursor		= NULL;
    entryPtr->exportSelection	= 1;
    entryPtr->justify		= TK_JUSTIFY_LEFT;
    entryPtr->relief		= TK_RELIEF_FLAT;
................................................................................
	    Tcl_WrongNumArgs(interp, 2, objv, "string");
	    goto error;
	}
	if (GetEntryIndex(interp, entryPtr, objv[2],
		&index) != TCL_OK) {
	    goto error;
	}





	Tcl_SetObjResult(interp, Tcl_NewWideIntObj(index));
	break;
    }

    case COMMAND_INSERT: {
	int index, code;

................................................................................
    Entry *entryPtr = (Entry *)memPtr;

    /*
     * Free up all the stuff that requires special handling, then let
     * Tk_FreeOptions handle all the standard option-related stuff.
     */


    ckfree((char *)entryPtr->string);




    if (entryPtr->textVarName != NULL) {
	Tcl_UntraceVar2(entryPtr->interp, entryPtr->textVarName,
		NULL, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
		EntryTextVarProc, entryPtr);
	entryPtr->flags &= ~ENTRY_VAR_TRACED;
    }
    if (entryPtr->textGC != NULL) {
................................................................................
	}
    }

    /*
     * Draw the text in two pieces: first the unselected portion, then the
     * selected portion on top of it.
     */

    if ((entryPtr->numChars != 0) || (entryPtr->placeholderChars == 0)) {
        Tk_DrawTextLayout(entryPtr->display, pixmap, entryPtr->textGC,
	    entryPtr->textLayout, entryPtr->layoutX, entryPtr->layoutY,
	    entryPtr->leftIndex, entryPtr->numChars);
    } else {
	Tk_DrawTextLayout(entryPtr->display, pixmap, entryPtr->placeholderGC,
	    entryPtr->placeholderLayout, entryPtr->placeholderX, entryPtr->layoutY,
................................................................................
}
 
/*
 *----------------------------------------------------------------------
 *
 * InsertChars --
 *
 *	Add new characters to an entry widget.
 *
 * Results:
 *	A standard Tcl result. If an error occurred then an error message is
 *	left in the interp's result.
 *
 * Side effects:
 *	New information gets added to entryPtr; it will be redisplayed soon,
................................................................................

static int
InsertChars(
    Entry *entryPtr,		/* Entry that is to get the new elements. */
    int index,			/* Add the new elements before this character
				 * index. */
    const char *value)		/* New characters to add (NULL-terminated
				 * string). */
{
    size_t byteIndex, byteCount, newByteCount, oldChars, charsAdded;
    const char *string;
    char *newStr;

    string = entryPtr->string;
    byteIndex = Tcl_UtfAtIndex(string, index) - string;
    byteCount = strlen(value);
    if (byteCount == 0) {
	return TCL_OK;
    }







    newByteCount = entryPtr->numBytes + byteCount + 1;
    newStr = (char *)ckalloc(newByteCount);
    memcpy(newStr, string, byteIndex);
    strcpy(newStr + byteIndex, value);
    strcpy(newStr + byteIndex + byteCount, string + byteIndex);

    if ((entryPtr->validate == VALIDATE_KEY ||
	    entryPtr->validate == VALIDATE_ALL) &&
	    EntryValidateChange(entryPtr, value, newStr, index,
		    VALIDATE_INSERT) != TCL_OK) {
	ckfree(newStr);
	return TCL_OK;
    }

    ckfree((char *)string);
    entryPtr->string = newStr;

    /*
     * The following construction is used because inserting improperly formed
     * UTF-8 sequences between other improperly formed UTF-8 sequences could
     * result in actually forming valid UTF-8 sequences; the number of
     * characters added may not be Tcl_NumUtfChars(string, -1), because of
     * context. The actual number of characters added is how many characters
................................................................................
     * are in the string now minus the number that used to be there.
     */

    oldChars = entryPtr->numChars;
    entryPtr->numChars = Tcl_NumUtfChars(newStr, TCL_INDEX_NONE);
    charsAdded = entryPtr->numChars - oldChars;
    entryPtr->numBytes += byteCount;








    if (entryPtr->displayString == string) {
	entryPtr->displayString = newStr;
	entryPtr->numDisplayBytes = entryPtr->numBytes;
    }


    /*
     * Inserting characters invalidates all indexes into the string. Touch up
     * the indexes so that they still refer to the same characters (at new
     * positions). When updating the selection end-points, don't include the
     * new text in the selection unless it was completely surrounded by the
     * selection.
................................................................................
}
 
/*
 *----------------------------------------------------------------------
 *
 * DeleteChars --
 *
 *	Remove one or more characters from an entry widget.
 *
 * Results:
 *	A standard Tcl result. If an error occurred then an error message is
 *	left in the interp's result.
 *
 * Side effects:
 *	Memory gets freed, the entry gets modified and (eventually)
................................................................................
 *
 *----------------------------------------------------------------------
 */

static int
DeleteChars(
    Entry *entryPtr,		/* Entry widget to modify. */
    int index,			/* Index of first character to delete. */
    int count)			/* How many characters to delete. */
{




    int byteIndex, byteCount, newByteCount;
    const char *string;
    char *newStr, *toDelete;

    if ((index + count) > entryPtr->numChars) {
	count = entryPtr->numChars - index;
    }
    if (count <= 0) {
	return TCL_OK;
    }
................................................................................
	ckfree(newStr);
	ckfree(toDelete);
	return TCL_OK;
    }

    ckfree(toDelete);
    ckfree((char *)entryPtr->string);
    entryPtr->string = newStr;
    entryPtr->numChars -= count;
    entryPtr->numBytes -= byteCount;






    if (entryPtr->displayString == string) {
	entryPtr->displayString = newStr;
	entryPtr->numDisplayBytes = entryPtr->numBytes;
    }

    /*
................................................................................
	    entryPtr->flags &= ~VALIDATE_ABORT;
	    ckfree((char *)value);
	    return;
	}
    }

    oldSource = entryPtr->string;


    ckfree((char *)entryPtr->string);

    if (malloced) {
	entryPtr->string = value;
    } else {
	char *tmp = (char *)ckalloc(valueLen + 1);

	strcpy(tmp, value);
	entryPtr->string = tmp;
    }
    entryPtr->numBytes = valueLen;
    entryPtr->numChars = Tcl_NumUtfChars(value, valueLen);





    if (entryPtr->displayString == oldSource) {
	entryPtr->displayString = entryPtr->string;
	entryPtr->numDisplayBytes = entryPtr->numBytes;
    }

    if (entryPtr->selectFirst >= 0) {
	if (entryPtr->selectFirst >= entryPtr->numChars) {
	    entryPtr->selectFirst = -1;
	    entryPtr->selectLast = -1;
	} else if (entryPtr->selectLast > entryPtr->numChars) {
................................................................................
 *---------------------------------------------------------------------------
 */

static int
GetEntryIndex(
    Tcl_Interp *interp,		/* For error messages. */
    Entry *entryPtr,		/* Entry for which the index is being
				 * specified. */
    Tcl_Obj *indexObj,	/* Specifies character in entryPtr. */
    int *indexPtr)		/* Where to store converted character index */
{
    TkSizeT length, idx;
    const char *string;


    if (TCL_OK == TkGetIntForIndex(indexObj, entryPtr->numChars - 1, 1, &idx)) {
	if (idx == TCL_INDEX_NONE) {
	    idx = 0;
	} else if (idx > (TkSizeT)entryPtr->numChars) {
	    idx = (TkSizeT)entryPtr->numChars;
	}
	*indexPtr = (int)idx;
	return TCL_OK;
    }




















    string = TkGetStringFromObj(indexObj, &length);

    switch (string[0]) {
    case 'a':
	if (strncmp(string, "anchor", length) != 0) {
	    goto badIndex;






|







<







 







>
>
|
|
|
>
>
>
>







 







>
>
>
>
>







 







>

>
>
>
>







 







<







 







|







 







|

|
<


<
<





>
>
>
>
>
>







|
|
|





<







 







>
>

>
>
>
>
>
|



>







 







|







 







|
|

>
>
>
>

<
|







 







<


>
>
>
>
>







 







>
>












>
>
>
>



|







 







|
|





>









>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

495
496
497
498
499
500
501
...
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
...
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
....
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
....
1778
1779
1780
1781
1782
1783
1784

1785
1786
1787
1788
1789
1790
1791
....
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
....
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153

2154
2155


2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181

2182
2183
2184
2185
2186
2187
2188
....
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
....
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
....
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264

2265
2266
2267
2268
2269
2270
2271
2272
....
2291
2292
2293
2294
2295
2296
2297

2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
....
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
....
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
 *	See the user documentation.
 *
 *--------------------------------------------------------------
 */

int
Tk_EntryObjCmd(
    ClientData dummy,		/* NULL. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Entry *entryPtr;
    Tk_OptionTable optionTable;
    Tk_Window tkwin;

    (void)dummy;

    if (objc < 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "pathName ?-option value ...?");
	return TCL_ERROR;
    }

................................................................................
    entryPtr->display		= Tk_Display(tkwin);
    entryPtr->interp		= interp;
    entryPtr->widgetCmd		= Tcl_CreateObjCommand(interp,
	    Tk_PathName(entryPtr->tkwin), EntryWidgetObjCmd, entryPtr,
	    EntryCmdDeletedProc);
    entryPtr->optionTable	= optionTable;
    entryPtr->type		= TK_ENTRY;
#ifndef USE_GLYPH_INDEXES
    {
	char *tmp = (char *)ckalloc(1);
	tmp[0] = '\0';
	entryPtr->string = tmp;
    }
#else
    entryPtr->manager		= TkpTextManagerCreate(&entryPtr->string);
#endif
    entryPtr->selectFirst	= -1;
    entryPtr->selectLast	= -1;

    entryPtr->cursor		= NULL;
    entryPtr->exportSelection	= 1;
    entryPtr->justify		= TK_JUSTIFY_LEFT;
    entryPtr->relief		= TK_RELIEF_FLAT;
................................................................................
	    Tcl_WrongNumArgs(interp, 2, objv, "string");
	    goto error;
	}
	if (GetEntryIndex(interp, entryPtr, objv[2],
		&index) != TCL_OK) {
	    goto error;
	}

#ifdef USE_GLYPH_INDEXES
	index = TkpTextManagerContainingCluster(entryPtr->manager, index);
#endif

	Tcl_SetObjResult(interp, Tcl_NewWideIntObj(index));
	break;
    }

    case COMMAND_INSERT: {
	int index, code;

................................................................................
    Entry *entryPtr = (Entry *)memPtr;

    /*
     * Free up all the stuff that requires special handling, then let
     * Tk_FreeOptions handle all the standard option-related stuff.
     */

#ifndef USE_GLYPH_INDEXING
    ckfree((char *)entryPtr->string);
#else
    TkpTextManagerDestroy(entryPtr->manager);
#endif
	
    if (entryPtr->textVarName != NULL) {
	Tcl_UntraceVar2(entryPtr->interp, entryPtr->textVarName,
		NULL, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
		EntryTextVarProc, entryPtr);
	entryPtr->flags &= ~ENTRY_VAR_TRACED;
    }
    if (entryPtr->textGC != NULL) {
................................................................................
	}
    }

    /*
     * Draw the text in two pieces: first the unselected portion, then the
     * selected portion on top of it.
     */

    if ((entryPtr->numChars != 0) || (entryPtr->placeholderChars == 0)) {
        Tk_DrawTextLayout(entryPtr->display, pixmap, entryPtr->textGC,
	    entryPtr->textLayout, entryPtr->layoutX, entryPtr->layoutY,
	    entryPtr->leftIndex, entryPtr->numChars);
    } else {
	Tk_DrawTextLayout(entryPtr->display, pixmap, entryPtr->placeholderGC,
	    entryPtr->placeholderLayout, entryPtr->placeholderX, entryPtr->layoutY,
................................................................................
}
 
/*
 *----------------------------------------------------------------------
 *
 * InsertChars --
 *
 *	Add new grapheme clusters to an entry widget.
 *
 * Results:
 *	A standard Tcl result. If an error occurred then an error message is
 *	left in the interp's result.
 *
 * Side effects:
 *	New information gets added to entryPtr; it will be redisplayed soon,
................................................................................

static int
InsertChars(
    Entry *entryPtr,		/* Entry that is to get the new elements. */
    int index,			/* Add the new elements before this character
				 * index. */
    const char *value)		/* New characters to add (NULL-terminated
				 * UTF-8 encoded string). */
{
    int byteCount, oldChars, charsAdded;

    char *newStr;



    byteCount = strlen(value);
    if (byteCount == 0) {
	return TCL_OK;
    }

#ifndef USE_GLYPH_INDEXES
    size_t  byteIndex, newByteCount;
    const char *string;
    string = entryPtr->string;
    byteIndex = Tcl_UtfAtIndex(string, index) - string;

    newByteCount = entryPtr->numBytes + byteCount + 1;
    newStr = (char *)ckalloc(newByteCount);
    memcpy(newStr, string, byteIndex);
    strcpy(newStr + byteIndex, value);
    strcpy(newStr + byteIndex + byteCount, string + byteIndex);

    if ((entryPtr->validate == VALIDATE_KEY ||
	entryPtr->validate == VALIDATE_ALL) &&
	EntryValidateChange(entryPtr, value, newStr, index,
			    VALIDATE_INSERT) != TCL_OK) {
	ckfree(newStr);
	return TCL_OK;
    }

    ckfree((char *)string);


    /*
     * The following construction is used because inserting improperly formed
     * UTF-8 sequences between other improperly formed UTF-8 sequences could
     * result in actually forming valid UTF-8 sequences; the number of
     * characters added may not be Tcl_NumUtfChars(string, -1), because of
     * context. The actual number of characters added is how many characters
................................................................................
     * are in the string now minus the number that used to be there.
     */

    oldChars = entryPtr->numChars;
    entryPtr->numChars = Tcl_NumUtfChars(newStr, TCL_INDEX_NONE);
    charsAdded = entryPtr->numChars - oldChars;
    entryPtr->numBytes += byteCount;
#else
    oldChars = entryPtr->numChars;

    newStr = (char *) TkpTextManagerInsert(entryPtr->manager, index, value,
			  &entryPtr->numChars, &entryPtr->numBytes);
    charsAdded = entryPtr->numChars - oldChars;
#endif

    if (entryPtr->displayString == entryPtr->string) {
	entryPtr->displayString = newStr;
	entryPtr->numDisplayBytes = entryPtr->numBytes;
    }
    entryPtr->string = newStr;

    /*
     * Inserting characters invalidates all indexes into the string. Touch up
     * the indexes so that they still refer to the same characters (at new
     * positions). When updating the selection end-points, don't include the
     * new text in the selection unless it was completely surrounded by the
     * selection.
................................................................................
}
 
/*
 *----------------------------------------------------------------------
 *
 * DeleteChars --
 *
 *	Remove one or more grapheme clusters from an entry widget.
 *
 * Results:
 *	A standard Tcl result. If an error occurred then an error message is
 *	left in the interp's result.
 *
 * Side effects:
 *	Memory gets freed, the entry gets modified and (eventually)
................................................................................
 *
 *----------------------------------------------------------------------
 */

static int
DeleteChars(
    Entry *entryPtr,		/* Entry widget to modify. */
    int index,			/* Index of first cluster to delete. */
    int count)			/* How many clusters to delete. */
{
    char *newStr;
    const char *string = entryPtr->string;

#ifndef USE_GLYPH_INDEXES
    int byteIndex, byteCount, newByteCount;

    char *toDelete;

    if ((index + count) > entryPtr->numChars) {
	count = entryPtr->numChars - index;
    }
    if (count <= 0) {
	return TCL_OK;
    }
................................................................................
	ckfree(newStr);
	ckfree(toDelete);
	return TCL_OK;
    }

    ckfree(toDelete);
    ckfree((char *)entryPtr->string);

    entryPtr->numChars -= count;
    entryPtr->numBytes -= byteCount;
#else
    newStr = (char *) TkpTextManagerDelete(entryPtr->manager, index, count,
			  &entryPtr->numChars, &entryPtr->numBytes, &count);
#endif
    entryPtr->string = newStr;

    if (entryPtr->displayString == string) {
	entryPtr->displayString = newStr;
	entryPtr->numDisplayBytes = entryPtr->numBytes;
    }

    /*
................................................................................
	    entryPtr->flags &= ~VALIDATE_ABORT;
	    ckfree((char *)value);
	    return;
	}
    }

    oldSource = entryPtr->string;

#ifndef USE_GLYPH_INDEXES
    ckfree((char *)entryPtr->string);

    if (malloced) {
	entryPtr->string = value;
    } else {
	char *tmp = (char *)ckalloc(valueLen + 1);

	strcpy(tmp, value);
	entryPtr->string = tmp;
    }
    entryPtr->numBytes = valueLen;
    entryPtr->numChars = Tcl_NumUtfChars(value, valueLen);
#else
    entryPtr->string = TkpTextManagerSet(entryPtr->manager, value,
			   &entryPtr->numChars, &entryPtr->numBytes);
#endif

    if (entryPtr->displayString == oldSource) {
	entryPtr->displayString = entryPtr->string;
	entryPtr->numDisplayBytes = entryPtr->numChars;
    }

    if (entryPtr->selectFirst >= 0) {
	if (entryPtr->selectFirst >= entryPtr->numChars) {
	    entryPtr->selectFirst = -1;
	    entryPtr->selectLast = -1;
	} else if (entryPtr->selectLast > entryPtr->numChars) {
................................................................................
 *---------------------------------------------------------------------------
 */

static int
GetEntryIndex(
    Tcl_Interp *interp,		/* For error messages. */
    Entry *entryPtr,		/* Entry for which the index is being
				   specified. */
    Tcl_Obj *indexObj,		/* Specifies character in entryPtr. */
    int *indexPtr)		/* Where to store converted character index */
{
    TkSizeT length, idx;
    const char *string;

#ifndef USE_GLYPH_INDEXES
    if (TCL_OK == TkGetIntForIndex(indexObj, entryPtr->numChars - 1, 1, &idx)) {
	if (idx == TCL_INDEX_NONE) {
	    idx = 0;
	} else if (idx > (TkSizeT)entryPtr->numChars) {
	    idx = (TkSizeT)entryPtr->numChars;
	}
	*indexPtr = (int)idx;
	return TCL_OK;
    }
#else

    /*
     * If we are doing glyph indexing, integer objects are given as glyph
     * indexes so we need to convert them to character indexes.
     */

    TkSizeT clusterLength = (TkSizeT) TkpTextManagerNumClusters(
	                                  entryPtr->manager);
    if (TCL_OK == TkGetIntForIndex(indexObj, clusterLength, 1, &idx)) {
	if (idx == TCL_INDEX_NONE) {
	    idx = 0;
	} else if (idx > clusterLength) {
	    idx = (TkSizeT) clusterLength + 1;
	}
	*indexPtr = TkpTextManagerClusterBaseChar(entryPtr->manager, idx);
	return TCL_OK;
    }
#endif

    string = TkGetStringFromObj(indexObj, &length);

    switch (string[0]) {
    case 'a':
	if (strncmp(string, "anchor", length) != 0) {
	    goto badIndex;

Changes to generic/tkEntry.h.

34
35
36
37
38
39
40

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
				 * other things, so that resources can be
				 * freed even after tkwin has gone away. */
    Tcl_Interp *interp;		/* Interpreter associated with entry. */
    Tcl_Command widgetCmd;	/* Token for entry's widget command. */
    Tk_OptionTable optionTable;	/* Table that defines configuration options
				 * available for this widget. */
    enum EntryType type;	/* Specialized type of Entry widget */


    /*
     * Fields that are set by widget commands other than "configure".
     */

    const char *string;		/* Pointer to storage for string;
				 * NULL-terminated; malloc-ed. */
    int insertPos;		/* Character index before which next typed
				 * character will be inserted. */

    /*
     * Information about what's selected, if any.
     */

    int selectFirst;		/* Character index of first selected character
				 * (-1 means nothing selected. */






>





|
|
<
<







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48


49
50
51
52
53
54
55
				 * other things, so that resources can be
				 * freed even after tkwin has gone away. */
    Tcl_Interp *interp;		/* Interpreter associated with entry. */
    Tcl_Command widgetCmd;	/* Token for entry's widget command. */
    Tk_OptionTable optionTable;	/* Table that defines configuration options
				 * available for this widget. */
    enum EntryType type;	/* Specialized type of Entry widget */
    ClientData manager;		/* Platform-specific TextManager. */

    /*
     * Fields that are set by widget commands other than "configure".
     */

    const char *string;		/* Pointer to storage for string. */
    int insertPos;		/* Index of the character after the cursor */



    /*
     * Information about what's selected, if any.
     */

    int selectFirst;		/* Character index of first selected character
				 * (-1 means nothing selected. */

Changes to generic/tkInt.h.

1345
1346
1347
1348
1349
1350
1351
1352
1353


































1354
1355
1356
1357
1358
1359
1360
MODULE_SCOPE void	TkpCancelWarp(TkDisplay *dispPtr);
MODULE_SCOPE int	TkListCreateFrame(ClientData clientData,
			    Tcl_Interp *interp, Tcl_Obj *listObj,
			    int toplevel, Tcl_Obj *nameObj);
MODULE_SCOPE void	TkRotatePoint(double originX, double originY,
			    double sine, double cosine, double *xPtr,
			    double *yPtr);
MODULE_SCOPE int TkGetIntForIndex(Tcl_Obj *, TkSizeT, int lastOK, TkSizeT*);




































#ifdef _WIN32
#define TkParseColor XParseColor
#else
MODULE_SCOPE Status TkParseColor (Display * display,
				Colormap map, const char* spec,
				XColor * colorPtr);






|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
MODULE_SCOPE void	TkpCancelWarp(TkDisplay *dispPtr);
MODULE_SCOPE int	TkListCreateFrame(ClientData clientData,
			    Tcl_Interp *interp, Tcl_Obj *listObj,
			    int toplevel, Tcl_Obj *nameObj);
MODULE_SCOPE void	TkRotatePoint(double originX, double originY,
			    double sine, double cosine, double *xPtr,
			    double *yPtr);
MODULE_SCOPE int	TkGetIntForIndex(Tcl_Obj *, TkSizeT, int lastOK, TkSizeT*);

/*
 * Unicode strings describing text are actually sequences of so-called grapheme
 * clusters, each of which describes what the user perceives as a single glyph.
 * When editing text, users expect to insert or delete entire glyphs, so the
 * underlying string operations should insert or delete entire grapheme clusters.
 * Also, indexes into the string, such as the insert cursor, should refer to
 * a glyph, not a character in the string and underlying character indexes should
 * always point to the base character of a grapheme cluster.
 *
 * The functions declared below provide an interface to an abstract TextManager
 * object which can recognize boundaries of grapheme clusters and manage a
 * glyph-based indexing system for Tk text-related widgets.  To enable the features
 * described above, a platform port should define the conditional compilation
 * variable USE_GLYPH_INDEXES and implement these functions.
 */

#if defined(MAC_OSX_TK)
#define USE_GLYPH_INDEXES
MODULE_SCOPE ClientData  TkpTextManagerCreate(const char **initialString);
MODULE_SCOPE void	 TkpTextManagerDestroy(ClientData clientData);
MODULE_SCOPE int	 TkpTextManagerClusterBaseChar(ClientData clientData,
							   int clusterIndex);
MODULE_SCOPE int	 TkpTextManagerContainingCluster(ClientData clientData,
							   int charIndex);
MODULE_SCOPE int	 TkpTextManagerNumClusters(ClientData clientData);
MODULE_SCOPE const char* TkpTextManagerInsert(ClientData clientData,
			     int charIndex, const char *value,
			     int *numChars, int *numBytes);
MODULE_SCOPE const char* TkpTextManagerSet(ClientData clientData,
			     const char *value, int *numChars, int *numBytes);
MODULE_SCOPE const char* TkpTextManagerDelete(ClientData clientData, int charIndex,
			     int count, int *numChars, int *numBytes,
			     int *charsDeleted);
#endif

#ifdef _WIN32
#define TkParseColor XParseColor
#else
MODULE_SCOPE Status TkParseColor (Display * display,
				Colormap map, const char* spec,
				XColor * colorPtr);

Changes to macosx/tkMacOSXFont.c.

179
180
181
182
183
184
185

















































































































































































































































































































































































































186
187
188
189
190
191
192
    return _ds;
}

#ifndef __clang__
@synthesize UTF8String = _UTF8String;
#endif
@end

















































































































































































































































































































































































































 
#define GetNSFontTraitsFromTkFontAttributes(faPtr) \
	((faPtr)->weight == TK_FW_BOLD ? NSBoldFontMask : NSUnboldFontMask) | \
	((faPtr)->slant == TK_FS_ITALIC ? NSItalicFontMask : NSUnitalicFontMask)
 
/*
 *---------------------------------------------------------------------------






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
    return _ds;
}

#ifndef __clang__
@synthesize UTF8String = _UTF8String;
#endif
@end
 
/*
 * Implementation of the TextManager for macOS.
 *
 * The TextManager is really nothing more than an NSMutableString, except
 * that Apple says this about the UTF8String property:
 *
 *    "This C string is a pointer to a structure inside the string object,
 *     which may have a lifetime shorter than the string object and will
 *     certainly not have a longer lifetime. Therefore, you should copy the C
 *     string if it needs to be stored outside of the memory context in which
 *     you use this property."
 *
 */

typedef struct TextManager {
    NSMutableString *string;
    char *utf8string;
    int numClusters;
} TextManager;

/*
 * Static functions used to access a TextManager.
 */

/*
 * Called after the NSMutableString has been changed.  It resets the counts of
 * bytes and chars and saves a copy of the UTF8String of the NSMutableString.
 */

static char *
TextManagerUpdate(
    TextManager *managerPtr,
    int *numChars,
    int *numBytes)
{
    *numChars = [managerPtr->string length];
    *numBytes = [managerPtr->string lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    if (managerPtr->utf8string) {
	ckfree(managerPtr->utf8string);
    }
    managerPtr->utf8string = ckalloc(*numBytes + 1);
    strcpy(managerPtr->utf8string, [managerPtr->string UTF8String]);
    managerPtr->numClusters = -1;  /* recomputed by TkpTextManagerNumClusters */
    return managerPtr->utf8string;
}

/*
 * Returns the character index of the base character of the cluster with
 * given index, or the string length.
 *
 * NOTE: A future optimization could cache the character index of the last base
 * character which was looked up inside the TextManager, as a hint.  Then the
 * next time a base character index were needed, the search could begin at the
 * hint location instead of the beginning of the string.  Since changes to the
 * insert cursor are usually small, this would be quite a bit faster.
 */

static NSUInteger
IndexOfClusterBase(
    NSString *string,
    NSUInteger clusterIndex)
{
    NSRange clusterRange = NSMakeRange(0, 0);
    NSUInteger i, charIndex = 0, end = [string length];

    if (end > 0) {
	for (i = 0; i < clusterIndex; i++) {
	    clusterRange = [string rangeOfComposedCharacterSequenceAtIndex:charIndex];
	    charIndex = clusterRange.location + clusterRange.length;
	    if (charIndex >= end) {
		charIndex = end;
		break;
	    }
	}
    }
    return charIndex;
}

/*
 * Returns the index of the cluster which constains the the character with
 * given index, or the total number of clusters.
 *
 * NOTE: This could also benefit from a cached hint.
 */

static NSUInteger
IndexOfContainingCluster(
    NSString *string,
    NSUInteger charIndex)
{
    NSRange clusterRange;
    NSUInteger idx, clusterIndex;

    if (charIndex > string.length) {
	charIndex = string.length;
    }
    for (idx = 0, clusterIndex = 0; idx < charIndex; clusterIndex++) {
	clusterRange = [string rangeOfComposedCharacterSequenceAtIndex:idx];
	idx += clusterRange.length;
	if (idx > charIndex) {
	    return clusterIndex;
	}
    }
    return clusterIndex;
}

/*
 * Computes the range of characters filled by a range of clusters of given
 * length, such that a given character is contained in the first cluster.
 * Used by TkpTextManagerDelete to determine which chars to delete from
 * the NSMutableString.
 */

static NSRange
CharRangeFromClusterRange(
    NSString *string,
    NSUInteger charIndex,
    NSUInteger clusterCount)
{
    NSRange clusterRange;
    NSUInteger max = string.length, charLocation, charLength = 0;

    if (max == 0 || charIndex >= max) {
	return NSMakeRange(max, 0);
    }
    clusterRange = [string rangeOfComposedCharacterSequenceAtIndex:charIndex];
    charLocation = clusterRange.location;
    charIndex = charLocation;

    while (clusterCount--) {
	clusterRange = [string rangeOfComposedCharacterSequenceAtIndex:charIndex];
	charLength += clusterRange.length;
	charIndex += clusterRange.length;
	if (charIndex >= max) {
	    return NSMakeRange(charLocation, max - charLocation);
	}
    }
    return NSMakeRange(charLocation, charLength);
}

/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerCreate --
 *
 *	Allocate and initialize a TextManager to handle glyph-based indexing.
 *
 * Results:
 *	A pointer to a TextManager, cast as an opaque ClientData type.
 *
 * Side effects:
 *	Allocates a TextManager, an NSString and a UTF-8 char buffer.
 *      Also stores a pointer to the initial (empty) UTF-8 string in
 *      the variable referenced by the initialString parameter.
 *
 *---------------------------------------------------------------------------
 */

ClientData
TkpTextManagerCreate(
    const char **initialString)
{
    TextManager *managerPtr = (TextManager *) ckalloc(sizeof(TextManager));
    int dummy;

    managerPtr->string = [[NSMutableString string] retain];
    managerPtr->utf8string = NULL;
    *initialString = TextManagerUpdate(managerPtr, &dummy, &dummy);
    return (ClientData) managerPtr;
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerDestroy --
 *
 *	Free the resources associated to a TextManager
 *
 * Results:
 *	None
 *
 * Side effects:
 *	Release the NSString, frees the UTF8 string and the TextManager.
 *
 *---------------------------------------------------------------------------
 */

void
TkpTextManagerDestroy(
ClientData clientData)
{
    TextManager *managerPtr = (TextManager *) clientData;

    [managerPtr->string release];
    if (managerPtr->utf8string) {
	ckfree(managerPtr->utf8string);
    }
    ckfree(managerPtr);
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerClusterBaseChar --
 *
 *	Computes the character index of the base character of the cluster
 *      with the given cluster index.
 *
 * Results:
 *	A cluster index.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

int
TkpTextManagerClusterBaseChar(
    ClientData clientData,
    int clusterIndex)
{
    TextManager *managerPtr = (TextManager *) clientData;
    if (clusterIndex < 0) {
	return 0;
    }
    return (int) IndexOfClusterBase(managerPtr->string, clusterIndex);
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerContainingCluster --
 *
 *	Given a character index, find the index of the cluster which contains
 *      the indexed character.
 *
 * Results:
 *	A cluster index.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

int
TkpTextManagerContainingCluster(
    ClientData clientData,
    int charIndex)
{
    TextManager *managerPtr = (TextManager *) clientData;
    if (charIndex < 0) {
	return 0;
    }
    return (int) IndexOfContainingCluster(managerPtr->string, charIndex);
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerNumClusters --
 *
 *	Return the (cached) number of clusters in the entire NSMutableString.
 *
 * Results:
 *	The number of clusters.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

int
TkpTextManagerNumClusters(
    ClientData clientData)
{
    TextManager *managerPtr = (TextManager *) clientData;
    NSString *string = managerPtr->string;

    if (managerPtr->numClusters < 0) {
	managerPtr->numClusters = IndexOfContainingCluster(string,
	    string.length);
    }
    return managerPtr->numClusters;
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerSet --
 *
 *	Set the contents of the NSMutableString from a UTF-8 encoded string.
 *
 * Results:
 *	A pointer to the TextManager's cached UTF-8 string.
 *
 * Side effects:
 *	The number of unichars and bytes are recorded in the integer variables
 *      referenced by the numChars and numBytes parameters.
 *
 *---------------------------------------------------------------------------
 */

const char *
TkpTextManagerSet(
    ClientData clientData,
    const char *value,
    int *numChars,
    int *numBytes)
{
    TextManager *managerPtr = (TextManager *) clientData;
    NSString *valueString = [[NSString alloc] initWithUTF8String: value];

    [managerPtr->string setString:valueString];
    return TextManagerUpdate(managerPtr, numChars, numBytes);
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerInsert --
 *
 *	Insert the string, as described by the UTF-8 encoded byte array
 *      referenced by the value parameter, at the provided character index.
 *      Inserting a cluster may be done with sequential calls that each provide
 *      a single unicode code point.  The macOS port always provides both
 *      surrogate pairs in a single XEvent, so there should not be misplaced
 *      surrogates, but the modifiers following the base char may be incomplete
 *      after calling this.  Also, the provided character index may not refer
 *      to a base character in some calls.
 *
 * Results:
 *	A pointer to the TextManager's cached UTF-8 string.
 *
 * Side effects:
 *	The number of unichars and bytes are recorded in the integer variables
 *      referenced by the numChars and numBytes parameters.
 *
 *---------------------------------------------------------------------------
 */

const char*
TkpTextManagerInsert(
    ClientData clientData,
    int charIndex,
    const char *value,
    int *numChars,
    int *numBytes)
{
    TextManager *managerPtr = (TextManager *) clientData;
    NSMutableString *str = managerPtr->string;
    NSString *valueString = [[NSString alloc] initWithUTF8String: value];

    [str insertString:valueString atIndex:charIndex];
    return TextManagerUpdate(managerPtr, numChars, numBytes);
}
 
/*
 *---------------------------------------------------------------------------
 *
 *  TkpTextManagerDelete --
 *
 *      Delete the number of clusters specified by the count parameter,
 *      starting at the cluster containing the character referenced by
 *      the charIndex parameter.  If that character is not a base character
 *      the entire cluster which contains it will be deleted, so the
 *      resulting string is a sequence of well-formed clusters.
 *
 * Results:
 *	A pointer to the TextManager's cached UTF-8 string.
 *
 * Side effects:
 *      The number of unichars and bytes in the modified NSMutableString are
 *	recorded in the integer variables referenced by the numChars and
 *	numBytes parameters.  Also the number of characters which were removed
 *	is recorded in the integer variable charsDeleted.  The index of each
 *	remaining base character will change by addition or subtraction of this
 *	number.
 *
 *---------------------------------------------------------------------------
 */

const char*
TkpTextManagerDelete(
    ClientData clientData,
    int charIndex,
    int count,
    int *numChars,
    int *numBytes,
    int *charsDeleted)
{
    TextManager *managerPtr = (TextManager *) clientData;
    NSRange deleteRange = CharRangeFromClusterRange(managerPtr->string,
						    charIndex, count);
    [managerPtr->string deleteCharactersInRange: deleteRange];
    *charsDeleted = deleteRange.length;
    return TextManagerUpdate(managerPtr, numChars, numBytes);
}
 
#define GetNSFontTraitsFromTkFontAttributes(faPtr) \
	((faPtr)->weight == TK_FW_BOLD ? NSBoldFontMask : NSUnboldFontMask) | \
	((faPtr)->slant == TK_FS_ITALIC ? NSItalicFontMask : NSUnitalicFontMask)
 
/*
 *---------------------------------------------------------------------------