(*
/*

// KOL version

CLASS {Delphi_to_al4|_convert_from_KOL},
UNTESTED, STOPPING, BITWISE, BYTES:

IMPORT: {Bitmap}, {Bitmap_direct}, {Canvas}, {Timer}, {File_path}, {Geometry},
		{String_functions}, {Stream}, {AL4_form}, {File_stream},
		{System_functions}, {Hardware} .
		
IMPORT: {Form}, {Dialog}, {Control},
		{Paintbox}, {Paint_table}, {Paint_tree}, {Progress} .
		
IMPORT: {Bmp} .
		
FUN AsInt|_from_both_real_and_int(
REAL V|alue_from) ==> INT
	:= V.Int .
	
FUN Str_as_is(STR S|tring_to_return) ==> STR := S .
	
FUN LongBool(INT Value|_just_pass_through) ==> BOOL
	:= Value != 0 .
	
FUN PChar|_dummy(STR S|tring_to_pchar) ==> STR := S .

FUN Pos|ition_of_substring(
STR Substr|ing, STR Source|_string) ==> INT
	:= Source.Find(Substr) + 1 .
	
FUN Copy|_part_of_string(
STR S|tring_copy_from, INT Pos|ition_from_1, INT Count|_to_copy) ==> STR
	:= S[Pos-1 TO Pos+Count-2] .

FUN AnsiCompareStrNoCase(
STR S1|string, STR S2|string) ==> INT
	:
	STR us1|_uppered = S1.Upper
	STR us2|_uppered = S2.Upper
	CASE us1 < us2 ? RESULT = -1 ==> ;
	CASE us1 > us2 ? RESULT = +1 ; .

FUN Integer|_from_any(INT X|_parameter) ==> INT := X .

FUN Length|_of_string(
STR S|tring_to_measure) ==> INT := S.Len .

FUN High|_index_in_array(
{BYTE,INT,REAL,STR,{Object}} A|ny_type_array[]) ==> INT
	:
	RESULT = A[].Count-1 .
	
FUN Int64_2Double(
REAL Value|_passed_through) ==> REAL := Value .
                                             
CONST INT:
	SSSHIFT	||	ssShift|_shift_key_pressed = 1
	SSCTRL	||	ssCtrl|_control_key_pressed = 2
	SSALT 	||	ssAlt|_alt_key_pressed = 4 .
	
FUN Rect|angle(REAL X|_coordinate, REAL Y|_coordinate,
	REAL Right|_side, REAL Bottom|_side) ==> {rect}
	:
	RESULT = {rect}(Loc = Pt(X, Y), Width= Right - X, Height= Bottom - Y) .
	
FUN ClientBounds|_of_control_in_pixels(
{Control} C|ontrol_passed) ==> {rect}
	:= {rect}(Width = C.Width.Points_to_pixels,
			  Height = C.Height.Points_to_pixels) .
	
FUN PtInRect||PointInRect(
{rect} R|ectangle, {point} P|oint_checking) ==> BOOL
	:= R.Contains_pt(P) .

FUN Point|_of_2_coords(
REAL X|_coordinate, REAL Y|_coordinate) ==> {Geometry}.{point}
	:= Pt(X, Y) .
	
FUN Release|_objects({Object} A|rray_to_clear[]) :
	FOR a IN A[* DOWNTO 0] : A[a.Index] = NONE ;
	A[].Clear .
	
FUN Form_X_Points_to_Control_Pixels(
{Control} C|ontrol_where_mouse_event_occur, REAL X|_mouse_coordinate) ==> INT
	:= (X - C.Bounds.Loc.X).Points_to_pixels . 

FUN Form_Y_Points_to_Control_Pixels(
{Control} C|ontrol_where_mouse_event_occur, REAL Y|_mouse_coordinate) ==> INT
	:= (Y - C.Bounds.Loc.Y).Points_to_pixels .
	
FUN AppletTerminated ==> BOOL
	:= Application_terminated .

FUN NewBitmap(
INT Width|_bmp, INT Height|_bmp, INT Bits|_per_pixel) ==> {Bitmap}
	:= New_bitmap(Width, Height, Bits) .
	
FUN NewBitmapDirect(
INT Width|_bmp, INT Height|_bmp, INT Bits|_per_pixel) ==> {Bitmap_direct}
	:= New_bitmap_direct(Width, Height, Bits) .
	
FUN Get_bitmap_pixel(
{Bitmap_direct} B|itmap_source, INT X|_coordinate, INT Y|_coordinate) ==> INT
	:
	--------------------- 'locking Y row', REUSED
	CASE B.Locked && B.Locked_height > 0 
		 && (B.Locked_h != 1 || B.Locked_Y != Y)
		 ?
	     B.Unlock(TRUE) ;
	CASE !B.Locked ?
		 B.Lock_rows(Y, 1, TRUE)
	; 
	INT bpp||bytes_per_pixel = B.Bpp / 8
	CASE bpp < 3 ? ==> ;
	-------------------- 'locked'
	RESULT = B.Pixels[X * bpp] | B.Pixels[X * bpp + 1].ShiftL(8)
		| B.Pixels[X * bpp + 2].ShiftL(16) .
	
FUN Set_bitmap_pixel(
{Bitmap_direct} B|itmap_source, INT X|_coordinate, INT Y|_coordinate,
INT Color|_value_to_set_R_G_B)
	:
	LIKE Get_bitmap_pixel .......... 'locking Y row'
	B.Pixels[X * bpp] = Color
	B.Pixels[X * bpp + 1] = Color.ShiftR(8)
	B.Pixels[X * bpp + 2] = Color.ShiftR(16) .
	
FUN LoadBitmap(STR Path|_file) ==> {Bitmap}, NEW :
	//RESULT = {Bitmap}.Load(Path) .
	RESULT = {Bmp}.From_file(Path) .
	
FUN SaveBitmap(
{Bitmap} B|itmap_source, STR Path|_destination)
	:
	NONE = B.Save(Path) .
	
FUN DrawBitmap(
{Canvas} Dest|ination, REAL X|_coordinate, REAL Y|_coordinate,
{Bitmap} Bitmap|_object)
	:
	Bitmap.Draw(Dest, Pt(X, Y)) .
	
FUN CopyRect(
{Canvas} Dest|ination, {rect} R_dest|ination,
{Canvas} Source|_canvas, {rect} R_source)
	:
	Dest.Copy_rect(R_dest, Source, R_source) .
	
FUN FillRect(
{Canvas} Dest|ination, {Geometry}.{rect} Filling_r|ectangle)
	:
	PUSH Dest.Pen_style = 'CLEAR_PEN' :
		 Dest.Rect(Filling_r) ; .
	
FUN SetBrush(
{Canvas} Canv|as_destination, BOOL Solid|_ifTrue_or_clear_ifFalse,
INT Color|_for_solid_color)
	:
	CASE Solid ? Canv.Brush = 'SOLID_BRUSH'
	ELSE Canv.Brush = 'CLEAR_BRUSH' ;
	Canv.Background = Color .
	
FUN SetPen|_width_and_color(
{Canvas} Canv|as_destination, INT Pen_w|idth, INT Color|_for_pen)
	:
	Canv.Pen_width = Pen_w
	Canv.Foreground = Color .
	
FUN SetUnitsPixels(
{Canvas} C|anvas_of_bitmap)
	: C.Units = 'PIXELS' .
	
FUN DrawRect({Canvas} Dest|ination, {rect} R|ectangle) :
	Dest.Rect(R) .
	
FUN Invalidate({Control} PBox|_control) :
	PBox.{Paintbox}.Invalidate_all .
	
CONST INT:
	CLBLACK|_0 = 0xFF000000
	CLWHITE|_FFFFFF = 0xFFFFFFFF
	CLRED|_red = 0xFF0000FF
	CLGREEN|_green = 0xFF00FF00
	CLYELLOW = 0xFF00FFFF .
	
CONST INT {system_colors} :
	CLACTIVECAPTION = 0xFF0000_02
	CLGRADIENTACTIVECAPTION = 0xFF0000_1B
	CLACTIVEBORDER = 0xFF000000_0A
	CLCAPTIONTEXT = 0xFF000000_09
	CLBTNFACE = 0xFF000000_0F
	CLBTNTEXT = 0xFF000000_12
	CLBTNHIGHLIGHT = 0xFF000000_14
	CLBTNSHADOW = 0xFF0000_0A
	CLAPPWORKSPACE = 0xFF0000_0C
	CLGRAYTEXT = 0xFF0000_11
	CLINFOBK = 0xFF0000_18
	CLINFOTEXT = 0xFF0000_17
	CLINACTIVECAPTION = 0xFF0000_03
	CLGRADIENTINACTIVECAPTION = 0xFF0000_1C
	CLINACTIVEBORDER = 0xFF0000_0B
	CLINACTIVECAPTIONTEXT = 0xFF0000_13
	CLMENU|_BACKGROUND = 0xFF0000_04
	CLMENUTEXT = 0xFF0000_07
	CLMENUHIGHLIGHT = 0xFF0000_1D
	CLSCROLLBAR = 0xFF0000_05
	CLWINDOW = 0xFF0000_05
	CLWINDOWTEXT = 0xFF0000_08
	CLWINDOWFRAME = 0xFF0000_06 .
	
FUN SystemColor(INT Color_index) ==> INT,
IF Color_index IS CONST, THEN Color_index IN {system_colors}
	:
	CASE Color_index ?
	[CLACTIVECAPTION]: RESULT = System_color('ACTIVE_CAPTION')
	[CLGRADIENTACTIVECAPTION]: RESULT = System_color('ACTIVE_GRADIENT')
	[CLACTIVEBORDER]: RESULT = System_color('ACTIVE_BORDER')
	[CLCAPTIONTEXT]: RESULT = System_color('ACTIVE_TEXT')
	[CLBTNFACE]: RESULT = System_color('BUTTON')
	[CLBTNTEXT]: RESULT = System_color('BUTTON_TEXT')
	[CLBTNHIGHLIGHT]: RESULT = System_color('BUTTON_HIGHLIGHT')
	[CLBTNSHADOW]: RESULT = System_color('BUTTON_SHADOW')
	[CLAPPWORKSPACE]: RESULT = System_color('DESKTOP')
	[CLGRAYTEXT]: RESULT = System_color('GRAY_TEXT')
	[CLINFOBK]: RESULT = System_color('HELP')
	[CLINFOTEXT]: RESULT = System_color('HELP_TEXT')
	[CLINACTIVECAPTION]: RESULT = System_color('INACTIVE_CAPTION')
	[CLGRADIENTINACTIVECAPTION]: RESULT = System_color('INACTIVE_GRADIENT')
	[CLINACTIVEBORDER]: RESULT = System_color('INACTIVE_BORDER')
	[CLINACTIVECAPTIONTEXT]: RESULT = System_color('INACTIVE_TEXT')
	[CLMENU]: RESULT = System_color('MENU')
	[CLMENUTEXT]: RESULT = System_color('MENU_TEXT')
	[CLMENUHIGHLIGHT]: RESULT = System_color('MENU_HIGHLIGHT')
	//[CLSCROLLBAR]: RESULT = System_color('SCROLLBAR')
	[CLWINDOW]: RESULT = System_color('WINDOW')
	[CLWINDOWTEXT]: RESULT = System_color('WINDOW_TEXT')
	[CLWINDOWFRAME]: RESULT = System_color('WINDOW_FRAME') ; .
	 
FUN BoundsRect({Control} C|ontrol_delphi) ==> {rect} :
	RESULT = C.Bounds .
	
FUN GetKeyState(INT VK|ey_virtual) ==> INT :
	CASE Global_key(VK) ? RESULT = -1
	ELSE RESULT = 0 ; .
	
FUN ActivateTimer(
{Timer} T|imer_to_activate_or_deactrivate, BOOL A|ctivate_if_true)
	:
	T.Set_active(A) .
	
FUN TimerIsActive({Timer} T|imer_to_inspect) ==> BOOL
	:= T.Active .
	
FUN ExtractFilePath(STR Path|_full) ==> STR
	:= Extract_path(Path) .
	
FUN ExtractFileExt(STR Path|_full) ==> STR
	:= Path.Ext.Lower .
	
FUN DirectoryEmpty(STR Path|_of_directory) ==> BOOL :
	STR cat|alog_files[]
	Catalog(Path, cat[])
	RESULT = cat[].Count == 0 .
	
FUN IntToStr|ing(REAL V|alue_to_convert) ==> STR 
	:= V.Int.S .

FUN Int2Str|ing(REAL V|alue_to_convert) ==> STR := V.Int.S .

FUN RemoveTrailingSlash(
STR Path|_to_remove_ending_slash) ==> STR
	:= Path.Remove_ending(Path_sep) .
	
FUN DirectoryExists(STR Path|_directory) ==> BOOL := Path.Dir_exists .
	
FUN FileExists(STR Path|_file) ==> BOOL := Path.Exists .

FUN DeleteFile(STR Path|_file) : Path.Delete_file .

FUN DeleteFile2Recycle(
STR Paths|_semicolon_separated_list) ==> BOOL
	:
	Recycle_file(Paths)
	STR spl|itted[]
	Split(spl[], Paths, ";")
	FOR s IN spl[] :
	    CASE s.Exists ? ==> ;
	;
	RESULT = TRUE .

FUN CopyMoveFiles(
STR Source_paths|_list_semicolob_separated,
STR Dest|ination_path,
BOOL Move|_false_to_copy) ==> BOOL
	:
	STR source|_list_splitted[]
	Split(source[], Source_paths, ";")
	RESULT = TRUE
	FOR s IN source[] :
		CASE Move ?
		     Move_file(s, Dest.Trailing_slash s.Extract_file_name)
		     RESULT &&= !s.Exists 
		     RESULT &&= (Dest.Trailing_slash s.Extract_file_name).Exists
		ELSE Copy_file(s, Dest.Trailing_slash s.Extract_file_name)
			 RESULT &&= (Dest.Trailing_slash s.Extract_file_name).Exists 
		;
	; .

FUN NewReadFileStream(STR Path|_of_file) ==> {Stream}
	:= Read_file_stream(Path) .
	
FUN NewWriteFileStream(STR Path|_of_file) ==> {Stream}
	:= Write_file_stream(Path, FALSE) .

FUN NewReadWriteFileStream(STR Path|_of_file) ==> {Stream}
	:= Write_file_stream(Path, TRUE) .
	
CONST INT {disposition} :
	spBegin|ning = 0
	spCurrent = 1
	cpEnding = 2 .
	
FUN Seek|_stream_position(
{Stream} S|tream_obj, INT Pos|ition, INT Disp|osition),
IF Disp IS CONST, THEN Disp IN {disposition}
	:
	CASE Disp ?
	[1]:	S.Set_position(Pos, 'CURRENT')
	[2]:	S.Set_position(Pos, 'ENDING')
	ELSE	S.Set_position(Pos, 'BEGINNING') ; .
	
FUN ReadBytes(
{Stream} S|ource_stream, BYTE Buf|er_bytes[], INT Size|_to_read) ==> INT
	:
	CASE S.Load_bytes(Buf[], Size) ? RESULT = Buf[].Count ; .
	
FUN Halt|_program :
	STOP "Program halted" .
	
CONST INT: SW_SHOW|_COMMAND = 5 .
	
FUN ShellExecute(
INT Parent_HWND|dummy, STR Op|eration_DUMMY, STR File|_to_run,
STR Param|eters_to_pass, STR Dir|ectory_working_DUMMY,
INT Show|_command_DUMMY) ==> INT,
RESTRICT Parent_HWND IN [0],
IF Op IS CONST, THEN Op IN ["", "open"],
RESTRICT Show IN [5]
	:
	BOOL ok_started = Start_program_ok(File, Param) 
	CASE ok_started ? RESULT = 32 ; .
	
FUN CompareBytes(
BYTE A|_parameter[], BYTE B|_parameter[], INT C|ount_max) ==> BOOL
	:
	FOR i IN [0 TO C-1] :
	    CASE A[i] != B[i] ? ==> ;
	;
	RESULT = TRUE .
	
CONST INT: FILE_ATTRIBUTE_NORMAL = 0x80 .

FUN DiskFreeSpace(STR Letter|_disk) ==> REAL
	:= Get_drive_free_space(Letter) .
	
---------- 'form'

FUN SetCaption({Form} F|orm_ref, STR Value|_new_caption) :
	F.Set_caption(Value) .
	
FUN ShowModal({Form} Form|_dialog) :
	NONE = Form.{Dialog}.Show_modal(Get_screen.Main_form_) .
		
---------------------- 'listview'

FUN LVCount|_items({Paint_table} ListView) ==> INT
	:= ListView.Count .
	
FUN SetLVCount({Control} ListView, INT New_count) :
	ListView.{Paint_table}.Set_count(New_count) .

CONST INT {textalig} :
	taLeft|_align = 0
	taRight|_align = 1
	taCenter|_align = 2 .

FUN LVColAdd(
{Control} ListView, STR Value|_caption, INT Align|ment, 
INT Width|_pixels),
IF Align IS CONST, THEN Align IN {textalign}
	:
	CASE Align ?
	[taLeft]: {Paint_table}.{text_align} a|lign_text = {text_align}.'LEFT'
	[taRight]: a = {text_align}.'RIGHT'
	[taCenter]: a = {text_align}.'CENTER' ;
	ListView.{Paint_table}.Add_column(Value, a, Width.Pixels_to_points) .
	
FUN LV_get|_subitem(
{Control} ListView, INT Item|_index, INT Sub_item) ==> STR
	:= ListView.{Paint_table}.[Item, Sub_item] .
	
FUN LV_selected(
{Control} ListView, INT Item|_index) ==> BOOL
	:= Item IN ListView.{Paint_table}.Selection[] .
	
FUN LV_unselect({Control} ListView) :
	ListView.{Paint_table}.Unselect .
	
FUN LV_select(
{Control} ListView, INT Index|_row, BOOL Do_select)
	:
	ListView.{Paint_table}.Select(Index, Do_select) .
	
FUN LV_first_sel|ected(
{Control} ListView) ==> INT
	:
	CASE ListView.{Paint_table}.Selection[].Count == 0 ? RESULT = -1 ==> ;
	RESULT = ListView.{Paint_table}.Selection[0] .
	
FUN LV_next_sel(
{Control} ListView, INT From|_item) ==> INT
	:
	RESULT = -1
	FOR i IN ListView.{Paint_table}.Selection[] :
	    CASE i > From && (RESULT < 0 || i < RESULT) ? RESULT = i ;
	; .
	
FUN LVDelete(
{Control} ListView, INT Index|_row)
	:
	ListView.{Paint_table}.Delete(Index) .
	
FUN LVItemAdd(
{Control} ListView, STR Cap|tion_item)
	:
	ListView.{Paint_table}.Insert(ListView.Count, Cap) .
	
FUN LVCurItem(
{Control} ListView) ==> INT
	:
	CASE ListView.{Paint_table}.Selection[].Count == 0 ? RESULT = -1
	ELSE RESULT = ListView.{Paint_table}.Selection[0] ; .

	
------------------- 'treeview'
	
FUN TVRoot|_only_0_item({Control} T|ree_view) ==> INT :
	RESULT = T.{Paint_tree}.Root(0) .
	
CONST INT {treeview_special|_items_ids} :
	TVI_ROOT                = 0xFFFF_0000
  	TVI_FIRST               = 0xFFFF_0001
  	TVI_LAST                = 0xFFFF_0002 .
	
FUN TVInsert(
{Control} T|ree_view, INT Parent|_id, INT After|_index,
STR S|tring_value) ==> INT,
IF After IS CONST, THEN After IN {treeview_special}
	:
	CASE ?
	[After == 0xFFFF0002] ? // TVI_LAST
		 RESULT = T.{Paint_tree}.Insert(Parent,
		 	T.{Paint_tree}.Node(Parent).Child[].Count,
		 	S)
		 
	[After >= 0] ? RESULT = T.{Paint_tree}.Insert(Parent, After+1, S)
	ELSE RESULT = T.{Paint_tree}.Insert(Parent, 0, S) ; 
	T.{Paint_tree}.Invalidate_all .
	
FUN TVDelete(
{Control} T|ree_view, INT Id|entifier)
	:
	T.{Paint_tree}.Delete(Id) .
	
FUN TVItemVisible(
{Control} T|ree_view, INT Id|entifier_node) ==> BOOL
	:= !T.{Paint_tree}.Node(Id).Hidden .
	
FUN SetTVItemVisible(
{Control} T|ree_view, INT Id|entifier_node, BOOL Value|_visiblle)
	:
	T.{Paint_tree}.Node(Id).Hidden = !Value
	T.{Paint_tree}.Invalidate_all .
	
FUN TVItemIcon(
{Control} T|ree_view, INT Id|entifier_node) ==> INT
	:
	RESULT = T.{Paint_tree}.Node(Id).Icon .
	
FUN TVItemStateIcon(
{Control} T|ree_view, INT Id|entifier_node) ==> INT
	:= T.{Paint_tree}.Node(Id).State_icon .
	
FUN SetTVItemIcon(
{Control} T|ree_view, INT Id|entifier_node, INT Value|_index_in_images_list)
	:
	T.{Paint_tree}.Node(Id).Icon = Value
	T.{Paint_tree}.Invalidate_all .
	
FUN SetTVItemStateIcon(
{Control} T|ree_view, INT Id|entifier_node, INT Value|_index_in_images_list)
	:
	T.{Paint_tree}.Node(Id).State_icon = Value
	T.{Paint_tree}.Invalidate_all .
	
FUN TVItemObject(
{Control} T|ree_view, INT Id|entifier_node) ==> {Object}
	:= T.{Paint_tree}.Node(Id).Data_ .
	
FUN SetTVItemObject(
{Control} T|ree_view, INT Id|entifier_node, {Object} Data|_to_assign)
	: 
	T.{Paint_tree}.Node(Id).Data_ = Data .
	
FUN TVChildren(
{Control} T|ree_view, INT Parent|_node, INT Index|_of_subnode) ==> INT
	:= T.{Paint_tree}.Node(Parent).Child[Index] .
	
FUN TVItemText(
{Control} T|ree_view, INT Id|entifier) ==> STR	
	:= T.{Paint_tree}.Node(Id).Text .
	
FUN TVItemNext(
{Control} T|ree_view, INT Id|entifier) ==> INT
	:
	INT i|ndex_in_parent = T.{Paint_tree}.Node(
		T.{Paint_tree}.Node(Id).Parent).Child[].Find(Id)
		
	RESULT = T.{Paint_tree}.Node(T.{Paint_tree}.Node(Id).Parent).Child[i+1] .
	
FUN TVItemParent(
{Control} T|ree_view, INT Id|entifier) ==> INT
	:= T.{Paint_tree}.Node(Id).Parent .
	
FUN TVSelected({Control} T|ree_view) ==> INT
	:= T.{Paint_tree}.Selected .
	
------------------------- 'progress bar'

FUN SetProgressBarProgress(
{Control} P|rogress_control, INT V|alue_to_set)
	:
	P.{Progress}.Set_progress(V) .
	
----------- 'all controls'

FUN SetControlText(
{Control} C|ontrol_obj, STR Value|_text)
	:
	C.Set_text(Value) .
	
END

*/
*)

