SOL9 Sample: ScrolledImageViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


#define _CONSOLE_

#include <sol/ModuleFileName.h>
#include <sol/DropFiles.h>
#include <sol/FileDialog.h>

#include <sol/opencv/OpenCVApplicationView.h>
#include <sol/opencv/OpenCVScrolledImageView.h>
#include <sol/opencv/OpenCVMat.h>

namespace SOL {

class MainView :public OpenCVApplicationView {

private:
  ////////////////////////////////////////////////////////////////////////////////////////
  //Inner class startss
  class SimpleView :public OpenCVScrolledImageView {
    
  public:
    SimpleView(View* parent, const TCHAR* name, Args& args)
    :OpenCVScrolledImageView(parent, name, args)
    {
      //1 Let this view accept WM_DROPFILES message.
      dragAcceptFiles(TRUE);
      
      //2 DropFiles event hanlder.
      //When this view gets WM_DROPFILES event, then dropFiles method in parent will be called 
      addEventHandler(WM_DROPFILES, parent,
        (Handler)&MainView::dropFiles, NULL);

       try {
        loadImage("..\\images\\flower.png");
        
      } catch (SOL::Exception ex) {
        caught(ex);
      }
    }
    
    ~SimpleView()
    {
    }
  };
  //Inner class ends.
  ////////////////////////////////////////////////////////////////////////////////////////

  SmartPtr<SimpleView>  view;
  FileDialog            filedlg;

  void resize(int w, int h)
  {
    if (view) {
      view -> reshape(0, 0, w, h);
//      view -> postResizeRequest(w, h);
    }
  }

  void openFile(const char* filename)
  {
    try {
      view -> loadImage(filename);
      char title[MAX_PATH];
      sprintf_s(title, CountOf(title), "%s - %s", filename, getAppName());
      setText(title);
      
    } catch (Exception& ex) {
      caught(ex);
    }
  }
  
  
  long dropFiles(Event& event)
  {
    char fileName[MAX_PATH] = { 0 };
    DropFiles drop((HDROP)event.getWParam());
    //fileName[0] = ZERO;
    int num = drop.queryFile(0, fileName, CountOf(fileName));
    if(num > 0) {
      if (filedlg.isImageFileName(fileName)) {
        openFile(fileName);
        bringUp();
      } else {        
        bringUp(); //Activate and raise this view
        
        showErrorDialog("Invalid image filename", fileName,  MB_OK);
      }
    }    
    return 0;
  }

  void confirm(Action& action)
  {
    int rc = MessageBox(NULL, "Are you sure to close this window?", "Confirmation", 
                MB_OKCANCEL|MB_ICONEXCLAMATION);
    if (rc == IDOK) {
      savePlacement();
      exit(action);
    }
  }
  
public:
  MainView(OpenCVApplication& applet, const char* name, Args& args)
  :OpenCVApplicationView(applet, name, args)
  {
    try {
      Args ar;
      view = new SimpleView(this, "sview", ar); 
      ar.reset();
      ar.set(XmNfilter, FileDialog::getImageFilesFilter());
      filedlg.create(this, "OpenFile", ar);
    } catch (Exception& ex) {
      caught(ex);
    }
    
    addCallback(XmNmenuCallback, IDM_EXIT, this,
          (Callback)&MainView::confirm, NULL);
    restorePlacement();
  }

  ~MainView()
  {
  }
  
  void open(Action& action)
  {
    Args ar;
    
    char dir[MAX_PATH];
    memset(dir, (TCHAR)0, CountOf(dir));
    //Restore previously select folder from a registry(profile of this application) for fileDialog
    if (restoreFileFolder(dir, CountOf(dir))) {
      ar.set(XmNdirectory, dir);
      filedlg.setValues(ar);
    }
    
    try {    
      if(filedlg.open()) {
    
        const char* filename = filedlg.getFileName();
        saveFileFolder(filename);

        openFile(filename);
        
      }
    } catch (Exception& ex) {
      caught(ex);
    }
  }

};
}

//
void main(int argc, char** argv) 
{
  try {
    ModuleFileName module(argv[0]);
    
    const char*  name = module.getAppName();
    OpenCVApplication applet(name, argc, argv);

    Args args;
    args.set(XmNwidth,  500);
    args.set(XmNheight, 300);
    MainView view(applet, name, args);
    view.realize();

    applet.run();
    
  } catch (SOL::Exception& ex) {
    caught(ex);
  }
}


Last modified: 2 Dec. 2017

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