home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / OWLDEMOS.ZIP / VDLGAPP.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  6.5 KB  |  263 lines

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