unit Delphi_to_AL4;
{@BYTES}

{$I KOLDEF.inc}

interface

uses Types, Windows, KOL;

function AsInt(X: Integer): Integer; overload;
function AsInt(X: Double): Integer; overload;
function Inflate(R: TRect; dX, dY: Double): TRect;
//function ClientBounds(C: TControl): TRect;
//function Form_X_Points_to_Control_Pixels(C: TControl; X: Double): Integer;
//function Form_Y_Points_to_Control_Pixels(C: TControl; Y: Double): Integer;

function NewBitmap(Width, Height: Integer; BitsPerPixel: Integer): PBitmap;
function NewBitmapDirect(
	Width, Height: Integer; BitsPerPixel: Integer): PBitmap;
//function Get_bitmap_pixel(Bmp: TBitmap; x, y: Integer): Integer;
//procedure Set_bitmap_pixel(Bmp: TBitmap; x, y: Integer; Color: Integer);
function LoadBitmap(const Path: String): PBitmap;
//procedure SaveBitmap(B: TBitmap; Path: String);

procedure DrawBitmap(Dest: PCanvas; X, Y: Integer; Bmp: PBitmap);
//procedure SetBrush(Dest: TCanvas; Solid: Boolean; Color: Integer);
//procedure SetPen(Dest: TCanvas; Pen_w: Double; Color: Integer);
//procedure SetUnitsPixels(Dest: TCanvas);
//procedure DrawRect(Dest: TCanvas; R: TRect);
function SystemColor(c: Integer): Integer;

