SOL9 2.0 Sample: SolHexViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SolHexViewer.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



// SOL9
// 2008/09/05
// 2008/09/16 Modified to use restoreFileFolder/saveFileFolder.
// 2008/12/18 Modified to show FileVersion.

#include <sol\ApplicationView.h>
#include <sol\StatusBar.h>
#include <sol\FileDialog.h>

#include <sol\HexView.h>
#include <sol\FileVersionDialog.h>
#include <sol/DropTarget.h>

#include "resource.h"

namespace SOL {

/**
 * HexViewer class is a sample Appplication program to display hex data of a file 
 * by using SOL::HexView class
 */
class SolHexViewer :public ApplicationView {
private:
    HexView     hexv;

    StatusBar    statusbar;

    FileDialog fileDialog;
    
    FileVersionDialog fileVersion;

    CDropTarget dropTarget;

public:
    /**
     * Constructor
     */
    SolHexViewer(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name, 
            args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1)))
    {
        //Create a window of SOL::HexView.
        Args ar;
        ar.reset();
        ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
        hexv.create(this, NULL, ar);    
        
        ar.reset();
        statusbar.create(this, NULL, ar);

        ar.reset();
        ar.set(XmNaccessMode, FileDialog::OPEN);
        fileDialog.create(this, NULL, ar);

        fileVersion.create(this);
        //2009/11/01
        HWND hlistv = hexv.getWindow();
        
        dropTarget.setTarget(hlistv);

        dropTarget.setEventReceiver(getWindow());
        //

        addCallback(XmNmenuCallback, IDM_OPEN, this, 
            (Callback)&SolHexViewer::open, NULL);
        addCallback(XmNmenuCallback, IDM_EXIT, this, 
            (Callback)&SolHexViewer::exit, NULL);
        addCallback(XmNmenuCallback, IDM_VERSION, this, 
            (Callback)&SolHexViewer::version, NULL);

        addEventHandler(WM_SIZE, this, 
            (Handler)&SolHexViewer::size, NULL);

        addEventHandler(WM_CLOSE, this, 
            (Handler)&SolHexViewer::close, NULL);

        addEventHandler(WM_SOL_DROPFILES, this, 
            (Handler)&SolHexViewer::dropFiles, NULL);

        restorePlacement();
    }


public:
    long size(Event& event)
    {
        LPARAM l = event.getLParam();
        
        statusbar.send(WM_SIZE, event.getWParam(), event.getLParam());
        int w, h;
        statusbar.getSize(w, h);
        
        hexv.reshape(0, 0, w, HIWORD(l) - h);
        
        return 0;
    }

public:
    /**
     * WM_SOL_DROPFILES eventhandler.
     */
    long dropFiles(Event& event)
    {
        WPARAM wParam = event.getWParam();
        HDROP hDrop = (HDROP)wParam;
        if (hDrop) {
            int n = DragQueryFile(hDrop, 0xffffffff, NULL, 0);
            if (n >0) {

                //Get the first file only
                int i = 0;
                TCHAR path[_MAX_PATH];
                DragQueryFile(hDrop, i, path, sizeof(path));
            
                DWORD attr = GetFileAttributes(path);
                if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {

                    display(path);
                }
            }
        }
        return 0;
    }

public:
    long close(Event& event)
    {
        savePlacement();
        return defaultProc(event);
    }

public:
    void open(Action& action)
    {
        TCHAR dir[_MAX_PATH];
        if (restoreFileFolder(dir, sizeof(dir))) {
            Args ar;
            ar.set(XmNdirectory, dir);
            fileDialog.setValues(ar);
        }
        int rc = fileDialog.open();
        if (rc == IDOK) {
            const TCHAR* fileName = fileDialog.getFileName();
            display(fileName);
            saveFileFolder(fileName);
        }
    }


public:
    void version(Action& action)
    {
        fileVersion.popupAt(action);
    }

private:

    bool display(const TCHAR* fileName)
    {
        bool rc = false;
        hexv.clear();

        //Disable redrawing of HexView
        hexv.setRedraw(FALSE);
    
        FILE* fp = fopen(fileName, _T("rb"));         
        if (fp) {
            TCHAR caption[1024];
            _stprintf_s(caption, SizeOf(caption), _T("%s - SolHexViewer"), fileName);
            this->setText(caption);

            //GetFileLength
            int rest = _filelength(fileno(fp));
            int kb = rest/1024;
            int remain = rest%1024;
            if (remain>0) {
                kb++;
            }
            TCHAR text[1024];
            _stprintf_s(text, SizeOf(text), _T("%s : %d(KB)"), 
                            fileName, kb);
            statusbar.setText(text);

            const int   MAX_BUFF = 1024*64;
            char*  buff = new char[MAX_BUFF+1];
            int id = 0;
            while (rest>0) {
                int len = MAX_BUFF;
                if (rest<=MAX_BUFF) {
                    len = rest;
                }
                
                int fileIn = fread(buff, 1, len, fp);
                if (fileIn<=0) {
                    break;
                }
                hexv.addHexData(id, (unsigned char*)buff, fileIn);
                rest -= fileIn;
            }
            delete [] buff;
            fclose(fp);
            rc = true;
        }

        //Redrawing of HexView
        hexv.setRedraw(TRUE);

        return rc;
    }

};

}

//    Program entry point.
void    Main(int argc, TCHAR** argv)
{
    //The following is for the OLE dragdrop.
    OleInitialize(NULL);

    const String appClass = "SolHexViewer";
    try {
        Application applet(appClass, argc, argv);

        Args args;
    
        SolHexViewer viewer(applet, appClass, args);
        viewer.realize();

        applet.run();

    } catch (...) {

    }
    OleUninitialize();

}


Last modified: 11 Nov 2009

Copyright (c) 2009 Antillia.com ALL RIGHTS RESERVED.