SOL9 Sample: BinaryTreeViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL++2000
// 2000.02.16
// 2009/10/10

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

namespace SOL {

class BinaryTreeViewer :public ApplicationView {
private:
  Font           font;
  int            range;
  SmartPtr<DrawableBinaryTree> tree;
  FileDialog   fileDialog;

public:
  /**
   * Constructor
   */
  BinaryTreeViewer(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"));
    fileDialog.create(this, _T("TreeList"), ar);

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

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

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

    LOGFONT lf;
    getDisplayFont(lf);
    font.create(&lf);

    setText(_T("BinaryTreeViewer: Please load a file sample.tre."));
  }

public:
  /**
   * Destructor
   */
  ~BinaryTreeViewer()
  {
  }

private:
  void setTree(DrawableBinaryTree* tree1)
  {
    tree = NULL;

    tree  = tree1;
    range = FALSE;

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

private:
  FileDialog* getFileSelector()
  {
    return &fileDialog;
  }

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

      if(tree1 -> load(fileName) == TRUE) {
        _stprintf_s(buffer, CountOf(buffer), _T("%s - BinaryTreeViewer"), fileName);
        setText(buffer);
        setTree(tree1);
        tree1 -> setConnectionPolicy(DrawableBinaryNode::ZIGZAG);
        tree1 -> setVertSpacing(10);
        tree1 -> setFont(&font);
        ClientDC dc(this);
        setTreeScrollRange(&dc);
        update(NULL);
      }
      else {
        delete tree1;
      }
    }
  }

private:
  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;
  }

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

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

private:
  long size(Event& event)
  {
    if(tree) {
      ClientDC dc(this);
      setTreeScrollRange(&dc);
    }
    return 0;
  }

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

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

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

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

private:
  long leftButtonDoubleClick(Event& event)
  {
    if(tree) {
      int  x, y;
      event.getMousePosition(x, y);
      DrawableBinaryNode* 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));//512);
        MessageBox(getWindow(),  buffer,
          node->getName(),
          MB_OK);
      }
    }
    return 0;
  }

private:
  void buildName(DrawableBinaryNode* node, 
      TCHAR* delim, 
       TCHAR* buffer, int len)
  {
    TCHAR* temp = new TCHAR[len];
  
    strcpy_s(temp, len, _T(""));
    strcpy_s(buffer, len,  _T(""));
    DrawableBinaryNode* 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 = (DrawableBinaryNode*) 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;
    BinaryTreeViewer 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.