type Trgb = (rgbR, rgbG, rgbB, rgbA);
function Mask_RGB(Component: Trgb): Integer;
function Shift_RGB(Component: Trgb): Integer;

//procedure ActivateTimer(Timer: TTimer; Activate: Boolean);
//function TimerIsActive(Timer: TTimer): Boolean;
//procedure Catalog(const Path: String; cat: TStringList);

procedure SetControlVisible(C: PControl; Show: Boolean);
procedure SetControlText(C: PControl; const Value: String);

function RemoveTrailingSlash(const s: String): String;
function Replace_all(const S, From, To_: String): String;

function CompareBytes(const A, B: array of Byte; c: Integer): Boolean;

procedure SetCaption(F: PControl; const S: String);

procedure SetLVCount(ListView: PControl; NewCount: Integer);
function LV_get(ListView: PControl; Item, Subitem: Integer): String;
function LV_selected(ListView: PControl; Index: Integer): Boolean;
procedure LV_unselect(ListView: PControl);
procedure LV_select(ListView: PControl; Index: Integer; Do_select: Boolean);
function LV_first_sel(ListView: PControl): Integer;
function LV_next_sel(ListView: PControl; from: Integer): Integer;

function TVItemIcon(T: PControl; N: Integer): Integer;
procedure SetTVItemIcon(T: PControl; N: Integer; V: Integer);
function TVItemStateIcon(T: PControl; N: Integer): Integer;
procedure SetTVItemStateIcon(T: PControl; N: Integer; V: Integer);
function TVItemVisible(T: PControl; N: Integer): Boolean;
procedure SetTVItemVisible(T: PControl; N: Integer; V: Boolean);

