unit edicion;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogos,
  StdCtrls, ExtCtrls, Menus, Buttons, ComCtrls,checa,Printers, Dialogs,describe;

type
  TFormaEdicion = class(TForm)
    Caja: TScrollBox;
    marco: TBevel;
    PapelContinuo: TPaintBox;
    lineas: TLabel;
    Posicion: TLabel;
    VistaPreliminar: TPaintBox;
    Label1: TLabel;
    MainMenu1: TMainMenu;
    Archivo1: TMenuItem;
    Nuevo1: TMenuItem;
    Abrir1: TMenuItem;
    Guardar1: TMenuItem;
    GuardarComo1: TMenuItem;
    Salir1: TMenuItem;
    Edicin1: TMenuItem;
    Copiar1: TMenuItem;
    Cortar1: TMenuItem;
    Pegar1: TMenuItem;
    Ver1: TMenuItem;
    Zoom1: TMenuItem;
    Tabla1: TMenuItem;
    Herramientas1: TMenuItem;
    Compilar1: TMenuItem;
    Ayuda1: TMenuItem;
    AbreFormato: TOpenDialog;
    Guardaformato: TSaveDialog;
    PopupMenu1: TPopupMenu;
    hola1: TMenuItem;
    liz1: TMenuItem;
    PanelHoja: TPanel;
    Hoja: TRichEdit;
    panelCodigo: TPanel;
    Codigo: TRichEdit;
    Campos: TListBox;
    PanelPresentacion: TPanel;
    BotonAumento: TSpeedButton;
    BotonDisminuye: TSpeedButton;
    BotonImprime: TSpeedButton;
    PanelEdicion: TPanel;
    BotonNuevo: TSpeedButton;
    BotonAbrir: TSpeedButton;
    BotonGuardar: TSpeedButton;
    BotonCopiar: TSpeedButton;
    BotonCortar: TSpeedButton;
    BotonPegar: TSpeedButton;
    BotonAumentar: TSpeedButton;
    BotonCampos: TSpeedButton;
    BotonSintaxis: TSpeedButton;
    BotonNotas: TSpeedButton;
    Comentario: TMemo;
    BotonSalir: TSpeedButton;
    procedure CodigoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Abrir1Click(Sender: TObject);
    procedure Guardar1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Nuevo1Click(Sender: TObject);
    procedure BotonCortarClick(Sender: TObject);
    procedure BotonCopiarClick(Sender: TObject);
    procedure BotonPegarClick(Sender: TObject);
    procedure BotonCamposClick(Sender: TObject);
    procedure CodigoMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Compilar1Click(Sender: TObject);
    procedure BotonAumentoClick(Sender: TObject);
    procedure BotonDisminuyeClick(Sender: TObject);
    procedure BotonImprimeClick(Sender: TObject);
    procedure PapelContinuoPaint(Sender: TObject);
    procedure CamposDblClick(Sender: TObject);
    procedure CamposKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure VistaPreliminarPaint(Sender: TObject);
    procedure BotonNuevoMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonAbrirMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonGuardarMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonCopiarMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonCortarMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonPegarMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonAumentarMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonCamposMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonSintaxisMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonAumentoMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonDisminuyeMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure BotonImprimeMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Salir1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure PanelHojaResize(Sender: TObject);
    procedure VistaPreliminarMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ComentarioMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BotonNotasClick(Sender: TObject);
    procedure BotonSalirClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormaEdicion: TFormaEdicion;

procedure activadiseno;
procedure activapreliminar;
implementation

uses catfor;

{$R *.DFM}

function PosicionComentario(Tope,izqu:Integer):String;
var
ancho,largo,x,y:integer;
begin
With FormaEdicion do
begin
{VistaPreliminar.Top:=Marco.Top+10;
VistaPreliminar.Left:=Marco.Left+10;}
ancho:=Trunc(VistaPreliminar.Width/80);
largo:=Trunc(VistaPreliminar.Height/55);
x:=Trunc((izqu-VistaPreliminar.Left)/ancho);
y:=Trunc((tope-VistaPreliminar.Top)/largo);

PosicionComentario:='Renglon '+IntToStr(y)+' Columna '+IntToStr(x);

end;
end;


procedure AumentaVistaPreliminar(tipo:Byte);
var
ajuste:integer;
begin
With FormaEdicion do
begin
case tipo of

1:begin
ajuste:=(Caja.Width-90);

Marco.Top:= Caja.Top-20;
Marco.Left:=Caja.Left+40;
Marco.Width:=Caja.Width-80;
Marco.Height:=Trunc(Marco.Width*1.3);
VistaPreliminar.Top:=Marco.Top+10;
VistaPreliminar.Left:=Marco.Left+10;
VistaPreliminar.Width:=Marco.Width-20;
VistaPreliminar.Height:=Marco.Height-20;
end;

0:begin
ajuste:=(Caja.Width-100);
Marco.Top:= 20;
Marco.Left:=227;
Marco.Width:=193;
Marco.Height:=315;
VistaPreliminar.Top:=Marco.Top+10;
VistaPreliminar.Left:=Marco.Left+10;
VistaPreliminar.Width:=Marco.Width-20;
VistaPreliminar.Height:=Marco.Height-20;
end;

