{***************************************************************************}
{ TMS IntraWeb Component Pack Pro                                           }
{ for Delphi & C++Builder                                                   }
{                                                                           }
{ written by TMS Software                                                   }
{            copyright © 2002 - 2007                                        }
{            Email : info@tmssoftware.com                                   }
{            Web : http://www.tmssoftware.com                               }
{                                                                           }
{ The source code is given as is. The author is not responsible             }
{ for any possible damage done due to the use of this code.                 }
{ The component can be freely used in any application. The complete         }
{ source code remains property of the author and may not be distributed,    }
{ published, given or sold in any form as such. No parts of the source      }
{ code can be included in any other component or application without        }
{ written authorization of the author.                                      }
{***************************************************************************}

{$I TMSDEFS.INC}

unit IWTMSBase;

interface
uses
  IWControl
  {$IFDEF TMSIW6}
  , IWRenderContext
  {$ENDIF}
  ;

{$IFDEF TMSIW7}
type
   TIWBaseComponentContext = TIWBaseHTMLComponentContext;
{$ENDIF}
  
  function GetResourceText(ResName:string): string;
  {$IFDEF TMSIW6}
  function GetCacheDir(Control: TIWCustomControl): string;
  function GetControlHTML(Control: TIWCustomControl; AContext: TIWBaseComponentContext): string;
  {$ELSE}
  function GetCacheDir(Control: TIWControl): string;
  function GetControlHTML(Control: TIWControl): string;
  {$ENDIF}

  function GetProgramFiles: string;
  {$IFNDEF DELPHI6_LVL}
  function ForceDirectories(Dir: string): Boolean;
  {$ENDIF}

  {$IFNDEF DELPHI6_LVL}
  function StrToBool(s: string): boolean;
  {$ENDIF}

implementation

uses
  IWGlobal, Classes, SysUtils, IWHTMLTag, ShlObj, ActiveX , Windows
  {$IFDEF FREEWARE}
  {$IFDEF TMSIW51}
  , IWKlooch
  {$ELSE}
  {$IFNDEF DELPHI7_LVL}
  , IWKlooch
  {$ENDIF}
  {$ENDIF}
  {$ENDIF}
  ;

{$IFNDEF DELPHI6_LVL}
function StrToBool(s: string): boolean;
begin
  Result :=  Uppercase(s) = 'TRUE';
end;
{$ENDIF}


{$IFDEF TMSIW6}
function GetControlHTML(Control: TIWCustomControl;
  AContext: TIWBaseComponentContext): string;
{$ELSE}
function GetControlHTML(Control: TIWControl): string;
{$ENDIF}
var
  it: TIWHTMLTag;
begin
  // !!!!!!!!!!!!!! IMPORTANT NOTICE !!!!!!!!!!!!!!!!!!
  //
  // If you encounter a compilation problem here,
  // make sure that you followed the installation steps
  // in INSTALL.TXT, in particular, that you're using the
  // correct TMSDEFS.INC file
  //
  // !!!!!!!!!!!!!! IMPORTANT NOTICE !!!!!!!!!!!!!!!!!!

  {$IFDEF TMSIW6}
  it := Control.RenderHTML(AContext);
  {$ELSE}
  it := Control.RenderHTML;
  {$ENDIF}

  {$IFDEF TMSIW51}
  Result := it.Render;
  {$ELSE}
  it.Contents.Render(Result);
  {$ENDIF}
  it.Free;
end;

{$IFDEF TMSIW6}
function GetCacheDir(Control:TIWCustomControl): string;
{$ELSE}
function GetCacheDir(Control:TIWControl): string;
{$ENDIF}
begin
  {$IFDEF TMSIW51}

  //7.1 code for user cache dir
  {$IFDEF TMSIW71}
  Result := GServerController.UserCacheURL;
  {$ENDIF}
  //pre 7.1 code for user cache dir
  {$IFNDEF TMSIW71}
  Result := GServerController.CacheURL;
  {$ENDIF}

  {$ELSE}
  Result := Control.WebApplication.URLBase + '/cache/';
  {$ENDIF}
end;

function GetResourceText(ResName: string): string;
var
  p: ansichar;
  RS: TResourceStream;
  i: Integer;
begin
  RS := TResourceStream.Create(HInstance, ResName, RT_RCDATA);

  Result := '';

  for i := 1 to RS.Size do
  begin
    RS.Read(p,1);
    Result := Result + string(p);
  end;

  RS.Free;
end;


procedure FreePidl( pidl: PItemIDList );
var
  Allocator: IMalloc;
begin
  if Succeeded(SHGetMalloc(allocator)) then
  begin
    Allocator.Free(pidl);
  end;
end;

function GetProgramFiles: string;
const
  CSIDL_PROGRAM_FILES = $0026;
var
  pidl: PItemIDList;
  Path: array [0..MAX_PATH-1] of char;
begin
  Result := '';
  if Succeeded(SHGetSpecialFolderLocation(0, CSIDL_PROGRAM_FILES, pidl)) then
  begin
    if SHGetPathFromIDList(pidl, Path) then
      Result := Path;
    FreePidl(pidl);
  end;
end;

{$IFNDEF DELPHI6_LVL}
function DirectoryExists(const Name: string): Boolean;
var
  Code: Integer;
begin
  Code := GetFileAttributes(PChar(Name));
  Result := (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0);
end;

function ForceDirectories(Dir: string): Boolean;
begin
  Result := True;
  if Length(Dir) = 0 then
    raise Exception.Create('Cannot create directory');

  if Dir[length(Dir)] = '\' then
    Delete(Dir,length(Dir),1);

  if (Length(Dir) < 3) or DirectoryExists(Dir)
    or (ExtractFilePath(Dir) = Dir) then Exit; // avoid 'xyz:\' problem.
  Result := ForceDirectories(ExtractFilePath(Dir)) and CreateDir(Dir);
end;
{$ENDIF}



initialization

{$IFDEF FREEWARE}
   {$IFDEF TMSIW51}
   GLicense.ThirdPartyHook('TMS IntraWeb Components evaluation',True);
   {$ELSE}
   {$IFNDEF DELPHI7_LVL}
   GLicense.ThirdPartyHook('TMS IntraWeb Components evaluation',True);
   {$ELSE}
   AddThirdPartyLogo('TMS IntraWeb Components evaluation',True);
   {$ENDIF}
   {$ENDIF}
{$ENDIF}

end.