function TVItemObject(T: PControl; N: Integer): PObj;
procedure SetTVItemObject(T: PControl; N: Integer; Data_: PObj);

function TVChildren(T: PControl; N: Integer; Index: Integer): Integer;

function TVItemText(T: PControl; N: Integer): String;

function TVItemNext(T: PControl; N: Integer): Integer;
function TVItemParent(T: PControl; N: Integer): Integer;

function NewPaintTurtleButton(Parent_panel: PControl; const Alias: String;
	const Text: String; const Commands: String): PControl;

procedure SetProgressBarProgress(P: PControl; V: Integer);

function NewReadFileStream(const Path: String): PStream;
function NewWriteFileStream(const Path: String): PStream;
function NewReadWriteFileStream(const Path: String): PStream;
function ReadBytes(FS: PStream; var AB: array of Byte; Sz: Integer): Integer;

implementation

function AsInt(X: Integer): Integer;
begin
	Result := X;
end;

function AsInt(X: Double): Integer;
begin
	Result := Trunc(X);
end;


function Inflate(R: TRect; dX, dY: Double): TRect;
begin
	Result := R;
	InflateRect(Result, Trunc(dX), Trunc(dY));
end;

//function ClientBounds(C: TControl): TRect;
//begin
//    Result := C.ClientRect;
//end;

