Tk Source Code

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

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

Overview
Comment:Make MODULE_SCOPE functions start with "Tk". Also simplify TkUtfToNSString(), making use of Tcl_UtfToUniCharDString()
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | core-8-6-branch
Files: files | file ages | folders
SHA3-256: f98feb70beed0f7bae6d737ce9f3530b889e4fcecbdde2f9caff9d2fc412e64d
User & Date: jan.nijtmans 2019-12-04 20:35:17
Context
2019-12-05
13:05
Build without -DUNICDE -D_UNICODE, since it's not necessary any-more (all code uses implicit *W-API now). This also eliminates the need for the TK_ASCII_MAIN hack. check-in: a548d14c user: jan.nijtmans tags: core-8-6-branch
2019-12-04
22:38
Merge 8.6. Introduce the new Unicode-handling functions here too: Tk 8.7 could be loaded in Tcl 8.6 .... Further improvements are still possible check-in: 2c72c39e user: jan.nijtmans tags: trunk
20:35
Make MODULE_SCOPE functions start with "Tk". Also simplify TkUtfToNSString(), making use of Tcl_UtfToUniCharDString() check-in: f98feb70 user: jan.nijtmans tags: core-8-6-branch
14:17
Fix [749bd9bb1b]: systemControlAccentColor can have incorrect RGB on older macOS systems. check-in: 00b45e1d user: marc_culler tags: core-8-6-branch
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to macosx/tkMacOSXClipboard.c.

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	    [type isEqualToString:NSStringPboardType]) {
	for (TkClipboardTarget *targetPtr = dispPtr->clipTargetPtr; targetPtr;
		targetPtr = targetPtr->nextPtr) {
	    if (targetPtr->type == XA_STRING ||
		    targetPtr->type == dispPtr->utf8Atom) {
		for (TkClipboardBuffer *cbPtr = targetPtr->firstBufferPtr;
			cbPtr; cbPtr = cbPtr->nextPtr) {
		    NSString *s = TclUniToNSString(cbPtr->buffer, cbPtr->length);
		    [string appendString:s];
		    [s release];
		}
		break;
	    }
	}
    }
