SOL9 Sample: ImageViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * ImageViewer.cpp 
 * Copyright (c) 2012 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9

// 2012/04/15 Updated to accept dropfiles.
// 2012/04/15 Updated to be able to select gif files in a fileDialog.

#include <sol\ApplicationView.h>
#include <sol\PaintDC.h>
//#include <sol\ClientDC.h>
#include <sol\FileDialog.h>
#include <sol\DropFiles.h>
#include <sol\Profile.h>
#include <sol\Folder.h>
#include <comutil.h>
#include "resource.h"


using namespace Gdiplus;

namespace SOL {

class ImageViewer :public ApplicationView {

private:

    FileDialog        fileDialog;

    _bstr_t imageFileName;

    Image*  image;

public:
    /**
     * Constructor
     */
    ImageViewer(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name,
        args.set(XmNstyle, (ulong)WS_VSCROLL|WS_HSCROLL)
            .set(XmNexStyle,(ulong)WS_EX_ACCEPTFILES) ) //2012/04/15 Accept dropFiles
    {

        addCallback(XmNmenuCallback, IDM_OPEN, this,
            (Callback)&ImageViewer::open, NULL);
        addCallback(XmNmenuCallback, IDM_EXIT, this,
            (Callback)&ImageViewer::exit, NULL);

        addCallback(XmNmenuCallback, IDM_VERSION, this,
            (Callback)&ImageViewer::version, NULL);

        addEventHandler(WM_PAINT, this, 
            (Handler)&ImageViewer::paint, NULL);

        addEventHandler(WM_DROPFILES, this, 
            (Handler)&ImageViewer::dropFiles, NULL);

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

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

    
        Args ar;
        //2012/04/16 Updated to incude gif fies.
        ar.set(XmNfilter, 
            _T("Image File(*.bmp,*.jpg,*.png,*.tif,*.ico,*.gif)\0*.bmp;*.jpg;*.png;*.tif;*.ico;*.gif\0JPEG File*.jpg)\0*.jpg\0PNG File(*.png)\0*.png\0Tiff File(*.tif)\0*.tif\0Icon File(*.ico)\0*.ico;\0GIF File(*.gif)\0*.gif;\0"));

        fileDialog.create(this, _T(""), ar);
        image = NULL;

        restorePlacement();
    }

public:
    ~ImageViewer()
    {
        clear();
    }

private:
    void clear()
    {
        if (image) {
            delete image;
            image = NULL;
        }
    }

private:
    void drawImage(_bstr_t& imageFieName)
    {
        TCHAR title[MAX_PATH];
        clear();
        try {
            image = new Image((const wchar_t*)imageFileName, TRUE);

            _stprintf_s(title, SizeOf(title), _T("%s - ImageViewer"), (const TCHAR*)imageFileName);
            
            setText(title);

            setScrollPos(HORIZONTAL, 0);

            setScrollPos(VERTICAL, 0);

            UINT width = image->GetWidth();
            UINT height = image->GetHeight();

            setScrollExtent(width, height);

            invalidate((const RECT*)NULL);

            update();
        } catch (...) {

        }
    }

private:
    //2012/04/16 EventHandler for WM_DROPFILES
    long dropFiles(Event& event) 
    {
        TCHAR fileName[1024];
        DropFiles drop((HDROP)event.getWParam());
        fileName[0] = Zero;
        int num = drop.queryFile(0, fileName, SizeOf(fileName));

        if(num > 0) {
            imageFileName = fileName;
            drawImage(imageFileName);
        }
        return 0;
    }

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

private:
    void    open(Action& action)
    {
        Args ar;
        
        TCHAR dir[MAX_PATH];
        memset(dir, (TCHAR)0, SizeOf(dir));
        //Call getFileFolder method in SOL::ApplicationView.
        //This gets a previously select folder from a registry(profile of this application) for fileDialog
        if (restoreFileFolder(dir, SizeOf(dir))) {
            ar.set(XmNdirectory, dir);
            fileDialog.setValues(ar);
        }
    
        if(fileDialog.open()){
            TCHAR title[MAX_PATH];

            imageFileName = fileDialog.getFileName();
            TCHAR* filename = fileDialog.getFileName();

            TCHAR* ftitle = fileDialog.getFileTitle();
        
            File file;
            if (file.isExistent(filename)) {
                //Call saveFileFolder method in SOL::ApplicationView.
                saveFileFolder(filename);

                drawImage(imageFileName);
            }
        }    
    }


private:
    void updateScrollRange()
    {
        if (image) {
            UINT width = image->GetWidth();
            UINT height = image->GetHeight();

            setScrollExtent(width, height);
        }
    }

private:
    long size(Event& event)
    {
        updateScrollRange();
        return 0;
    }

private:
    void version(Action& action)
    {
        showMessageDialog(_T("Version"),
            _T("ImageViewer 1.0.0.3 \r\n Copyright(c) 2012 Antillia.com"), MB_ICONINFORMATION|MB_OK);
    }

private:
    long paint(Event& event)
    {
        PaintDC pdc(this);

        Graphics graphics(pdc.get());

        //Image image((const wchar_t*)imageFileName);
        if (image) {
            int x = getScrollPos(Composite::HORIZONTAL);
            int y = getScrollPos(Composite::VERTICAL);
            UINT width = image->GetWidth();
            UINT height = image->GetHeight();

            graphics.DrawImage(image, -x, -y, width, height );
        }

        return 0;
    }

};

}


//////////////////////////////////////////////
//
void    Main(int argc, TCHAR** argv)
{
    const TCHAR* appClass = _T("ImageViewer");
  
    try {
        Application applet(appClass, argc, argv);

        Args args;
        //args.set(XmNclassStyle, 0);
        ImageViewer imageViewer(applet, appClass, args);
        imageViewer.realize();

        applet.run();
    } catch (...) {

    }
}


Last modified: 17 Apr 2012

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