3:begin
BotonAumento.Tag:=0;
BotonDisminuye.Tag:=1;
Comentario.Visible:=False;

ajuste:=(Caja.Width-100);
Marco.Top:= 20;
Marco.Left:=367;
Marco.Width:=193;
Marco.Height:=315;
VistaPreliminar.Top:=Marco.Top+10;
VistaPreliminar.Left:=Marco.Left+10;
VistaPreliminar.Width:=Marco.Width-20;
VistaPreliminar.Height:=Marco.Height-20;
end;

end;

end;

end;

function checavalor(valorcadena,tipodato:string):boolean;
var
entero:integer;
flotante:Real;
codigo:integer;
aux:string;

begin

if tipodato='num' then
begin
val(valorcadena,flotante,codigo);
if codigo=0 then
checavalor:=True;

end;

if tipodato='numE' then
begin
val(valorcadena,entero,codigo);
if codigo=0 then
checavalor:=True;


end;

if tipodato='texto' then
begin


end;

if tipodato='campo' then
begin


end;



end;


function lineavalida(li:string;numerolinea:word):Boolean;
const
  valoresvalidos: array[0..83] of string[12]=
    ('@'      ,'num','num','campo','-','-',
     'color'  ,'rojo','azul','verde','amarillo','negro',
     'cuadro' ,'numE','numE','numE','numE','-',
     'tamaņo' ,'numE','-','-','-','-',
     'tipo'   ,'num','num','campo','-','-',
     'logo'   ,'num','num','num','num','archivo',
     'dia'    ,'num','num','campo','-','-',
     'mes'    ,'num','num','campo','-','-',
     'aņo'    ,'num','num','campo','-','-',
     'texto'  ,'num','num','texto','-','-',
     'moneda' ,'num','num','campo','-','-',
     'estilo' ,'letra','-','-','-','-',
     'separa' ,'numE','-','-','-','-',
     'formato','texto','','','','');
var
l:string[3];
i,conta:integer;
tipodepalabra:word;
palabras:array[1..6]of string;
parametro1,parametro2,parametro3,parametro4,parametro5,parametro6:Boolean;
begin
conta:=1;
i:=1;
palabras[1]:='';
palabras[2]:='';
palabras[3]:='';
palabras[4]:='';
palabras[5]:='';
if li[1]='@' then
begin
palabras[1]:='@';
conta:=2;
i:=2;
end;

while (i<=length(li))and (conta<=5) do
begin
if ((li[i]=',')and (palabras[1]<>'formato'))or(li[i]='(')or(li[i]=')') then
conta:=conta+1

else
begin
if conta<4 then
begin
if (li[i]<>' ') and (li[i]<>')') then
palabras[conta]:=palabras[conta]+li[i];
end
else
if (li[i]<>'@') and (li[i]<>')') then
palabras[conta]:=palabras[conta]+li[i];
end;
i:=i+1;
end;


tipodepalabra:=100;
l:='';
l:=li;
l:=li;if l[1]='@' then tipodepalabra:=1;
if l='col' then tipodepalabra:=2;
if l='tra' then tipodepalabra:=3;
if l='cua' then tipodepalabra:=4;
if l='tam' then tipodepalabra:=5;
if l='tip' then tipodepalabra:=6;
if  l='log' then tipodepalabra:=7;
if l='dia' then tipodepalabra:=8;
if l='mes' then tipodepalabra:=9;
if l='aņo' then tipodepalabra:=10;
if l='tex' then tipodepalabra:=11;
if l='mon' then tipodepalabra:=12;
if l='est' then tipodepalabra:=13;
if l='sep' then tipodepalabra:=14;
if l='for' then tipodepalabra:=15;

parametro1:=True;parametro2:=True;
parametro3:=True;parametro4:=True;
parametro5:=True;parametro6:=True;


if tipodepalabra<>100 then
begin
{MuestraMensaje(li+' '+IntToStr(tipodepalabra));}

parametro1:=checavalor(palabras[1],valoresvalidos[(tipodepalabra-1)*6]);
parametro2:=checavalor(palabras[2],valoresvalidos[((tipodepalabra-1)*6)+1]);
parametro3:=checavalor(palabras[3],valoresvalidos[((tipodepalabra-1)*6)+2]);
parametro4:=checavalor(palabras[4],valoresvalidos[((tipodepalabra-1)*6)+3]);
parametro5:=checavalor(palabras[5],valoresvalidos[((tipodepalabra-1)*6)+4]);
parametro6:=checavalor(palabras[6],valoresvalidos[((tipodepalabra-1)*6)+5]);

FormaEdicion.Codigo.Lines.Delete(numerolinea);
FormaEdicion.Codigo.Lines.Insert(numerolinea,li);


if not (parametro2 and parametro3 and parametro4 and parametro5 and parametro6) then
begin
{****pone la linea en un color distinto***}
with FormaEdicion.Codigo.SelAttributes do
begin
    Color := clRed;
end;
FormaEdicion.Codigo.Lines.Delete(numerolinea);
FormaEdicion.Codigo.Lines.Insert(numerolinea,li);

 with FormaEdicion.Codigo.SelAttributes do
 begin
    Color := clBlack;
 end;

