1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
__fastcall jTree::jTree(TComponent *AOwner)
: TGraphicControl(AOwner)
{
Flag = false;
if(AOwner->InheritsFrom(__classid(TWinControl)))
{
Parent = (TWinControl*)AOwner;
}
Bitmap = new Graphics::TBitmap;
FCaption = "a&b";
Tree = FCaption;
FCellDim = 40;
FRadius = 10;
}
//------------------------------------
__fastcall jTree::~jTree()
{
delete Bitmap;
Bitmap = NULL;
}
//------------------------------------
void __fastcall jTree::SetCaption(AnsiString Value)
{
if(Value != "")
{
FCaption = Value;
AnsiString A = "";
TReplaceFlags Flags = Flags << rfReplaceAll;
Tree = StringReplace(Value, "(", A, Flags);
Tree = StringReplace(Tree, ")", A, Flags);
Tree = StringReplace(Tree, " ", A, Flags);
Repaint();
}
}
//------------------------------------
void __fastcall jTree::Leaf(TPoint P1, int I)
{
LeafPos.x = LeafPos.x + CellDim;
TPoint P = LeafPos;
if(I <= Tree.Length())
{
TCanvas *C = Bitmap->Canvas;
C->MoveTo(P1.x, P1.y);
C->LineTo(P.x, P.y);
Node(NodePos, I+1); // <<
B = Tree.SubString(I, 1);
C->Ellipse(P.x - Radius, P.y - Radius, P.x + Radius, P.y + Radius);
C->TextOut(P.x - (C->TextWidth(B) / 2),
P.y - (C->TextHeight(B) / 2),
B);
}
}
//------------------------------------
void __fastcall jTree::Node(TPoint P1, int I)
{
NodePos.x = NodePos.x + CellDim;
NodePos.y = NodePos.y - CellDim;
TPoint P = NodePos;
if(I <= Tree.Length())
{
TCanvas *C = Bitmap->Canvas;
C->MoveTo(P1.x, P1.y);
C->LineTo(P.x, P.y);
Leaf(P, I+1); // <<
C->Ellipse(P.x - Radius, P.y - Radius, P.x + Radius, P.y + Radius);
B = Tree.SubString(I, 1);
C->TextOut(P.x - (C->TextWidth(B) / 2),
P.y - (C->TextHeight(B) / 2),
B);
}
}
//------------------------------------
void __fastcall jTree::Paint()
{
if(Flag) return;
int n = Tree.Length();
int we = ((n / 2) + 2) * CellDim;
int he = ((n / 2) + 2) * CellDim;
if((we != Width) || (he != Height))
{
Flag = true;
SetBounds(Left,Top, we, he);
Flag = false;
}
if(Bitmap != NULL)
{
if(Bitmap->Width != we) Bitmap->Width = we;
if(Bitmap->Height != he) Bitmap->Height = he;
TCanvas *C = Bitmap->Canvas;
C->Brush->Color = clWhite;
C->Brush->Style = bsSolid;
C->FillRect(Rect(0, 0, we, he));
//REM : a&b&c&d...
LeafPos = Point(CellDim, he - CellDim);
NodePos = Point((CellDim / 2), he - CellDim);
int i = 1;
TPoint P1 = LeafPos;
Node(P1, i+1); // <<
C->Ellipse(P1.x - Radius,
P1.y - Radius,
P1.x + Radius,
P1.y + Radius);
B = Tree.SubString(i, 1);
C->TextOut(P1.x - (C->TextWidth(B) / 2),
P1.y - (C->TextHeight(B) / 2),
B);
C->TextOut((Width - C->TextWidth(Caption)) / 2,
Height - 2 - C->TextHeight(Caption),
Caption);
Canvas->Draw(0, 0, Bitmap);
}
} |
Partager