Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / C++

HexEdit - Window Binary File Editor

Rate me:
Please Sign up or sign in to vote.
4.96/5 (137 votes)
17 Oct 2012MIT45 min read 497.1K   22.4K   321  
Open-source hex editor with powerful binary templates
/*****************************************************************************
    COPYRIGHT (C) 2000-2001, Ken Bertelson <kbertelson@yahoo.com>


*****************************************************************************/
#include "stdafx.h"
#include "GridBtnCellCombo.h"

//#include "InPlaceList.h"
#include "../NewCellTypes/GridCellCombo.h"  // For CComboEdit & CInPlaceList

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNCREATE(CGridBtnCellCombo, CGridBtnCell)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CGridBtnCellCombo::CGridBtnCellCombo()
    : CGridBtnCell()
{

}

CGridBtnCellCombo::~CGridBtnCellCombo()
{

}

void CGridBtnCellCombo::Reset()
{
    CGridBtnCell::Reset();

    m_dwComboStyle = CBS_SIMPLE;
    m_StringArrayCombo.RemoveAll();
}

void CGridBtnCellCombo::OnClick( CPoint PointCellRelative)
{
    // immediately edit if user clicked on scroll down button picture

    int iCtlHit = RelPointInCtl( PointCellRelative);  // Relative point coords
    // returns:  Index of control that this point is within bounds of or -1 if no control matches

    BOOL bHitScrollDown = FALSE;
    if( iCtlHit >= 0)
    {
        // if user clicked on scroll down button picture, then show
        //  the drop-down, too

        UINT uiType = GetDrawCtlType( iCtlHit);
        UINT uiState = GetDrawCtlState( iCtlHit);

        bHitScrollDown =    ( uiType == DFC_SCROLL)
                            && uiState & DFCS_SCROLLDOWN;
    }
    if( bHitScrollDown)
    {
        // invoke the edit, now -- similar to CGridCtl::OnEditCell() logic
        CGridCtrl* pGrid = GetGrid();
        ASSERT( pGrid != NULL);

        CRect rect;
        if (!pGrid->GetCellRect( m_iRow, m_iCol, rect))
            return;

        SendMessageToParent(m_iRow, m_iCol, GVN_BEGINLABELEDIT);

        Edit( m_iRow, m_iCol, rect, PointCellRelative, IDC_INPLACE_CONTROL, VK_LBUTTON);
        return;
    }

    CGridBtnCell::OnClick( PointCellRelative);
}

/*****************************************************************************
May mean end of a button click sequence or to edit text

*****************************************************************************/
BOOL CGridBtnCellCombo::Edit(int nRow, int nCol, CRect rect, CPoint point, UINT nID, UINT nChar)
{
    int iCtlHit = RelPointInCtl( point);  // Relative point coords
    // returns:  Index of control that this point is within bounds of or -1 if no control matches

    BOOL bShowDropDownNow = FALSE;
    if( iCtlHit >= 0)
    {
        // if user clicked on scroll down button picture, then show
        //  the drop-down, too

        UINT uiType = GetDrawCtlType( iCtlHit);
        UINT uiState = GetDrawCtlState( iCtlHit);

        bShowDropDownNow =  ( uiType == DFC_SCROLL)
                            && uiState & DFCS_SCROLLDOWN;
    }
    BOOL bHitNonComboButton =   ( iCtlHit >= 0)
                                && !bShowDropDownNow;

    if( HasCellText()
        && !bHitNonComboButton )
    {
        m_ucEditing = TRUE;

        // InPlaceList auto-deletes itself
        CGridCtrl* pGrid = GetGrid();
        CInPlaceList* pInPlaceList = new CInPlaceList(  pGrid, rect, m_dwComboStyle, nID, nRow, nCol,
                                                        GetTextClr(), GetBackClr(), m_StringArrayCombo, (CString)GetText(), nChar);

        if( bShowDropDownNow)
        {
            if( m_dwComboStyle & CBS_DROPDOWN
                || m_dwComboStyle & CBS_DROPDOWNLIST)
            {
                pInPlaceList->ShowDropDown( TRUE);
            }
        }

        m_pBtnDataBase->SetEditWnd( pInPlaceList);
        return TRUE;
    }
    // call base class, otherwise
    return CGridBtnCell::Edit( nRow, nCol, rect, point, nID, nChar);
}

void CGridBtnCellCombo::EndEdit()
{
    ASSERT( m_pBtnDataBase != NULL);

    CWnd* pWnd = m_pBtnDataBase->GetEditWnd();

    if (pWnd)
        ((CInPlaceList*)pWnd)->EndEdit();

}

void CGridBtnCellCombo::OnEndEdit()
{
    ASSERT( m_pBtnDataBase != NULL);

    m_ucEditing = FALSE;
    m_pBtnDataBase->SetEditWnd( NULL);

    // make sure that all pushbuttons appear in "up" state
    const int iCtlNbr = GetDrawCtlNbr();
    for( int i1=0; i1 < iCtlNbr; i1++)
    {
        UINT uiType = GetDrawCtlType( i1);
        UINT uiState = GetDrawCtlState( i1);

        BOOL bIsScrollDown =    ( uiType == DFC_SCROLL)
                                && uiState & DFCS_SCROLLDOWN;
        BOOL bIsPushButton =    ( uiType == DFC_BUTTON)
                                && uiState & DFCS_BUTTONPUSH;

        if( bIsScrollDown
            || bIsPushButton)
        {
            ClickedCellCtl( WM_LBUTTONUP,   // Command that invoked.  e.g. WM_LBUTTONDOWN
                            i1);    // zero-based index of image to draw
        }
    }


}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Australia Australia
Andrew has a BSc (1983) from Sydney University in Computer Science and Mathematics. Andrew began programming professionally in C in 1984 and has since used many languages but mainly C, C++, and C#.

Andrew has a particular interest in STL, .Net, and Agile Development. He has written articles on STL for technical journals such as the C/C++ User's Journal.

In 1997 Andrew began using MFC and released the source code for a Windows binary file editor called HexEdit, which was downloaded more than 1 million times. From 2001 there was a shareware version of HexEdit (later called HexEdit Pro). HexEdit has been updated to uses the new MFC (based on BCG) and is once more open source.

Comments and Discussions