end;

end
else
begin
FormaEdicion.Codigo.Lines.Delete(numerolinea);

  with FormaEdicion.Codigo.SelAttributes do
  begin
    Color := clRed;
  end;


FormaEdicion.Codigo.Lines.Insert(numerolinea,li);

 with FormaEdicion.Codigo.SelAttributes do
  begin
    Color := clBlack;
  end;


end;


begin

end;



end;




procedure dibujabitmap(x,y,nombre:String);
var
  mapa: TBitmap;
  rectangulo:TRect;
  a,b:integer;
begin

  mapa := TBitmap.Create;
{ MuestraMensaje(nombre);}
  if FileExists(nombre)then
  begin
  mapa.LoadFromFile(nombre);
  a:=StrToInt(x);
  b:=StrToInt(y);
  rectangulo.Left:=5+a;
  rectangulo.Top:=5+b;
  rectangulo.Right:=30+a;
  rectangulo.Bottom:=50+b;
  FormaEdicion.VistaPreliminar.Canvas.StretchDraw(rectangulo,mapa);
  end;

end;





procedure posicionacampos;
begin
with FormaEdicion do
begin
Campos.Top:=StrToInt(Lineas.Caption)*(Codigo.Font.Size+5)+Codigo.Top;
{Campos.Left:=StrToInt(Lineas.Caption)*Codigo.Font.Size+Codigo.Top+4;}
end;
end;


function existe(archivo:string):Boolean;
begin
if FileExists(archivo) then
existe:=True
else
existe:=False;

end;



procedure ponecampos;
begin
with FormaEdicion do
begin
if FormaCampos.Bases.Text='Clientes' then
if existe(leeruta+'\define\Clientes.cam')then Campos.Items.LoadFromFile(leeruta+'\define\Clientes.cam');
if FormaCampos.Bases.Text='Compras' then
if existe(leeruta+'\define\Compras.cam')then Campos.Items.LoadFromFile(leeruta+'\define\Compras.cam');
if FormaCampos.Bases.Text='Ctas. Cobrar' then
if existe(leeruta+'\define\CtasXC.cam')then Campos.Items.LoadFromFile(leeruta+'\define\CtasXC.cam');
if FormaCampos.Bases.Text='Ctas. Pagar' then
if existe(leeruta+'\define\CtasXP.cam')then Campos.Items.LoadFromFile(leeruta+'\define\CtasXP.cam');
if FormaCampos.Bases.Text='Facturas' then
if existe(leeruta+'\define\Factu.cam')then Campos.Items.LoadFromFile(leeruta+'\define\Factu.cam');
if FormaCampos.Bases.Text='Inventarios' then
if existe(leeruta+'\define\Inventa.cam')then Campos.Items.LoadFromFile(leeruta+'\define\Inventa.cam');
if FormaCampos.Bases.Text='Mov. Inventario' then
if existe(leeruta+'\define\MovInv.cam')then Campos.Items.LoadFromFile(leeruta+'\define\MovInv.cam');
if FormaCampos.Bases.Text='Vendedores' then
if existe(leeruta+'\define\Vende.cam')then Campos.Items.LoadFromFile(leeruta+'\define\Vende.cam');
if FormaCampos.Bases.Text='Proveedores' then
if existe(leeruta+'\define\Provee.cam')then Campos.Items.LoadFromFile(leeruta+'\define\Provee.cam');
end;

end;



function calculalinea(caracteres:integer):integer;

var
linea,p,conta,i:integer;
begin
conta:=0;i:=0;

while (conta<caracteres)and (i<=FormaEdicion.Codigo.Lines.Count) do
begin
conta:=conta+length(FormaEdicion.Codigo.Lines[i])+2;
i:=i+1;
{conta:=conta+2;}
end;
calculalinea:=i;

end;


procedure dibujabolitas;
var i,col:integer;
begin
with FormaEdicion do
begin
col:=Trunc(PapelContinuo.Height/30);
for i:=0 to 32 do
begin
PapelContinuo.Canvas.Ellipse(5,i*col,10,i*col+5);
end;
for i:=0 to 32 do
begin
PapelContinuo.Canvas.Ellipse(PapelContinuo.Width-10,i*col,PapelContinuo.Width-5,i*col+5);
end;
end;
end;

procedure imprimelineas(x,y:integer;texto:string;tamo:integer;letra,color,formateo:String);
begin
Printer.Canvas.Font.Name:=letra;
Printer.Canvas.Font.Size:=tamo;
Printer.Canvas.Font.Color:=clBlack;
  if color='rojo' then
  Printer.Canvas.Font.Color:=clRed;
  if color='azul' then
  Printer.Canvas.Font.Color:=clBlue;
  if color='verde' then
  Printer.Canvas.Font.Color:=clGreen;
  if color='amarillo' then
  Printer.Canvas.Font.Color:=clYellow;
  if color='negro' then
  Printer.Canvas.Font.Color:=clBlack;

{  if formateo<>'' then
  begin
  Printer.Canvas.Font.Name:='Courier';
  end; }