//function Form_X_Points_to_Control_Pixels(C: TControl; X: Double): Integer;
//begin
//    Result := Trunc(X);
//end;

//function Form_Y_Points_to_Control_Pixels(C: TControl; Y: Double): Integer;
//begin
//    Result := Trunc(Y);
//end;

function NewBitmap(Width, Height: Integer; BitsPerPixel: Integer): PBitmap;
begin
    Result := KOL.NewBitmap(Width, Height);
    if BitsPerPixel <> 0 then
        case BitsPerPixel of
        1: Result.PixelFormat := pf1bit;
        4: Result.PixelFormat := pf4bit;
        8: Result.PixelFormat := pf8bit;
        15: Result.PixelFormat := pf15bit;
        16: Result.PixelFormat := pf16bit;
        24: Result.PixelFormat := pf24bit;
        else Result.PixelFormat := pf32bit;
        end;
end;

function NewBitmapDirect(
	Width, Height: Integer; BitsPerPixel: Integer): PBitmap;
begin
	Result := NewBitmap(Width, Height, BitsPerPixel);
end;

//function Get_bitmap_pixel(Bmp: TBitmap; x, y: Integer): Integer;
//begin
//    Result := Bmp.Canvas.Pixels[x, y];
//end;

//procedure Set_bitmap_pixel(Bmp: TBitmap; x, y: Integer; Color: Integer);
//begin
//    Bmp.Canvas.Pixels[x, y] := Color;
//end;

