{ Simple drag/drop demonstration program. }
unit Lista;

interface

uses
  Messages,Windows,WinTypes, WinProcs, Classes, Graphics, Forms, Controls, StdCtrls,
  Menus, Buttons, DB, DBTables,Dialogs,SysUtils, ExtCtrls,checa,describe,FileCtrl;

{  Windows, Messages, SysUtils,
  Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls;}



type
  TFormaDisenoVista = class(TForm)
    AreaCreacion: TMemo;
    ListaCampos: TListBox;
    MainMenu1: TMainMenu;
    Archivo1: TMenuItem;
    Salir1: TMenuItem;
    Ayuda1: TMenuItem;
    ListaClaves: TListBox;
    TablaReporte: TTable;
    Abrir1: TMenuItem;
    AbreBase: TOpenDialog;
    Guardar2: TMenuItem;
    GuardaBase: TSaveDialog;
    AreaClaves: TMemo;
    Descr: TEdit;
    Describe: TMemo;
    Label1: TLabel;
    Tipos: TEdit;
    Label2: TLabel;
    Panel1: TPanel;
    BotonAbrir: TSpeedButton;
    BotonGuardar2: TSpeedButton;
    Temas1: TMenuItem;
    Label3: TLabel;
    Lista: TComboBox;
    BotonSalir: TSpeedButton;
    procedure AreaCreacionDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure AreaCreacionDragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure BotonGuardar2Click(Sender: TObject);
    procedure Abrir1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure ListaChange(Sender: TObject);
    procedure BotonSalirClick(Sender: TObject);
    procedure BotonAbrirMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonGuardar2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Panel2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Salir1Click(Sender: TObject);
  end;
Type
datoscampo=record
campo:String[12];
descripcion:String[40];
tipo:String[12];
longitud:Integer;
end;


var
  FormaDisenoVista: TFormaDisenoVista;
  n:integer;
  dat:datoscampo;

implementation

{$R *.DFM}

function leerangocampos(inicio,fin:integer):boolean;
var
i,conta:integer;
nume:String[5];
begin
with FormaDisenoVista do
begin
conta:=0;


for i:=inicio to fin do
begin

if (leecampos(i,2)<>'')and (leecampos(i,2)<>'No Disponible') then
begin
ListaClaves.Items.Add(leecampos(i,1));
ListaClaves.Items.Add(leecampos(i,2));
ListaClaves.Items.Add(leecampos(i,3));
ListaClaves.Items.Add(leecampos(i,4));
conta:=conta+1;
end;

end;
end;
end;




