SOL9 2.0 Class: DrawableBinaryTree

 SOL9 C++ Class Library  SOL9 Samples  SOL9 Tutorial  SOL9 FAQ  SOL9 ClassTree 

Source code

/*
 * DrawableBinaryTree.h 
 * Copyright (c) 2011 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2009/10/10
#pragma once

#include <sol\BinaryTree.h>
#include <sol\DrawableBinaryNode.h>
#include <sol\Font.h>
#include <sol\Pen.h>
#include <sol\FileStream.h>
#include <sol\StringTokenizerT.h>
#include <sol\StringConverter.h>


namespace SOL {

class DrawableBinaryTree :public BinaryTree {
private:
    int     arranged;
    int     leftMargin;
    int     topMargin;
    int     horizSpacing;
    int        vertSpacing;
    int        arrangementPolicy;
    int        connectionPolicy;    
    int        width;
    int        height;

    DrawableBinaryNode* activeNode;    
    Font*    font;
    Pen*    pen;

public:
    static const int CONSTANT = 0;
    static const int VARIABLE = 1;

public:
    DrawableBinaryTree()
    {
        arranged    = FALSE;
        leftMargin  = 20;
        topMargin   = 20;

        horizSpacing = 20;
        vertSpacing  = 15;
        arrangementPolicy = VARIABLE;
        connectionPolicy  = DrawableBinaryNode::LINEAR;

        activeNode = NULL;
        width       = 0;
        height      = 0;
        font        = NULL;
        pen            = NULL; //new Pen(PS_SOLID, 1, RGB(128,128,128));    //gray
    }

public:
    ~DrawableBinaryTree()
    {
    }

public:
    void draw(DC* dc, int x, int y)
    {
        DrawableBinaryNode* root = (DrawableBinaryNode*) getRoot();
        if(root) {
    
            HGDIOBJ hfont = NULL;
            if(font) {
                hfont = dc-> select(font);
            }
            HPEN hpen = NULL;
            if(pen) {
                hpen =(HPEN) dc->select(pen);
            }

            if(arranged == FALSE) {
                TCHAR* name = root -> getName();
                SIZE size;

                dc -> getTextExtent(name, strlen(name), &size);
                int height  = size.cy;
                if(arrangementPolicy == VARIABLE) {
                    root -> arrange(dc, horizSpacing, 
                        height + vertSpacing,
                        leftMargin, topMargin);
                }
                else {
                    int maxLen = root -> nodeMaxLength(dc, 0);
                    root -> arrange(maxLen+ horizSpacing,
                        height + vertSpacing,
                        leftMargin, topMargin);
                }
                arranged = TRUE;
            }
            root -> draw(dc, root, connectionPolicy, x, y);

            if(pen) {
                dc->select(hpen);
            }
            if(font) {
                dc -> select(hfont);
            }
        }
    }

public:

    DrawableBinaryNode* search(DC* dc, int x, int y)
    {
        DrawableBinaryNode* root = (DrawableBinaryNode*) getRoot();
        if(root) {
            HGDIOBJ hfont = NULL;
            if(font) {
                hfont = dc-> select(font);
            }
            DrawableBinaryNode* node = root->search(dc, x, y);
            if(font) {
                dc -> select(hfont);
            }
            return node;
        }
        else {
            return NULL;
        }
    }

public:

    int load(const TCHAR* fileName)
    {
        int status = 0;
        char buffer[MAX_PATH];
        char parent[MAX_PATH];
        char word[MAX_PATH];

        FileStream stream;
        if(stream.openReadOnly(fileName) < 0) {
            return FALSE;
        }

        DrawableBinaryNode* node;
        DrawableBinaryNode* root;

        StringTokenizerT<char> analyzer;
                    
        StringConverter converter;
                    
        StringT<TCHAR> parentNode;

        while(stream.getLine(buffer, sizeof(buffer)-1) != FALSE) {
            if(strcmp(buffer, "") == 0) continue;

            analyzer.getToken(buffer, word, sizeof(word)-1);
            if(strcmp(word, "") == 0) continue;
            if(strcmp(word, "{") == 0) {
                status = 1;
                continue;
            }
            if(strcmp(word, "}") == 0) {
                status = 0;
                continue;
            }
            if(strcmp(word, "") !=0 && status == 0) {
                strcpy_s(parent, sizeof(parent), word);
                    
                converter.convert(parent, parentNode);

                if(getRoot() == NULL) {
                    setRoot(new DrawableBinaryNode(NULL, (const TCHAR*)parentNode));
                }
                continue;
            }
            if(strcmp(word, "") != 0 && status == 1) {
                root = (DrawableBinaryNode*) getRoot();
                if (root) {
                    StringT<TCHAR> leafNode;
                    converter.convert(word, leafNode);
                    node = root -> search((const TCHAR*)parentNode);
                    if (node) {
                        if (node ->getLeft() == NULL) {
                            node -> setLeft(new DrawableBinaryNode(node, (const TCHAR*)leafNode));
                            continue;
                        }
                        if (node ->getRight() == NULL) {
                            node -> setRight(new DrawableBinaryNode(node, (const TCHAR*)leafNode));
                            continue;
                        }
                    }
                }
                continue;
            }
        }
        stream.close();

        root = (DrawableBinaryNode*) getRoot();
        root -> configure(0, 0);
        root -> reconfigure();
        return TRUE;
    }

public:

    void setActiveNode(DrawableBinaryNode* node, DC* dc, int x, int y)
    {
        HGDIOBJ hfont = NULL;
        if(font) {
            hfont = dc->select(font);
        }
        SIZE size;
        if(activeNode && node) {
            activeNode->setStatus(FALSE);
            activeNode->drawSelf(dc, -x, -y, &size);
        }
        activeNode = node;
        if(activeNode) {
            activeNode->setStatus(TRUE);
            activeNode->drawSelf(dc, -x, -y, &size);
        }

        if(font) {
            dc -> select(hfont);
        }
    }

public:
    void setBackground(COLORREF normal, COLORREF active)
    {
        DrawableBinaryNode* root = (DrawableBinaryNode*)getRoot();
        if(root) {
            root -> setBackground(normal, active);
        }
    }

public:
    void setTextColor(COLORREF normal, COLORREF active)
    {
        DrawableBinaryNode* root = (DrawableBinaryNode*)getRoot();
        if(root) {
            root -> setTextColor(normal, active);
        }
    }

public:
    void computeExtent(DC* dc, int* w, int* h)
    {
        width  = 0;
        height = 0;
        DrawableBinaryNode* root = (DrawableBinaryNode*)getRoot();

        HGDIOBJ hfont = NULL;
        if(font) {
            hfont = dc->select(font);
        }

        if(root) {    
            root -> getExtent(dc, &width, &height);
            width  += leftMargin;
            height += topMargin;
        }
        *w = width;
        *h = height;

        if(font) {
            dc -> select(hfont);
        }
    }

    void    getExtent(int* w, int* h) { 
        *w = width; *h = height; 
    }

    int        getLeftMargin() { 
        return leftMargin; 
    }

    int        getHorizSpacing() { 
        return horizSpacing; 
    }

    int        getVertSpacing()  { 
        return vertSpacing; 
    }

    int        getTopMargin() { 
        return topMargin; 
    }


    void    setArrangementPolicy(int value) {
            arrangementPolicy = value;
    }

    void    setConnectionPolicy(int value) {
            connectionPolicy = value;
    }

    void    setHorizSpacing(int value) { 
        horizSpacing = value; 
    }

    void    setVertSpacing(int value) { 
        vertSpacing = value; 
    }

    void    setLeftMargin(int margin) { 
        leftMargin = margin; 
    }

    void    setTopMargin(int margin) { 
        topMargin = margin; 
    }

    void    setFont(Font* afont) { 
        font = afont; 
    }

    void    setPen(Pen* apen) { 
        pen = apen; 
    }
};

}



Last modified: 1 Feb 2012

Copyright (c) 2009-2012 Antillia.com ALL RIGHTS RESERVED.