home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1998 March / pcx19_9803.iso / PC-XUSER / PC-XUSER.10 / OOP / PELDA02.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1997-10-15  |  12.6 KB  |  488 lines

  1. {*********************************************************}
  2. {                                                         }
  3. {   A program forráskódja az IDG - PCX szerkesztôségének, }
  4. {   és Bérczi László-nak a tulajdona.                     }
  5. {   A forráskód a kereskedelmi célokat kivéve szabadon    }
  6. {                    terjeszthetô !                       }
  7. {                                                         }
  8. {   PC-X User (c) 1997, november                          }
  9. {*********************************************************}
  10. program Pelda_Dialogus_tervezesre_II_with_Validating;
  11.  
  12. uses App, Objects, Menus, Drivers, Views, Dialogs, Editors, Memory, StdDlg, Validate;
  13.  
  14. type
  15.   PAdatok = ^TAdatok;
  16.   TAdatok = record
  17.     VNev,
  18.     KNev,
  19.     Varos     : String[16];
  20.     Irszam    : String[4];
  21.     Utca      : String[16];
  22.     HazSzam   : String[3];
  23.     EgyebHossz: Word;
  24.     Egyeb     : Array[1..16] of Char;
  25.     Tel       : String[16];
  26.     NoteHossz : Word;
  27.     Note      : Array[1..80] of Char;
  28.   end;
  29.  
  30.   PProbaDialog = ^TProbaDialog;
  31.   TProbaDialog = Object(TDialog)
  32.     Vezetek, Kereszt,
  33.     Varos,Iranytszam, Utca, Hazszam,
  34.     Tel          : PInputLine;
  35.     Egyeb, Note  : PMemo;
  36.     Adatok       : TAdatok;
  37.     constructor Init;
  38.     destructor  Done; virtual;
  39.     procedure HandleEvent(var Event: TEvent); virtual;
  40.   private
  41.     AdatFile    : File of TAdatok;
  42.     MelyikRekord: Word;
  43.     procedure FileOpen;
  44.     procedure ReadRecord(var x: Word); {Read to Adatok variable}
  45.     procedure SaveRecordToLastPos;
  46.     procedure FileClose;
  47.   end;
  48.  
  49.   TMyApp = Object(TApplication)
  50.     constructor Init;
  51.     procedure HandleEvent(var Event: TEvent); virtual;
  52.     procedure InitMenuBar; virtual;
  53.     procedure InitStatusLine; virtual;
  54.     procedure NewWindow;
  55.     procedure OpenWindow;
  56.     procedure About;
  57.     procedure SaveDesktop;
  58.     procedure LoadDesktop;
  59.     procedure OpenProbaDialog;
  60.   private
  61.     ClipboardWindow: PEditWindow;
  62.     ProbaDialog    : PProbaDialog;
  63.   end;
  64.  
  65. const
  66.   cmAbout       = 1000;
  67.   cmClipShow    = 1001;
  68.   cmSaveDsk     = 1002;
  69.   cmLoadDsk     = 1003;
  70.   cmTHatra      =  247;
  71.   cmTElore      =  248;
  72.   cmSaveUj      =  249;
  73.   cmUjAdat      =  250;
  74.   cmProbaDialog =  251;
  75.   cmProbaDialogLetezel = 1004;
  76.   hcProbaDialog = $F000;
  77.   hcWindow      = $1F00;
  78.  
  79.   WinNo: Word = 1;
  80.  
  81. constructor TProbaDialog.Init;
  82. var R    : TRect;
  83. begin
  84.   R.Assign(0,0,63,20);
  85.   Inherited Init(R, 'Próba dialógus');
  86.   Options := Options or OfCentered;
  87.   HelpCtx := hcProbaDialog;
  88.  
  89.   R.Assign(16, 2, 28, 3);
  90.   New(Vezetek, Init(R, 16));
  91.   Insert(Vezetek);
  92.   R.Assign(2, 2, 14, 3);
  93.   Insert(New(PLabel, Init(R, 'Ve~z~etéknév:', Vezetek)));
  94.  
  95.   R.Assign(47, 2, 59, 3);
  96.   New(Kereszt, Init(R, 16));
  97.   Insert(Kereszt);
  98.   R.Assign(33, 2, 45, 3);
  99.   Insert(New(PLabel, Init(R, 'Ke~r~esztnév:', Kereszt)));
  100.  
  101.   R.Assign(11, 4, 28, 5);
  102.   New(Varos, Init(R, 16));
  103.   Insert(Varos);
  104.   R.Assign(2, 4, 9, 5);
  105.   Insert(New(PLabel, Init(R, 'Vár~o~s:', Varos)));
  106.  
  107.   R.Assign(50, 4, 59, 5);
  108.   New(Iranytszam, Init(R, 4));
  109.   if CommandEnabled(cmUjAdat) {and not Valid(cmClose)} then
  110.     Iranytszam^.SetValidator(New(PRangeValidator, Init(1000, 9999)));
  111.   Insert(Iranytszam);
  112.   R.Assign(33, 4, 47, 5);
  113.   Insert(New(PLabel, Init(R, '~I~rányítószám:', Iranytszam)));
  114.  
  115.   R.Assign(11, 6, 28, 7);
  116.   New(Utca, Init(R, 16));
  117.   Insert(Utca);
  118.   R.Assign(2, 6, 9, 7);
  119.   Insert(New(PLabel, Init(R, '~U~tca:', Utca)));
  120.  
  121.   R.Assign(50, 6, 59, 7);
  122.   New(Hazszam, Init(R, 3));
  123.   if CommandEnabled(cmUjAdat) {and not Valid(cmClose)} then
  124.     Hazszam^.SetValidator(New(PRangeValidator, Init(1, 999)));
  125.   Insert(Hazszam);
  126.   R.Assign(33, 6, 47, 7);
  127.   Insert(New(PLabel, Init(R, 'Ház~s~zám:', Hazszam)));
  128.  
  129. {  R.Assign(21, 8, 40, 11);
  130.   Insert(New(PR4sFrame3D, Init(R, ' ', HalfGraphFrame, FrameWhite)));}
  131.  
  132.   R.Assign(22, 9, 39, 10);
  133.   New(Egyeb, Init(R, nil, nil, nil, 16));
  134.   Insert(Egyeb);
  135.   R.Assign(22, 8, 30, 9 );
  136.   Insert(New(PLabel, Init(R, 'E~g~yéb:', Egyeb)));
  137.  
  138.   R.Assign(29, 12, 46, 13);
  139.   New(Tel, Init(R, 16));
  140.   if CommandEnabled(cmUjAdat) then Tel^.SetValidator(New(PPXPictureValidator, Init('##-##-###-###[#]', True)));
  141.     Insert(Tel);
  142.   R.Assign(20, 12, 27, 13);
  143.   Insert(New(PLabel, Init(R, '~T~el:', Tel)));
  144.  
  145.   {R.Assign(2, 13, 60, 17);
  146.   Insert(New(PR4sFrame3D, Init(R, ' ', HalfGraphFrame, FrameWhite)));}
  147.   R.Assign(3, 14, 59, 16);
  148.   New(Note, Init(R, nil, nil, nil, 80));
  149.   Insert(Note);
  150.   R.Assign(1, 13, 17, 14);
  151.   Insert(New(PLabel, Init(R, ' Meg~j~egyzés: ', Note)));
  152.  
  153.   R.Assign(3 , 17, 13, 19);
  154.   Insert(New(PButton, Init(R, 'ú~j~', cmUjAdat, bfDefault)));
  155.   R.Assign(14, 17, 24, 19);
  156.   Insert(New(PButton, Init(R, 'Elme~n~t', cmSaveUj, bfNormal)));
  157.   R.Assign(25, 17, 35, 19);
  158.   Insert(New(PButton, Init(R, '~M~égsem', cmClose, bfNormal)));
  159.   R.Assign(36, 17, 49, 19);
  160.   Insert(New(PButton, Init(R, '~K~övetkezô', cmTElore, bfNormal)));
  161.   R.Assign(50, 17, 59, 19);
  162.   Insert(New(PButton, Init(R, 'E~l~ôzô', cmTHatra, bfNormal)));
  163.  
  164.   MelyikRekord:=0;
  165.   FileOpen;
  166.   ReadRecord(MelyikRekord);
  167.   SetData(Adatok);
  168. end;
  169.  
  170. destructor TProbaDialog.Done;
  171. begin
  172.   Inherited Done;
  173.   FileClose;
  174. end;
  175.  
  176. procedure TProbaDialog.HandleEvent(var Event: TEvent);
  177. var B: Boolean;
  178. begin
  179.   B:=True;
  180.   Inherited HandleEvent(Event);
  181.   if (Event.What = evBroadcast) and (Event.Command = cmProbaDialogLetezel)
  182.   then ClearEvent(Event);
  183.   if (Event.What = evCommand) then
  184.   begin
  185.     case Event.Command of
  186.       cmClose : Close;
  187.       cmTHatra: begin
  188.                   if MelyikRekord <> 0 then Dec(MelyikRekord);
  189.                   ReadRecord(MelyikRekord);
  190.                   SetData(Adatok);
  191.                 end;
  192.       cmTElore: begin
  193.                   Inc(MelyikRekord);
  194.                   ReadRecord(MelyikRekord);
  195.                   SetData(Adatok);
  196.                 end;
  197.       cmSaveUj: begin GetData(Adatok); SaveRecordToLastPos; end;
  198.       cmUjAdat: begin
  199.                   ClearEvent(Event);
  200.                   Event.What:=evKeyDown;
  201.                   Event.KeyCode:=kbAltZ;
  202.                   PutEvent(Event);
  203.                   FillChar(Adatok, SizeOf(Adatok), 0);
  204.                   SetData(Adatok);
  205.                   B:=False;
  206.                 end;
  207.     end;
  208.     if B then ClearEvent(Event);
  209.   end;
  210. end;
  211.  
  212. procedure TProbaDialog.FileOpen;
  213. begin
  214.   MelyikRekord:=0;
  215.   Assign(AdatFile, 'adatok.dat');
  216.   Reset(AdatFile);
  217. end;
  218.  
  219. procedure TProbaDialog.ReadRecord(var x: Word);
  220. begin
  221.   if Word(FileSize(AdatFile)-1) >= x then
  222.   begin
  223.     Seek(AdatFile, x);
  224.     Read(AdatFile, Adatok);
  225.   end                                 else Dec(x);
  226. end;
  227.  
  228. procedure TProbaDialog.SaveRecordToLastPos;
  229. begin
  230.   Seek(AdatFile, FileSize(AdatFile));
  231.   Write(AdatFile, Adatok);
  232. end;
  233.  
  234. procedure TProbaDialog.FileClose;
  235. begin
  236.   System.Close(AdatFile);
  237. end;
  238.  
  239. procedure TMyStreamError(var S: TStream); far;
  240. var ErrorMessage: String;
  241. begin
  242.   case S.Status of
  243.     stError: ErrorMessage := 'Stream access error';
  244.     stInitError: ErrorMessage := 'Cannot initialize stream';
  245.     stReadError: ErrorMessage := 'Read beyond end of stream';
  246.     stWriteError: ErrorMessage := 'Cannot expand stream';
  247.     stGetError: ErrorMessage := 'Unregistered type read from stream';
  248.     stPutError: ErrorMessage := 'Unregistered type written to stream';
  249.     end;
  250.   DoneVideo;
  251.   PrintStr('Error: ' + ErrorMessage);
  252.   Halt(Abs(S.Status));
  253. end;
  254.  
  255. constructor TMyApp.Init;
  256. var
  257.   R: TRect;
  258. begin
  259.   MaxHeapSize := 8192;
  260.   EditorDialog := StdEditorDialog;
  261.   StreamError := @TMyStreamError;
  262.   RegisterObjects;
  263.   RegisterViews;
  264.   RegisterApp;
  265.   RegisterEditors;
  266.   Inherited Init;
  267.   Desktop^.GetExtent(R);
  268.   ClipboardWindow := New(PEditWindow, Init(R, '', wnNoNumber));
  269.   if ValidView(ClipboardWindow) <> nil then
  270.   begin
  271.     ClipboardWindow^.Hide;
  272.     InsertWindow(ClipboardWindow);
  273.     Clipboard := ClipboardWindow^.Editor;
  274.     Clipboard^.CanUndo := False;
  275.   end;
  276. end;
  277.  
  278. procedure TMyApp.HandleEvent(var Event: TEvent);
  279. var R: TRect;
  280. begin
  281.   Inherited HandleEvent(Event);
  282.   if Event.What = evCommand then
  283.   begin
  284.     case Event.Command of
  285.       cmNew:
  286.       begin
  287.         NewWindow;
  288.         ClearEvent(Event);
  289.       end;
  290.  
  291.       cmOpen:
  292.       begin
  293.         OpenWindow;
  294.         ClearEvent(Event);
  295.       end;
  296.  
  297.       cmAbout:
  298.       begin
  299.         About;
  300.         ClearEvent(Event);
  301.       end;
  302.  
  303.       cmClipShow:
  304.         with ClipboardWindow^ do
  305.         begin
  306.           Select;
  307.           Show;
  308.           ClearEvent(Event);
  309.         end;
  310.       cmSaveDsk:
  311.       begin
  312.         SaveDesktop;
  313.         ClearEvent(Event);
  314.       end;
  315.       cmLoadDsk:
  316.       begin
  317.         LoadDesktop;
  318.         ClearEvent(Event);
  319.       end;
  320.       cmProbaDialog:
  321.       begin
  322.         OpenProbaDialog;
  323.         ClearEvent(Event);
  324.       end;
  325.  
  326.       {cmXXXX:
  327.       begin
  328.         ClearEvent(Event);
  329.       end;}
  330.     end;
  331.   end;
  332. end;
  333.  
  334. procedure TMyApp.InitMenuBar;
  335. var R: TRect;
  336. begin
  337.   GetExtent(R);
  338.   R.B.Y := R.A.Y + 1;
  339.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  340.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  341.       StdFileMenuItems(nil)),
  342.     NewSubMenu('~E~dit', hcNoContext, NewMenu(
  343.       StdEditMenuItems(
  344.        NewLine(
  345.       NewItem('~S~how clipboard', '', kbNoKey, cmClipShow, hcNoContext,
  346.       nil)))),
  347.     NewSubMenu('O~p~tions', hcNoContext, NewMenu(
  348.       NewItem('~S~ave desktop', '', kbNoKey, cmSaveDsk, hcNoContext,
  349.       NewItem('~L~oad desktop', '', kbNoKey, cmLoadDsk, hcNoContext,
  350.       nil))),
  351.     NewSubMenu('~W~indow', hcNoContext, NewMenu(
  352.       NewItem('~P~róbaDialog', '', kbNoKey, cmProbaDialog, hcNoContext,
  353.       StdWindowMenuItems(nil))),
  354.     NewSubMenu('~H~elp', hcNoContext, NewMenu(
  355.       NewItem('~H~elp', '', kbNoKey, cmHelp, hcNoContext,
  356.        NewLine(
  357.       NewItem('~A~bout', '', kbNoKey, cmAbout, hcNoContext,
  358.       nil)))),
  359.     nil))))))));
  360. end;
  361.  
  362. procedure TMyApp.InitStatusLine;
  363. var R: TRect;
  364. begin
  365.   GetExtent(R);
  366.   R.A.Y := R.B.Y - 1;
  367.   New(StatusLine, Init(R,
  368.     NewStatusDef(0, $0FFF,
  369.       NewStatusKey('~F3~ Open dialog', kbF5, cmNew,
  370.       NewStatusKey('~ALT-X~ Exit', kbALTX, cmQuit,
  371.       NewStatusKey('~F10~ Menu', kbF10, cmMenu,
  372.       StdStatusKeys(nil)))),
  373.     NewStatusDef($1F00, $FFFF,
  374.       NewStatusKey('~F2~ Save', kbF2, cmSave,
  375.       NewStatusKey('~ALT-F3~ Close', kbALTF3, cmClose,
  376.       NewStatusKey('~PgUp~ Elôre', kbPgUp, cmTElore,
  377.       NewStatusKey('~PgDown~ Hátra', kbPgDn, cmTHatra,
  378.       StdStatusKeys(nil))))),
  379.     nil))));
  380. end;
  381.  
  382. procedure TMyApp.About;
  383. var
  384.   R     : TRect;
  385.   Dialog: PDialog;
  386. begin
  387.   R.Assign(0, 0, 49, 11);
  388.   New(Dialog, Init(R, 'PC-X User'));
  389.   with Dialog^ do
  390.   begin
  391.     Options:=Options or OfCentered;
  392.     R.Assign(20, 5, 30, 7);
  393.     Insert(New(PButton, Init(R, '~O~k', cmOk, bfDefault)));
  394.     HelpCtx:=hcWindow;
  395.   end;
  396.   DeskTop^.ExecView(Dialog);
  397. end;
  398.  
  399. procedure TMyApp.OpenWindow;
  400. var
  401.   R     : TRect;
  402.   Window: PEditWindow;
  403.  
  404.   FileDialog: PFileDialog;
  405.   TheFile   : FNameStr;
  406. const
  407.   FDOptions: Word = fdOKButton or fdOpenButton;
  408. begin
  409.  
  410.   TheFile:='*.TXT';
  411.   New(FileDialog, Init(TheFile, 'Open file', '~F~ile name', FDOptions, 1));
  412.   if ExecuteDialog(FileDialog, @TheFile) <> cmCancel then
  413.   begin
  414.     R.Assign(0, 0, 75, 20);
  415.     New(Window, Init(R, TheFile, WinNo));
  416.     with Window^ do
  417.     begin
  418.       Options:=Options or OfCentered;
  419.       HelpCtx:=hcWindow;
  420.     end;
  421.     Inc(WinNo);
  422.     InsertWindow(Window);
  423.   end;
  424. end;
  425.  
  426. procedure TMyApp.NewWindow;
  427. var
  428.   R     : TRect;
  429.   Window: PEditWindow;
  430. begin
  431.   R.Assign(0, 0, 75, 20);
  432.   New(Window, Init(R, '', WinNo));
  433.   with Window^ do
  434.   begin
  435.     Options:=Options or OfCentered;
  436.     HelpCtx:=hcWindow;
  437.   end;
  438.   Inc(WinNo);
  439.   InsertWindow(Window);
  440. end;
  441.  
  442. procedure TMyApp.SaveDesktop;
  443. var SaveDsk: PBufStream;
  444. begin
  445.   Desktop^.Delete(ClipboardWindow);
  446.   New(SaveDsk, Init('PELDAS.DSK', stCreate, 2048));
  447.   SaveDsk^.Put(Desktop);
  448.   Dispose(SaveDsk, Done);
  449.   InsertWindow(ClipboardWindow);
  450. end;
  451.  
  452. procedure TMyApp.LoadDesktop;
  453. var
  454.   LoadDsk    : PBufStream;
  455.   TempDesktop: PDesktop;
  456.   R          : TRect;
  457. begin
  458.   New(LoadDsk, Init('PELDAS.DSK', stOpenRead, 2048));
  459.   TempDesktop := PDesktop(LoadDsk^.Get);
  460.   Dispose(LoadDsk, Done);
  461.   if ValidView(TempDesktop) <> nil then
  462.   begin
  463.     Desktop^.Delete(ClipboardWindow);
  464.     Delete(Desktop);
  465.     Dispose(Desktop, Done);
  466.     Desktop := TempDesktop;
  467.     Insert(Desktop);
  468.     GetExtent(R);
  469.     R.Grow(0, -1);
  470.     Desktop^.Locate(R);
  471.     InsertWindow(ClipboardWindow);
  472.   end;
  473. end;
  474.  
  475. procedure TMyApp.OpenProbaDialog;
  476. begin
  477.   New(ProbaDialog, Init);
  478.   InsertWindow(ProbaDialog);
  479. end;
  480.  
  481.  
  482. var MyApp: TMyApp;
  483.  
  484. BEGIN
  485.   MyApp.Init;
  486.   MyApp.Run;
  487.   MyApp.Done;
  488. END.