SOL9 Sample: ImageButton

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * ImageButton.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL9
// 2012/04/08


#include <sol\ApplicationView.h>

#include <sol\FileDialog.h>
#include <sol\ImageButton.h>
#include <sol\ImageBox.h>

#include "resource.h"

namespace SOL {

class AppView :public ApplicationView {
private:
  ImageButton imageButton[4];
  ImageBox    originalImage;

  FileDialog  fileDialog;

public:
  /**
   * Constructor
   */
  AppView(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name,
    args.set(XmNstyle, (ulong)(WS_VSCROLL|WS_HSCROLL) ))
  {
    String moduleDir = getModuleDirectory();

    Args ar;
    //Buddha Images of Godoji (Godo-Temple) of Chichibu Fudasho 18 in Japan.
    const TCHAR* names[] = {_T("\\images\\image_1.png"), _T("\\images\\image_2.jpg"),
      _T("\\images\\image_3.bmp"), _T("\\images\\image_4.tif") };

    //Create imageButtons to display image files of png, jpg, bmp and tif formats.
    for (int i = 0; i<4; i++) {
      TCHAR fileName[1024];
      _stprintf_s(fileName, CountOf(fileName), _T("%s%s"), (const TCHAR*)moduleDir, names[i]);
      ar.reset();
      ar.set(XmNx,      2+102*i);
      ar.set(XmNy,      2);
      ar.set(XmNwidth,  100);
      ar.set(XmNheight, 100);
      ar.set(XmNimageFileName, fileName);
      ar.set(XmNscaling, ImageButton::StretchImage);

      ar.set(XmNstyle,  (ulong)WS_BORDER);
      ar.set(XmNcursor, (ulong)LoadCursor(NULL, IDC_HAND));
      imageButton[i].create(this, NULL, ar);
      imageButton[i].addCallback(XmNactivateCallback, this,
        (Callback)&AppView::arm, (void*)names[i]);
    }

    //2012/06/19
    imageButton[0].disable();
    imageButton[2].disable();

    //Create a ImageButton to display an image of its original size.
    ar.reset();
    ar.set(XmNx,      2);
    ar.set(XmNy,      2+112);
    ar.set(XmNwidth,  128);
    ar.set(XmNheight, 128);
    ar.set(XmNscaling, ImageBox::StretchWindow);
    ar.set(XmNstyle,  (ulong)WS_BORDER);
    originalImage.create(this, NULL, ar);

    //Add some menuCallbacks.
    addCallback(XmNmenuCallback, ID_OPEN, this,
      (Callback)&AppView::open, NULL);
    addCallback(XmNmenuCallback, ID_EXIT, this,
      (Callback)&AppView::exit, NULL);

    addCallback(XmNmenuCallback, ID_VERSION, this,
      (Callback)&AppView::version, NULL);

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

    //GetWindowsDirectory
    TCHAR dir[_MAX_PATH];
    ::GetWindowsDirectory(dir, _MAX_PATH);
    TCHAR* p = (TCHAR*)strchr(dir, (TCHAR)'\\');
    if (p) {
      *(++p) = '\0';
    }
    //Create a file selection dialog.
    ar.reset();
    ar.set(XmNdirectory, dir);
    ar.set(XmNfilter, _T("JPEG (*.jpg)\0 *.jpg\0"));
    fileDialog.create(this, _T("FileSelection"), ar);
  
    restorePlacement();
  }

public:
  ~AppView()
  {
  }

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

private:
  void arm(Action& action)
  {    
    const TCHAR* param = (const TCHAR*)action.getData();
    showMessageDialog(param, _T("ImageButton has been clicked"));
  
    String moduleDir = getModuleDirectory();
    TCHAR fileName[1024];
    _stprintf_s(fileName, CountOf(fileName), _T("%s%s"), (const TCHAR*)moduleDir, param);
    //Load an original image of a file which was selected. 
    originalImage.loadImage(fileName);
    setCaption(fileName);
  }

private:
  void setCaption(const TCHAR* fileName)
  {
    TCHAR title[1024];
    _stprintf_s(title, CountOf(title), _T("ImageButton - %s"), fileName);
    setText(title);
  }

private:
  void open(Action& action)
  {
    TCHAR dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    if (restoreFileFolder(dir, CountOf(dir))) { //2012/06/26
      Args ar;
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }

    if(fileDialog.open() == IDOK) {
      TCHAR* fileName = fileDialog.getFileName();
      //Load an image file selected from the file-selection-dialog of fileDialog.
      originalImage.loadImage(fileName);
      setCaption(fileName);
      saveFileFolder(fileName);
    }
  }

private:
  void version(Action& action)
  {    
    showMessageDialog(_T("ImageButton Sample 1.0"), 
      _T("Buddha Images of Godoji (Godo-Temple) of Chichibu Fudasho 18 in Japan."));
  }
};

}
//
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  
  try {
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNclassStyle, NO_REDRAW);
    AppView appView(applet, name, args);
    appView.realize();

    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}


Last modified: 1 Feb 2017

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