Printer.Canvas.Textout(x*Trunc(Printer.PageWidth/80),y*Trunc(Printer.PageHeight/55),Texto);

end;


procedure leelineas;
type
dato=record
x:integer;
y:integer;
texto:String[125];
tamoletra:integer;
tipoletra:string[50];
color:String[8];
formato:String[25];
end;
var
arch:File of dato;
dat:dato;
i,ancho,largo,x,y:integer;
aux:string[50];
begin

if FileExists(leeruta+'\tempo\format.tmp') then
begin
Printer.BeginDoc;
AssignFile(arch,leeruta+'\tempo\format.tmp');
Reset(arch);
while not Eof(arch) do
begin
Read(arch,dat);
{ShoWMessage(dat.tipoletra);}
imprimelineas(dat.x,dat.y,dat.Texto,dat.tamoletra,dat.tipoletra,dat.color,dat.formato);
end;
CloseFile(arch);
end;


if FormaEdicion.Comentario.Visible=True then
if FormaEdicion.Comentario.Lines.Count>0 then
begin
with FormaEdicion do
begin
ancho:=Trunc(VistaPreliminar.Width/80);
largo:=Trunc(VistaPreliminar.Height/55);
x:=Trunc((Comentario.Left-VistaPreliminar.Left)/ancho);
y:=Trunc((Comentario.Top-VistaPreliminar.Top)/largo);
end;

for i:=0 to FormaEdicion.Comentario.Lines.Count-1 do
begin
imprimelineas(x,y+i,FormaEdicion.Comentario.Lines[i],10,'Arial','negro','');
end;
end;


Printer.EndDoc;
end;

{******************}
function ponecolor(colorletra:string):TColor;
begin

  if colorletra='rojo' then
  ponecolor:=clRed;
  if colorletra='azul' then
  ponecolor:=clBlue;
  if colorletra='verde' then
  ponecolor:=clGreen;
  if colorletra='amarillo' then
  ponecolor:=clYellow;
  if colorletra='negro' then
  ponecolor:=clBlack;

end;
{*****************}


procedure posiciona(x,y:integer;texto:string;tamo:integer;tipo,colorletra:string);
var
a,b:integer;
aux:string;
i:integer;
begin

{aux:=FormaEdicion.Hoja.Lines[y];
if aux='' then
aux:='                                                                                 ';}

{if length(aux)<x then
begin
for i:=length(aux)+1 to x do
 aux:=aux+' ';
end; }

{for i:=1 to length(texto) do
begin
if (x+i-1)<81 then
aux:=aux+texto[i];
end;
FormaEdicion.Hoja.lines[y]:=aux;

end;}

{for i:=1 to length(texto) do
begin
if (x+i-1)<80 then
aux[x+i-1]:=texto[i];
end;
FormaEdicion.Hoja.Lines[y]:=aux;
with formaedicion do
begin}

{****modo grafico******}
{  if color='rojo' then
  VistaPreliminar.Canvas.Pen.Color:=clRed;
  if color='azul' then
  VistaPreliminar.Canvas.Pen.Color:=clBlue;
  if color='verde' then
  VistaPreliminar.Canvas.Pen.Color:=clGreen;
  if color='amarillo' then
  VistaPreliminar.Canvas.Pen.Color:=clYellow;
  if color='negro' then
  VistaPreliminar.Canvas.Pen.Color:=clBlack;
 }
with formaedicion do
begin

VistaPreliminar.Canvas.Font.Color:=ponecolor(colorletra);
VistaPreliminar.Canvas.Font.Name:=tipo;
if BotonAumento.Tag=0 then
begin
if Trunc(tamo/3)>3 then
VistaPreliminar.Canvas.Font.Size:=Trunc(tamo/3)
else
VistaPreliminar.Canvas.Font.Size:=3;
end
else
VistaPreliminar.Canvas.Font.Size:=tamo-4;


x:=Trunc(x*(VistaPreliminar.Width/82));
y:=Trunc(y*(VistaPreliminar.Height/50));
VistaPreliminar.Canvas.TextOut(x,y,texto);

end;
end;


procedure leedatosarchivo;
type
dato=record
x:integer;
y:integer;
texto:String[125];
tamoletra:integer;
tipoletra:string[50];
color:String[8];
formato:String[25];
end;
var
arch:File of dato;
dat:dato;
i:integer;
begin
{for i:=1 to 54 do
FormaEdicion.Hoja.Lines.Add('');}

if FileExists(leeruta+'\tempo\format.tmp') then
begin
AssignFile(arch,leeruta+'\tempo\format.tmp');
Reset(arch);

FormaEdicion.VistaPreliminar.Canvas.Brush.Color:=clWhite;
FormaEdicion.VistaPreliminar.Canvas.Pen.Color:=clBlue;
FormaEdicion.VistaPreliminar.Canvas.Rectangle(0,0,FormaEdicion.VistaPreliminar.Width,FormaEdicion.VistaPreliminar.Height);


while not Eof(arch) do
begin
Read(arch,dat);
if (dat.x<80) and(dat.tamoletra<>-1) then
posiciona(dat.x,dat.y,dat.Texto,dat.tamoletra,dat.tipoletra,dat.color);
{ShowMessage(dat.tipoletra);}