................................................................................
	     * Encode the string using the encoding which is used in Tcl
	     * when TCL_UTF_MAX = 3.  This replaces each UTF-16 surrogate with
	     * a 3-byte sequence generated using the UTF-8 algorithm. (Even
	     * though UTF-8 does not allow encoding surrogates, the algorithm
	     * does produce a 3-byte sequence.)
	     */

	    char *bytes = NSStringToTclUni(string, NULL);
	    result = proc(clientData, interp, bytes);
	    if (bytes) {
		ckfree(bytes);
	    }
	}
    } else {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(






|







 







|







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
...
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	    [type isEqualToString:NSStringPboardType]) {
	for (TkClipboardTarget *targetPtr = dispPtr->clipTargetPtr; targetPtr;
		targetPtr = targetPtr->nextPtr) {
	    if (targetPtr->type == XA_STRING ||
		    targetPtr->type == dispPtr->utf8Atom) {
		for (TkClipboardBuffer *cbPtr = targetPtr->firstBufferPtr;
			cbPtr; cbPtr = cbPtr->nextPtr) {
		    NSString *s = TkUtfToNSString(cbPtr->buffer, cbPtr->length);
		    [string appendString:s];
		    [s release];
		}
		break;
	    }
	}
    }
................................................................................
	     * Encode the string using the encoding which is used in Tcl
	     * when TCL_UTF_MAX = 3.  This replaces each UTF-16 surrogate with
	     * a 3-byte sequence generated using the UTF-8 algorithm. (Even
	     * though UTF-8 does not allow encoding surrogates, the algorithm
	     * does produce a 3-byte sequence.)
	     */

	    char *bytes = TkNSStringToUtf(string, NULL);
	    result = proc(clientData, interp, bytes);
	    if (bytes) {
		ckfree(bytes);
	    }
	}
    } else {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(

Changes to macosx/tkMacOSXFont.c.

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
...
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144


145
146
147
148


149
150
151
152
153
154
155
156
157
158
159
160
161
162
...
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
...
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
...
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
...
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
....
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
#pragma mark -
#pragma mark Font Helpers:

/*
 *---------------------------------------------------------------------------
 *
 * TclUniToNSString --
 *
 * When Tcl is compiled with TCL_UTF_MAX = 3 (the default for 8.6) it cannot
 * deal directly with UTF-8 encoded non-BMP characters, since their UTF-8
 * encoding requires 4 bytes.
 *
 * As a workaround, these versions of Tcl encode non-BMP characters as a string
 * of length 6 in which the high and low UTF-16 surrogates have been encoded
................................................................................
 *
 * Side effects:
 *	None.
 *---------------------------------------------------------------------------
 */

MODULE_SCOPE NSString*
TclUniToNSString(
   const char *source,
   int numBytes)
{
    NSString *string = [[NSString alloc] initWithBytesNoCopy:(void *)source
						      length:numBytes
						    encoding:NSUTF8StringEncoding
						freeWhenDone:NO];
    if (!string) {
	const unichar *characters = ckalloc(numBytes*sizeof(unichar));
	const char *in = source;
	unichar *out = (unichar *) characters;
	while (in < source + numBytes) {
	    in += Tcl_UtfToUniChar(in, out++);
	}


	string = [[NSString alloc] initWithCharacters:characters
		     length:(out - characters)];
	ckfree(characters);
    }


    return string;
}
 
/*
 *---------------------------------------------------------------------------
 *
 * TclUniAtIndex --
 *
 *  Write a sequence of bytes up to length 6 which is an encoding of a UTF-16
 *  character in an NSString.  Also record the unicode code point of the character.
 *  this may be a non-BMP character constructed by reading two surrogates from
 *  the NSString.
 *
 * Results:
................................................................................
 *	Bytes are written to the char array referenced by the pointer uni and
 *      the unicode code point is written to the integer referenced by the
 *      pointer code.
 *
 */

MODULE_SCOPE int
TclUniAtIndex(
    NSString *string,
    int index,
    char *uni,
    unsigned int *code)
{
    char *ptr = uni;
    UniChar uniChar = [string characterAtIndex: index];
................................................................................
	return strlen(uni);
    }
}
 
/*
 *---------------------------------------------------------------------------
 *
 * NSStringToTclUni --
 *
 * Encodes the unicode string represented by an NSString object with the
 * internal encoding that Tcl uses when TCL_UTF_MAX = 3.  This encoding
 * is similar to UTF-8 except that non-BMP characters are encoded as two
 * successive 3-byte sequences which are constructed from UTF-16 surrogates
 * by applying the UTF-8 algorithm.  Even though the UTF-8 encoding does not
 * allow encoding surrogates, the algorithm does produce a well-defined
................................................................................
 * Side effects:
 *      Memory is allocated to hold the byte array, which must be freed with
 *      ckalloc.  If the pointer numBytes is not NULL the number of non-null
 *      bytes written to the array is stored in the integer it references.
 */

MODULE_SCOPE char*
NSStringToTclUni(
   NSString *string,
   int *numBytes)
{
    unsigned int code;
    int i;
    char *ptr, *bytes = ckalloc(6*[string length] + 1);

    ptr = bytes;
    if (ptr) {
	for (i = 0; i < [string length]; i++) {
	    ptr += TclUniAtIndex(string, i, ptr, &code);
	    if (code > 0xffff){
		i++;
	    }
	}
	*ptr = '\0';
    }
    if (numBytes) {
................................................................................
	    (maxLength == 0 && !(flags & TK_AT_LEAST_ONE))) {
	*lengthPtr = 0;
	return 0;
    }
    if (maxLength > 32767) {
	maxLength = 32767;
    }
    string = TclUniToNSString((const char *)source, numBytes);
    if (!string) {
	length = 0;
	fit = rangeLength;
	goto done;
    }
    attributedString = [[NSAttributedString alloc] initWithString:string
	    attributes:fontPtr->nsAttributes];
................................................................................
    int h;

    if (rangeStart < 0 || rangeLength <= 0 ||
	    rangeStart + rangeLength > numBytes ||
	    !TkMacOSXSetupDrawingContext(drawable, gc, 1, &drawingContext)) {
	return;
    }
    string = TclUniToNSString((const char *)source, numBytes);
    if (!string) {
	return;
    }
    context = drawingContext.context;
    fg = TkMacOSXCreateCGColor(gc, gc->foreground);
    attributes = [fontPtr->nsAttributes mutableCopy];
    [attributes setObject:(id)fg forKey:(id)kCTForegroundColorAttributeName];






|







 







|

|

|
|
<
<
<
<
<
<
<
<
|
>
>
|
<
<
<
>
>






|







 







|







 







|







 







|










|







 







|







 







|







100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
...
123
124
125
126
127
128
129
130
131
132
133
134
135








136
137
138
139



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
...
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
...
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
....
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
#pragma mark -
#pragma mark Font Helpers:

/*
 *---------------------------------------------------------------------------
 *
 * TkUtfToNSString --
 *
 * When Tcl is compiled with TCL_UTF_MAX = 3 (the default for 8.6) it cannot
 * deal directly with UTF-8 encoded non-BMP characters, since their UTF-8
 * encoding requires 4 bytes.
 *
 * As a workaround, these versions of Tcl encode non-BMP characters as a string
 * of length 6 in which the high and low UTF-16 surrogates have been encoded
................................................................................
 *
 * Side effects:
 *	None.
 *---------------------------------------------------------------------------
 */

MODULE_SCOPE NSString*
TkUtfToNSString(
   const char *source,
   size_t numBytes)
{
    NSString *string;
    Tcl_DString ds;









    Tcl_DStringInit(&ds);
    Tcl_UtfToUniCharDString(source, numBytes, &ds);
    string = [[NSString alloc] initWithCharacters:(const unichar *)Tcl_DStringValue(&ds)



	    length:(Tcl_DStringLength(&ds)>>1)];
    Tcl_DStringFree(&ds);
    return string;
}
 
/*
 *---------------------------------------------------------------------------
 *
 * TkUtfAtIndex --
 *
 *  Write a sequence of bytes up to length 6 which is an encoding of a UTF-16
 *  character in an NSString.  Also record the unicode code point of the character.
 *  this may be a non-BMP character constructed by reading two surrogates from
 *  the NSString.
 *
 * Results:
................................................................................
 *	Bytes are written to the char array referenced by the pointer uni and
 *      the unicode code point is written to the integer referenced by the
 *      pointer code.
 *
 */

MODULE_SCOPE int
TkUtfAtIndex(
    NSString *string,
    int index,
    char *uni,
    unsigned int *code)
{
    char *ptr = uni;
    UniChar uniChar = [string characterAtIndex: index];
................................................................................
	return strlen(uni);
    }
}
 
/*
 *---------------------------------------------------------------------------
 *
 * TkNSStringToUtf --
 *
 * Encodes the unicode string represented by an NSString object with the
 * internal encoding that Tcl uses when TCL_UTF_MAX = 3.  This encoding
 * is similar to UTF-8 except that non-BMP characters are encoded as two
 * successive 3-byte sequences which are constructed from UTF-16 surrogates
 * by applying the UTF-8 algorithm.  Even though the UTF-8 encoding does not
 * allow encoding surrogates, the algorithm does produce a well-defined
................................................................................
 * Side effects:
 *      Memory is allocated to hold the byte array, which must be freed with
 *      ckalloc.  If the pointer numBytes is not NULL the number of non-null
 *      bytes written to the array is stored in the integer it references.
 */

MODULE_SCOPE char*
TkNSStringToUtf(
   NSString *string,
   int *numBytes)
{
    unsigned int code;
    int i;
    char *ptr, *bytes = ckalloc(6*[string length] + 1);

    ptr = bytes;
    if (ptr) {
	for (i = 0; i < [string length]; i++) {
	    ptr += TkUtfAtIndex(string, i, ptr, &code);
	    if (code > 0xffff){
		i++;
	    }
	}
	*ptr = '\0';
    }
    if (numBytes) {
................................................................................
	    (maxLength == 0 && !(flags & TK_AT_LEAST_ONE))) {
	*lengthPtr = 0;
	return 0;
    }
    if (maxLength > 32767) {
	maxLength = 32767;
    }
    string = TkUtfToNSString((const char *)source, numBytes);
    if (!string) {
	length = 0;
	fit = rangeLength;
	goto done;
    }
    attributedString = [[NSAttributedString alloc] initWithString:string
	    attributes:fontPtr->nsAttributes];
................................................................................
    int h;

    if (rangeStart < 0 || rangeLength <= 0 ||
	    rangeStart + rangeLength > numBytes ||
	    !TkMacOSXSetupDrawingContext(drawable, gc, 1, &drawingContext)) {
	return;
    }
    string = TkUtfToNSString((const char *)source, numBytes);
    if (!string) {
	return;
    }
    context = drawingContext.context;
    fg = TkMacOSXCreateCGColor(gc, gc->foreground);
    attributes = [fontPtr->nsAttributes mutableCopy];
    [attributes setObject:(id)fg forKey:(id)kCTForegroundColorAttributeName];

Changes to macosx/tkMacOSXKeyEvent.c.

343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
     * sequence using the UTF-8 algorithm (ignoring the fact that the UTF-8
     * encoding specification does not allow encoding UTF-16 surrogates).
     * This gives a 6-byte encoding of the non-BMP character which we write into
     * the trans_chars field of the XEvent.
     */

    for (i = 0; i < len; i++) {
	xEvent.xkey.nbytes = TclUniAtIndex(str, i, xEvent.xkey.trans_chars,
					   &xEvent.xkey.keycode);
	if (xEvent.xkey.keycode > 0xffff){
	    i++;
	}
    	xEvent.xany.type = KeyPress;
    	Tk_QueueWindowEvent(&xEvent, TCL_QUEUE_TAIL);
    }






|







343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
     * sequence using the UTF-8 algorithm (ignoring the fact that the UTF-8
     * encoding specification does not allow encoding UTF-16 surrogates).
     * This gives a 6-byte encoding of the non-BMP character which we write into
     * the trans_chars field of the XEvent.
     */

    for (i = 0; i < len; i++) {
	xEvent.xkey.nbytes = TkUtfAtIndex(str, i, xEvent.xkey.trans_chars,
					   &xEvent.xkey.keycode);
	if (xEvent.xkey.keycode > 0xffff){
	    i++;
	}
    	xEvent.xany.type = KeyPress;
    	Tk_QueueWindowEvent(&xEvent, TCL_QUEUE_TAIL);
    }

Changes to macosx/tkMacOSXPrivate.h.

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
MODULE_SCOPE long tkMacOSXMacOSXVersion;

/*
 * Prototypes for TkMacOSXRegion.c.
 */

#if 0
MODULE_SCOPE void	TkMacOSXEmtpyRegion(TkRegion r);
MODULE_SCOPE int	TkMacOSXIsEmptyRegion(TkRegion r);
#endif
MODULE_SCOPE HIShapeRef	TkMacOSXGetNativeRegion(TkRegion r);
MODULE_SCOPE void	TkMacOSXSetWithNativeRegion(TkRegion r,
			    HIShapeRef rgn);
MODULE_SCOPE void	TkMacOSXOffsetRegion(TkRegion r, short dx, short dy);
MODULE_SCOPE HIShapeRef	TkMacOSXHIShapeCreateEmpty(void);
MODULE_SCOPE HIMutableShapeRef TkMacOSXHIShapeCreateMutableWithRect(
			    const CGRect *inRect);
................................................................................
			    Tcl_Obj *const objv[]);
MODULE_SCOPE void       TkMacOSXDrawSolidBorder(Tk_Window tkwin, GC gc,
			    int inset, int thickness);
MODULE_SCOPE int 	TkMacOSXServices_Init(Tcl_Interp *interp);
MODULE_SCOPE int	TkMacOSXRegisterServiceWidgetObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
MODULE_SCOPE NSString*  TclUniToNSString(const char *source, int numBytes);
MODULE_SCOPE int        TclUniAtIndex(NSString *string, int index, char *uni,
				      unsigned int *code);
MODULE_SCOPE char*      NSStringToTclUni(NSString *string, int *numBytes);

#pragma mark Private Objective-C Classes

#define VISIBILITY_HIDDEN __attribute__((visibility("hidden")))

enum { tkMainMenu = 1, tkApplicationMenu, tkWindowsMenu, tkHelpMenu};







<
<
<
<







 







|
|

|







139
140
141
142
143
144
145




146
147
148
149
150
151
152
...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
MODULE_SCOPE long tkMacOSXMacOSXVersion;

/*
 * Prototypes for TkMacOSXRegion.c.
 */





MODULE_SCOPE HIShapeRef	TkMacOSXGetNativeRegion(TkRegion r);
MODULE_SCOPE void	TkMacOSXSetWithNativeRegion(TkRegion r,
			    HIShapeRef rgn);
MODULE_SCOPE void	TkMacOSXOffsetRegion(TkRegion r, short dx, short dy);
MODULE_SCOPE HIShapeRef	TkMacOSXHIShapeCreateEmpty(void);
MODULE_SCOPE HIMutableShapeRef TkMacOSXHIShapeCreateMutableWithRect(
			    const CGRect *inRect);
................................................................................
			    Tcl_Obj *const objv[]);