function LoadBitmap(const Path: String): PBitmap;
begin
	Result := KOL.NewBitmap(1, 1);
	Result.LoadFromFile(Path);
end;

//procedure SaveBitmap(B: TBitmap; Path: String);
//begin
//    B.SaveToFile(Path);
//end;

procedure DrawBitmap(Dest: PCanvas; X, Y: Integer; Bmp: PBitmap);
begin
  Bmp.Draw(Dest.Handle, X, Y);
end;

//procedure SetBrush(Dest: TCanvas; Solid: Boolean; Color: Integer);
//begin
//    Dest.Brush.Color := Color;
//    if Solid then Dest.Brush.BrushStyle := bsSolid
//    else Dest.Brush.BrushStyle := bsClear;
//end;

//procedure SetPen(Dest: TCanvas; Pen_w: Double; Color: Integer);
//begin
//    Dest.Pen.PenWidth := Trunc(Pen_w);
//    Dest.Pen.Color := Color;
//end;

//procedure SetUnitsPixels(Dest: TCanvas);
//begin
//    // in usual Delphi, just do nothing
//end;

//procedure DrawRect(Dest: TCanvas; R: TRect);
//var c: TColor;
//    bs: TBrushStyle;
//begin
//    Dest.FillRect(R);
//    c := Dest.Brush.Color;
//    bs := Dest.Brush.BrushStyle;
//    //----- pen --> brush
//    Dest.Brush.Color := Dest.Pen.Color;
//    Dest.Brush.BrushStyle := bsSolid;
//    Dest.FrameRect(R);
//    //----- restore brush
//    Dest.Brush.Color := c;
//    Dest.Brush.BrushStyle := bs;
//end;