if dat.tamoletra=-1 then
dibujabitmap(IntToStr(dat.x),IntToStr(dat.y),dat.Texto);

end;
CloseFile(arch);
end;

end;


procedure despliegaformato;
begin


end;



function checaerror(a,b,s:string;t:integer):Boolean;
var
valor,cod1,cod2:integer;
begin
checaerror:=True;
val(a,valor,cod1);
if cod1<>0 then
begin
 checaerror:=False;
 {MuestraMensaje('Error en el parametro 1 linea : '+IntToStr(t));}
end;
val(b,valor,cod2);
if cod2<>0 then
begin
 checaerror:=False;
  {MuestraMensaje('Error en el parametro 2 linea : '+IntToStr(t));}
end;

end;

function checaerrores(t:integer):Boolean;

var
linea,campo:String;
i,j,x,y,valor,cod:integer;

begin
with FormaEdicion do
begin
j:=0;
while j<=Codigo.Lines.Count-1 do
begin
linea:=Codigo.lines[j];
x:=0;y:=0;campo:='';
j:=j+1;
if linea[1]<>'&' then
if linea[1]='@' then
for i:=2 to length(linea) do
begin
if linea[i]<>',' then
campo:=campo+linea[i]
else
begin
val(campo,valor,cod);
if cod=0 then
begin
if (y=0)and (x<>0) then y:=StrToInt(campo);
if x=0 then
begin
 x:=StrToInt(campo);
 checaerrores:=True;

end;
end
else
begin
if linea[1]<>'&' then
begin
{MuestraMensaje('Error en la Linea  '+InttoStr(j));}
j:=Codigo.lines.Count;
checaerrores:=False;
end;
end;

campo:='';
end;

end;



{*************}


end;

end;
end;

procedure dibujahoja;
begin
with FormaEdicion do
begin
VistaPreliminar.Canvas.Brush.Color:=clWhite;
VistaPreliminar.Canvas.Rectangle(0,0,VistaPreliminar.Width,VistaPreliminar.Height);
end;
end;

procedure aumenta;
begin
with FormaEdicion do
begin
Marco.Width:=Marco.Width*BotonAumentar.Tag;
Marco.Height:=Marco.Height*BotonAumentar.Tag;
VistaPreliminar.Width:=VistaPreliminar.Width*BotonAumentar.Tag;
VistaPreliminar.Height:=VistaPreliminar.Height*BotonAumentar.Tag;

dibujahoja;
end;

end;



procedure pinta(px,py:integer;pcampo:String);

begin
{MuestraMensaje('x : '+IntToStr(px)+'y : '+IntToStr(py)+'  '+pcampo);}
{FormaEdicion.VistaPreliminar.Canvas.Rectangle(px,py,px+20,py+2);}

FormaEdicion.VistaPreliminar.Canvas.Font.Name:='Arial';
{FormaEdicion.VistaPreliminar.Canvas.Font.Color:=clred;}


FormaEdicion.VistaPreliminar.Canvas.TextOut(px*2,py*5,pcampo);

end;

procedure procesapalabra(linea:string;num,j:integer);
var
campo:String;
lin:String[4];
i,e:integer;
palabra,conta:Word;
palabras:array[1..6]of String;

x,y:String[6];
begin
i:=1;
x:='';y:='';campo:='';
conta:=1;
e:=0;
palabras[1]:='';palabras[2]:='';palabras[3]:='';palabras[4]:='';palabras[5]:='';palabras[6]:='';

{****separar la linea en palabras****}

{MuestraMensaje('procesar  '+linea);}

while (i<=length(linea))and (conta<=6) do
begin

if (linea[i]=',')or(linea[i]='(')or(linea[i]=')') then
conta:=conta+1
else
begin
if (linea[i]<>' ')and (linea[i]<>'@') then
palabras[conta]:=palabras[conta]+linea[i];
end;
i:=i+1;
end;

{***termina separacion*****}
case num of

1:begin
if linea<>'&' then
if linea<>'' then
if linea[1]='@' then
for i:=2 to length(linea) do
begin
if linea[i]<>',' then
campo:=campo+linea[i]
else
begin
if (y='')and (x<>'') then y:=campo;
if x='' then x:=campo;
campo:='';
end;

end;

if (linea<>'')and(linea[1]<>'&') then
if checaerror(x,y,campo,j) then
begin
pinta(StrToInt(x),StrToInt(y),campo);
e:=1;
end;
end;

2:begin
  {MuestraMensaje(palabras[1]);}
  if (palabras[1]='&iniciaciclo')or(palabras[1]='&terminaciclo') then
  {MuestraMensaje('ciclo valido linea '+inttostr(j));}
  e:=1;
  end;

