SOL9 Sample: ImageBoxFromUrl

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2012/04/08


#include <sol\ApplicationView.h>
#include <sol\ListView.h>
#include <sol\ImageBox.h>
#include "resource.h"

namespace SOL {

class AppView :public ApplicationView {
private:
  ListView urlList;
  ImageBox imageBox;
  String   imageUrl;

public:
  /**
   * Constructor
   */
  AppView(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name,
    args.set(XmNstyle, (ulong)(WS_VSCROLL|WS_HSCROLL) ))
  {
    //Create ImageBoxes to display image files of png, jpg, bmp and tif formats.
    String moduleDir = getModuleDirectory();

    Args ar;
    ar.set(XmNstyle, LVS_REPORT);

    urlList.create(this, _T(""), ar);
    ListViewColumn items[] = {
        {_T("Image url"),    LVCFMT_LEFT, 200},
      };
    urlList.setColumn(items, sizeof(items)/sizeof(items[0]));

    const TCHAR* urls[] = {
      _T("http://www.antillia.com/pictures/33kannon.bmp"),
      _T("http://www.antillia.com/pictures/3shin.gif"),
      _T("http://www.antillia.com/pictures/chichibu_shrine.tif"),
      _T("http://www.antillia.com/pictures/goshinmon.png"),
      _T("http://www.antillia.com/pictures/red_leaves.jpg"),

      _T("http://www.antillia.com/pictures/muzepark_icho_namiki.jpg"),
      _T("http://www.antillia.com/pictures/muzepark_outdoor_stage.jpg"),
      _T("http://www.antillia.com/pictures/muzepark_parthenon.jpg"),
      _T("http://www.antillia.com/pictures/chichibu_musepark_bridge_fall.jpg"),
      _T("http://www.antillia.com/pictures/hitsujiyama_park_shibazakura.jpg"),
    };
    for (int i = 0; i<CountOf(urls); i++) {
      urlList.insertText(i, urls[i]);
    }
    urlList.addCallback(XmNdoubleClickCallback, this,
      (Callback)&AppView::urlSelected, NULL);

    urlList.addCallback(XmNreturnCallback, this,
      (Callback)&AppView::urlSelected, NULL);
    //Create a ImageBox to display an image of its original size.
    ar.reset();
    ar.set(XmNx,      2);
    ar.set(XmNy,      130);
    ar.set(XmNwidth,  128);
    ar.set(XmNheight, 128);
    ar.set(XmNscaling, ImageBox::StretchWindow);
    //ar.set(XmNstyle,  (ulong)WS_BORDER);
    imageBox.create(this, NULL, ar);

    addCallback(XmNmenuCallback, ID_EXIT, this,
      (Callback)&AppView::exit, NULL);

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

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

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


    restorePlacement();
  }

public:
  ~AppView()
  {
  }


private:
  void urlSelected(Action& action)
  {
    String url = "";

    if (urlList.getSelectedItemText(url)) {

      String moduleDir = getModuleDirectory();
      const TCHAR* turl = (const TCHAR*)url;
      const TCHAR* slash = strrchr(turl, '/');
      if (slash) {
        slash++;
        TCHAR saveFileName[MAX_PATH];
        _stprintf_s(saveFileName, CountOf(saveFileName), _T("%s\\%s"), (const TCHAR*)moduleDir, slash);
        //MessageBox(NULL, saveFileName, turl, MB_OK);
        if (imageBox.downloadImage(turl,  saveFileName)) {
          setCaption(turl);
        }
      }
    }
  }

private:
  long size(Event& event)
  {
    RECT r;

    getClientRect(&r);
    int w = r.right - r.left;
    int h = r.bottom - r.top;

    urlList.reshape(2, 2, w-4, 2+120);
    urlList.setColumnWidth(0, w-4-40);
    return 0;
  }

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


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


private:
  void version(Action& action)
  {    
    showMessageDialog(_T("ImageBoxFromUrl 1.0"), 
      _T("ImageBox sample program to display a jpeg file of Url."));

  }
};

}
//
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.