{ Only accept if the source of the drag is a label. Note that }
{ all the labels' DragMode properties are set to dmAutomatic. }

procedure leeclaves(archcampos:string);
var
arch:File of datoscampo;
i:integer;
nombre:string;
begin
nombre[length(nombre)]:='r';
nombre[length(nombre)-1]:='o';
nombre[length(nombre)-2]:='f';
FormaDisenoVista.Tipos.Text:=leeruta+'\report\'+nombre;

{Assign(arch,archcampos);
Reset(arch);
i:=length(archcampos);
nombre:='';

while archcampos[i]<>'\' do
begin
nombre:=archcampos[i]+nombre;
i:=i-1;
end;
nombre[length(nombre)]:='r';
nombre[length(nombre)-1]:='o';
nombre[length(nombre)-2]:='f';
FormaDisenoVista.Tipos.Text:=leeruta+'\report\'+nombre;

while not Eof(arch) do
begin
read(arch,dat);
With FormaDisenoVista do
begin

ListaClaves.Items.Add(dat.campo);
ListaClaves.Items.Add(dat.descripcion);
ListaClaves.Items.Add(dat.tipo);
ListaClaves.Items.Add(IntToStr(dat.longitud));
end;

end;       }

end;


procedure leenombrecampos;
var
i:integer;
begin
With FormaDisenoVista do
begin
i:=1;
ListaCampos.Clear;
while i< ListaClaves.Items.Count-1 do
begin
ListaCampos.Items.Add(ListaClaves.Items[i]);
i:=i+4;
end;
end;
end;
{*****************}
procedure ponecampos(archivo:string);
begin
{if archivo='Clientes' then
leeclaves(leeruta+'\consulta\clientes.cmp');
if archivo='Proveedores' then
leeclaves(leeruta+'\consulta\proveedo.cmp');
if archivo='Vendedores' then
leeclaves(leeruta+'\consulta\vendedor.cmp');
if archivo='Facturación' then
leeclaves(leeruta+'\consulta\facturae.cmp');
if archivo='Cuentas por Cobrar' then
leeclaves(leeruta+'\consulta\cxc.cmp');
if archivo='Compras' then
leeclaves(leeruta+'\consulta\comprase.cmp');
if archivo='Cuentas por Pagar' then
leeclaves(leeruta+'\consulta\cxp.cmp');
if archivo='Inventario' then
leeclaves(leeruta+'\consulta\inventar.cmp');
if archivo='Movimientos al Inventario' then
leeclaves(leeruta+'\consulta\Movinven.cmp');}

{FormaDisenoVista.ListaClaves.Clear;}
{leeclaves(AbreBase.FileName);}



if archivo='Clientes' then
begin
leerangocampos(1,18);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\clientes.for';
end;

if archivo='Compras' then
begin
leerangocampos(47,61);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\comprasE.for';
end;

if archivo='Cuentas por Cobrar' then
begin
leerangocampos(268,280);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\cxc.for';
end;

if archivo='Cuentas por Pagar' then
begin
leerangocampos(295,306);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\cxp.for';
end;

if archivo='Facturación' then
begin
leerangocampos(74,101);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\facturaE.for';
end;

if archivo='Inventario' then
begin
leerangocampos(228,255);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\Inventar.for';
end;

if archivo='Movimientos al Inventario' then
begin
leerangocampos(185,196);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\MovInven.for';
end;

if archivo='Vendedores' then
begin
leerangocampos(31,34);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\vendedor.for';
end;

if archivo='Proveedores' then
begin
leerangocampos(47,61);
FormaDisenoVista.Tipos.Text:=leeruta+'\report\proveedo.for';
end;


leenombrecampos;



end;





{***************}



procedure iniciabase;
begin
With FormaDisenoVista do
begin
TablaReporte.DataBaseName:='c:\proyecto';
TablaReporte.TableName:='FACTURAE.DBF';
TablaReporte.IndexName:='INUMENC';
TablaReporte.Open;
end;
end;

procedure calculaparametros;
begin
With FormaDisenoVista do
begin
AreaClaves.Clear;
AreaClaves.Lines.LoadFromFile('c:\Listas\Reporte.vis');
n:=AreaClaves.Lines.Count;
end;
end;

procedure generareporte;

var
i:integer;
valor:string[8];
arch:TextFile;

begin
With FormaDisenoVista do
begin
AssignFile(arch,'c:\listas\repor.rep');
ReWrite(arch);
TablaReporte.First;
while not TablaReporte.Eof do
begin

for i:=0 to n-1 do
begin
valor:=TablaReporte.FieldByName(AreaClaves.Lines[i]).AsString;

write(arch,valor+'*');

end;
writeln(arch,'');
TablaReporte.Next;
end;

closefile(arch);
end;
end;


procedure borraclave(clave:String);

begin


end;

procedure TFormaDisenoVista.AreaCreacionDragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
 {Accept := Source is TLabel;  }
  Accept := Source is TListBox;

end;

{ Assign the label's font to the memo field. }
procedure TFormaDisenoVista.AreaCreacionDragDrop(Sender, Source: TObject; X, Y: Integer);
var Dato,Dato2:string;
begin

Dato:=AreaCreacion.Lines[0]+'  '+(Source as  TListBox).Items[ListaCampos.ItemIndex];
AreaCreacion.Lines[0]:=Dato;
if ListaCampos.ItemIndex=0 then
Dato2:=(ListaClaves.Items[ListaCampos.ItemIndex])
else
Dato2:=(ListaClaves.Items[ListaCampos.ItemIndex*4]);
AreaClaves.Lines.Add(Dato2);

Dato2:=(ListaClaves.Items[(ListaCampos.ItemIndex*4)+2]);
AreaClaves.Lines.Add(Dato2);

Dato2:=(ListaClaves.Items[(ListaCampos.ItemIndex*4)+3]);
AreaClaves.Lines.Add(Dato2);
Dato2:=(ListaClaves.Items[(ListaCampos.ItemIndex*4)+1]);
AreaClaves.Lines.Add(Dato2);



{  Memo1.Lines.add( (Source as  TLabel).Caption); }

end;

procedure asignatipo;
type
datos=record
archivo:String[60];
descripcion:String[125];
end;

var
dat:datos;
arch:file of datos;


begin

with FormaDisenoVista do
begin
{ShowMessage(tipos.text);}
if not FileExists(Tipos.Text) then
begin
if (not DirectoryExists(leeruta+'\report')) then
CreateDir(leeruta+'\report');
AssignFile(arch,Tipos.Text);
ReWrite(arch);
CloseFile(arch);
end;

AssignFile(arch,Tipos.Text);
Reset(arch);

Dat.archivo:=GuardaBase.FileName;
Dat.Descripcion:=Descr.Text;
Seek(arch,FileSize(arch));
Write(arch,dat);
CloseFile(arch);
end;
end;


procedure TFormaDisenoVista.BotonGuardar2Click(Sender: TObject);
var
arch1:Textfile;
i:integer;
begin
if GuardaBase.Execute then
begin
AreaClaves.Lines.SaveToFile(GuardaBase.FileName);
Descr.Text:=Describe.Lines[0]+Describe.Lines[1]+Describe.Lines[2];
asignatipo;
end;

{begin
Descr.Text:=Describe.Lines[0]+Describe.Lines[1]+Describe.Lines[2];
AreaClaves.Lines.SaveToFile('c:\Listas\Reporte.vis');}
end;

procedure TFormaDisenoVista.Abrir1Click(Sender: TObject);
begin
ListaCampos.Clear;
ListaClaves.Clear;
AreaCreacion.Clear;
AreaClaves.Clear;
if AbreBase.Execute then
begin
ListaClaves.Clear;
leeclaves(AbreBase.FileName);
{ListaClaves.Items.LoadFromFile(AbreBase.FileName);}
leenombrecampos;
end;
end;


procedure TFormaDisenoVista.FormActivate(Sender: TObject);
begin
ListaCampos.Clear;
ListaClaves.Clear;
AreaCreacion.Clear;
AreaClaves.Clear;
end;




procedure TFormaDisenoVista.ListaChange(Sender: TObject);
begin
FormaDisenoVista.ListaClaves.Clear;
ponecampos(Lista.Text);

end;

procedure TFormaDisenoVista.BotonSalirClick(Sender: TObject);
begin
Close;

end;

procedure TFormaDisenoVista.BotonAbrirMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaDisenoVista.Left+BotonAbrir.Left,FormaDisenoVista.Top+BotonAbrir.Top+BotonAbrir.Height,100);
end;

procedure TFormaDisenoVista.BotonGuardar2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaDisenoVista.Left+BotonGuardar2.Left,FormaDisenoVista.Top+BotonGuardar2.Top+BotonGuardar2.Height,101);
end;

procedure TFormaDisenoVista.Panel2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
SendMessage(FormaDisenoVista.Handle,WM_SYSCOMMAND, $F012, 0);

end;

procedure TFormaDisenoVista.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
BotonSalir.Tag:=1000;
end;

procedure TFormaDisenoVista.Salir1Click(Sender: TObject);
begin
Close;
end;

end.