3:begin
  {MuestraMensaje(palabras[1]);}
  if (palabras[1]='color')and ((palabras[2]='rojo')or (palabras[2]='azul')or (palabras[2]='verde')
  or (palabras[2]='amarillo')or (palabras[2]='negro') ) then
  begin
  e:=1;
{  MuestraMensaje(palabras[2]);}
  if palabras[2]='rojo' then  FormaEdicion.VistaPreliminar.Canvas.Font.color:=clRed;
  if palabras[2]='azul' then  FormaEdicion.VistaPreliminar.Canvas.Font.color:=clBlue;
  if palabras[2]='verde' then  FormaEdicion.VistaPreliminar.Canvas.Font.color:=clGreen;
  if palabras[2]='amarillo' then  FormaEdicion.VistaPreliminar.Canvas.Font.color:=clYellow;
  if palabras[2]='negro' then  FormaEdicion.VistaPreliminar.Canvas.Font.color:=clBlack;

  {FormaEdicion.VistaPreliminar.Canvas.Font.Color:=clred;}
  {MuestraMensaje('ciclo valido linea '+inttostr(j));}
  end
  else
   {MuestraMensaje('error en linea '+inttostr(j));}

  end;

4:begin
  {MuestraMensaje(palabras[1]);}
  if (palabras[1]='traduce') then
  begin
  e:=1;

  {MuestraMensaje(' valida linea '+inttostr(j));}
  end
  else
  {MuestraMensaje('error en linea '+inttostr(j));}

  end;

5:begin
  {MuestraMensaje(palabras[1]);}
  if (palabras[1]='cuadro') then
  begin
  FormaEdicion.VistaPreliminar.Canvas.Rectangle(StrToInt(palabras[2]),StrToInt(palabras[3]),StrToInt(palabras[4]),StrToInt(palabras[5]));
  e:=1;
  {MuestraMensaje(' valida linea '+inttostr(j));}
  end
  else
  {MuestraMensaje('error en linea '+inttostr(j));}

  end;

6:begin
{  MuestraMensaje(palabras[1]);}
  if (palabras[1]='tamaņo') then
  begin
  e:=1;
  FormaEdicion.VistaPreliminar.Canvas.Font.Size:=(StrToInt(palabras[2])div 3);
{  MuestraMensaje(' valida linea '+inttostr(j));}
  end
  else
  {MuestraMensaje('error en linea '+inttostr(j));}

  end;
7:begin
{  MuestraMensaje(palabras[1]);}
  if (palabras[1]='tipo') then
  begin
  e:=1;
{  MuestraMensaje(' valida linea '+inttostr(j));}
  end
  else
  {MuestraMensaje('error en linea '+inttostr(j));}

  end;
8:begin
{  MuestraMensaje(palabras[1]);}
  if (palabras[1]='logo') then
  begin
  dibujabitmap(palabras[2],palabras[3],palabras[6]);
    e:=1;
{  MuestraMensaje(' valida linea '+inttostr(j));}
  end
  else
  {MuestraMensaje('error en linea '+inttostr(j));}
  end;


end;
{termina case}

{if e=0 then dibujahoja;}



end;


procedure preliminar;
var
linea,campo:String;
lin:String[4];
i,j,e:integer;
palabra:Word;
x,y:String[6];
aux:string;

begin
with FormaEdicion do
begin
dibujahoja;

for j:=0 to Codigo.lines.Count-1 do
begin

aux:=Codigo.lines[j];
FormaEdicion.Codigo.Lines.Delete(j);
 with FormaEdicion.Codigo.SelAttributes do
 begin
    Color := clBlack;
 end;

FormaEdicion.Codigo.Lines.Insert(j,aux);

 with FormaEdicion.Codigo.SelAttributes do
 begin
    Color := clBlack;
 end;

end;


for j:=0 to Codigo.lines.Count-1 do
begin

if Codigo.lines[j]<>'' then
lineavalida(Codigo.lines[j],j);
linea:=Codigo.lines[j];
x:='';y:='';campo:='';
e:=0;
lin:=linea;

if lin[1]='@' then palabra:=1;
if lin[1]='&' then palabra:=2;
if lin='colo' then palabra:=3;
if lin='trad' then palabra:=4;
if lin='cuad' then palabra:=5;
if lin='tama' then palabra:=6;
if lin='tipo' then palabra:=7;
if lin='logo' then palabra:=8;

case palabra of
1:  procesapalabra(linea,1,j);
3:  procesapalabra(linea,3,j);
4:  procesapalabra(linea,4,j);
5:  procesapalabra(linea,5,j);
6:  procesapalabra(linea,6,j);
7:  procesapalabra(linea,7,j);
8:  procesapalabra(linea,8,j);



end;
      
end;

end;
end;


{************************}

procedure activapreliminar;
begin
with formaEdicion do
begin
PanelEdicion.Visible:=False;
PanelPresentacion.Visible:=True;

Codigo.Visible:=False;
Marco.Visible:=True;
PapelContinuo.Visible:=True;
VistaPreliminar.Visible:=True;

Hoja.Clear;
{Hoja.Visible:=True;}
Hoja.Width:=250;
Hoja.Height:=330;
Hoja.Font.Size:=4;
Hoja.Clear;
{dibujahoja;
despliegaformato;
leedatosarchivo;}
end;
end;



procedure activadiseno;
begin
with formaEdicion do
begin
Hoja.Visible:=False;
PanelPresentacion.Visible:=False;
PanelEdicion.Visible:=True;
Codigo.Visible:=True;
Marco.Visible:=True;

{dibujahoja;}
end;

