unit resu;

interface

uses WinTypes, WinProcs, Classes, Graphics, Forms, Controls,
  StdCtrls, DBTables, DB, DBCtrls, Grids, DBGrids, ExtCtrls, Buttons,
  FileCtrl, BtrMain, BseMain;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Panel1: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    DatabaseListbox: TListBox;
    TableListbox: TListBox;
    Button1: TButton;
    DirectoryListBox1: TDirectoryListBox;
    FileListBox1: TFileListBox;
    DriveComboBox1: TDriveComboBox;
    FilterComboBox1: TFilterComboBox;
    Edit1: TEdit;
    Button4: TButton;
    Label3: TLabel;
    Edit2: TEdit;
    Label4: TLabel;
    Memo1: TMemo;
    Table: TtbTable;
    procedure TableListboxClick(Sender: TObject);
    procedure DatabaseListboxClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    Procedure OpenTable;
    Procedure CloseTable;
    procedure BitBtn1Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation
Uses
  SysUtils, BseRes, Dialogs;

{$R *.DFM}

Procedure TForm1.OpenTable;
begin
  If Table.Active then
    CloseTable;
  Table.Active := TRUE;
  Button1.Enabled := Table.Active;
  If ( Button1.Enabled ) then
  begin
    Edit1.Text := ChangeFileExt( UpperCase( Table.TableName ), '.RES' );
    Edit2.Text := UpperCase( Table.TableName );
    Button1.Caption := 'Write Resource File ' + Edit1.Text;
  end;
end;


Procedure TForm1.CloseTable;
begin
  Table.Active        := FALSE;
  Datasource1.DataSet := NIL;
  Button1.Enabled     := Table.Active;
  Button1.Caption     := 'Write Resource File';
  Edit2.Text          := '';
end;


procedure TForm1.TableListboxClick(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  Try
    CloseTable;
    Table.DatabaseName := DatabaseListbox.Items[DatabaseListbox.ItemIndex];
    Table.TableName    := TableListbox.Items[TableListbox.ItemIndex];
    OpenTable;
  Finally
    Screen.Cursor := crDefault;
  end;
end;

procedure TForm1.DatabaseListboxClick(Sender: TObject);
begin
  Screen.Cursor := crHourGlass;
  Try
    CloseTable;
    TableListbox.Clear;
    tbSession.GetTableNames(DatabaseListbox.Items[DatabaseListbox.ItemIndex],
      '', True, False, TableListbox.Items);
  Finally
    Screen.Cursor := crDefault;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  tbSession.GetDatabaseNames( DatabaseListbox.Items );
  DataSource1.DataSet := NIL;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  CloseTable;
  Close;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Res : TTblResWriter;
begin
  Res := TTblResWriter.Create( Table );
  Try
    Edit1.Text := DirectoryListBox1.Directory + '\' + Edit1.Text;
    If Res.WriteResource( Edit2.Text, Edit1.Text ) then
      ShowMessage('Resource created successfully.' )
    else
      ShowMessage( 'Error creating resource.' );
  Finally
    Res.Free;
  end;
  CloseTable;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  Panel1.Enabled := FALSE;
  Panel1.Visible := FALSE;
end;

end.
