SOL9 Sample: SolNameSpaceBrowser

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9 2.0
// 2012/11/03
// This is a simple sample program to browse folders, which is based on
// NameSpaceTreeControl and NameSpaceTreeControlEvents classes.
// 2012/11/15 Updated

#include <sol\ApplicationView.h>
#include <sol\shell\NameSpaceTreeControl.h>
#include <sol\shell\NameSpaceTreeControlEvents.h>
#include <sol\shell\ShellItem.h>
#include <sol\shell\ShellItemArray.h>
#include "resource.h"


namespace SOL {

class SolNameSpaceBrowser :public ApplicationView {

private:
  //Inner Class ------------------------------------
  //<NameSpaceTreeControlEventsListener>
  class NameSpaceTreeControlEventsListener :public NameSpaceTreeControlEvents {

  private:
    SolNameSpaceBrowser* solNameSpaceBrowser;

  public:
     NameSpaceTreeControlEventsListener()
    :solNameSpaceBrowser(NULL)
    {
    }

  public:
    void setApplicationView(SolNameSpaceBrowser* view)
    {
      solNameSpaceBrowser = view;
    }
  
  public:
    HRESULT STDMETHODCALLTYPE OnItemClick( 
        IShellItem *psi,
        NSTCEHITTEST nstceHitTest,
        NSTCECLICKTYPE nstceClickType)
    {
      HRESULT hr = S_FALSE;
      const TCHAR* title = _T("SolNameSpaceBrowser");

      solNameSpaceBrowser->setText(title);

      if ((nstceHitTest == NSTCEHT_ONITEMICON || nstceHitTest == NSTCEHT_ONITEMLABEL) 
          && nstceClickType == NSTCECT_LBUTTON) {

        NameSpaceTreeControl& control = solNameSpaceBrowser->getNameSpaceTreeControl();

        try { 
          ShellItem item(psi);
          _bstr_t name = item.getDisplayName();
          solNameSpaceBrowser->setText((const TCHAR*)name);
          hr = S_OK; //2012/11/15
        } catch (...) {
          ;
        }
      }
      return hr;
    }

  public:
    HRESULT STDMETHODCALLTYPE OnKeyboardInput(
        UINT uMsg, 
        WPARAM wParam, 
        LPARAM lParam)
    {
      HRESULT hr = S_FALSE;
      const TCHAR* title = _T("SolNameSpaceBrowser");

      solNameSpaceBrowser->setText(title);

      if (uMsg == WM_KEYDOWN && wParam == VK_RETURN) {
        NameSpaceTreeControl& control = solNameSpaceBrowser->getNameSpaceTreeControl();

        try {
          ShellItemArray array = control.getSelectedItems();

          ShellItem item = array.getItemAt(0);
          _bstr_t name = item.getDisplayName();

          solNameSpaceBrowser->setText((const TCHAR*)name);
          hr = S_OK; //2012/11/15
        } catch (...) {
          ;
        }
      }
      return hr;  
    }
  };
  //</NameSpaceTreeControlEventsListener>


private:
  NameSpaceTreeControl nameSpaceTreeControl;
  NameSpaceTreeControlEventsListener eventListener;
  
public:
  /**
   * Constructor
   */
  SolNameSpaceBrowser(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name, args)
  {
    nameSpaceTreeControl.initialize(*this, 
      NULL, NSTCS_HASEXPANDOS | NSTCS_SHOWSELECTIONALWAYS);
        
    eventListener.setApplicationView(this);

    nameSpaceTreeControl.treeAdvise(
      static_cast<INameSpaceTreeControlEvents *>(&eventListener));

    //Get a shellItem for Desktop folder.
    ShellItem shellItem(FOLDERID_Desktop);

    nameSpaceTreeControl.appendRoot(shellItem, SHCONTF_FOLDERS, 
              NSTCRS_VISIBLE|NSTCRS_EXPANDED, NULL);

    try {
      //nameSpaceTreeControl.setSelection(_T("C:\\"));
      nameSpaceTreeControl.expand(L"C:\\"); // 2012/1115
    } catch(...) {
    }

    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&ApplicationView::exit, NULL);

    addCallback(XmNmenuCallback, IDM_VERSION, this, 
      (Callback)&SolNameSpaceBrowser::version, NULL);
        
    addEventHandler(WM_SIZE, this,
        (Handler)&SolNameSpaceBrowser::size, NULL);

    addEventHandler(WM_CLOSE, this,
        (Handler)&SolNameSpaceBrowser::close, NULL);
    
    restorePlacement();
  }

  
private:
  void version(Action& action)
  {
    showMessageDialog(_T("Version"),
      _T("SolNameSpaceBrowser Version 1.0.0.0\r\nCopyright(C) 2012 Antillia.com"),
      MB_OK|MB_ICONINFORMATION);
  }

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

    nameSpaceTreeControl.treeUnadvise();
    return defaultProc(event);
  }

private:
  long size(Event& event)
  {
    int w, h;
    event.getSize(w, h);
    nameSpaceTreeControl.reshape(0, 0, w, h);
    return 0;
  }

public:
  NameSpaceTreeControl& getNameSpaceTreeControl()
  {
    return nameSpaceTreeControl;
  }
};

}

//
void  Main(int argc, TCHAR** argv)
{
  OleInitialize(NULL);

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

    Args args;
    args.set(XmNclassStyle, NO_REDRAW);

    args.set(XmNwidth,  600);
    args.set(XmNheight, 500);
    SolNameSpaceBrowser SolNameSpaceBrowser(applet, name, args);
    SolNameSpaceBrowser.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.