home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / dos / prg / pas / ktools / source / ogenview.pas < prev    next >
Pascal/Delphi Source File  |  1994-11-13  |  14KB  |  649 lines

  1. Unit OGenView;
  2. { objet générique pour applications en POO avec gestion des évènements }
  3. { K.B. octobre 1994 }
  4.  
  5. {$IFDEF debug}
  6.  {$A+,B-,D+,E-,F-,I+,L+,N-,R+,S+,V-,W+,X+}
  7. {$ELSE}
  8.  {$A+,B-,D-,E-,F-,I+,L-,N-,R-,S-,V-,W+,X+}
  9. {$ENDIF}
  10.  
  11. INTERFACE
  12.  
  13. Uses UDrivers;
  14.  
  15. Const
  16.  { Constantes d'état des objets }
  17.  stVisible    = $0001;
  18.  stSelectable = $0002;
  19.  stSelected   = $0004;
  20.  
  21.  { Constantes des commandes standards }
  22.  cmGeneric = 000;
  23.  cmOK      = cmGeneric+01;
  24.  cmAnnule  = cmGeneric+02;
  25.  cmNew     = cmGeneric+03;
  26.  cmOpen    = cmGeneric+04;
  27.  cmSave    = cmGeneric+05;
  28.  cmSaveAs  = cmGeneric+06;
  29.  cmOui     = cmGeneric+07;
  30.  cmNon     = cmGeneric+08;
  31.  cmClose   = cmGeneric+09;
  32.  cmQuit    = cmGeneric+10;
  33.  
  34.  { Constantes des codes de sortie de Exec }
  35.  exOK         = 1;
  36.  exAnnule     = 2;
  37.  exOui        = 3;
  38.  exNon        = 4;
  39.  
  40.  { Constantes des codes d'erreur }
  41.  erMemoire    = 1;
  42.  erOpenFile   = 2;
  43.  erFormatFile = 3;
  44.  erReadFile   = 4;
  45.  erWriteFile  = 5;
  46.  erCloseFile  = 6;
  47.  erChild      = 7;
  48.  
  49. Type
  50.  PGenView  = ^TGenView;
  51.  TGenView  = Object
  52.   Ident       : String[10];     { identificateur }
  53.   Prec,Suiv   : PGenView;       { éléments suivant et précédent
  54.                                   dans le groupe parent }
  55.   Owner       : PGenView;       { élément parent }
  56.   Child       : PGenView;       { élément enfant }
  57.   Origin      : TPoint;         { coin supérieur gauche }
  58.   Size        : TPoint;         { taille }
  59.   Etat        : Word;           { état (visible, actif, etc..) }
  60.   ErrorFlag   : Byte;           { indicateur d'erreur }
  61.   ExitCode    : Byte;           { code de sortie }
  62.   MouseInView : Boolean;     { présence de la souris }
  63.   MouseAspect : PMouseAspect;   { aspect de la souris }
  64.   Constructor Init(X,Y,L,H:Integer);
  65.   { initialise les dimensions }
  66.   Destructor Done; virtual;
  67.   { retire l'élément du groupe parent et le détruit }
  68.   Procedure HandleEvent(Var Event : TEvent); virtual;
  69.   { gestion des évènements }
  70.   Procedure Draw; virtual;
  71.   { affichage du fond puis des enfants }
  72.   Procedure ReDraw;
  73.   { affichage des enfants }
  74.   Procedure BackGround; virtual;
  75.   { dessine le fond }
  76.   Procedure MakeGlobal(Src:TPoint; Var Dst:TPoint);
  77.   { conversion de coordonnées }
  78.   Procedure MakeLocal(Src:TPoint; Var Dst:TPoint);
  79.   { conversion de coordonnées }
  80.   Procedure Insert(W:PGenView);
  81.   { ajoute un élément dans le groupe Enfant }
  82.   Function Find(Id:String):PGenView;
  83.   { cherche un enfant à partir de son identificateur }
  84.   Function FindSelect:PGenView;
  85.   { cherche l'enfant sélectionné }
  86.   Procedure DrawBegin; virtual;
  87.   { appelée avant un affichage : masque la souris }
  88.   Procedure DrawEnd; virtual;
  89.   { appelée après un affichage : rétablit la souris }
  90.   Procedure Decroche;
  91.   { retire l'élément du parent }
  92.   Function Suivant : PGenView;
  93.   { élément suivant du parent, après le dernier on renvoie le premier }
  94.   Function Precedent : PGenView;
  95.   { élément précédent du parent, avant le premier on renvoie le dernier }
  96.   Procedure SetData(Var Rec); virtual;
  97.   { initialisation des données }
  98.   Procedure GetData(Var Rec); virtual;
  99.   { récupération des données }
  100.   Function DataSize:Integer; virtual;
  101.   { taille des données utilisées par SetData et GetData }
  102.   Procedure GetEvent(Var Event:TEvent); virtual;
  103.   { pour recueillir les évènements }
  104.   Procedure SetCommand(Command:Word);
  105.   { envoie un évènement commande }
  106.   Procedure SauveEcran; virtual;
  107.   { sauve la partie d'écran recouverte }
  108.   Procedure RestitueEcran; virtual;
  109.   { restitue la partie d'écran sauvée avec SauveEcran }
  110.   Function Exec : Byte;
  111.   { boucle d'exécution }
  112.   Procedure Select; virtual;
  113.   { fait passer à l'état sélectionné }
  114.   Procedure UnSelect; virtual;
  115.   { quitte l'état sélectionné }
  116.   Function Isvalid : Boolean; virtual;
  117.   { indique une erreur }
  118.   Function GetErrorMsg : String; virtual;
  119.   { renvoie un message d'erreur }
  120.  End;
  121.  
  122.  PGenApp = ^TGenApp;
  123.  TGenApp = Object(TGenView)
  124.   Constructor Init;
  125.   { initialise les drivers écran, clavier,souris,...}
  126.   Destructor Done; virtual;
  127.   { pour quitter l'application }
  128.   Procedure VideoInit; virtual;
  129.   { initialise le mode vidéo choisi }
  130.   Procedure VideoDone; virtual;
  131.   { quitte le mode vidéo choisi }
  132.   Procedure HandleEvent(Var Event : TEvent); virtual;
  133.   { gestion des évènements : commande cmQuit et touche AltX }
  134.   Procedure GetEvent(Var Event : TEvent); virtual;
  135.   { lecture des évènements }
  136.   Procedure Quit; virtual;
  137.   Procedure TempsMort; virtual;
  138.   End;
  139.  
  140. Var
  141.  Application : PGenView;   { application }
  142.  
  143. Function ExecView(W:PGenView):Byte;
  144.  
  145. IMPLEMENTATION
  146.  
  147. Type
  148.  { zone de transfert des données }
  149.  PDataArea = ^TDataArea;
  150.  TDataArea = Array[0..60000] of Byte;
  151.  
  152. Var MouseView : PGenView;     { Objet qui contient la souris }
  153.  
  154. { objet TGenView }
  155.  
  156. Constructor TGenView.Init(X,Y,L,H:Integer);
  157. Begin
  158.  Ident:='GENVIEW';
  159.  Prec:=nil;
  160.  Suiv:=nil;
  161.  Owner:=nil;
  162.  Child:=nil;
  163.  Origin.X:=X;
  164.  Origin.Y:=Y;
  165.  Size.X:=L;
  166.  Size.Y:=H;
  167.  ExitCode:=0;
  168.  ErrorFlag:=0;
  169.  Etat:=0;
  170.  MouseInView:=false;
  171.  MouseAspect:=nil;
  172. End;
  173.  
  174. Destructor TGenView.Done;
  175. Var W,Tmp : PGenView;
  176. Begin
  177.  { destruction des enfants }
  178.  W:=Child;
  179.  while (W<>nil) do
  180.   begin
  181.    Tmp:=W^.Suiv;
  182.    Dispose(W,Done);
  183.    W:=Tmp;
  184.   end;
  185.  { sortie du parent }
  186.  Decroche;
  187. End;
  188.  
  189. Procedure TGenView.MakeGlobal(Src:TPoint; Var Dst:TPoint);
  190. Var W : PGenView;
  191. Begin
  192.  Dst:=Src;
  193.  W:=Owner;
  194.  while (W<>nil) do
  195.   begin
  196.    Dst.X:=Dst.X+W^.Origin.X;
  197.    Dst.Y:=Dst.Y+W^.Origin.Y;
  198.    W:=W^.Owner;
  199.   end;
  200. End;
  201.  
  202. Procedure TGenView.MakeLocal(Src:TPoint; Var Dst:TPoint);
  203. Var W : PGenView;
  204. Begin
  205.  Dst:=Src;
  206.  W:=Owner;
  207.  while (W<>nil) do
  208.   begin
  209.    Dst.X:=Dst.X-W^.Origin.X;
  210.    Dst.Y:=Dst.Y-W^.Origin.Y;
  211.    W:=W^.Owner;
  212.   end;
  213. End;
  214.  
  215. Procedure TGenView.HandleEvent(Var Event : TEvent);
  216. Var P : TPoint;
  217.     W : PGenView;
  218. Begin
  219.  if Event.What=evKeyDown
  220.     then if Event.KeyCode=AltX
  221.             then begin
  222.                   ExitCode:=exAnnule;
  223.                   SetEvent(Event);
  224.                   exit;
  225.                  end;
  226.  { gestion du curseur souris et du champ MouseInView }
  227.  MakeGlobal(Origin,P);
  228.  if (Event.What = evMouseMove) and (Etat and stVisible <> 0)
  229.     then if (Event.Where.X>=P.X) and
  230.             (Event.Where.X<=P.X+Size.X-1) and
  231.             (Event.Where.Y>=P.Y) and
  232.             (Event.Where.Y<=P.Y+Size.Y-1)
  233.             then begin
  234.                   if not MouseInView
  235.                      then begin
  236.                            MouseInView:=true;
  237.                            ChangeMouseAspect(MouseAspect);
  238.                            MouseView:=@Self;
  239.                           end;
  240.                  end
  241.             else begin
  242.                   if MouseInView
  243.                      then begin
  244.                            MouseInView:=false;
  245.                            if MouseView=@Self
  246.                               then begin
  247.                                     MouseView:=Owner;
  248.                                     ChangeMouseAspect(Owner^.MouseAspect);
  249.                                    end;
  250.                           end;
  251.                  end;
  252.  case Event.What of
  253.   evNothing : exit;
  254.   evKeyDown,evCommand,evMouseAuto:
  255.  { gestion des évènements claviers et commandes : envoi à l'élément
  256.    sélectionné }
  257.        begin
  258.         W:=FindSelect;
  259.         if W<>nil
  260.            then W^.HandleEvent(Event);
  261.        end;
  262.   { les autres évènements sont envoyés à tous les enfants }
  263.   else begin
  264.         W:=Child;
  265.         while ((W<>nil) and (Event.What<>evNothing)) do
  266.          begin
  267.           W^.HandleEvent(Event);
  268.           W:=W^.Suiv;
  269.          end;
  270.        end;
  271.   end;
  272. End;
  273.  
  274. Procedure TGenView.Draw;
  275. { affichage complet : fond et enfants }
  276. Begin
  277.  Etat:=Etat or stVisible;
  278.  BackGround;
  279.  Redraw;
  280. End;
  281.  
  282. Procedure TGenView.ReDraw;
  283. { affichage des enfants }
  284. Var W : PGenView;
  285. Begin
  286.  W:=Child;
  287.  while (W<>nil) do
  288.   begin
  289.    W^.Draw;
  290.    W:=W^.Suiv;
  291.   end;
  292. End;
  293.  
  294. Procedure TGenView.BackGround;
  295. { affichage du fond }
  296. Begin
  297. End;
  298.  
  299. Procedure TGenView.Decroche;
  300. Begin
  301.  if Owner<>nil
  302.     then if Owner^.Child=@Self
  303.             then Owner^.Child:=Suiv;
  304.  if (Suiv<>nil)
  305.     then Suiv^.Prec:=Prec;
  306.  if (Prec<>nil)
  307.     then Prec^.Suiv:=Suiv;
  308.  Prec:=nil;
  309.  Suiv:=nil;
  310.  Owner:=nil;
  311. End;
  312.  
  313. Procedure TGenView.Insert(W : PGenView);
  314. Var Tmp:PGenView;
  315. Begin
  316.  if W=nil
  317.     then begin
  318.           ErrorFlag:=erMemoire;
  319.           exit;
  320.          end;
  321.  Tmp:=W;
  322.  { tous les enfants frères ont le même parent }
  323.  while (Tmp<>nil) do
  324.   begin
  325.    Tmp^.Owner:=@Self;
  326.    Tmp:=Tmp^.Suiv;
  327.   end;
  328.  if (Child=nil)
  329.     then Child:=W
  330.     else begin
  331.           Tmp:=Child;
  332.           { chercher le dernier enfant }
  333.           while (Tmp^.Suiv<>nil) do Tmp:=Tmp^.Suiv;
  334.           W^.Prec:=Tmp;
  335.           Tmp^.Suiv:=W;
  336.          end;
  337.  if not W^.IsValid
  338.     then ErrorFlag:=erChild;
  339. End;
  340.  
  341. Function TGenView.Suivant : PGenView;
  342. { élément suivant du parent, après le dernier on renvoie le premier }
  343. Var W : PGenView;
  344. Begin
  345.  W:=Suiv;
  346.  if W=nil
  347.     then begin
  348.           W:=@Self;
  349.           while W^.Prec<>nil do W:=W^.Prec;
  350.          end;
  351.  Suivant:=W;
  352. End;
  353.  
  354. Function TGenView.Precedent : PGenView;
  355. { élément précédent du parent, avant le premier on renvoie le dernier }
  356. Var W : PGenView;
  357. Begin
  358.  W:=Prec;
  359.  if W=nil
  360.     then begin
  361.           W:=@Self;
  362.           while W^.Suiv<>nil do W:=W^.Suiv;
  363.          end;
  364.  Precedent:=W;
  365. End;
  366.  
  367. Procedure TGenView.SetData(Var Rec);
  368. Var Pos : Integer;
  369.     W   : PGenView;
  370.     Tbl : PDataArea;
  371. Begin
  372.  Tbl:=@Rec;
  373.  Pos:=0;
  374.  W:=Child;
  375.  while (W<>nil) do
  376.   begin
  377.    W^.SetData(Tbl^[Pos]);
  378.    Pos:=Pos+W^.DataSize;
  379.    W:=W^.Suiv;
  380.   end;
  381. End;
  382.  
  383. Procedure TGenView.GetData(Var Rec);
  384. Var Pos : Integer;
  385.     W   : PGenView;
  386.     Tbl : PDataArea;
  387. Begin
  388.  Tbl:=@Rec;
  389.  Pos:=0;
  390.  W:=Child;
  391.  while (W<>nil) do
  392.   begin
  393.    W^.GetData(Tbl^[Pos]);
  394.    Pos:=Pos+W^.DataSize;
  395.    W:=W^.Suiv;
  396.   end;
  397. End;
  398.  
  399. Function TGenView.DataSize : Integer;
  400. Var Result : Integer;
  401.     W      : PGenView;
  402. Begin
  403.  Result:=0;
  404.  W:=Child;
  405.  while (W<>nil) do
  406.   begin
  407.    Result:=Result+W^.DataSize;
  408.    W:=W^.Suiv;
  409.   end;
  410.  DataSize:=Result;
  411. End;
  412.  
  413. Function TGenView.Isvalid:Boolean;
  414. Var W:PGenView;
  415.     ok:Boolean;
  416. Begin
  417.  ok:=ErrorFlag=0;
  418.  W:=Child;
  419.  while ok and (W<>nil) do
  420.   begin
  421.    ok:=W^.IsValid;
  422.    W:=W^.Suiv;
  423.   end;
  424.  IsValid:=ok;
  425. End;
  426.  
  427. Function TGenView.GetErrorMsg:String;
  428. Var W:PGenView;
  429.     S:String;
  430. Begin
  431.  S:='';
  432.  W:=Child;
  433.  while (W<>nil) and (S='') do
  434.   begin
  435.    S:=W^.GetErrorMsg;
  436.    W:=W^.Suiv;
  437.   end;
  438.  GetErrorMsg:=S;
  439. End;
  440.  
  441. Procedure TGenView.GetEvent(Var Event : TEvent);
  442. Begin
  443.  if Owner<>nil
  444.     then Owner^.GetEvent(Event)
  445.     else ReadEvent(Event);
  446. End;
  447.  
  448. Procedure TGenView.SetCommand(Command : Word);
  449. Var Event : TEvent;
  450. Begin
  451.  Event.What:=evCommand;
  452.  Event.Command:=Command;
  453.  Event.InfoPtr:=@self;
  454.  SetEvent(Event);
  455. End;
  456.  
  457. Procedure TGenView.SauveEcran;
  458. Begin
  459. End;
  460.  
  461. Procedure TGenView.RestitueEcran;
  462. Begin
  463. End;
  464.  
  465. Function TGenView.Exec : Byte;
  466. Var Event : TEvent;
  467.     W     : PGenView;
  468. Begin
  469.  if not IsValid
  470.     then begin
  471.           Exec:=$FF;
  472.           exit;
  473.          end;
  474.  SauveEcran;
  475.  if Owner<>nil
  476.     then W:=Owner^.FindSelect
  477.     else W:=nil;
  478.  Select;
  479.  Draw;
  480.  { mise à jour du pointeur souris }
  481.  Event.What:=evMouseMove;
  482.  Event.Where.X:=MouseX;
  483.  Event.Where.Y:=MouseY;
  484.  HandleEvent(Event);
  485.  { boucle de gestion des évènements }
  486.  repeat
  487.   GetEvent(Event);
  488.   if Event.What<>evNothing
  489.      then HandleEvent(Event);
  490.  until ExitCode<>0;
  491.  RestitueEcran;
  492.  if W<>nil
  493.     then W^.Select;
  494.  Exec:=ExitCode;
  495. End;
  496.  
  497. Procedure TGenView.DrawBegin;
  498. Var P : TPoint;
  499. Begin
  500.  MakeGlobal(Origin,P);
  501.  MouseClipON(P.X,P.Y,P.X+Size.X,P.Y+Size.Y);
  502. End;
  503.  
  504. Procedure TGenView.DrawEnd;
  505. Begin
  506.  MouseClipOFF;
  507. End;
  508.  
  509. Procedure TGenView.Select;
  510. Var W  : PGenView;
  511.     ok : Boolean;
  512. Begin
  513.  { désélectionner le frère sélectionné }
  514.  if Owner<>nil
  515.     then begin
  516.           W:=Owner^.Child;
  517.           ok:=false;
  518.           while (W<>nil) and not ok do
  519.            begin
  520.             if W^.Etat and stSelected <> 0
  521.                then begin
  522.                      W^.UnSelect;
  523.                      ok:=true;
  524.                     end;
  525.             W:=W^.Suiv;
  526.            end;
  527.          end;
  528.  if Etat and stSelectable <> 0
  529.     then Etat:=Etat or stSelected;
  530. End;
  531.  
  532. Procedure TGenView.UnSelect;
  533. Begin
  534.  if Etat and stSelected <> 0
  535.     then Etat:=Etat xor stSelected;
  536. End;
  537.  
  538. Function TGenView.FindSelect:PGenView;
  539. Var W : PGenView;
  540.     ok:Boolean;
  541. Begin
  542.  ok:=false;
  543.  W:=Child;
  544.  while (W<>nil) and not ok do
  545.   begin
  546.    if W^.Etat and stSelected <> 0
  547.       then ok:=true
  548.       else W:=W^.suiv;
  549.   end;
  550.  if ok
  551.     then FindSelect:=W
  552.     else FindSelect:=nil;
  553. End;
  554.  
  555. Function TGenView.Find(Id:String):PGenView;
  556. Var W : PGenView;
  557.     ok:Boolean;
  558. Begin
  559.  ok:=false;
  560.  W:=Child;
  561.  while (W<>nil) and not ok do
  562.   begin
  563.    if W^.Ident=Id
  564.       then ok:=true
  565.       else W:=W^.suiv;
  566.   end;
  567.  if ok
  568.     then Find:=W
  569.     else Find:=nil;
  570. End;
  571.  
  572. Function ExecView(W:PGenView):Byte;
  573. Var result:Byte;
  574. Begin
  575.  Application^.Insert(W);
  576.  result:=W^.Exec;
  577.  Dispose(W,Done);
  578.  ExecView:=result;
  579. End;
  580.  
  581. { objet TGenApp }
  582.  
  583. Constructor TGenApp.Init;
  584. Begin
  585.  TGenView.Init(0,0,1,1);
  586.  Etat:=stSelectable+stSelected;
  587.  VideoInit;
  588.  DriversInit;
  589.  MouseView:=nil;
  590.  Ident:='GENAPP';
  591.  Application:=@Self;
  592. End;
  593.  
  594. Destructor TGenApp.Done;
  595. Begin
  596.  TGenView.Done;
  597.  DriversDone;
  598.  VideoDone;
  599. End;
  600.  
  601. Procedure TGenApp.TempsMort;
  602. Begin
  603. End;
  604.  
  605. Procedure TGenApp.VideoInit;
  606. Begin
  607. End;
  608.  
  609. Procedure TGenApp.VideoDone;
  610. Begin
  611. End;
  612.  
  613. Procedure TGenApp.GetEvent(Var Event:TEvent);
  614. Begin
  615.  ReadEvent(Event);
  616.  if Event.What=evNothing
  617.     then TempsMort;
  618. End;
  619.  
  620. Procedure TGenApp.Quit;
  621. Begin
  622.  ExitCode:=exAnnule;
  623. End;
  624.  
  625. Procedure TGenApp.HandleEvent(Var Event : TEvent);
  626. Begin
  627.  if Event.What=evKeyDown
  628.     then if Event.KeyCode=AltX
  629.             then begin
  630.                   SetCommand(cmQuit);
  631.                   Event.What:=evNothing;
  632.                  end;
  633.  TGenView.HandleEvent(Event);
  634.  case Event.What of
  635.   evCommand:
  636.    case Event.Command of
  637.      cmQuit: Quit;
  638.      else Exit;
  639.    end;
  640.   else exit;
  641.   end;
  642.  Event.What:=evNothing;
  643. End;
  644.  
  645. END.
  646.  
  647. {                        Fin du Fichier OGenView.Pas                        }
  648.  
  649.