2009-12-25 18:56:25 -05:00
unit framefunctionlist;
{$mode objfpc} {$H+}
interface
uses
2010-01-03 14:27:09 -05:00
Classes, SysUtils, FileUtil, LResources, Forms, ComCtrls, StdCtrls, Controls,
ExtCtrls, Buttons;
2009-12-25 18:56:25 -05:00
type
{ TFunctionListFrame }
TFunctionListFrame = class( TFrame)
editSearchList: TEdit;
FunctionList: TTreeView;
2010-01-03 14:27:09 -05:00
FunctionListLabel: TLabel;
CloseButton: TSpeedButton;
2009-12-25 18:56:25 -05:00
procedure editSearchListChange( Sender: TObject) ;
2010-01-03 14:27:09 -05:00
procedure FrameEndDock( Sender, Target: TObject; X, Y: Integer ) ;
procedure FunctionListDblClick( Sender: TObject) ;
2009-12-26 10:45:05 -05:00
procedure FunctionListDeletion( Sender: TObject; Node: TTreeNode) ;
2010-01-03 14:27:09 -05:00
procedure FunctionListLabelMouseDown( Sender: TObject; Button: TMouseButton;
2009-12-25 18:56:25 -05:00
Shift: TShiftState; X, Y: Integer ) ;
2010-01-03 14:27:09 -05:00
procedure FunctionListMouseDown( Sender: TObject; Button: TMouseButton;
2009-12-25 18:56:25 -05:00
Shift: TShiftState; X, Y: Integer ) ;
procedure DockFormOnClose( Sender: TObject; var CloseAction: TCloseAction) ;
2010-01-03 14:27:09 -05:00
procedure CloseButtonClick( Sender: TObject) ;
2009-12-25 18:56:25 -05:00
private
2009-12-26 10:45:05 -05:00
FFilterTree : TTreeView;
2009-12-26 07:44:40 -05:00
procedure FilterTreeVis( Vis : boolean ) ;
2009-12-26 10:45:05 -05:00
function GetFilterTree: TTreeView;
2009-12-25 18:56:25 -05:00
{ private declarations }
public
DraggingNode : TTreeNode;
ScriptNode : TTreeNode;
InCodeCompletion : boolean ;
CompletionCaret : TPoint;
StartWordCompletion : TPoint;
CompletionLine : string ;
CompletionStart : string ;
2009-12-26 10:45:05 -05:00
property FilterTree : TTreeView read GetFilterTree;
procedure LoadScriptTree( Script : String ) ;
2010-01-03 14:27:09 -05:00
function Find( Next : boolean ; backwards : boolean = false ) : boolean ;
2009-12-25 18:56:25 -05:00
{ public declarations }
end ;
implementation
uses
TestUnit, Graphics, simpleanalyzer;
{ TFunctionListFrame }
procedure TFunctionListFrame. editSearchListChange( Sender: TObject) ;
begin
Find( false ) ;
end ;
2010-01-03 14:27:09 -05:00
procedure TFunctionListFrame. FrameEndDock( Sender, Target: TObject; X, Y: Integer
) ;
begin
if Target is TPanel then
2010-01-12 13:07:47 -05:00
begin
Form1. SplitterFunctionList. Visible : = true ;
CloseButton. Visible: = true ;
end
2010-01-03 14:27:09 -05:00
else if Target is TCustomDockForm then
begin
2010-01-12 13:07:47 -05:00
TCustomDockForm( Target) . Caption : = 'Functionlist' ;
2010-01-03 14:27:09 -05:00
TCustomDockForm( Target) . OnClose : = @ DockFormOnClose;
Form1. SplitterFunctionList. Visible: = false ;
2010-01-12 13:07:47 -05:00
CloseButton. Visible: = false ;
2010-01-03 14:27:09 -05:00
end ;
end ;
procedure TFunctionListFrame. FunctionListDblClick( Sender: TObject) ;
var
Node : TTreeNode;
begin
if FilterTree. Visible then
Node : = FilterTree. Selected
else
node : = FunctionList. Selected;
if node< > nil then
if node. Level > 0 then
if node. Data < > nil then
begin ;
Form1. CurrScript. SynEdit. InsertTextAtCaret( GetMethodName( PChar( node. Data) , true ) ) ;
Form1. RefreshTab;
end ;
end ;
2009-12-26 10:45:05 -05:00
procedure TFunctionListFrame. FunctionListDeletion( Sender: TObject;
Node: TTreeNode) ;
begin
if node. data < > nil then
StrDispose( PChar( Node. Data) ) ;
end ;
2010-01-03 14:27:09 -05:00
procedure TFunctionListFrame. FunctionListLabelMouseDown( Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer ) ;
begin
Self. DragKind : = dkDock;
Self. BeginDrag( false , 4 0 ) ;
end ;
2009-12-25 18:56:25 -05:00
procedure TFunctionListFrame. DockFormOnClose( Sender: TObject; var CloseAction: TCloseAction) ;
begin
CloseAction : = caHide;
Form1. MenuItemFunctionList. Checked : = False ;
end ;
2010-01-03 14:27:09 -05:00
procedure TFunctionListFrame. CloseButtonClick( Sender: TObject) ;
begin
self. Hide;
Form1. MenuItemFunctionList. Checked : = False ;
end ;
2009-12-26 07:44:40 -05:00
procedure TFunctionListFrame. FilterTreeVis( Vis: boolean ) ;
begin
FunctionList. Visible: = not Vis;
FilterTree. Visible : = Vis;
end ;
2009-12-26 10:45:05 -05:00
function TFunctionListFrame. GetFilterTree: TTreeView;
begin
Result : = FFilterTree;
if Assigned( Result ) then
exit;
FFilterTree : = TTreeView. Create( Self) ;
FFilterTree. Parent : = Self;
FFilterTree. Visible : = false ;
FFilterTree. SetBounds( FunctionList. Left, FunctionList. Top, FunctionList. Width, FunctionList. Height) ;
FFilterTree. Align : = alClient;
FFilterTree. ReadOnly : = True ;
FFilterTree. ScrollBars: = ssAutoBoth;
FFilterTree. OnMouseDown: = FunctionList. OnMouseDown;
FFilterTree. OnMouseUp: = FunctionList. OnMouseUp;
FFilterTree. OnChange: = FunctionList. OnChange;
FFilterTree. OnExit : = FunctionList. OnExit;
2010-01-03 14:27:09 -05:00
FFilterTree. OnDblClick: = FunctionList. OnDblClick;
2009-12-26 10:45:05 -05:00
Result : = FFilterTree;
//We do not want to delete the data from the FilterTree
// FilterTree.OnDeletion:= FunctionList.OnDeletion;
end ;
procedure TFunctionListFrame. LoadScriptTree( Script: String ) ;
var
I : integer ;
Analyzer : TScriptAnalyzer;
tmpNode : TTreeNode;
begin
2009-12-26 11:43:06 -05:00
if ScriptNode = nil then
exit;
2009-12-26 10:45:05 -05:00
if FilterTree. Visible then
Writeln( 'Might get some acces violations now..' ) ;
ScriptNode. DeleteChildren;
Analyzer : = TScriptAnalyzer. create;
Analyzer. ScriptToAnalyze: = Script;
Analyzer. analyze;
for i : = 0 to Analyzer. MethodLen - 1 do
begin
tmpNode : = FunctionList. Items. AddChild( ScriptNode, Analyzer. Methods[ i] . Name ) ;
tmpNode. Data: = strnew( PChar( Analyzer. Methods[ i] . CreateMethodStr) ) ;
end ;
ScriptNode. Expand( true ) ;
2010-01-12 13:07:47 -05:00
Analyzer. free;
2009-12-26 10:45:05 -05:00
end ;
2010-01-03 14:27:09 -05:00
function TFunctionListFrame. Find( Next : boolean ; backwards : boolean = false ) : boolean ;
2009-12-25 18:56:25 -05:00
var
2010-01-03 14:27:09 -05:00
Start, Len, i, index , posi, c: Integer ;
2009-12-25 18:56:25 -05:00
FoundFunction : boolean ;
2009-12-26 07:44:40 -05:00
LastSection : string ;
2009-12-25 18:56:25 -05:00
str : string ;
2009-12-26 07:44:40 -05:00
RootNode : TTreeNode;
NormalNode : TTreeNode;
2009-12-25 18:56:25 -05:00
Node : TTreeNode;
2009-12-26 07:44:40 -05:00
InsertStr : string ;
2009-12-25 18:56:25 -05:00
begin
if( editSearchList. Text = '' ) then
begin
editSearchList. Color : = clWhite;
FunctionList. FullCollapse;
if InCodeCompletion then
begin ;
Form1. CurrScript. SynEdit. Lines[ CompletionCaret. y - 1 ] : = CompletionStart;
Form1. CurrScript. SynEdit. LogicalCaretXY: = point( CompletionCaret. x, CompletionCaret. y) ;
Form1. CurrScript. SynEdit. SelEnd: = Form1. CurrScript. SynEdit. SelStart;
end ;
2009-12-26 07:44:40 -05:00
FilterTreeVis( False ) ;
2009-12-26 10:45:05 -05:00
ScriptNode. Expand( true ) ;
2009-12-25 18:56:25 -05:00
exit;
end ;
2009-12-26 07:44:40 -05:00
//We only have to search the next item in our filter tree.. Fu-king easy!
if next then
begin ;
if FilterTree. Visible = false then
begin ;
Writeln( 'ERROR: You cannot search next, since the Tree isnt generated yet' ) ;
Find( false ) ;
exit;
end ;
if FilterTree. Selected < > nil then
2010-01-03 14:27:09 -05:00
begin ;
if backwards then
start : = FilterTree. Selected. AbsoluteIndex - 1
else
Start : = FilterTree. Selected. AbsoluteIndex + 1 ;
end
2009-12-26 07:44:40 -05:00
else
2010-01-03 14:27:09 -05:00
begin
if backwards then
Start : = FilterTree. Items. Count - 1
else
Start : = 0 ;
end ;
2009-12-26 07:44:40 -05:00
Len : = FilterTree. Items. Count;
2010-01-03 14:27:09 -05:00
i : = start + len; //This is for the backwards compatibily, we do mod anways.. it just makes sure -1 isn't negative.
c : = 0 ;
2010-01-04 16:55:34 -05:00
while c < ( len ) do
2010-01-03 14:27:09 -05:00
begin ;
2009-12-26 07:44:40 -05:00
if FilterTree. Items[ i mod len] . Level = 1 then
2009-12-25 18:56:25 -05:00
begin
2009-12-26 07:44:40 -05:00
FilterTree. Items[ i mod len] . Selected: = true ;
InsertStr : = FilterTree. Items[ i mod len] . Text ;
Result : = true ;
2009-12-25 18:56:25 -05:00
break;
end ;
2010-01-03 14:27:09 -05:00
if backwards then
dec( i)
else
inc( i) ;
inc( c) ;
end ;
2009-12-26 07:44:40 -05:00
end else
begin
FilterTree. Items. Clear;
FoundFunction : = False ;
if FunctionList. Selected < > nil then
Start : = FunctionList. Selected. AbsoluteIndex
else
Start : = 0 ;
Len : = FunctionList. Items. Count;
LastSection : = '' ;
for i : = start to start + FunctionList. Items. Count - 1 do
begin ;
Node : = FunctionList. Items[ i mod FunctionList. Items. Count] ;
if( Node. Level = 1 ) then
if( pos( lowercase( editSearchList. Text ) , lowercase( Node. Text ) ) > 0 ) then
begin
if not FoundFunction then
begin
FoundFunction : = True ;
index : = i mod FunctionList. Items. Count;
InsertStr: = node. Text ;
end ;
if LastSection < > Node. Parent. Text then //We enter a new section, add it to the filter tree!
RootNode : = FilterTree. Items. AddChild( nil , Node. Parent. Text ) ;
FilterTree. Items. AddChild( RootNode, Node. Text ) . Data : = Node. Data;
LastSection: = RootNode. Text ;
// break;
end ;
end ;
Result : = FoundFunction;
2009-12-25 18:56:25 -05:00
2009-12-26 07:44:40 -05:00
if Result then
begin ;
FilterTreeVis( True ) ;
FilterTree. FullExpand;
FilterTree. Items[ 1 ] . Selected: = True ;
Writeln( FunctionList. Items[ Index ] . Text ) ;
FunctionList. FullCollapse;
FunctionList. Items[ Index ] . Selected : = true ;
FunctionList. Items[ index ] . ExpandParents;
editSearchList. Color : = clWhite;
2009-12-25 18:56:25 -05:00
2009-12-26 07:44:40 -05:00
end else
begin
FilterTreeVis( false ) ;
editSearchList. Color : = 6 7 1 1 0 3 9 ;
if InCodeCompletion then
Form1. CurrScript. SynEdit. Lines[ CompletionCaret. y - 1 ] : = CompletionStart;
end ;
end ;
if result and InCodeCompletion then
2009-12-25 18:56:25 -05:00
begin ;
2009-12-26 07:44:40 -05:00
str : = format( CompletionLine, [ InsertStr] ) ;
2009-12-25 18:56:25 -05:00
with Form1. CurrScript. SynEdit do
begin ;
Lines[ CompletionCaret. y - 1 ] : = str;
LogicalCaretXY: = StartWordCompletion;
i : = SelStart;
2009-12-26 07:44:40 -05:00
posi : = pos( lowercase( editSearchList. text ) , lowercase( InsertStr) ) + length( editSearchList. text ) - 1 ; //underline the rest of the word
if Posi = Length( InsertStr) then //Special occasions
begin ;
if Length( editSearchList. Text ) < > Posi then //We found the last part of the text -> for exmaple when you Search for bitmap, you can find LoadBitmap -> We underline 'Load'
begin ;
SelStart : = i;
SelEnd : = i + pos( lowercase( editSearchList. text ) , lowercase( InsertStr) ) - 1 ;
Exit;
end ;
//We searched for the whole text -> for example LoadBitmap, and we found LoadBitmap -> Underline the whole text
Posi : = 0 ;
end ;
//Underline the rest of the word
SelStart : = i + posi;
SelEnd : = SelStart + Length( InsertStr) - posi;
2009-12-25 18:56:25 -05:00
end ;
end ;
end ;
procedure TFunctionListFrame. FunctionListMouseDown( Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer ) ;
var
N: TTreeNode;
begin
if InCodeCompletion then
begin ;
Writeln( 'Not yet implemented' ) ;
exit;
end ;
2009-12-26 07:44:40 -05:00
if not ( Sender is TTreeView) then
exit;
N : = TTreeView( Sender) . GetNodeAt( x, y) ;
2009-12-25 18:56:25 -05:00
if( N = nil ) then
begin
Self. DragKind : = dkDock;
Self. BeginDrag( false , 4 0 ) ;
exit;
end ;
Self. DragKind : = dkDrag;
if( Button = mbLeft) and ( N. Level > 0 ) then
Self. BeginDrag( False , 1 0 ) ;
DraggingNode : = N;
end ;
initialization
{$I framefunctionlist.lrs}
end .