| 1 |
/* AbiSource Application Framework |
| 2 |
* Copyright (C) 1998 AbiSource, Inc. |
| 3 |
* Copyright (C) 2001, 2003 Hubert Figuiere |
| 4 |
* Copyright (C) 2005 Net Integration Technologies Inc. (written by Hubert Figuiere) |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "ut_debugmsg.h" |
| 25 |
#include "ut_string.h" |
| 26 |
#include "ut_assert.h" |
| 27 |
|
| 28 |
#include "xap_CocoaClipboard.h" |
| 29 |
|
| 30 |
const char * XAP_CocoaClipboard::XAP_CLIPBOARD_TEXTPLAIN_8BIT = "TEXT"; |
| 31 |
const char * XAP_CocoaClipboard::XAP_CLIPBOARD_STRING = "STRING"; |
| 32 |
const char * XAP_CocoaClipboard::XAP_CLIPBOARD_COMPOUND_TEXT = "COMPOUND_TEXT"; |
| 33 |
const char * XAP_CocoaClipboard::XAP_CLIPBOARD_RTF = "text/rtf"; |
| 34 |
const char * XAP_CocoaClipboard::XAP_CLIPBOARD_IMAGE = "image/png"; |
| 35 |
|
| 36 |
XAP_CocoaClipboard::XAP_CocoaClipboard() |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
XAP_CocoaClipboard::~XAP_CocoaClipboard() |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
////////////////////////////////////////////////////////////////// |
| 46 |
////////////////////////////////////////////////////////////////// |
| 47 |
/*! |
| 48 |
Should be called beore adding text data to clipboard |
| 49 |
*/ |
| 50 |
void XAP_CocoaClipboard::prepareForText(void) |
| 51 |
{ |
| 52 |
NSPasteboard* pb = _getPasteboard(); |
| 53 |
[pb declareTypes:[NSArray arrayWithObjects:NSRTFPboardType, NSStringPboardType, nil] owner:nil]; |
| 54 |
} |
| 55 |
|
| 56 |
bool XAP_CocoaClipboard::addData(const char* format, void* pData, UT_sint32 iNumBytes) |
| 57 |
{ |
| 58 |
NSPasteboard* pb = _getPasteboard(); |
| 59 |
NSData* data = [[NSData alloc] initWithBytes:pData length:iNumBytes]; |
| 60 |
if (strcmp(format, XAP_CLIPBOARD_RTF) == 0) { |
| 61 |
[pb setData:data forType:NSRTFPboardType]; |
| 62 |
} |
| 63 |
else if (strcmp(format, XAP_CLIPBOARD_TEXTPLAIN_8BIT) == 0) { |
| 64 |
[pb setData:data forType:NSStringPboardType]; |
| 65 |
} |
| 66 |
[data release]; |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
bool XAP_CocoaClipboard::getClipboardData(const char** formatAccepted, void ** ppData, UT_uint32 * pLen, const char ** szFormatFound) |
| 71 |
{ |
| 72 |
const char** current = formatAccepted; |
| 73 |
NSString* pbType = nil; |
| 74 |
NSPasteboard* pb = _getPasteboard(); |
| 75 |
while (*current) { |
| 76 |
pbType = _abi2ns_cbType(*current); |
| 77 |
if (pbType) { |
| 78 |
NSData* data = [pb dataForType:pbType]; |
| 79 |
if (data) { |
| 80 |
*pLen = [data length]; |
| 81 |
*ppData = g_try_malloc(*pLen); |
| 82 |
memcpy(*ppData, [data bytes], *pLen); |
| 83 |
*szFormatFound = *current; |
| 84 |
return true; |
| 85 |
} |
| 86 |
} |
| 87 |
current++; |
| 88 |
} |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
bool XAP_CocoaClipboard::clearClipboard(void) |
| 94 |
{ |
| 95 |
// User requested us to clear the clipboard. |
| 96 |
|
| 97 |
UT_DEBUGMSG(("Clipboard: explicit clear\n")); |
| 98 |
// UT_ASSERT (UT_NOT_IMPLEMENTED); |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
bool XAP_CocoaClipboard::hasFormat(const char* format) |
| 104 |
{ |
| 105 |
NSString *pbType = _abi2ns_cbType(format); |
| 106 |
|
| 107 |
if (pbType) { |
| 108 |
NSString * availableFormat = [_getPasteboard() availableTypeFromArray:[NSArray arrayWithObject:pbType]]; |
| 109 |
|
| 110 |
if (availableFormat) { |
| 111 |
return true; |
| 112 |
} |
| 113 |
} |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
bool XAP_CocoaClipboard::hasFormats(const char** format) |
| 120 |
{ |
| 121 |
bool found = false; |
| 122 |
while(*format) { |
| 123 |
found = hasFormat(*format); |
| 124 |
|
| 125 |
format++; |
| 126 |
|
| 127 |
if (found) { |
| 128 |
break; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
return found; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
NSString *XAP_CocoaClipboard::_abi2ns_cbType(const char *cbType) |
| 138 |
{ |
| 139 |
NSString *pbType = nil; |
| 140 |
|
| 141 |
if (strcmp(cbType, XAP_CLIPBOARD_RTF) == 0) { |
| 142 |
pbType = NSRTFPboardType; |
| 143 |
} |
| 144 |
else if (strcmp(cbType, XAP_CLIPBOARD_TEXTPLAIN_8BIT) == 0) { |
| 145 |
pbType = NSStringPboardType; |
| 146 |
} |
| 147 |
else if (strcmp(cbType, XAP_CLIPBOARD_IMAGE) == 0) { |
| 148 |
pbType = NSTIFFPboardType; |
| 149 |
} |
| 150 |
return pbType; |
| 151 |
} |
| 152 |
|