HexEdit/GridCtrl_src/GridCtrl.cpp
BOOL CGridCtrl::PasteTextToGrid(CCellID cell, COleDataObject* pDataObject)
{
    if (!IsValid(cell) || !IsCellEditable(cell) || !pDataObject->IsDataAvailable(CF_TEXT))
        return FALSE;

    // Get the text from the COleDataObject
    HGLOBAL hmem = pDataObject->GetGlobalData(CF_TEXT);
    CMemFile sf((BYTE*) ::GlobalLock(hmem), ::GlobalSize(hmem));

    // CF_TEXT is ANSI text, so we need to allocate a char* buffer
    // to hold this.
    LPSTR szBuffer = new char[::GlobalSize(hmem)];
    if (!szBuffer)
        return FALSE;

    sf.Read(szBuffer, ::GlobalSize(hmem));
    ::GlobalUnlock(hmem);

    // Now store in generic TCHAR form so we no longer have to deal with
    // ANSI/UNICODE problems
    CString strText = szBuffer;
    delete[] szBuffer;

    // Parse text data and set in cells...
    strText.LockBuffer();
    CString strLine = strText;
    int nLine = 0;

    // Find the end of the first line
    int nIndex;
    do
    {
        int nColumn = 0;
        nIndex = strLine.Find(_T("\n"));

        // Store the remaining chars after the newline
        CString strNext = (nIndex < 0)? _T("")  : strLine.Mid(nIndex + 1);

        // Remove all chars after the newline
        if (nIndex >= 0)
            strLine = strLine.Left(nIndex);

        int nLineIndex = strLine.FindOneOf(_T("\t,"));
        CString strCellText = (nLineIndex >= 0)? strLine.Left(nLineIndex) : strLine;

        // skip hidden rows
        int iRowVis = cell.row + nLine;
        while( iRowVis < GetRowCount())
        {
            if( GetRowHeight( iRowVis) > 0)
                break;
            nLine++;
            iRowVis++;
        }

        while (!strLine.IsEmpty())
        {
            // skip hidden columns
            int iColVis = cell.col + nColumn;
            while( iColVis < GetColumnCount())
            {
                if( GetColumnWidth( iColVis) > 0)
                    break;
                nColumn++;
                iColVis++;
            }

            CCellID TargetCell(iRowVis, iColVis);
            if (IsValid(TargetCell))
            {
                strCellText.TrimLeft();
                strCellText.TrimRight();

#if _MSC_VER >= 1300
                ValidateAndModifyCellContents(TargetCell.row, TargetCell.col, CStringW(strCellText));
#else
                ValidateAndModifyCellContents(TargetCell.row, TargetCell.col, strCellText);
#endif
                // Make sure cell is not selected to avoid data loss
                SetItemState(TargetCell.row, TargetCell.col,
                    GetItemState(TargetCell.row, TargetCell.col) & ~GVIS_SELECTED);
            }

            strLine = (nLineIndex >= 0)? strLine.Mid(nLineIndex + 1) : _T("");
            nLineIndex = strLine.FindOneOf(_T("\t,"));
            strCellText = (nLineIndex >= 0)? strLine.Left(nLineIndex) : strLine;

            nColumn++;
        }

        strLine = strNext;
        nLine++;
    } while (nIndex >= 0);

    strText.UnlockBuffer();
    Refresh();

    return TRUE;
}
// Pastes text from the clipboard to the selected cells

