static PyObject *ScrapObj_GetScrapFlavorData(ScrapObject *_self, PyObject *_args) { PyObject *_res = NULL; OSStatus _err; ScrapFlavorType flavorType; Size byteCount; if (!PyArg_ParseTuple(_args, "O&", PyMac_GetOSType, &flavorType)) return NULL; _err = GetScrapFlavorSize(_self->ob_itself, flavorType, &byteCount); if (_err != noErr) return PyMac_Error(_err); _res = PyString_FromStringAndSize(NULL, (int)byteCount); if ( _res == NULL ) return NULL; _err = GetScrapFlavorData(_self->ob_itself, flavorType, &byteCount, PyString_AS_STRING(_res)); if (_err != noErr) { Py_XDECREF(_res); return PyMac_Error(_err); } return _res; }
bool MCMacOSXScrapPasteboard::FetchFlavor(ScrapFlavorType p_type, MCSharedString*& r_data) { bool t_success; t_success = true; Size t_size; t_size = 0; if (t_success) if (GetScrapFlavorSize(m_scrap, p_type, &t_size) != noErr) t_success = false; void *t_buffer; t_buffer = NULL; if (t_success) { t_buffer = malloc(t_size); if (t_buffer == NULL) t_success = false; } if (t_success) if (GetScrapFlavorData(m_scrap, p_type, &t_size, t_buffer) != noErr) t_success = false; if (t_success) r_data = MCSharedString::CreateNoCopy(t_buffer, (uint4)t_size); else { if (t_buffer != NULL) free(t_buffer); } return t_success; }
long MyGetScrap(Handle hDest, ResType theType) { // returns the length // If the handle is not nil, it sizes handle to hold the data and fills in the data #if TARGET_API_MAC_CARBON ScrapRef scrapRef; ScrapFlavorType flavorType = theType; Size byteCount,numBytesAllocated; OSStatus err = GetCurrentScrap (&scrapRef); if(err) return 0; // err = GetScrapFlavorSize(scrapRef,flavorType,&byteCount); if(err) return 0; if(byteCount == 0) return 0; if(hDest) { // then the caller wants the data filled in ( _SetHandleSize(hDest,byteCount); numBytesAllocated = _GetHandleSize(hDest); // _MemError(); if(byteCount != numBytesAllocated){// we had a memory err, the requested space was not allocated SysBeep(5);return(0); } _HLock(hDest); err = GetScrapFlavorData(scrapRef,flavorType,&byteCount,*hDest); _HUnlock(hDest); if(err) return 0; } return byteCount; #else long offset; // note: returned value is not used return GetScrap(hDest, theType, &offset); #endif }
void GetScrap(void **handle, uint32 type, int32 offset) { #if defined(__LP64__) D(bug("GetScrap handle %p, type %08x, offset %d\n", handle, type, offset)); #warning Carbon scrapbook function are not implemented in 64-bit mode #else D(bug("GetScrap handle %p, type %08x, offset %d\n", handle, type, offset)); ScrapRef theScrap; if (GetCurrentScrap(&theScrap) != noErr) { D(bug(" could not open scrap\n")); return; } Size byteCount; if (GetScrapFlavorSize(theScrap, type, &byteCount) == noErr) { // Allocate space for new scrap in MacOS side M68kRegisters r; r.d[0] = byteCount; Execute68kTrap(0xa71e, &r); // NewPtrSysClear() uint32 scrap_area = r.a[0]; // Get the native clipboard data if (scrap_area) { uint8 * const data = Mac2HostAddr(scrap_area); if (GetScrapFlavorData(theScrap, type, &byteCount, data) == noErr) { SwapScrapData(type, data, byteCount, FALSE); // Add new data to clipboard static uint8 proc[] = { 0x59, 0x8f, // subq.l #4,sp 0xa9, 0xfc, // ZeroScrap() 0x2f, 0x3c, 0, 0, 0, 0, // move.l #length,-(sp) 0x2f, 0x3c, 0, 0, 0, 0, // move.l #type,-(sp) 0x2f, 0x3c, 0, 0, 0, 0, // move.l #outbuf,-(sp) 0xa9, 0xfe, // PutScrap() 0x58, 0x8f, // addq.l #4,sp M68K_RTS >> 8, M68K_RTS & 0xff }; r.d[0] = sizeof(proc); Execute68kTrap(0xa71e, &r); // NewPtrSysClear() uint32 proc_area = r.a[0]; if (proc_area) { Host2Mac_memcpy(proc_area, proc, sizeof(proc)); WriteMacInt32(proc_area + 6, byteCount); WriteMacInt32(proc_area + 12, type); WriteMacInt32(proc_area + 18, scrap_area); we_put_this_data = true; Execute68k(proc_area, &r); r.a[0] = proc_area; Execute68kTrap(0xa01f, &r); // DisposePtr } } r.a[0] = scrap_area; Execute68kTrap(0xa01f, &r); // DisposePtr }
boolean getscrap (tyscraptype scraptype, Handle hscrap) { /* return true if something was loaded from the scrap, false otherwise. 12/26/91 dmb: return true if scraptype exists; generate error alert on real error. 5.0a16 dmb: need to scan more than last 32 bytes for NUL char */ #ifdef MACVERSION #if TARGET_API_MAC_CARBON == 1 ScrapRef theScrap; ScrapFlavorType flavorType = (ScrapFlavorType)scraptype; Size byteCount; OSStatus status; ScrapFlavorFlags flavorFlags; boolean retVal = true; Size prevCount; status = GetCurrentScrap(&theScrap); if(status != noErr) oserror (status); status = GetScrapFlavorFlags(theScrap, flavorType, &flavorFlags); if(status == noTypeErr) { retVal = false; } else if(status < 0) { retVal = false; oserror (status); } //if we get here and the handles nil, we really don't want the data //also if retVal is false, it means the type does not exist if(hscrap == nil || !retVal) return retVal; status = GetScrapFlavorSize(theScrap, flavorType, &byteCount); if(status != noErr) oserror (status); prevCount = byteCount; SetHandleSize(hscrap, byteCount); if (MemError () != noErr) return (false); //lock the handle before getting the data HLock(hscrap); status = GetScrapFlavorData(theScrap, flavorType, &byteCount, *(hscrap)); HUnlock(hscrap); if(status != noErr) oserror (status); //only return true if we got all the data. return (byteCount == prevCount); #else //precarbon mac long result; long offset; result = GetScrap (hscrap, scraptype, &offset); if (result == noTypeErr) return (false); if (result < 0) oserror (result); return (true); /*there was something on the scrap of the given type*/ #endif #endif #ifdef WIN95VERSION Handle hdata; long ctbytes; char *pdata; boolean fl; /*long i;*/ UINT wintype; wintype = shell2winscraptype (scraptype); if (wintype == 0) return (false); releasethreadglobals (); //getting clipboard data can send us a WM_RENDERFORMAT msg hdata = GetClipboardData (wintype); grabthreadglobals (); if (hdata == NULL) { oserror (GetLastError ()); // may be no error return (false); } ctbytes = GlobalSize (hdata); pdata = GlobalLock (hdata); if (pdata == NULL) return (false); if (scraptype == textscraptype) { //Handle reducing the scrap size to the NULL /* i = 0x40; if (i > ctbytes) i = ctbytes; while (i > 0) { if (*(pdata + ctbytes - i) == 0) { ctbytes = ctbytes - i; break; } --i; } /%while%/ */ ctbytes = strlen (pdata); /*7.1b23: call strlen to find the first terminator: removes junk from end*/ } /*if*/ fl = sethandlecontents (pdata, ctbytes, hscrap); GlobalUnlock (hdata); return (fl); #endif } /*getscrap*/
// // I_GetClipboardText // // by Denis Lukianov - 20 Mar 2006 // Cross-platform clipboard functionality // std::string I_GetClipboardText (void) { #ifdef X11 std::string ret; Display *dis = XOpenDisplay(NULL); int screen = DefaultScreen(dis); if(!dis) { Printf(PRINT_HIGH, "I_GetClipboardText: XOpenDisplay failed"); return ""; } XLockDisplay(dis); Window WindowEvents = XCreateSimpleWindow(dis, RootWindow(dis, screen), 0, 0, 1, 1, 0, BlackPixel(dis, screen), BlackPixel(dis, screen)); if(XGetSelectionOwner(dis, XA_PRIMARY) != None) { if(!XConvertSelection(dis, XA_PRIMARY, XA_STRING, XA_PRIMARY, WindowEvents, CurrentTime)) { XDestroyWindow(dis, WindowEvents); XUnlockDisplay(dis); XCloseDisplay(dis); Printf(PRINT_HIGH, "I_GetClipboardText: XConvertSelection failed"); return ""; } XFlush (dis); // Wait for the reply for(;;) { XEvent e; XNextEvent(dis, &e); if(e.type == SelectionNotify) break; } Atom type; int format, result; u_long len, bytes_left, temp; u_char *data; result = XGetWindowProperty(dis, WindowEvents, XA_PRIMARY, 0, 0, False, AnyPropertyType, &type, &format, &len, &bytes_left, &data); if(result != Success) { XDestroyWindow(dis, WindowEvents); XUnlockDisplay(dis); XCloseDisplay(dis); Printf(PRINT_HIGH, "I_GetClipboardText: XGetWindowProperty failed(1)"); return ""; } if(!bytes_left) { XDestroyWindow(dis, WindowEvents); DPrintf("I_GetClipboardText: Len was: %d", len); XUnlockDisplay(dis); XCloseDisplay(dis); return ""; } result = XGetWindowProperty(dis, WindowEvents, XA_PRIMARY, 0, bytes_left, False, AnyPropertyType, &type, &format, &len, &temp, &data); if(result != Success) { XDestroyWindow(dis, WindowEvents); XUnlockDisplay(dis); XCloseDisplay(dis); Printf(PRINT_HIGH, "I_GetClipboardText: XGetWindowProperty failed(2)"); return ""; } ret = std::string((const char *)data, len); XFree(data); } XDestroyWindow(dis, WindowEvents); XUnlockDisplay(dis); XCloseDisplay(dis); return ret; #endif #if defined _WIN32 && !defined _XBOX std::string ret; if(!IsClipboardFormatAvailable(CF_TEXT)) return ""; if(!OpenClipboard(NULL)) return ""; HANDLE hClipboardData = GetClipboardData(CF_TEXT); if(!hClipboardData) { CloseClipboard(); return ""; } const char *cData = reinterpret_cast<const char *>(GlobalLock(hClipboardData)); SIZE_T uiSize = static_cast<SIZE_T>(GlobalSize(hClipboardData)); if(cData && uiSize) { for(size_t i = 0; i < uiSize; i++) { if(!cData[i]) { uiSize = i; break; } } ret = std::string(cData, uiSize); } GlobalUnlock(hClipboardData); CloseClipboard(); return ret; #endif #ifdef OSX ScrapRef scrap; Size size; int err = GetCurrentScrap(&scrap); if(err) { Printf(PRINT_HIGH, "GetCurrentScrap error: %d", err); return ""; } err = GetScrapFlavorSize(scrap, FOUR_CHAR_CODE('TEXT'), &size); if(err) { Printf(PRINT_HIGH, "GetScrapFlavorSize error: %d", err); return ""; } char *data = new char[size+1]; err = GetScrapFlavorData(scrap, FOUR_CHAR_CODE('TEXT'), &size, data); data[size] = 0; if(err) { Printf(PRINT_HIGH, "GetScrapFlavorData error: %d", err); delete[] data; return ""; } std::string ret(data); delete[] data; return ret; #endif return ""; }
int TkSelGetSelection( Tcl_Interp *interp, /* Interpreter to use for reporting * errors. */ Tk_Window tkwin, /* Window on whose behalf to retrieve * the selection (determines display * from which to retrieve). */ Atom selection, /* Selection to retrieve. */ Atom target, /* Desired form in which selection * is to be returned. */ Tk_GetSelProc *proc, /* Procedure to call to process the * selection, once it has been retrieved. */ ClientData clientData) /* Arbitrary value to pass to proc. */ { int result; int err; long length; ScrapRef scrapRef; char * buf; if ((selection == Tk_InternAtom(tkwin, "CLIPBOARD")) && (target == XA_STRING)) { /* * Get the scrap from the Macintosh global clipboard. */ err=GetCurrentScrap(&scrapRef); if (err != noErr) { Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection), " GetCurrentScrap failed.", (char *) NULL); return TCL_ERROR; } err=GetScrapFlavorSize(scrapRef,'TEXT',&length); if (err != noErr) { Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection), " GetScrapFlavorSize failed.", (char *) NULL); return TCL_ERROR; } if (length > 0) { Tcl_DString encodedText; buf = (char *)ckalloc(length+1); buf[length] = 0; err = GetScrapFlavorData(scrapRef, 'TEXT', &length, buf); if (err != noErr) { Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection), " GetScrapFlavorData failed.", (char *) NULL); return TCL_ERROR; } Tcl_ExternalToUtfDString(TkMacOSXCarbonEncoding, buf, length, &encodedText); result = (*proc)(clientData, interp, Tcl_DStringValue(&encodedText)); Tcl_DStringFree(&encodedText); ckfree(buf); return result; } } Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection), " selection doesn't exist or form \"", Tk_GetAtomName(tkwin, target), "\" not defined", (char *) NULL); return TCL_ERROR; }
//----------------------------------------------------------------------------- const char* Platform::getClipboard() { // mac clipboards can contain multiple items, // and each item can be in several differnt flavors, // such as unicode or plaintext or pdf, etc. // scan through the clipboard, and return the 1st piece of actual text. ScrapRef clip; char *retBuf = ""; OSStatus err = noErr; char *dataBuf = ""; // get a local ref to the system clipboard GetScrapByName( kScrapClipboardScrap, kScrapGetNamedScrap, &clip ); // First try to get unicode data, then try to get plain text data. Size dataSize = 0; bool plaintext = false; err = GetScrapFlavorSize(clip, kScrapFlavorTypeUnicode, &dataSize); if( err != noErr || dataSize <= 0) { Con::errorf("some error getting unicode clip"); plaintext = true; err = GetScrapFlavorSize(clip, kScrapFlavorTypeText, &dataSize); } // kick out if we don't have any data. if( err != noErr || dataSize <= 0) { Con::errorf("no data, kicking out. size = %i",dataSize); return ""; } if( err == noErr && dataSize > 0 ) { // ok, we've got something! allocate a buffer and copy it in. char buf[dataSize+1]; dMemset(buf, 0, dataSize+1); dataBuf = buf; // plain text needs no conversion. // unicode data needs to be converted to normalized utf-8 format. if(plaintext) { GetScrapFlavorData(clip, kScrapFlavorTypeText, &dataSize, &buf); retBuf = Con::getReturnBuffer(dataSize + 1); dMemcpy(retBuf,buf,dataSize); } else { GetScrapFlavorData(clip, kScrapFlavorTypeUnicode, &dataSize, &buf); // normalize CFStringRef cfBuf = CFStringCreateWithBytes(NULL, (const UInt8*)buf, dataSize, kCFStringEncodingUnicode, false); CFMutableStringRef normBuf = CFStringCreateMutableCopy(NULL, 0, cfBuf); CFStringNormalize(normBuf, kCFStringNormalizationFormC); // convert to utf-8 U32 normBufLen = CFStringGetLength(normBuf); U32 retBufLen = CFStringGetMaximumSizeForEncoding(normBufLen,kCFStringEncodingUTF8) + 1; // +1 for the null terminator retBuf = Con::getReturnBuffer(retBufLen); CFStringGetCString( normBuf, retBuf, retBufLen, kCFStringEncodingUTF8); dataSize = retBufLen; } // manually null terminate, just in case. retBuf[dataSize] = 0; } // return the data, or the empty string if we did not find any data. return retBuf; }