SOL9 Sample: TreeViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL++2000
// 1999.09.23 Modified
// 2000.02.18


#include <sol\ApplicationView.h>
#include <sol\FileDialog.h>
#include <sol\DrawableTree.h>
#include <sol\clientdc.h>
#include <sol\paintdc.h>
#include "resource.h"

namespace SOL {

class TreeViewer :public ApplicationView {
private:

  Font      font;
  int            range;
  DrawableTree* tree;
  FileDialog    fileSelector;
  

public:
  TreeViewer(Application& applet, const TCHAR* caption, Args& args)
  :ApplicationView(applet, caption,
        args.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE)
                .set(XmNstyle, (ulong)WS_HSCROLL|WS_VSCROLL))
{
  tree  = NULL;
  range = FALSE;

  Args ar;
  ar.set(XmNfilter, _T("(Tree *.tre)\0 *.tre\0"));
  fileSelector.create(this, _T("TreeList"), ar);

  addCallback(XmNmenuCallback, ID_LOAD, this, 
    (Callback)&TreeViewer::load, NULL);

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

  addEventHandler(WM_LBUTTONDOWN, this, 
    (Handler)&TreeViewer::leftButtonDown, NULL);
  addEventHandler(WM_LBUTTONDBLCLK, this, 
    (Handler)&TreeViewer::leftButtonDoubleClick, NULL);
  addEventHandler(WM_PAINT, this, 
    (Handler)&TreeViewer::paint, NULL);

  //2008/07/02
  //ClientDC dc(this);
  //UINT TCHARSet = dc.getTextCharsetInfo(NULL);
  ar.reset();
  ar.set(XmNpointSize, 9);
  //ar.set(XmNcharSet, (ulong)charSet);//SHIFTJIS_CHARSET);
  //ar.set(XmNweight,  FW_BOLD);
  ar.set(XmNfaceName,  _T("Arial"));
  // 1999.09.23
  font.create(ar);

  setText(_T("TreeView: Please load a Childv.tre file."));
}

public:
  DrawableTree* getTree() 
  { 
    return tree; 
  }

protected:
  void setTree(DrawableTree* tree1)
  {
    if(tree != NULL) {
      delete tree;
    }
    tree  = tree1;
    range = FALSE;

    setScrollPos(HORIZONTAL, 0);
    setScrollPos(VERTICAL, 0);
  }

private:

  FileDialog* getFileSelector()
  {
    return &fileSelector;
  }

public:
  ~TreeViewer()
  {
    if(tree) {
      delete tree;
    }
  }

protected:
  void load(Action& action)
  {
    if(fileSelector.open()) {
      TCHAR* fileName = fileSelector.getFileName();
      TCHAR buffer[256];
      DrawableTree* tree1 = new DrawableTree();

      if(tree1 -> load(fileName) == TRUE) {
        _stprintf_s(buffer, CountOf(buffer), _T("%s - TreeViewer"), fileName);
        setText(buffer);
        setTree(tree1);
        // 1999.09.23
        tree1 -> setConnectionPolicy(DrawableNode::ZIGZAG);
        tree1 -> setVertSpacing(10);
        tree1 -> setFont(&font);

        ClientDC dc(this);
        setTreeScrollRange(&dc);
        update(NULL);
      }
      else {
        delete tree1;
      }
    }
  }

protected:
  void setTreeScrollRange(DC* dc)
  {
    int maxx = 0;
    int maxy = 0;
    tree -> computeExtent(dc, &maxx, &maxy);
    maxx = maxx + tree->getLeftMargin();
    maxy = maxy + tree->getTopMargin() ;

    setScrollExtent(maxx, maxy);

    range = TRUE;
  }

protected:
  long paint(Event& event)
  {
    PaintDC pdc(this);
    int x = getScrollPos(HORIZONTAL);
    int y = getScrollPos(VERTICAL);

    if(tree) {
      tree -> draw(&pdc, -x, -y);
    }
    return 0L;
  }

protected:
  long size(Event& event)
  {

    if(tree) {
      ClientDC dc(this);
      setTreeScrollRange(&dc);
    }
    return 0L;
  }

protected:
  DrawableNode* search(int x, int y)
  {
    int x1 = getScrollPos(HORIZONTAL);
    int y1 = getScrollPos(VERTICAL);

    ClientDC cdc(this);
    return tree->search(&cdc, x+x1, y+y1);
  }

protected:
  long leftButtonDown(Event& event)
  {
    if(tree) {
      int  x, y;
      event.getMousePosition(x, y);
      DrawableNode* node = search(x, y);

      if(node) {
        int mx = getScrollPos(HORIZONTAL);
        int my = getScrollPos(VERTICAL);
        ClientDC cdc(this);
        tree -> setActiveNode(node, &cdc, mx, my);
      }
    }
    return 0L;
  }

protected:
  long leftButtonDoubleClick(Event& event)
  {
    if(tree) {
      int  x, y;
      event.getMousePosition(x, y);
      DrawableNode* node = search(x, y);

      if(node) {
        int mx = getScrollPos(HORIZONTAL);
        int my = getScrollPos(VERTICAL);
        ClientDC cdc(this);
        tree -> setActiveNode(node, &cdc, mx, my);
  
        TCHAR buffer[1024];
        buffer[0] = NULL;

        buildName(node, _T("-"), buffer, CountOf(buffer));
        MessageBox(getWindow(),  buffer,
          node->getName(),
          MB_OK);
      }
    }
    return 0L;
  }

protected:
  void buildName(DrawableNode* node, const TCHAR* delim, 
       TCHAR* buffer, int len)
  {
    TCHAR* temp = new TCHAR[len];

    strcpy_s(temp, len, _T(""));
    strcpy_s(buffer, len, _T(""));
    DrawableNode* parent;
    parent = node;

    while(parent) {
      if(strcmp(temp, _T("")) == 0) {
        _stprintf_s(buffer, len, _T("%s"), parent->getName());
      }
      else {
        _stprintf_s(buffer, len, _T("%s%s%s"), parent->getName(), 
            delim,  temp);
      }
      strcpy_s(temp, len, buffer);
      parent = (DrawableNode*) parent->getParent();
    }
    delete [] temp;
  }
};

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

    Args args;
    TreeViewer treev(applet, name, args);
    treev.realize();

    applet.run();

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


Last modified: 1 Feb 2017

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