Pastes text from the clipboard to the selected cells
Pastes text from the clipboard to the selected cells
BOOL CGridCtrl::PasteTextToGrid(CCellID cell, COleDataObject* pDataObject)
{
    if (!IsValid(cell) || !IsCellEditable(cell) || !pDataObject->IsDataAvailable(CF_TEXT))
        return FALSE;
    HGLOBAL hmem = pDataObject->GetGlobalData(CF_TEXT);
    CMemFile sf((BYTE*) ::GlobalLock(hmem), ::GlobalSize(hmem));
    LPSTR szBuffer = new char[::GlobalSize(hmem)];
    if (!szBuffer)
        return FALSE;
    sf.Read(szBuffer, ::GlobalSize(hmem));
    ::GlobalUnlock(hmem);
    CString strText = szBuffer;
    delete[] szBuffer;
    strText.LockBuffer();
    CString strLine = strText;
    int nLine = 0;
    int nIndex;
    do
    {
        int nColumn = 0;
        nIndex = strLine.Find(_T("\n"));
        CString strNext = (nIndex < 0)? _T("")  : strLine.Mid(nIndex + 1);
        if (nIndex >= 0)
            strLine = strLine.Left(nIndex);
        int nLineIndex = strLine.FindOneOf(_T("\t,"));
        CString strCellText = (nLineIndex >= 0)? strLine.Left(nLineIndex) : strLine;
        int iRowVis = cell.row + nLine;
        while( iRowVis < GetRowCount())
        {
            if( GetRowHeight( iRowVis) > 0)
                break;
            nLine++;
            iRowVis++;
        }
        while (!strLine.IsEmpty())
        {
            int iColVis = cell.col + nColumn;
            while( iColVis < GetColumnCount())
            {
                if( GetColumnWidth( iColVis) > 0)
                    break;
                nColumn++;
                iColVis++;
            }
            CCellID TargetCell(iRowVis, iColVis);
            if (IsValid(TargetCell))
            {
                strCellText.TrimLeft();
                strCellText.TrimRight();
#if _MSC_VER >= 1300
                ValidateAndModifyCellContents(TargetCell.row, TargetCell.col, CStringW(strCellText));
#else
                ValidateAndModifyCellContents(TargetCell.row, TargetCell.col, strCellText);
#endif
                SetItemState(TargetCell.row, TargetCell.col,
                    GetItemState(TargetCell.row, TargetCell.col) & ~GVIS_SELECTED);
            }
            strLine = (nLineIndex >= 0)? strLine.Mid(nLineIndex + 1) : _T("");
            nLineIndex = strLine.FindOneOf(_T("\t,"));
            strCellText = (nLineIndex >= 0)? strLine.Left(nLineIndex) : strLine;
            nColumn++;
        }
        strLine = strNext;
        nLine++;
    } while (nIndex >= 0);
    strText.UnlockBuffer();
    Refresh();
    return TRUE;
}
BOOL CGridCtrl :: PasteTextToGrid ( CCellID cell , COleDataObject * pDataObject ) { if ( ! IsValid ( cell ) || ! IsCellEditable ( cell ) || ! pDataObject -> IsDataAvailable ( CF_TEXT ) ) return FALSE ; HGLOBAL hmem = pDataObject -> GetGlobalData ( CF_TEXT ) ; CMemFile sf ( ( BYTE * ) :: GlobalLock ( hmem ) , :: GlobalSize ( hmem ) ) ; LPSTR szBuffer = new char [ :: GlobalSize ( hmem ) ] ; if ( ! szBuffer ) return FALSE ; sf . Read ( szBuffer , :: GlobalSize ( hmem ) ) ; :: GlobalUnlock ( hmem ) ; CString strText = szBuffer ; delete [ ] szBuffer ; strText . LockBuffer ( ) ; CString strLine = strText ; int nLine = 0 ; int nIndex ; do { int nColumn = 0 ; nIndex = strLine . Find ( _T ( " \n " ) ) ; CString strNext = ( nIndex < 0 ) ? _T ( " " ) : strLine . Mid ( nIndex + 1 ) ; if ( nIndex >= 0 ) strLine = strLine . Left ( nIndex ) ; int nLineIndex = strLine . FindOneOf ( _T ( " \t " ) ) ; CString strCellText = ( nLineIndex >= 0 ) ? strLine . Left ( nLineIndex ) : strLine ; int iRowVis = cell . row + nLine ; while ( iRowVis < GetRowCount ( ) ) { if ( GetRowHeight ( iRowVis ) > 0 ) break ; nLine ++ ; iRowVis ++ ; } while ( ! strLine . IsEmpty ( ) ) { int iColVis = cell . col + nColumn ; while ( iColVis < GetColumnCount ( ) ) { if ( GetColumnWidth ( iColVis ) > 0 ) break ; nColumn ++ ; iColVis ++ ; } CCellID TargetCell ( iRowVis , iColVis ) ; if ( IsValid ( TargetCell ) ) { strCellText . TrimLeft ( ) ; strCellText . TrimRight ( ) ; #if _MSC_VER >= 1300 ValidateAndModifyCellContents ( TargetCell . row , TargetCell . col , CStringW ( strCellText ) ) ; #else ValidateAndModifyCellContents ( TargetCell . row , TargetCell . col , strCellText ) ; #endif SetItemState ( TargetCell . row , TargetCell . col , GetItemState ( TargetCell . row , TargetCell . col ) & ~ GVIS_SELECTED ) ; } strLine = ( nLineIndex >= 0 ) ? strLine . Mid ( nLineIndex + 1 ) : _T ( " " ) ; nLineIndex = strLine . FindOneOf ( _T ( " \t " ) ) ; strCellText = ( nLineIndex >= 0 ) ? strLine . Left ( nLineIndex ) : strLine ; nColumn ++ ; } strLine = strNext ; nLine ++ ; } while ( nIndex >= 0 ) ; strText . UnlockBuffer ( ) ; Refresh ( ) ; return TRUE ; }
Pastes text from the clipboard to the selected cells
Pastes text from the clipboard to the selected cells
// Get the text from the COleDataObject
 // CF_TEXT is ANSI text, so we need to allocate a char* buffer
 // to hold this.
 // Now store in generic TCHAR form so we no longer have to deal with
 // ANSI/UNICODE problems
 // Parse text data and set in cells...
 // Find the end of the first line
 // Store the remaining chars after the newline
 // Remove all chars after the newline
 // skip hidden rows
 // skip hidden columns
 // Make sure cell is not selected to avoid data loss
