home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / owldemos / vdlgapp.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  7KB  |  264 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program VDlgApp;
  10.  
  11. {$R VDLGAPP.RES}
  12.  
  13. uses WObjects, WinTypes, WinProcs, Strings;
  14.  
  15. const
  16.  
  17.   cm_Input     = 201;
  18.   id_NameField = 101;
  19.   id_SSNField  = 105;
  20.   id_NumField  = 107;
  21.  
  22.   NameLen = 25;
  23.   SSNLen  = 11;
  24.  
  25. type
  26.  
  27. { Application object }
  28.  
  29.   TEmpDataApp = object(TApplication)
  30.     procedure InitMainWindow; virtual;
  31.   end;
  32.  
  33. { Name input field }
  34.  
  35.   PNameEdit = ^TNameEdit;
  36.   TNameEdit = object(TEdit)
  37.     function CanClose: Boolean; virtual;
  38.   end;
  39.  
  40. { Social security number string }
  41.  
  42.   TSSNStr = array[0..SSNLen] of Char;
  43.  
  44. { Social security number input field }
  45.  
  46.   PSSNEdit = ^TSSNEdit;
  47.   TSSNEdit = object(TEdit)
  48.     constructor Init(AParent: PWindowsObject; AnId: Integer;
  49.       ATitle: PChar; X, Y, W, H: Integer);
  50.     constructor InitResource(AParent: PWindowsObject; ResourceID: Word);
  51.     function CanClose: Boolean; virtual;
  52.   end;
  53.  
  54. { Numeric input field }
  55.  
  56.   PNumEdit = ^TNumEdit;
  57.   TNumEdit = object(TEdit)
  58.     MinValue, MaxValue: Longint;
  59.     constructor Init(AParent: PWindowsObject; AnId: Integer;
  60.       ATitle: PChar; X, Y, W, H: Integer; Digits: Word;
  61.       AMinValue, AMaxValue: Longint);
  62.     constructor InitResource(AParent: PWindowsObject; ResourceID: Word;
  63.       Digits: Word; AMinValue, AMaxValue: Longint);
  64.     function CanClose: Boolean; virtual;
  65.     function Transfer(DataPtr: Pointer; TransferFlag: Word): Word; virtual;
  66.   end;
  67.  
  68. { Data transfer record for employee data dialog }
  69.  
  70.   PDataRec = ^TDataRec;
  71.   TDataRec = record
  72.     Name: array [0..NameLen] of Char;
  73.     SSN: TSSNStr;
  74.     Number: Longint;
  75.   end;
  76.  
  77. { Application main window }
  78.  
  79.   PDataWindow = ^TDataWindow;
  80.   TDataWindow = object(TWindow)
  81.     DataRec: TDataRec;
  82.     constructor Init(AParent: PWindowsObject; TheTitle: PChar);
  83.     procedure Input(var Msg: TMessage); virtual cm_First + cm_Input;
  84.   end;
  85.  
  86. { TNameEdit }
  87.  
  88. function TNameEdit.CanClose: Boolean;
  89. const
  90.   CharSet = ['a'..'z','A'..'Z',' ','.'];
  91. var
  92.   I, Len: Integer;
  93.   Text: array[0..255] of Char;
  94.   Valid: Boolean;
  95. begin
  96.   GetText(Text, SizeOf(Text));
  97.   I := 0;
  98.   Len := StrLen(Text);
  99.   Valid := True;
  100.   while Valid and (I < Len) do
  101.   begin
  102.     Valid := Text[I] in CharSet;
  103.     Inc(I);
  104.   end;
  105.   if not Valid then
  106.   begin
  107.     MessageBox(HWindow, 'Invalid character in name', 'Data error',
  108.       mb_Ok or mb_IconExclamation);
  109.     SetSelection(0, MaxInt);
  110.     SetFocus(HWindow);
  111.   end;
  112.   CanClose := Valid;
  113. end;
  114.  
  115. { TSSNEdit }
  116.  
  117. constructor TSSNEdit.Init(AParent: PWindowsObject; AnId: Integer;
  118.   ATitle: PChar; X, Y, W, H: Integer);
  119. begin
  120.   TEdit.Init(AParent, AnId, ATitle, X, Y, W, H, SSNLen + 1, False);
  121. end;
  122.  
  123. constructor TSSNEdit.InitResource(AParent: PWindowsObject;
  124.   ResourceID: Word);
  125. begin
  126.   TEdit.InitResource(AParent, ResourceID, SSNLen + 1);
  127. end;
  128.  
  129. function TSSNEdit.CanClose: Boolean;
  130. const
  131.   NumSet = ['0'..'9'];
  132. var
  133.   Valid: Boolean;
  134.   I, Len: Integer;
  135.   SSN: TSSNStr;
  136. begin
  137.   GetText(SSN, SizeOf(SSN));
  138.   Len := StrLen(SSN);
  139.   Valid := (Len = SSNLen) and (SSN[3] = '-') and (SSN[6] = '-');
  140.   I := 0;
  141.   while Valid and (I < Len) do
  142.   begin
  143.     Valid := (I = 3) or (I = 6) or (SSN[I] in NumSet);
  144.     Inc(I);
  145.   end;
  146.   if not Valid then
  147.   begin
  148.     MessageBox(HWindow, 'SSN must be entered as 999-99-9999', 'Data error',
  149.       mb_Ok or mb_IconExclamation);
  150.     SetSelection(0, MaxInt);
  151.     SetFocus(HWindow);
  152.   end;
  153.   CanClose :=  Valid;
  154. end;
  155.  
  156. { TNumEdit }
  157.  
  158. constructor TNumEdit.Init(AParent: PWindowsObject; AnId: Integer;
  159.   ATitle: PChar; X, Y, W, H: Integer; Digits: Word;
  160.   AMinValue, AMaxValue: Longint);
  161. begin
  162.   TEdit.Init(AParent, AnId, ATitle, X, Y, W, H, Digits + 1, False);
  163.   MinValue := AMinValue;
  164.   MaxValue := AMaxValue;
  165. end;
  166.  
  167. constructor TNumEdit.InitResource(AParent: PWindowsObject;
  168.   ResourceID: Word; Digits: Word; AMinValue, AMaxValue: Longint);
  169. begin
  170.   TEdit.InitResource(AParent, ResourceID, Digits + 1);
  171.   MinValue := AMinValue;
  172.   MaxValue := AMaxValue;
  173. end;
  174.  
  175. function TNumEdit.CanClose: Boolean;
  176. var
  177.   Valid: Boolean;
  178.   ValCode: Integer;
  179.   Value: LongInt;
  180.   Text: array[0..15] of Char;
  181.   Msg: array[0..63] of Char;
  182. begin
  183.   GetText(Text, SizeOf(Text));
  184.   Val(Text, Value, ValCode);
  185.   Valid := (ValCode = 0) and
  186.     (Value >= MinValue) and (Value <= MaxValue);
  187.   if not Valid then
  188.   begin
  189.     WVSPrintF(Msg, 'Number must be between %ld and %ld', MinValue);
  190.     MessageBox(HWindow, Msg, 'Data error', mb_Ok or mb_IconExclamation);
  191.     SetSelection(0, MaxInt);
  192.     SetFocus(HWindow);
  193.   end;
  194.   CanClose := Valid;
  195. end;
  196.  
  197. function TNumEdit.Transfer(DataPtr: Pointer; TransferFlag: Word): Word;
  198. var
  199.   ValCode: Integer;
  200.   Text: array[0..15] of Char;
  201. begin
  202.   case TransferFlag of
  203.     tf_GetData:
  204.       begin
  205.         GetText(Text, SizeOf(Text));
  206.         Val(Text, Longint(DataPtr^), ValCode);
  207.       end;
  208.     tf_SetData:
  209.       begin
  210.         Str(Longint(DataPtr^), Text);
  211.         SetText(Text);
  212.       end;
  213.   end;
  214.   Transfer := SizeOf(Longint);
  215. end;
  216.  
  217. { TDataWindow }
  218.  
  219. constructor TDataWindow.Init(AParent: PWindowsObject; TheTitle: PChar);
  220. begin
  221.   TWindow.Init(AParent, TheTitle);
  222.   Attr.Menu := LoadMenu(HInstance, 'Commands');
  223.   FillChar(DataRec, SizeOf(DataRec), 0);
  224. end;
  225.  
  226. procedure TDataWindow.Input(var Msg: TMessage);
  227. var
  228.   Dialog: PDialog;
  229.   P: PWindowsObject;
  230.   Params: array[0..2] of Longint;
  231.   Result: array [0..255] of Char;
  232. begin
  233.   Dialog := New(PDialog, Init(@Self, 'DataDialog'));
  234.   Dialog^.TransferBuffer := @DataRec;
  235.   P := New(PNameEdit, InitResource(Dialog, id_NameField, NameLen + 1));
  236.   P := New(PSSNEdit, InitResource(Dialog, id_SSNField));
  237.   P := New(PNumEdit, InitResource(Dialog, id_NumField, 5, 0, 99999));
  238.   if Application^.ExecDialog(Dialog) = id_OK then
  239.   begin
  240.     Params[0] := Longint(@DataRec.Name);
  241.     Params[1] := Longint(@DataRec.SSN);
  242.     Params[2] := DataRec.Number;
  243.     WVSPrintF(Result,
  244.       'Name:'#9'%s'#13#10'SSN:'#9'%s'#13#10'ID:'#9'%ld', Params);
  245.     MessageBox(HWindow, Result, 'Employee Data Entered', 0);
  246.   end;
  247. end;
  248.  
  249. { TEmpDataApp }
  250.  
  251. procedure TEmpDataApp.InitMainWindow;
  252. begin
  253.   MainWindow := New(PDataWindow, Init(nil, 'Employee Data'));
  254. end;
  255.  
  256. var
  257.   EmpDataApp: TEmpDataApp;
  258.  
  259. begin
  260.   EmpDataApp.Init('EmpDataApp');
  261.   EmpDataApp.Run;
  262.   EmpDataApp.Done;
  263. end.
  264.