bool AP_Win32App::_cacheClipboardDoc(PD_DocumentRange *pDocRange) { UT_return_val_if_fail(m_pClipboard && pDocRange, false); UT_ByteBuf buf; UT_Error status;; UT_Byte b = 0; IE_Exp_RTF * pExpRtf = new IE_Exp_RTF(pDocRange->m_pDoc); if (pExpRtf) { status = pExpRtf->copyToBuffer(pDocRange,&buf); if(status != UT_OK) return false; buf.append(&b,1); // NULL terminate the string DELETEP(pExpRtf); } else { return false; } // now create a subdocument ... PD_Document * pDoc = new PD_Document(); if(!pDoc) return false; pDoc->newDocument(); PD_DocumentRange DocRange(pDoc, 2, 2); IE_Imp * pImp = 0; IE_Imp::constructImporter(pDoc, IE_Imp::fileTypeForSuffix(".rtf"),&pImp,0); if(pImp) { pImp->pasteFromBuffer(&DocRange,buf.getPointer(0),buf.getLength(),NULL); delete pImp; } else { return false; } m_pClipboard->setClipboardDoc(pDoc); return true; }
/*! * Add a cell to the list of selected regions. */ void FV_Selection::addCellToSelection(fl_CellLayout * pCell) { UT_ASSERT((m_iSelectionMode == FV_SelectionMode_TableColumn) || ( m_iSelectionMode == FV_SelectionMode_TableRow)); PL_StruxDocHandle sdhEnd = NULL; PL_StruxDocHandle sdhStart = pCell->getStruxDocHandle(); PT_DocPosition posLow = getDoc()->getStruxPosition(sdhStart) +1; // First block bool bres; bres = getDoc()->getNextStruxOfType(sdhStart,PTX_EndCell,&sdhEnd); PT_DocPosition posHigh = getDoc()->getStruxPosition(sdhEnd) -1; UT_ASSERT(bres && sdhEnd); PD_DocumentRange * pDocRange = new PD_DocumentRange(getDoc(),posLow,posHigh); m_vecSelRanges.addItem(pDocRange); IE_Exp_RTF * pExpRtf = new IE_Exp_RTF(pDocRange->m_pDoc); UT_ByteBuf * pByteBuf = new UT_ByteBuf; if (pExpRtf) { if(posLow < posHigh) { pDocRange->m_pos1++; pDocRange->m_pos2++; } pExpRtf->copyToBuffer(pDocRange,pByteBuf); if(posLow < posHigh) { pDocRange->m_pos1--; pDocRange->m_pos2--; } DELETEP(pExpRtf); } m_vecSelRTFBuffers.addItem(pByteBuf); FV_SelectionCellProps * pCellProps = new FV_SelectionCellProps; UT_sint32 iLeft,iRight,iTop,iBot; m_pView->getCellParams(posLow,&iLeft,&iRight,&iTop,&iBot); UT_DEBUGMSG(("In Selection left %d right %d top %d bot %d \n",iLeft,iRight,iTop,iBot)); pCellProps->m_iLeft = iLeft; pCellProps->m_iRight = iRight; pCellProps->m_iTop = iTop; pCellProps->m_iBot = iBot; m_vecSelCellProps.addItem(pCellProps); setSelectAll(false); }
/*! copy the given subset of the given document to the system clipboard in a variety of formats. to minimize the effects of race-conditions, we create all of the buffers we need and then post them to the server (well sorta) all at one time. \param pDocRange a range of the document to be copied */ void AP_UnixApp::copyToClipboard(PD_DocumentRange * pDocRange, bool bUseClipboard) { UT_ByteBuf bufRTF; UT_ByteBuf bufHTML4; UT_ByteBuf bufXHTML; UT_ByteBuf bufTEXT; UT_ByteBuf bufODT; // create RTF buffer to put on the clipboard IE_Exp_RTF * pExpRtf = new IE_Exp_RTF(pDocRange->m_pDoc); if (pExpRtf) { pExpRtf->copyToBuffer(pDocRange,&bufRTF); DELETEP(pExpRtf); } // create XHTML buffer to put on the clipboard IE_Exp_HTML * pExpHtml = new IE_Exp_HTML(pDocRange->m_pDoc); if (pExpHtml) { pExpHtml->set_HTML4 (false); pExpHtml->copyToBuffer (pDocRange, &bufXHTML); DELETEP(pExpHtml); } // create HTML4 buffer to put on the clipboard pExpHtml = new IE_Exp_HTML(pDocRange->m_pDoc); if (pExpHtml) { pExpHtml->set_HTML4 (true); pExpHtml->copyToBuffer(pDocRange, &bufHTML4); DELETEP(pExpHtml); } // Look to see if the ODT plugin is loaded IEFileType ftODT = IE_Exp::fileTypeForMimetype("application/vnd.oasis.opendocument.text"); bool bExpODT = false; if(ftODT != IEFT_Unknown) { // ODT plugin is present construct an exporter // IE_Exp * pODT = NULL; IEFileType genIEFT = IEFT_Unknown; GsfOutput * outBuf = gsf_output_memory_new(); UT_Error err = IE_Exp::constructExporter(pDocRange->m_pDoc,outBuf, ftODT,&pODT,& genIEFT); if(pODT && (genIEFT == ftODT)) { // // Copy to the buffer // err = pODT->copyToBuffer(pDocRange, &bufODT); bExpODT = (err == UT_OK); UT_DEBUGMSG(("Putting ODF on the clipboard...e:%d bExpODT:%d\n", err, bExpODT )); #ifdef DUMP_CLIPBOARD_COPY std::ofstream oss("/tmp/abiword-clipboard-copy.odt"); oss.write( (const char*)bufODT.getPointer (0), bufODT.getLength () ); oss.close(); #endif } } // create UTF-8 text buffer to put on the clipboard IE_Exp_Text * pExpText = new IE_Exp_Text(pDocRange->m_pDoc, "UTF-8"); if (pExpText) { pExpText->copyToBuffer(pDocRange,&bufTEXT); DELETEP(pExpText); } // NOTE: this clearData() will actually release our ownership of // NOTE: the CLIPBOARD property in addition to clearing any // NOTE: stored buffers. I'm omitting it since we seem to get // NOTE: clr callback after we have done some other processing // NOTE: (like adding the new stuff). // m_pClipboard->clearData(true,false); // TODO: handle CLIPBOARD vs PRIMARY XAP_UnixClipboard::T_AllowGet target = ((bUseClipboard) ? XAP_UnixClipboard::TAG_ClipboardOnly : XAP_UnixClipboard::TAG_PrimaryOnly); if (bufRTF.getLength () > 0) m_pClipboard->addRichTextData (target, bufRTF.getPointer (0), bufRTF.getLength ()); if (bufXHTML.getLength () > 0) m_pClipboard->addHtmlData (target, bufXHTML.getPointer (0), bufXHTML.getLength (), true); if (bufHTML4.getLength () > 0) m_pClipboard->addHtmlData (target, bufHTML4.getPointer (0), bufHTML4.getLength (), false); if (bExpODT && bufODT.getLength () > 0) m_pClipboard->addODTData (target, bufODT.getPointer (0), bufODT.getLength ()); if (bufTEXT.getLength () > 0) m_pClipboard->addTextData (target, bufTEXT.getPointer (0), bufTEXT.getLength ()); { // TODO: we have to make a good way to tell if the current selection is just an image FV_View * pView = NULL; if(getLastFocussedFrame()) pView = static_cast<FV_View*>(getLastFocussedFrame()->getCurrentView()); if (pView && !pView->isSelectionEmpty()) { // don't own, don't g_free const UT_ByteBuf * png = 0; pView->saveSelectedImage (&png); if (png && png->getLength() > 0) { m_pClipboard->addPNGData(target, static_cast<const UT_Byte*>(png->getPointer(0)), png->getLength()); } } } m_pClipboard->finishedAddingData(); return; }
/*! get the current contents of the selection in the window last known to have a selection using one of the formats in the given list. \param formatList the list of acceptable formats \param ppData \param pLen a pointer to an integer representing the length \param pszFormatFound a pointer for the data to be returned in \return True if successful, false otherwise. */ bool AP_UnixApp::getCurrentSelection(const char** formatList, void ** ppData, UT_uint32 * pLen, const char **pszFormatFound) { int j; *ppData = NULL; // assume failure *pLen = 0; *pszFormatFound = NULL; if (!m_pViewSelection || !m_pFrameSelection || !m_bHasSelection) return false; // can't do it, give up. PD_DocumentRange dr; if (m_cacheSelectionView == m_pViewSelection) { dr = m_cacheDocumentRangeOfSelection; } else { // TODO if we ever support multiple view types, we'll have to // TODO change this. FV_View * pFVView = static_cast<FV_View *>(m_pViewSelection); pFVView->getDocumentRangeOfCurrentSelection(&dr); } m_selectionByteBuf.truncate(0); for (j=0; (formatList[j]); j++) { if ( AP_UnixClipboard::isRichTextTag(formatList[j]) ) { IE_Exp_RTF * pExpRtf = new IE_Exp_RTF(dr.m_pDoc); if (!pExpRtf) return false; // give up on memory errors pExpRtf->copyToBuffer(&dr,&m_selectionByteBuf); DELETEP(pExpRtf); goto ReturnThisBuffer; } if ( AP_UnixClipboard::isHTMLTag(formatList[j]) ) { IE_Exp_HTML * pExpHTML = new IE_Exp_HTML(dr.m_pDoc); if (!pExpHTML) return false; pExpHTML->set_HTML4 (!strcmp (formatList[j], "text/html")); pExpHTML->copyToBuffer(&dr,&m_selectionByteBuf); DELETEP(pExpHTML); goto ReturnThisBuffer; } if ( AP_UnixClipboard::isImageTag(formatList[j]) ) { // TODO: we have to make a good way to tell if the current selection is just an image FV_View * pView = NULL; if(getLastFocussedFrame()) pView = static_cast<FV_View*>(getLastFocussedFrame()->getCurrentView()); if (pView && !pView->isSelectionEmpty()) { // don't own, don't g_free const UT_ByteBuf * png = 0; pView->saveSelectedImage (&png); if (png && png->getLength() > 0) { m_selectionByteBuf.ins (0, png->getPointer (0), png->getLength ()); goto ReturnThisBuffer; } } } if ( AP_UnixClipboard::isTextTag(formatList[j]) ) { IE_Exp_Text * pExpText = new IE_Exp_Text(dr.m_pDoc, "UTF-8"); if (!pExpText) return false; pExpText->copyToBuffer(&dr,&m_selectionByteBuf); DELETEP(pExpText); goto ReturnThisBuffer; } // TODO add other formats as necessary } UT_DEBUGMSG(("Clipboard::getCurrentSelection: cannot create anything in one of requested formats.\n")); return false; ReturnThisBuffer: UT_DEBUGMSG(("Clipboard::getCurrentSelection: copying %d bytes in format [%s].\n", m_selectionByteBuf.getLength(),formatList[j])); *ppData = const_cast<void *>(static_cast<const void *>(m_selectionByteBuf.getPointer(0))); *pLen = m_selectionByteBuf.getLength(); *pszFormatFound = formatList[j]; return true; }
/*! copy data in required format to the clipboard */ bool AP_Win32App::_copyFmtToClipboard(PD_DocumentRange * pDocRange, const char * pszFmt) { UT_return_val_if_fail(m_pClipboard && pszFmt, false); UT_ByteBuf buf; UT_Error status;; UT_Byte b = 0; if(0 == strcmp(AP_CLIPBOARD_TEXTPLAIN_8BIT, pszFmt)) { IE_Exp_Text * pExpText = new IE_Exp_Text(pDocRange->m_pDoc); if (pExpText) { status = pExpText->copyToBuffer(pDocRange,&buf); if(status != UT_OK) return false; buf.append(&b,1); // NULL terminate the string m_pClipboard->addData(AP_CLIPBOARD_TEXTPLAIN_8BIT, (UT_Byte *)buf.getPointer(0),buf.getLength()); DELETEP(pExpText); UT_DEBUGMSG(("CopyToClipboard: copying %d bytes in TEXTPLAIN format.\n", buf.getLength())); } else { return false; } } else if(0 == strcmp(AP_CLIPBOARD_TEXTPLAIN_UCS2, pszFmt)) { const char *szEnc = XAP_EncodingManager::get_instance()->getNativeUnicodeEncodingName(); IE_Exp_Text * pExpUnicodeText = new IE_Exp_Text(pDocRange->m_pDoc,szEnc); if (pExpUnicodeText) { status = pExpUnicodeText->copyToBuffer(pDocRange,&buf); if(status != UT_OK) return false; UT_Byte b[2] = {0,0}; buf.append(b,2); // NULL terminate the string m_pClipboard->addData(AP_CLIPBOARD_TEXTPLAIN_UCS2, (UT_Byte *)buf.getPointer(0),buf.getLength()); DELETEP(pExpUnicodeText); UT_DEBUGMSG(("CopyToClipboard: copying %d bytes in TEXTPLAIN UNICODE format.\n", buf.getLength()*2)); } else { return false; } } else if(0 == strcmp(AP_CLIPBOARD_RTF, pszFmt)) { IE_Exp_RTF * pExpRtf = new IE_Exp_RTF(pDocRange->m_pDoc); if (pExpRtf) { status = pExpRtf->copyToBuffer(pDocRange,&buf); if(status != UT_OK) return false; buf.append(&b,1); // NULL terminate the string m_pClipboard->addData(AP_CLIPBOARD_RTF,(UT_Byte *)buf.getPointer(0),buf.getLength()); DELETEP(pExpRtf); UT_DEBUGMSG(("CopyFmtToClipboard: copying %d bytes in RTF format.\n", buf.getLength())); } else { return false; } } return true; }