end;

procedure visualizadatos;
begin
with FormaEdicion do
begin
{Hoja.Width:=250;
Hoja.Height:=330;
PanelHoja.Width:=250+4;
panelHoja.Height:=330+4;

PapelContinuo.Width:=Hoja.Width+20;
PapelContinuo.Height:=Hoja.Height;
Hoja.Font.Size:=4;
Hoja.Clear;}
FormaEdicion.VertScrollBar.Visible:=False;
{dibujabolitas;}
VistaPreliminar.Canvas.Brush.Color:=clWhite;
VistaPreliminar.Canvas.Rectangle(0,0,VistaPreliminar.Width,VistaPreliminar.Height);
leedatosarchivo;
end;

end;




{**************************}



procedure TFormaEdicion.CodigoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);

  var
  aux:string[1];
    i,j,k:integer;
    cierto:Boolean;

begin

{aux:=Codigo.lines[Codigo.lines.Count-1];}
{Codigo.SendToBack;
Codigo.SelLength:=20; }
{Codigo.}
{lineas.Caption:=IntToStr(Codigo.SelLength);}
lineas.Caption:=IntToStr(calculalinea(Codigo.SelStart));

{MuestraMensaje(IntToStr(Texto.GetSelLength));}
Posicion.Caption:=IntToStr(Codigo.SelStart);



if (Ord(Char(key))=13) then
begin
{preliminar;}
end;

if key=113 then
begin
posicionacampos;
ponecampos;
Campos.Visible:=True;
Campos.SetFocus;
end;
end;

procedure TFormaEdicion.Abrir1Click(Sender: TObject);
begin
if AbreFormato.Execute then
begin
Codigo.lines.LoadFromFile(AbreFormato.FileName);
dibujahoja;
preliminar;
end;
end;

procedure TFormaEdicion.Guardar1Click(Sender: TObject);
var i:integer;
    arch:TextFile;
begin
if GuardaFormato.Execute then
begin
AssignFile(arch,GuardaFormato.FileName);
Rewrite(arch);
for i:=0 to Codigo.lines.Count-1 do
begin
Writeln(arch,Codigo.lines[i]);
end;
CloseFile(arch);
{Codigo.lines.SaveToFile(GuardaFormato.FileName);}
dibujahoja;
preliminar;
end;
end;

procedure TFormaEdicion.FormActivate(Sender: TObject);
begin

if Hoja.Tag=1 then
begin
AumentaVistaPreliminar(3);
PanelEdicion.Visible:=True;
PanelPresentacion.Visible:=False;
PanelCodigo.Visible:=True;
PanelHoja.Visible:=False;


end
else
begin

PanelPresentacion.Visible:=True;
PanelEdicion.Visible:=False;
PanelCodigo.Visible:=False;
{PanelHoja.Visible:=True;}
PanelHoja.Visible:=False;
{visualizadatos;}
end;

{if FormaEdicion.Tag=0 then
begin
Hoja.Visible:=False;
dibujahoja;
end
else
begin
Hoja.Visible:=True;
Hoja.Width:=250;
Hoja.Height:=330;
Hoja.Font.Size:=4;
Hoja.Clear;
dibujahoja;
despliegaformato;
leedatosarchivo;
end;
 }
end;

procedure TFormaEdicion.Nuevo1Click(Sender: TObject);
begin
dibujahoja;
Codigo.lines.Clear;
end;

procedure TFormaEdicion.BotonCortarClick(Sender: TObject);
begin
{Codigo.CutToClipboard;}
end;

procedure TFormaEdicion.BotonCopiarClick(Sender: TObject);
begin
{Codigo.CopyToClipboard;}
end;

procedure TFormaEdicion.BotonPegarClick(Sender: TObject);
begin
{Codigo.PasteFromClipboard;}
end;

procedure TFormaEdicion.BotonCamposClick(Sender: TObject);
begin

FormaCampos.Show;
end;

procedure TFormaEdicion.CodigoMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

lineas.Caption:=IntToStr(calculalinea(Codigo.SelStart));

{MuestraMensaje(IntToStr(Texto.GetSelLength));}
Posicion.Caption:=IntToStr(Codigo.SelStart);


if Button=mbRight then
if Campos.Visible=True then
Campos.Visible:=False
else
Campos.Visible:=True;
if Button=mbLeft then
Campos.Visible:=False

end;

procedure TFormaEdicion.Compilar1Click(Sender: TObject);
begin
preliminar;
end;


procedure TFormaEdicion.BotonAumentoClick(Sender: TObject);
begin

{Hoja.Width:=570;
Hoja.Height:=800;
PanelHoja.Width:=570+4;
panelHoja.Height:=800+4;

PapelContinuo.Width:=Hoja.Width+20;
PapelContinuo.Height:=Hoja.Height+20;
Hoja.Font.Size:=8;
Hoja.Clear;}
BotonAumento.Tag:=1;
BotonDisminuye.Tag:=0;
FormaEdicion.VertScrollBar.Visible:=True;
{leedatosarchivo;}
AumentaVistaPreliminar(1);


visualizadatos;
{dibujabolitas;}

end;


