summaryrefslogtreecommitdiffstats
path: root/macosx
diff options
context:
space:
mode:
Diffstat (limited to 'macosx')
-rw-r--r--macosx/tkMacOSXClipboard.c4
-rw-r--r--macosx/tkMacOSXDialog.c16
-rw-r--r--macosx/tkMacOSXFont.c78
-rw-r--r--macosx/tkMacOSXPrivate.h32
4 files changed, 80 insertions, 50 deletions
diff --git a/macosx/tkMacOSXClipboard.c b/macosx/tkMacOSXClipboard.c
index 88a0d93..529b5fa 100644
--- a/macosx/tkMacOSXClipboard.c
+++ b/macosx/tkMacOSXClipboard.c
@@ -35,7 +35,9 @@ static Tk_Window clipboardOwner = NULL;
targetPtr->type == dispPtr->utf8Atom) {
for (TkClipboardBuffer *cbPtr = targetPtr->firstBufferPtr;
cbPtr; cbPtr = cbPtr->nextPtr) {
- NSString *s = TkUtfToNSString(cbPtr->buffer, cbPtr->length);
+ NSString *s = [[TKNSString alloc]
+ initWithTclUtfBytes:cbPtr->buffer
+ length:cbPtr->length];
[string appendString:s];
[s release];
}
diff --git a/macosx/tkMacOSXDialog.c b/macosx/tkMacOSXDialog.c
index 10a756d..76bb742 100644
--- a/macosx/tkMacOSXDialog.c
+++ b/macosx/tkMacOSXDialog.c
@@ -210,7 +210,8 @@ getFileURL(
}
- (void) tkFilePanelDidEnd: (NSSavePanel *) panel
- returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
+ returnCode: (NSModalResponse) returnCode
+ contextInfo: (void *) contextInfo
{
FilePanelCallbackInfo *callbackInfo = contextInfo;
@@ -343,15 +344,16 @@ static NSInteger showOpenSavePanel(
{
NSInteger modalReturnCode;
- if (parent && ![parent attachedSheet] && [NSApp macMinorVersion] < 15) {
+ if (parent && ![parent attachedSheet]) {
[panel beginSheetModalForWindow:parent
- completionHandler:^(NSInteger returnCode) {
+ completionHandler:^(NSModalResponse returnCode) {
[NSApp tkFilePanelDidEnd:panel
returnCode:returnCode
contextInfo:callbackInfo ];
}];
+
modalReturnCode = callbackInfo->cmdObj ? modalOther :
- [NSApp runModalForWindow:panel];
+ [panel runModal];
} else {
modalReturnCode = [panel runModal];
[NSApp tkFilePanelDidEnd:panel returnCode:modalReturnCode
@@ -654,8 +656,6 @@ Tk_GetOpenFileObjCmd(
NSInteger modalReturnCode = modalError;
BOOL parentIsKey = NO;
- [openpanel setDelegate:NSApp];
-
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], openOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
@@ -928,8 +928,6 @@ Tk_GetSaveFileObjCmd(
NSInteger modalReturnCode = modalError;
BOOL parentIsKey = NO;
- [savepanel setDelegate:NSApp];
-
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], saveOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
@@ -1171,8 +1169,6 @@ Tk_ChooseDirectoryObjCmd(
NSInteger modalReturnCode = modalError;
BOOL parentIsKey = NO;
- [panel setDelegate:NSApp];
-
for (i = 1; i < objc; i += 2) {
if (Tcl_GetIndexFromObjStruct(interp, objv[i], chooseOptionStrings,
sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c
index 2d8168d..89df102 100644
--- a/macosx/tkMacOSXFont.c
+++ b/macosx/tkMacOSXFont.c
@@ -102,46 +102,46 @@ static void DrawCharsInContext(Display *display, Drawable drawable,
#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
- * using the UTF-8 algorithm. The UTF-8 encoding does not allow encoding
- * surrogates, so these 6-byte strings are not valid UTF-8, and hence Apple's
- * NString class will refuse to instantiate an NSString from the 6-byte
- * encoding. This function allows creating an NSString from a C-string which
- * has been encoded using this scheme.
- *
- * Results:
- * An NSString, which may be nil.
- *
- * Side effects:
- * None.
- *---------------------------------------------------------------------------
+ * To avoid an extra copy, a TKNSString object wraps a Tcl_DString with an
+ * NSString that uses the DString's buffer as its character buffer.
*/
-MODULE_SCOPE NSString*
-TkUtfToNSString(
- const char *source,
- size_t numBytes)
+@implementation TKNSString
+
+- (instancetype)initWithTclUtfBytes:(const void *)bytes
+ length:(NSUInteger)len
{
- 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;
+ if (self = [self init]) {
+ Tcl_DStringInit(&_ds);
+ Tcl_UtfToUniCharDString(bytes, len, &_ds);
+ _string = [[NSString alloc]
+ initWithCharactersNoCopy:(unichar *)Tcl_DStringValue(&_ds)
+ length:Tcl_DStringLength(&_ds)>>1
+ freeWhenDone:NO];
+ self.UTF8String = _string.UTF8String;
+ }
+ return self;
}
-
+
+- (void)dealloc
+{
+ Tcl_DStringFree(&_ds);
+ [_string release];
+ [super dealloc];
+}
+
+- (NSUInteger)length
+{
+ return _string.length;
+}
+
+- (unichar)characterAtIndex:(NSUInteger)index
+{
+ return [_string characterAtIndex:index];
+}
+
+@end
+
/*
*---------------------------------------------------------------------------
*
@@ -150,7 +150,7 @@ TkUtfToNSString(
* 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.
+ * the NSString. See the documentation for TKNSString in tkMacOSXPrivate.h.
*
* Results:
* Returns the number of bytes written.
@@ -979,7 +979,7 @@ TkpMeasureCharsInContext(
if (maxLength > 32767) {
maxLength = 32767;
}
- string = TkUtfToNSString((const char *)source, numBytes);
+ string = [[TKNSString alloc] initWithTclUtfBytes:source length:numBytes];
if (!string) {
length = 0;
fit = rangeLength;
@@ -1258,7 +1258,7 @@ DrawCharsInContext(
!TkMacOSXSetupDrawingContext(drawable, gc, 1, &drawingContext)) {
return;
}
- string = TkUtfToNSString((const char *)source, numBytes);
+ string = [[TKNSString alloc] initWithTclUtfBytes:source length:numBytes];
if (!string) {
return;
}
diff --git a/macosx/tkMacOSXPrivate.h b/macosx/tkMacOSXPrivate.h
index 711569f..747ebd4 100644
--- a/macosx/tkMacOSXPrivate.h
+++ b/macosx/tkMacOSXPrivate.h
@@ -499,6 +499,38 @@ VISIBILITY_HIDDEN
- (void) setAppleMenu: (NSMenu *) menu;
@end
+/*
+ *---------------------------------------------------------------------------
+ *
+ * TKNSString --
+ *
+ * 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
+ * using the UTF-8 algorithm. The UTF-8 encoding does not allow encoding
+ * surrogates, so these 6-byte strings are not valid UTF-8, and hence Apple's
+ * NString class will refuse to instantiate an NSString from the 6-byte
+ * encoding.
+ *
+ * This subclass of NSString adds a new initialization method which accepts
+ * a C string encoded with the scheme described above.
+ *
+ *---------------------------------------------------------------------------
+ */
+
+@interface TKNSString:NSString {
+@private
+ Tcl_DString _ds;
+ NSString *_string;
+}
+@property const char *UTF8String;
+- (instancetype)initWithTclUtfBytes:(const void *)bytes
+ length:(NSUInteger)len;
+@end
+
#endif /* _TKMACPRIV */
int TkMacOSXGetAppPath(ClientData cd, Tcl_Interp *ip, int objc, Tcl_Obj *const objv[]);