Készítsünk Turbo Vision segítségével szótár programot.

 

Program Szotar5;

Uses App, Drivers, Objects, Menus, Views, Dialogs;

 

Const cmSzotarDialogus= 100;

      cmSzoItemFocused= 101;

      cmASzoBeiro     = 102;

      cmMSzoBeiro     = 103;

 

      SzoVolt:Boolean= False;

Type SzoDataRec= Record

                   ASzo:       String[60];

                   MSzo:       String[60];

                   Szavak:     PSortedCollection;

                   SzoFocused: Integer;

                 End;

 

Const cSzoDataRec: SzoDataRec= (ASzo:        '';

                                Mszo:        '';

                                Szavak:     Nil;

                                SzoFocused:  0);

 

Type PSzoCollection= ^TSzoCollection;

     TSzoCollection= Object(TSortedCollection)

                       Function KeyOf(Item:Pointer):Pointer;Virtual;

                       Function Compare(Key1,Key2:Pointer):Integer;Virtual;

                     End;

 

     PSzotarListBox= ^TSzotarListBox;

     TSzotarListBox= Object(TListBox)

                       Function GetText(Item:Integer;MaxLen:Integer):String;Virtual;

                       Procedure FocusItem(Item:Integer);Virtual;

                     End;

 

     PASzoInputLine= ^TASzoInputLine;

     TASzoInputLine= Object(TInputLine)

                       Constructor Init(Var Bounds:TRect;AMaxLen:Integer);

                       Procedure HandleEvent(Var Event:TEvent);Virtual;

                     End;

 

     PMSzoInputLine= ^TMSzoInputLine;

     TMSzoInputLine= Object(TInputLine)

                       Constructor Init(Var Bounds:TRect;AMaxLen:Integer);

                       Procedure HandleEvent(Var Event:TEvent);Virtual;

                     End;

 

     PSzotar= ^TSzotar;

     TSzotar= Object(TObject)

                FSzo: SzoDataRec;

                Constructor Init(ISzo:SzoDataRec);

                Constructor Load(Var S:TStream);

                Procedure   Store(Var S:TStream);

              End;

 

     TTVApp= Object(TApplication)

               SzotarDialogData:SzoDataRec;

               Constructor Init;

               Procedure HandleEvent(Var Event:TEvent);Virtual;

               Procedure InitMenuBar;Virtual;

               Destructor Done;Virtual;

             End;

 

Const RSzo:           TStreamRec= (

      ObjType:        150;

      VMTLink:        Ofs(TypeOf(TSzotar)^);

      Load:           @TSzotar.Load;

      Store:          @TSzotar.Store);

 

      RSzoCollection: TStreamRec= (

      ObjType:        154;

      VMTLink:        Ofs(TypeOf(TSzoCollection)^);

      Load:           @TSzoCollection.Load;

      Store:          @TSzoCollection.Store);

 

Var Szotar:       PSzoCollection;

    SzotarStream: TBufStream;

 

Function TSzoCollection.KeyOf(Item:Pointer):Pointer;

Var S: String;

Begin

  S:= PSzotar(Item)^.FSzo.ASzo;

  KeyOf:=@S;

End;

Function TSzoCollection.Compare(Key1,Key2:Pointer):Integer;

Begin

  If PString(Key1)^= PString(Key2)^ Then Compare:=  0 Else

  If PString(Key1)^< PString(Key2)^ Then Compare:= -1 Else

                                         Compare:=  1;

End;

 

Function TSzotarListBox.GetText(Item:Integer;MaxLen:Integer):String;

Begin

  GetText:=PSzotar(List^.AT(Item))^.Fszo.ASzo;

End;

Procedure TSzotarListBox.FocusItem(Item:Integer);

Var S: String;

Begin

  Inherited FocusItem(Item);

  If Szotar^.Count<>0 Then

  Begin

    S:= PSzotar(List^.AT(Item))^.FSzo.ASzo;

    Message(Owner,evBroadcast,cmSzoItemFocused+cmASzoBeiro,@S);

    S:= PSzotar(List^.AT(Item))^.FSzo.MSzo;

    Message(Owner,evBroadcast,cmSzoItemFocused+cmMSzoBeiro,@S);

  End;

End;

 

Constructor TASzoInputLine.Init(Var Bounds:TRect;AMaxLen:Integer);

Begin

  Inherited Init(Bounds,AMaxLen);

  EventMask:=EventMask Or evBroadcast;

End;

Procedure TASzoInputLine.HandleEvent(Var Event:TEvent);

Begin

  Inherited HandleEvent(Event);

  If (Event.What=evBroadcast) And

     (Event.Command=cmSzoItemFocused+cmASzoBeiro) And

     (State And sfSelected=0) Then

  Begin

    Data^:=PString(Event.InfoPtr)^;

    DrawView;

  End;

End;

 

Constructor TMSzoInputLine.Init(Var Bounds:TRect;AMaxLen:Integer);

Begin

  Inherited Init(Bounds,AMaxLen);

  EventMask:=EventMask Or evBroadcast;

End;

Procedure TMSzoInputLine.HandleEvent(Var Event:TEvent);