MODULE_SCOPE void       TkMacOSXDrawSolidBorder(Tk_Window tkwin, GC gc,
			    int inset, int thickness);
MODULE_SCOPE int 	TkMacOSXServices_Init(Tcl_Interp *interp);
MODULE_SCOPE int	TkMacOSXRegisterServiceWidgetObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,
			    Tcl_Obj *const objv[]);
MODULE_SCOPE NSString*  TkUtfToNSString(const char *source, size_t numBytes);
MODULE_SCOPE int        TkUtfAtIndex(NSString *string, int index, char *uni,
				      unsigned int *code);
MODULE_SCOPE char*      TkNSStringToUtf(NSString *string, int *numBytes);

#pragma mark Private Objective-C Classes

#define VISIBILITY_HIDDEN __attribute__((visibility("hidden")))

enum { tkMainMenu = 1, tkApplicationMenu, tkWindowsMenu, tkHelpMenu};

Changes to macosx/tkMacOSXRegion.c.

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int
TkMacOSXIsEmptyRegion(
    TkRegion r)
{
    return HIShapeIsEmpty((HIMutableShapeRef) r) ? 1 : 0;
}
 
/*






|







171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static int
TkMacOSXIsEmptyRegion(
    TkRegion r)
{
    return HIShapeIsEmpty((HIMutableShapeRef) r) ? 1 : 0;
}
 
/*