function SystemColor(c: Integer): Integer;
begin
	Result := c;
end;

function Mask_RGB(Component: Trgb): Integer;
begin
	Result := $FF shl Shift_RGB(Component);
end;

function Shift_RGB(Component: Trgb): Integer;
begin
	Result := 0;
	case Component of
	rgbR: Result := 0;
	rgbG: Result := 8;
	rgbB: Result := 16;
	rgbA: Result := 24;
	end;
end;

procedure ActivateTimer(Timer: TTimer; Activate: Boolean);
begin
	Timer.Enabled := Activate;
end;

function TimerIsActive(Timer: TTimer): Boolean;
begin
	Result := Timer.Enabled;
end;

procedure SetControlVisible(C: PControl; Show: Boolean);
begin
    C.Visible := Show;
end;

procedure SetControlText(C: PControl; const Value: String);
begin
	C.Text := Value;
end;

function RemoveTrailingSlash(const s: String): String;
begin
	Result := s;
	if (Result <> '') and (Result[Length(Result)] = '\') then
		Result := Copy(Result, 1, Length(Result)-1);
end;

function Replace_all(const S, From, To_: String): String;
var i: Integer;
begin
  Result := S;
  for i := 1 to Length(S) do
  begin
      if not StrReplace(Result, From, To_) then Exit;
  end;
end;

function CompareBytes(const A, B: array of Byte; c: Integer): Boolean;
begin
	Result := CompareMem(@A[0], @B[0], c);
end;

procedure SetCaption(F: PControl; const S: String);
begin
	F.Caption := S;
end;

procedure SetLVCount(ListView: PControl; NewCount: Integer);
begin
    ListView.Count := NewCount;
end;

function LV_get(ListView: PControl; Item, Subitem: Integer): String;
begin
	Result := ListView.LVItems[Item, Subitem];
end;

function LV_selected(ListView: PControl; Index: Integer): Boolean;
begin
    Result := lvisSelect in ListView.LVItemState[ Index ];
end;

procedure LV_unselect(ListView: PControl);
begin
	ListView.LVItemState[-1] := [];
end;

procedure LV_select(ListView: PControl; Index: Integer; Do_select: Boolean);
begin
	if Do_select then
		ListView.LVItemState[Index] := [lvisSelect]
	else ListView.LVItemState[Index] := [];
end;

function LV_first_sel(ListView: PControl): Integer;
begin
	Result := ListView.LVNextItem(-1, LVIS_SELECTED);
end;

function LV_next_sel(ListView: PControl; from: Integer): Integer;
begin
	Result := ListView.LVNextItem(from, LVIS_SELECTED);
end;

function TVItemIcon(T: PControl; N: Integer): Integer;
begin
	Result := T.TVItemImage[N];
end;

procedure SetTVItemIcon(T: PControl; N: Integer; V: Integer);
begin
	T.TVItemImage[N] := V;
	T.TVItemSelImg[N] := V;
end;

function TVItemStateIcon(T: PControl; N: Integer): Integer;
begin
	Result := T.TVItemStateImg[N];
end;

procedure SetTVItemStateIcon(T: PControl; N: Integer; V: Integer);
begin
	T.TVItemStateImg[N] := V;
end;

function TVItemVisible(T: PControl; N: Integer): Boolean;
begin
	Result := T.TVItemVisible[N];
end;

procedure SetTVItemVisible(T: PControl; N: Integer; V: Boolean);
begin
	T.TVItemVisible[N] := V;
end;

function TVItemObject(T: PControl; N: Integer): PObj;
begin
	Result := T.TVItemData[ N ];
end;

procedure SetTVItemObject(T: PControl; N: Integer; Data_: PObj);
begin
	T.TVItemData[ N ] := Data_;
end;

function TVChildren(T: PControl; N: Integer; Index: Integer): Integer;
begin
	Result := T.TVItemChild[N];
	while Index > 0 do
	begin
		Result := T.TVItemNext[Result];
		dec(Index);
	end;
end;

function TVItemText(T: PControl; N: Integer): String;
begin
	Result := T.TVItemText[N];
end;

function TVItemNext(T: PControl; N: Integer): Integer;
begin
    Result := T.TVItemNext[N];
end;

function TVItemParent(T: PControl; N: Integer): Integer;
begin
    Result := T.TVItemParent[N];
end;

function NewPaintTurtleButton(Parent_panel: PControl; const Alias: String;
	const Text: String; const Commands: String): PControl;
begin
	Result := NewBitBtn(Parent_panel, Text, [ ], glyphLeft, 0, 0);
end;


procedure SetProgressBarProgress(P: PControl; V: Integer);
begin
	P.Progress := V;
end;

function NewReadFileStream(const Path: String): PStream;
begin
	Result := KOL.NewReadFileStream(Path);
end;

function NewWriteFileStream(const Path: String): PStream;
begin
	Result := KOL.NewWriteFileStream(Path);
end;

function NewReadWriteFileStream(const Path: String): PStream;
begin
	Result := KOL.NewReadWriteFileStream(Path);
end;

function ReadBytes(FS: PStream; var AB: array of Byte; Sz: Integer): Integer;
begin
	Result := FS.Read(AB[0], Sz);
end;

end.

