SOL9 Sample: FileListView

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL++2000 
// 2000.02.18

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\Static.h>
#include <sol\TextField.h>
#include <sol\ClientDC.h>
#include <sol\ListView.h>
#include <sol\ClientDC.h>
#include "resource.h"

namespace SOL {

class FileListView :public ApplicationView {
private:
  Static      label;
  TextField   textf;
  ListView  listview;

public:
  FileListView(Application& applet, const TCHAR* caption, Args& args)
        :ApplicationView(applet, caption, args) 
  {
    Args ar;

    ar.reset();
    label.create(this, _T("Filter (Ex. C:\\Windows\\*.*)"), ar);
  
    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    textf.create(this, NULL, ar);
    textf.addCallback(XmNactivateCallback, this,
      (Callback)&FileListView::listUp, NULL);

    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle, (ulong)LVS_REPORT|LVS_EDITLABELS|LVS_SORTASCENDING);
    listview.create(this, NULL, ar);

    addCallback(XmNmenuCallback, IDM_EXIT, this,
      (Callback)&FileListView::exit, NULL);
      
    StringList list;
    list.add("Name                     ");
    list.add("Size        ");
    list.add("Date        ");
    list.add("Time        ");
    IntegerList format;
    format.add(LVCFMT_LEFT);
    format.add(LVCFMT_RIGHT);
    format.add(LVCFMT_CENTER);
    format.add(LVCFMT_CENTER);

    listview.setColumn(&list, &format);

    restorePlacement();
  }

private:
  void listUp(Action& action)
  {  
    TCHAR pattern[_MAX_PATH];
    int len = textf.getText(pattern, CountOf(pattern)-1); //2012/06/26
    if(len) {
      listview.deleteAllItems();
      findFiles(pattern);
    }
  }


private:
  void findFiles(TCHAR* pattern)
  {
    WIN32_FIND_DATA data;
    memset(&data, 0, sizeof(WIN32_FIND_DATA));
    data.dwFileAttributes  = FILE_ATTRIBUTE_NORMAL;

    int n = 0;
    StringList list;

    TCHAR name[256];
    TCHAR size[20];
    TCHAR filedate[20];
    TCHAR filetime[20];
    HANDLE hf = ::FindFirstFile(pattern, &data);

    while(hf != INVALID_HANDLE_VALUE) {
      _stprintf_s(size, CountOf(size), _T("%d"), data.nFileSizeLow);

      SYSTEMTIME time;
      FILETIME ltime;
      ::FileTimeToLocalFileTime(&(data.ftLastWriteTime), &ltime);
      ::FileTimeToSystemTime(&ltime, &time);
      //<modifed date="2000.02.18" author="to-arai">
      _stprintf_s(filedate, CountOf(filedate), _T("%.4u/%.2u/%.2u"), 
        time.wYear, time.wMonth, time.wDay);
      //</modified>
      _stprintf_s(filetime, CountOf(filetime), _T("%.2u:%.2u:%.2u"), 
        time.wHour, time.wMinute, time.wSecond);

      strcpy_s(name, CountOf(name), (TCHAR*)data.cFileName);
      if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
        _stprintf_s(name, CountOf(name), _T("%s\\"), (TCHAR*)data.cFileName);
      }
      list.clear();
      list.add(name); 
      list.add(size);
      list.add(filedate);
      list.add(filetime);
      listview.insertLine(n++, &list);

      if(::FindNextFile(hf, &data)== FALSE){
        ::FindClose(hf);
        break;
      }
    }
  }

private:
  long size(Event& event)
  {
    LPARAM l = event.getLParam();
    ClientDC dc(this);
  //  HFONT hfont = (HFONT)dc.select(&font);
    int h = dc.getTextHeight();
  //  dc.select(hfont);

    int m = 0;
    label.reshape(0,  0, LOWORD(l), h);
    textf.reshape(0,   h+2, LOWORD(l), h+8);
    listview.reshape(0,  h*2+12, LOWORD(l), HIWORD(l)-h*2-12);
    return 0L;
  }
};

}


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(XmNbackground, (COLOR_BTNFACE +1));
    FileListView 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.