procedure TFormaEdicion.BotonDisminuyeClick(Sender: TObject);
begin
{HorzScrollBar.Visible:=False;
VertScrollBar.Visible:=False;  }
{visualizadatos;}
BotonAumento.Tag:=0;
BotonDisminuye.Tag:=1;
AumentaVistaPreliminar(0);
visualizadatos;
end;

procedure TFormaEdicion.BotonImprimeClick(Sender: TObject);
begin
if ImpresoraLista then
begin
leelineas;
end
else
MuestraMensaje('La Impresora No Se Encuentra Lista...');
end;


procedure TFormaEdicion.PapelContinuoPaint(Sender: TObject);
begin
{dibujabolitas;}
end;



procedure TFormaEdicion.CamposDblClick(Sender: TObject);
var
trunca:string[5];
begin
trunca:=Campos.Items[Campos.ItemIndex];
if StrToInt(FormaEdicion.lineas.Caption)-1>=0 then
FormaEdicion.Codigo.Lines[StrToInt(FormaEdicion.lineas.Caption)-1]:=FormaEdicion.Codigo.Lines[StrToInt(FormaEdicion.lineas.Caption)-1]+trunca;
Campos.Visible:=False;
Codigo.Setfocus;
end;

procedure TFormaEdicion.CamposKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
trunca:string[5];
begin

if key=13 then
begin
trunca:=Campos.Items[Campos.ItemIndex];
if StrToInt(FormaEdicion.lineas.Caption)-1>=0 then
FormaEdicion.Codigo.Lines[StrToInt(FormaEdicion.lineas.Caption)-1]:=FormaEdicion.Codigo.Lines[StrToInt(FormaEdicion.lineas.Caption)-1]+trunca;
Campos.Visible:=False;
Codigo.Setfocus;
end;
end;

procedure TFormaEdicion.VistaPreliminarPaint(Sender: TObject);
begin
{Hoja.Clear;
VistaPreliminar.Canvas.Brush.Color:=clWhite;
VistaPreliminar.Canvas.Rectangle(0,0,VistaPreliminar.Width,VistaPreliminar.Height);}
{leedatosarchivo;}
visualizadatos;
end;


procedure TFormaEdicion.BotonNuevoMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonNuevo.Left,FormaEdicion.Top+BotonNuevo.Top+BotonNuevo.Height,76);
end;

procedure TFormaEdicion.BotonAbrirMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonNuevo.Left,FormaEdicion.Top+BotonAbrir.Top+BotonAbrir.Height,77);
end;

procedure TFormaEdicion.BotonGuardarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonGuardar.Left,FormaEdicion.Top+BotonGuardar.Top+BotonGuardar.Height,78);
end;

procedure TFormaEdicion.BotonCopiarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonCopiar.Left,FormaEdicion.Top+BotonCopiar.Top+BotonCopiar.Height,79);
end;

procedure TFormaEdicion.BotonCortarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonCortar.Left,FormaEdicion.Top+BotonCortar.Top+BotonCortar.Height,80);
end;

procedure TFormaEdicion.BotonPegarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonPegar.Left,FormaEdicion.Top+BotonPegar.Top+BotonPegar.Height,81);
end;

procedure TFormaEdicion.BotonAumentarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonAumentar.Left,FormaEdicion.Top+BotonAumentar.Top+BotonAumentar.Height,82);
end;

procedure TFormaEdicion.BotonCamposMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonCampos.Left,FormaEdicion.Top+BotonCampos.Top+BotonCampos.Height,83);
end;

procedure TFormaEdicion.BotonSintaxisMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonSintaxis.Left,FormaEdicion.Top+BotonSintaxis.Top+BotonSintaxis.Height,84);
end;

procedure TFormaEdicion.BotonAumentoMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonAumento.Left,FormaEdicion.Top+BotonAumento.Top+BotonAumento.Height,85);
end;

procedure TFormaEdicion.BotonDisminuyeMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonDisminuye.Left,FormaEdicion.Top+BotonDisminuye.Top+BotonDisminuye.Height,86);
end;

procedure TFormaEdicion.BotonImprimeMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
describir(Button,FormaEdicion.Left+BotonImprime.Left,FormaEdicion.Top+BotonImprime.Top+BotonImprime.Height,87);
end;

procedure TFormaEdicion.Salir1Click(Sender: TObject);
begin
Close;

end;

procedure TFormaEdicion.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
Salir1.Tag:=1000;
end;








procedure TFormaEdicion.PanelHojaResize(Sender: TObject);
begin
{visualizadatos;}
end;

procedure TFormaEdicion.VistaPreliminarMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if Button=mbRight then
Comentario.Visible:=True;
end;

procedure TFormaEdicion.ComentarioMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
SendMessage(Comentario.Handle, WM_SYSCOMMAND, $F012, 0);
Comentario.Hint:=PosicionComentario(Comentario.Top,Comentario.Left);
end;

procedure TFormaEdicion.BotonNotasClick(Sender: TObject);
begin
if Comentario.Visible=True then
Comentario.Visible:=False
else
Comentario.Visible:=True;
end;


procedure TFormaEdicion.BotonSalirClick(Sender: TObject);
begin
Close;
end;

end.