Begin

  Inherited HandleEvent(Event);

  If (Event.What=evBroadcast) And

     (Event.Command=cmSzoItemFocused+cmMSzoBeiro) And

     (State And sfSelected=0) Then

  Begin

    Data^:=PString(Event.InfoPtr)^;

    DrawView;

  End;

End;

 

Constructor TSzotar.Init(ISzo:SzoDataRec);

Begin

  Inherited Init;

  FSzo:= ISzo;

End;

Constructor TSzotar.Load(Var S:TStream);

Begin

  S.Read(FSzo,SizeOf(FSzo));

End;

Procedure TSzotar.Store(Var S:TStream);

Begin

  S.Write(FSzo,SizeOf(FSzo));

End;

 

Constructor TTVApp.Init;

Begin

  Inherited Init;

  RegisterApp;

  RegisterDialogs;

  RegisterMenus;

  RegisterObjects;

  RegisterViews;

  RegisterType(RSzo);

  RegisterType(RSzoCollection);

 

  With SzotarStream Do

  Begin

    Init('szotar.str',STOpen,1024);

    Szotar:=PSzoCollection(Get);

    Done;

  End;

  If SzotarStream.Status<>0 Then

  Szotar:=New(PSzoCollection,Init(10,5));

End;

 

Procedure TTVApp.HandleEvent(Var Event:TEvent);

Var SzotarDialog: PDialog;

    R           : TRect;

    Control     : PView;

    V           : Integer;

  Procedure SzotarDialogus;

  Begin

    R.Assign(1,1,77,22);

    New(SzotarDialog,Init(R,'Angol-Magyar szótár'));

    With SzotarDialog^ Do

    Begin

      R.Assign(11,2,71,3);

      Control:=New(PASzoInputLine,Init(R,60));Insert(Control);

      R.Assign(2,2,10,3);

      Insert(New(PLabel,Init(R,'~A~ngol: ',Control)));

 

      R.Assign(11,4,71,5);

      Control:=New(PMSzoInputLine,Init(R,60));Insert(Control);

      R.Assign(2,4,10,5);

      Insert(New(PLabel,Init(R,'Ma~g~yar: ',Control)));

 

      R.Assign(73,8,74,20);

      Control:=New(PScrollBar,Init(R));

      Insert(Control);

      R.Assign(2,8,73,20);

      Control:=New(PSzotarListBox,Init(R,6,PScrollBar(Control)));

      Insert(Control);

 

      R.Assign(2,6,12,8);

      Insert(New(PButton,Init(R,'~B~eszúr',cmOk,bfDefault)));

      R.Assign(22,6,32,8);

      Insert(New(PButton,Init(R,'~J~avít',cmYes,bfNormal)));

      R.Assign(42,6,52,8);

      Insert(New(PButton,Init(R,'~T~öröl',cmNo,bfNormal)));

      R.Assign(62,6,72,8);

      Insert(New(PButton,Init(R,'~M~égsem',cmCancel,bfNormal)));

 

      SelectNext(False);

      SzotarDialogData:=cSzoDataRec;

      If Szotar^.Count<>0 Then SzotarDialogData.Szavak:=Szotar;

      SetData(SzotarDialogData);

      V:= DeskTop^.ExecView(SzotarDialog);

      GetData(SzotarDialogData);

      If (SzotarDialogData.ASzo<>'') And (V<>cmCancel) Then

      Begin

        SzoVolt:=True;

        Case V Of

           cmOK: Szotar^.Insert(New(PSzotar,Init(SzotarDialogData)));

          cmYes: Szotar^.AtPut(SzotarDialogData.SzoFocused,

                               New(PSzotar,Init(SzotarDialogData)));

           cmNo: Szotar^.AtDelete(SzotardialogData.SzoFocused);

        End;

      End;

    End;

    Szotar^.Pack;

    Dispose(SzotarDialog,Done);

  End;

Begin

  Inherited HandleEvent(Event);

  If Event.What=evCommand Then

  Case Event.Command Of

    cmSzotarDialogus: SzotarDialogus;

  End;

  ClearEvent(Event);

End;

 

Procedure TTVApp.InitMenuBar;

Var R: TRect;

Begin

  GetExtent(R);

  R.B.Y:= R.A.Y+1;

  MenuBar:= New(PMenuBar,Init(R,NewMenu(

    NewSubMenu('~S~zótár',hcNoContext,NewMenu(

      NewItem('~A~ngol-magyar szótár','F2',kbF2,cmSzotarDialogus,hcNoContext,

      NewLine(

      NewItem('E~x~it','Alt-x',kbAltX,cmQuit,hcNoContext,

    Nil)))),

  Nil))));

End;

 

Destructor TTVApp.Done;

Begin

  If SzoVolt Then With SzotarStream Do

  Begin

    Init('szotar.str',STCreate,1024);

    Put(Szotar);

    Done;

  End;

  Inherited Done;

  Dispose(Szotar,Done);

End;

 

Var TVApp:TTVApp;

 

Begin

  If Not

  TVApp.Init Then Halt(1);

  TVApp.Run;

  TVApp.Done;

End.