void CHexEdit::OnEditCopy() { COleDataSource* pSource = new COleDataSource(); EmptyClipboard(); int dwLen = GetSelLength(); HGLOBAL hMemb = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT, dwLen); HGLOBAL hMema = ::GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT, (dwLen) * 3); if (!hMemb || !hMema) return; LPBYTE p = (BYTE*)::GlobalLock(hMemb); // copy binary memcpy(p, m_pData+m_selStart, dwLen); ::GlobalUnlock(hMemb); p = (BYTE*)::GlobalLock(hMema); // copy ascii memcpy(p, m_pData+m_selStart, dwLen); ::GlobalUnlock(hMema); for(int i = 0; i < dwLen;i++) { if(m_currentMode != EDIT_ASCII) { TOHEX(m_pData[m_selStart+i], p); *p++ = ' '; } else if(!isprint(*p)) *p++ = '.'; } pSource->CacheGlobalData(RegisterClipboardFormat("BinaryData"), hMemb); pSource->CacheGlobalData(CF_TEXT, hMema); pSource->SetClipboard(); }
LPTSTR SciEdit::GetSelText(void) { long lLen = GetSelLength() + 1; if (lLen > 0) { TCHAR *pRet = new TCHAR[lLen + 1]; if (pRet != NULL) { *pRet = '\0'; Call(SCI_GETSELTEXT, 0, (LPARAM)pRet); return pRet; } } return NULL; }
void CHexEdit::OnEditPaste() { COleDataObject obj; if (obj.AttachClipboard()) { HGLOBAL hmem = NULL; if (obj.IsDataAvailable(RegisterClipboardFormat("BinaryData"))) { hmem = obj.GetGlobalData(RegisterClipboardFormat("BinaryData")); } else if (obj.IsDataAvailable(CF_TEXT)) { hmem = obj.GetGlobalData(CF_TEXT); } if(hmem) { LPBYTE p = (BYTE*)::GlobalLock(hmem); DWORD dwSizeMem=::GlobalSize(hmem); DWORD dwSizeSel=GetSelLength(); DWORD dwLen =dwSizeMem>dwSizeSel?dwSizeSel:dwSizeMem; int insert; NormalizeSel(); if(m_selStart == 0xffffffff) { if(m_currentMode == EDIT_LOW) m_currentAddress++; insert = m_currentAddress; SelInsert(m_currentAddress, dwLen); } else { insert = m_selStart; SelDelete(m_selStart,m_selStart+dwLen-1); SelInsert(insert, dwLen); } memcpy(m_pData+insert, p, dwLen); m_currentAddress = insert+dwLen; // RepositionCaret(m_currentAddress); ResetPos(); Invalidate(FALSE); ::GlobalUnlock(hmem); } } }
bool SciDoc::DoLoadFromFile(const char*filename,bool insert) { _lasterror=""; errno=0; memset(bom,0,sizeof(bom)); bool rv=true; bool ro=GetReadOnly(); FXTextCodec *codec=NULL; if (ro&&insert) { _lasterror=_("Document is marked read-only."); return false; } if (FXStat::isDirectory(filename)) { _lasterror=_("is a directory"); return false; } bool DefaultToAscii=SettingsBase::instance()->DefaultToAscii; bool DefaultToSbcs=SettingsBase::instance()->DefaultToSbcs; switch (get_file_encoding(filename)) { case 'B': { // Binary file if ( !ConfirmOpenBinary(this,filename) ) { _lasterror=BinaryFileMessage(); return false; } if (!insert) { SetUTF8(!DefaultToSbcs); } break; } case 'T': { // Plain US-ASCII text file if (!insert) { SetUTF8(!DefaultToAscii); } break; } case 'H': { // High (extended ASCII) text file. if (!insert) { SetUTF8(!DefaultToSbcs); } break; } case 'U': { // UTF-8 encoded text file w/o BOM. if (!insert) { SetUTF8(true); } break; } case 'M': { // UTF-8 BOM. if (!insert) { strcpy(bom,"\0357\0273\0277"); SetUTF8(true); } break; } case 'Z': { // Zero-length (empty) file. if (!insert) { SetUTF8(!DefaultToAscii); } break; } case 'e': { // UTF-16LE BOM. if (!insert) { codec=new FXUTF16LECodec(); strcpy(bom,"\377\376"); SetUTF8(false); } break; } case 'E': { // UTF-16BE BOM. if (!insert) { codec=new FXUTF16BECodec(); strcpy(bom,"\376\377"); SetUTF8(false); } break; } case 'F': { // Failure, could not read the file. _lasterror=SystemErrorStr(); return false; break; } } FXFile fh(filename, FXFile::Reading); if (fh.isOpen()) { if (ro) { // This might happen e.g. if we are updating a document that has been modified externally sendMessage(SCI_SETREADONLY,0,0); } static const int BUFSIZE=1025; char buf[BUFSIZE]; fh.position(strlen(bom),FXIO::Begin); long p=0; _loading=!insert; if (insert) { p=sendMessage(SCI_GETCURRENTPOS, 0,0); sendMessage(SCI_BEGINUNDOACTION, 0, 0); } else { sendMessage(SCI_CLEARALL,0,0); } do { memset(buf,0,BUFSIZE); FXival n=fh.readBlock(buf,BUFSIZE-1); if (n<0) { _lasterror=SystemErrorStr(); rv=false; break; } buf[n]='\0'; if (insert) { _dirty=true; if (GetSelLength()>0) { sendString(SCI_REPLACESEL,0,buf); p=sendMessage(SCI_GETCURRENTPOS, 0,0); } else { sendString(SCI_INSERTTEXT,p,buf); p+=n; } } else { sendString(SCI_APPENDTEXT,n,buf); } } while (!fh.eof()); fh.close(); if (rv) { if (insert) { GoToPos(p); sendMessage(SCI_CONVERTEOLS,sendMessage(SCI_GETEOLMODE,0,0),0); sendMessage(SCI_ENDUNDOACTION,0,0); } else { _filename=FXPath::absolute(filename); _filetime=FXStat::modified(_filename); _dirty=false; need_backup=false; if (codec) { const char *orig=(const char*)sendMessage(SCI_GETCHARACTERPOINTER,0,0); FXString recode; recode.length(codec->mb2utflen(orig,sendMessage(SCI_GETLENGTH,0,0))); codec->mb2utf((char*)recode.text(),recode.length(),orig,sendMessage(SCI_GETLENGTH,0,0)); delete codec; SetUTF8(true); sendString(SCI_SETTEXT,0,recode.text()); } SetEolModeFromContent(); sendMessage(SCI_EMPTYUNDOBUFFER,0,0); } AdjustHScroll(); } if (ro) { sendMessage(SCI_SETREADONLY,1,0); } } else { _lasterror=SystemErrorStr(); rv=false; } _loading=false; return rv; }