SOL9 Sample: DropTarget

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9
// 2009/11/01 Ole DropTarget sample program to use SOL::CDropTarget class, 
//This applcation can accept files list dropped from Windows Explorer.

#include <sol\ApplicationView.h>
#include <sol\StatusBar.h>
#include <sol/ListView.h>

#include <sol/DropTarget.h>
#include <sol/SystemImageList.h>
#include <sol/FileInfo.h>

#include "resource.h"

namespace SOL {

/**
 */

class DropTarget :public ApplicationView {
private:
  ListView listView;

  StatusBar  statusbar;
  
  CDropTarget dropTarget;

  SystemImageList imageList;

public:

  /**
   * Constructor
   */
  DropTarget(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);
    ar.set(XmNstyle, LVS_SORTASCENDING|LVS_REPORT);
    listView.create(this, NULL, ar);  
    ListViewColumn items[] = {
      {_T("FileName"),  LVCFMT_LEFT, 500},
      /*  {_T("Type"),      LVCFMT_LEFT, 180},
      {_T("Size(KB)"),   LVCFMT_RIGHT,   100},
      {_T("DateTime"),    LVCFMT_LEFT, 140},
      */
    };
    listView.setColumn(items, XtNumber(items));
    listView.setImageList(&imageList, LVSIL_SMALL);
    //listView.setImageList((HIMAGELIST)NULL, LVSIL_NORMAL);

    ar.reset();
    statusbar.create(this, NULL, ar);
    statusbar.setText(_T("Ole DropTarget is ready! Please drag and drop files from Windows Explorer into the above ListView."));

    //2009/11/01
    dropTarget.setTarget((HWND)listView);
    dropTarget.setEventReceiver((HWND)(*this));
    //

    addCallback(XmNmenuCallback, IDM_CLEAR, this, 
      (Callback)&DropTarget::clear, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&DropTarget::exit, NULL);
    //addCallback(XmNmenuCallback, IDM_VERSION, this, 
    //  (Callback)&DropTarget::version, NULL);

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

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

    addEventHandler(WM_SOL_DROPFILES, this, 
      (Handler)&DropTarget::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);
    
    listView.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
        for (int i = 0; i<n; i++) {
          TCHAR path[_MAX_PATH];
          DragQueryFile(hDrop, i, path, CountOf(path)); //2012/06/26
      
          addItem(path);
        }
      }
    }
    return 0;
  }

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

public:
  void clear(Action& action)
  {
    listView.clear();
  }


private:

  bool addItem(const TCHAR* fileName)
  {
    bool rc = false;
    
    //Disable redrawing of HexView
    listView.setRedraw(FALSE);
    const TCHAR* items[1];
    items[0] = fileName;
    FileInfo fileInfo;
    int iconId = fileInfo.getSmallIconIndex(fileName);
    listView.insertLineWithIcon(1, items, 1, iconId);

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

    return rc;
  }

};

}

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

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

    Args args;
  
    DropTarget viewer(applet, name, args);
    viewer.realize();

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

}


Last modified: 1 Feb 2017

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