Fix Mac OSX readUserPreferences missing off last char of pref data, add clipboard implementations for Mac OS X

This commit is contained in:
Simon Robertshaw 2013-11-19 21:23:04 +00:00
parent fc25d5a466
commit a32af1679a
2 changed files with 41 additions and 10 deletions

View File

@ -360,11 +360,13 @@ char * readUserPreferences() {
prefData = "";
char *prefDataCopy = calloc([prefDataNSString length]+1, 1);
SDL_strlcpy(prefDataCopy, prefData, [prefDataNSString length]);
SDL_strlcpy(prefDataCopy, prefData, [prefDataNSString length]+1);
[prefDataNSString release];
[prefs release];
puts(prefDataCopy);
return prefDataCopy;
}
@ -379,6 +381,36 @@ void writeUserPreferences(const char * prefData) {
[prefs release];
}
char * readClipboard() {
NSPasteboard *clipboard = [NSPasteboard generalPasteboard];
NSArray *classes = [[NSArray alloc] initWithObjects:[NSString class], nil];
NSDictionary *options = [NSDictionary dictionary];
NSArray *clipboardItems = [clipboard readObjectsForClasses:classes options:options];
if(clipboardItems == nil || [clipboardItems count] == 0) return NULL;
NSString *newString = [clipboardItems objectAtIndex:0];
const char * clipboardData = [newString UTF8String];
if(clipboardData == NULL)
clipboardData = "";
char *clipboardDataCopy = calloc([newString length]+1, 1);
SDL_strlcpy(clipboardDataCopy, clipboardData, [newString length]+1);
return clipboardDataCopy;
}
void writeClipboard(const char * clipboardData) {
NSPasteboard *clipboard = [NSPasteboard generalPasteboard];
NSString *newString = [NSString stringWithUTF8String: clipboardData];
[clipboard clearContents];
[clipboard setString:newString forType:NSStringPboardType];
}
#ifdef main
# undef main

View File

@ -24,6 +24,10 @@
#endif
#ifdef MACOSX
#include <ApplicationServices/ApplicationServices.h>
extern "C" {
char * readClipboard();
void writeClipboard(const char * clipboardData);
}
#endif
#include "Format.h"
@ -69,14 +73,7 @@ void ClipboardPush(char * text)
}
clipboardText = mystrdup(text);
#ifdef MACOSX
PasteboardRef newclipboard;
if (PasteboardCreate(kPasteboardClipboard, &newclipboard)!=noErr) return;
if (PasteboardClear(newclipboard)!=noErr) return;
PasteboardSynchronize(newclipboard);
CFDataRef data = CFDataCreate(kCFAllocatorDefault, (const UInt8*)text, strlen(text));
PasteboardPutItemFlavor(newclipboard, (PasteboardItemID)1, CFSTR("com.apple.traditional-mac-plain-text"), data, 0);
writeClipboard(text);
#elif defined(WIN)
if (OpenClipboard(NULL))
{
@ -109,7 +106,9 @@ void EventProcess(SDL_Event event);
char * ClipboardPull()
{
#ifdef MACOSX
printf("Not implemented: get text from clipboard\n");
char * data = readClipboard();
if(!data) return mystrdup("");
return mystrdup(data);
#elif defined(WIN)
if (OpenClipboard(NULL))
{