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 }
static PyObject *ScrapObj_GetScrapFlavorSize(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 = Py_BuildValue("l", byteCount); return _res; }
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; }
/*** ADJUST MENUS ***/ OSStatus AdjustMenus( void ) { register WindowRef theWin; short windowKind; Boolean isDA, isObjectWin, selection, scrapExists, undoExists, isGotoWin, isFindWin; EditWindowPtr dWin = NULL; Str31 menuStr; short i; long scrapSize; // LR: v1.6.5 Str255 frontWindowName, menuItemTitle; Boolean namesMatch; theWin = FrontNonFloatingWindow(); if( theWin ) { isGotoWin = (g.gotoDlg && theWin == GetDialogWindow( g.gotoDlg )); //LR: 1.7 - don't get window info on NULL! isFindWin = (g.searchDlg && theWin == GetDialogWindow( g.searchDlg )); windowKind = GetWindowKind( theWin ); isDA = ( windowKind < 0 ); isObjectWin = GetWindowKind( theWin ) == kHexEditWindowTag; if( isObjectWin ) { dWin = (EditWindowPtr)GetWRefCon( theWin ); //LR: 1.66 - don't set unless an edit window! selection = dWin->endSel > dWin->startSel; } else { selection = (isGotoWin || isFindWin); } } else // LR: v1.6.5 if no window is visible, then nothing is true! { isGotoWin = isFindWin = isObjectWin = isDA = selection = 0; } // LR: v1.6.5 - rewrite of scrap check if( isObjectWin || isFindWin || isGotoWin ) { #if TARGET_API_MAC_CARBON ScrapFlavorFlags flavorFlags; ScrapRef scrapRef; OSErr anErr; anErr = GetCurrentScrap( &scrapRef ); if( !anErr ) anErr = GetScrapFlavorFlags( scrapRef, kScrapFlavorTypeText, &flavorFlags ); // non-blocking check for scrap data if( !anErr ) anErr = GetScrapFlavorSize( scrapRef, kScrapFlavorTypeText, &scrapSize ); // blocking call to get size #else long offset; scrapSize = GetScrap( NULL, 'TEXT', &offset ); #endif scrapExists = scrapSize > 0; } else scrapExists = false; undoExists = (isObjectWin && gUndo.type != 0 && gUndo.theWin == dWin); // check for NULL gUndo! // LR: - enable file menu items during search, via Aaron D. // LR: _enableMenuItem( fileMenu, FM_New, g.searchDlg == NULL ); // LR: _enableMenuItem( fileMenu, FM_Open, g.searchDlg == NULL ); // LR: 1.65 moved print names to string for localization GetIndString( menuStr, strPrint, (isObjectWin && dWin->startSel < dWin->endSel) ? 2 : 1 ); SetMenuItemText( fileMenu, FM_Print, menuStr ); //LR 188 -- page setup should always be enabled // _enableMenuItem( fileMenu, FM_PageSetup, isObjectWin ); //SEL: 1.7 - enabled for carbon _enableMenuItem( fileMenu, FM_Print, isObjectWin ); // HR/LR 050328 - Enable and check FM_Disassemble menu item _enableMenuItem( fileMenu, FM_Disassemble, isObjectWin ); CheckMenuItem( fileMenu, FM_Disassemble, g.disassemble ); _enableMenuItem( fileMenu, FM_OtherFork, isObjectWin ); _enableMenuItem( fileMenu, FM_Close, isDA || isObjectWin || isFindWin || isGotoWin ); // LR: v1.6.5 rewrite via Max Horn _enableMenuItem( fileMenu, FM_Save, isObjectWin && dWin->dirtyFlag ); _enableMenuItem( fileMenu, FM_SaveAs, isObjectWin ); _enableMenuItem( fileMenu, FM_Revert, isObjectWin && dWin->refNum && dWin->dirtyFlag ); _enableMenuItem( editMenu, 0, theWin != NULL ); _enableMenuItem( editMenu, EM_Undo, isDA || undoExists ); _enableMenuItem( editMenu, EM_Cut, isDA || (selection && (!gPrefs.overwrite || (gPrefs.overwrite && !gPrefs.nonDestructive))) ); _enableMenuItem( editMenu, EM_Copy, isDA || selection ); _enableMenuItem( editMenu, EM_Paste, isDA || (scrapExists && (!gPrefs.overwrite || (gPrefs.overwrite && !gPrefs.nonDestructive))) ); _enableMenuItem( editMenu, EM_Clear, isDA || selection ); _enableMenuItem( editMenu, EM_SelectAll, isDA || isObjectWin || isFindWin || isGotoWin ); _enableMenuItem( findMenu, 0, isObjectWin || isFindWin || isGotoWin ); /* 1.65 _enableMenuItem( findMenu, SM_Find, isObjectWin ); _enableMenuItem( findMenu, SM_GotoAddress, isObjectWin ); */ _enableMenuItem( findMenu, SM_FindForward, selection || (isObjectWin && dWin->fileSize && g.searchBuffer[0]) ); //LR 1.72 -- only enable w/something to search :) _enableMenuItem( findMenu, SM_FindBackward, selection || (isObjectWin && dWin->fileSize && g.searchBuffer[0]) ); _enableMenuItem( findMenu, SM_Replace, selection || (isObjectWin && dWin->fileSize && g.searchBuffer[0]) ); _enableMenuItem( optionsMenu, OM_NonDestructive, gPrefs.overwrite ); //LR 1.74 -- only available in overwrite mode CheckMenuItem( optionsMenu, OM_HiAscii, gPrefs.asciiMode ); CheckMenuItem( optionsMenu, OM_DecimalAddr, gPrefs.decimalAddr ); CheckMenuItem( optionsMenu, OM_Backups, gPrefs.backupFlag ); CheckMenuItem( optionsMenu, OM_WinSize, gPrefs.constrainSize ); CheckMenuItem( optionsMenu, OM_Overwrite, gPrefs.overwrite ); CheckMenuItem( optionsMenu, OM_NonDestructive, !gPrefs.nonDestructive ); //LR 190 -- REVERSE (updated text, not code) CheckMenuItem( optionsMenu, OM_MoveOnlyPaging, gPrefs.moveOnlyPaging ); //LR 180 -- optional move only paging CheckMenuItem( optionsMenu, OM_Unformatted, !gPrefs.formatCopies ); CheckMenuItem( optionsMenu, OM_VertBars, gPrefs.vertBars ); CheckMenuItem( optionsMenu, OM_OpenOnLaunch, gPrefs.dialogAtLaunch ); //WD_rpw 12-18-04 // LR: v1.6.5 Lots of re-writing on handling the color scheme menu #if !TARGET_API_MAC_CARBON // no color usage if not displayable! if( !g.colorQDFlag ) gPrefs.useColor = false; #endif // LR: v1.6.5 CheckMenuItem( gColorMenu, CM_UseColor, gPrefs.useColor ); // allow turning on even if not usable // LR: v1.6.5 Try to show status of color in new windows to help "intuitive" nature of menu GetIndString( menuStr, strColor, (gPrefs.useColor) ? 2 : 1 ); SetMenuItemText( colorMenu, CM_UseColor, menuStr ); //LR 181 -- show the current window color (or default if no windows) if( _cmCheckedItem ) CheckMenuItem( colorMenu, _cmCheckedItem, false ); _cmCheckedItem = isObjectWin ? dWin->csMenuID : gPrefs.csMenuID; CheckMenuItem( colorMenu, _cmCheckedItem, true ); selection = gPrefs.useColor; //LR 190 -- && isObjectWin && dWin->csResID > 0; i = CountMenuItems( colorMenu ); do { _enableMenuItem( colorMenu, i, selection ); // LR: v1.6.5 only enable for color windows } while( --i > 2 ); // NS: v1.6.6 checkmark front window in window menu if( theWin ) GetWTitle( theWin, frontWindowName ); //LR: 1.66 - don't use NULL window! else frontWindowName[0] = 0; i = CountMenuItems( windowMenu ); _enableMenuItem(windowMenu, 0, i != 0); // dim out Window menu if no windows up. while( i ) { GetMenuItemText( windowMenu, i, menuItemTitle ); // if you open more than one file with the same name (or edit the other fork)É namesMatch = EqualPStrings( frontWindowName, menuItemTitle ); // Éyou will have multiple items in the menu with the same text, and all will be checkmarked CheckMenuItem( windowMenu, i, namesMatch ); i--; } return( noErr ); }