home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / GET10.ZIP / GETTIME.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-11-09  |  7.5 KB  |  339 lines

  1. procedure gettimestr
  2.  
  3.    (time_prompt:      screen_text;
  4.     atr,atc:          byte;        {screen co-ords}
  5. var timestr:          string;
  6.     status:           byte);       {bit 2: use system time (hh:mm) as default}
  7.  
  8. begin
  9.    gettimestring(time_prompt,Default_pattr,atr,atc,Default_dattr,
  10.                  Default_cursor_attr,Default_pattr,timestr,status);
  11. end;
  12.  
  13.  
  14. procedure gettimestring
  15.  
  16.    (time_prompt:      screen_text;
  17.     pattr,                         {prompt attribute}
  18.     atr,atc,                       {screen co-ords}
  19.     tattr,                         {time attribute}
  20.     cursor_attr,                   {date and cursor attributes}
  21.     separator_attr:   byte;        {time subfield separator attribute}
  22. var timestr:          string;
  23.     status:           byte);       {bit 2: use system time (hh:mm) as default}
  24.  
  25. var
  26.     old_timestr,
  27.     hours,mins:     string;
  28.     code:           integer;
  29.     H,M:            longint; {for compatibility with 'value' procedure}
  30.     lastedit:       word;
  31.  
  32.     finished,
  33.     OldInsLock,
  34.     OldAutoTab,
  35.     OldPainting,
  36.     OldStringFieldWrap,
  37.     OldTop_n_tail,
  38.     SystemDefault,
  39.     TimeOk:         boolean;
  40.  
  41.  
  42.     OldTabsize,
  43.     field:          byte;
  44.  
  45.  
  46. function LegalExitCommand: boolean;
  47. {
  48.   This function is used to take out of line some code used for determining
  49.   when a subfield can be exited, to keep code readable. Modify as needed.
  50. }
  51. var LocalAction: edit;
  52. begin
  53.   LocalAction := get_edit(command);
  54.   LegalExitCommand :=
  55.     LocalAction in [del_block, restore_block, exit_screen];
  56. end;
  57.  
  58.  
  59. procedure get_system_time;
  60. {
  61.   Puts 24hr system time in timestr; sets values for hours and mins variables.
  62. }
  63. var
  64.   system_hours,
  65.   system_mins,
  66.   system_secs,
  67.   sec100:     word;
  68.  
  69. begin
  70.  
  71.   gettime(system_hours, system_mins, system_secs, sec100);
  72.  
  73.   str(system_hours,hours);
  74.   if system_hours < 10 then
  75.     hours := '0' + hours;
  76.  
  77.   str(system_mins,mins);
  78.   if system_mins < 10 then
  79.     mins := '0' + mins;
  80.  
  81.   timestr := hours + ':' + mins;
  82.   val(hours,H,code);
  83.   val(mins,M,code);
  84.   finished := true;
  85. end;
  86.  
  87.  
  88. procedure gethours;
  89. var GoodHours: boolean;
  90. begin
  91.   repeat
  92.  
  93.     if TimeZeroAsBlank and (hours = '00') and (mins = '00') then
  94.       hours := '';
  95.  
  96.     getstring('',Default_pattr,atr,atc,tattr,cursor_attr,
  97.               hours,'99',2,0);
  98.  
  99.     if hours = '' then
  100.       hours := '00';
  101.  
  102.     GoodHours := escaped or PaintingFields or LegalExitCommand;
  103.  
  104.     if not GoodHours then
  105.       begin
  106.         value(hours,H,24,code); {24hr clock}
  107.  
  108.         GoodHours := (code = 0);
  109.  
  110.         if not GoodHours then
  111.           begin
  112.             error2(Enter_a_number_between +
  113.                    brighten('0') + AndWord + brighten('24'));
  114.             cp := 1;
  115.           end;
  116.       end;
  117.   until GoodHours;
  118. end;
  119.  
  120.  
  121. procedure getmins;
  122. var GoodMins: boolean;
  123. begin
  124.   repeat
  125.  
  126.     if TimeZeroAsBlank and (hours = '00') and (mins = '00') then
  127.       mins := '';
  128.  
  129.     getstring('',Default_pattr,atr,atc + 3,tattr,cursor_attr,
  130.               mins,'99',2,0);
  131.  
  132.     if mins = '' then
  133.       mins := '00';
  134.  
  135.     GoodMins := escaped or PaintingFields or LegalExitCommand;
  136.  
  137.     if not GoodMins then
  138.       begin
  139.         value(mins,M,60,code);
  140.  
  141.         GoodMins := (code = 0);
  142.  
  143.         if not GoodMins then
  144.           begin
  145.             error2(Enter_a_number_between +
  146.                    brighten('0') + AndWord + brighten('60'));
  147.             cp := 1;
  148.           end;
  149.       end;
  150.   until GoodMins;
  151. end;
  152.  
  153.  
  154. procedure get_subfield (field: byte);
  155. begin
  156.   case field of
  157.     1: gethours;
  158.     2: getmins;
  159.   end;
  160. end;
  161.  
  162.  
  163. procedure assemble_time;
  164. {
  165.   Get hours, mins, H, M and turn '' into '00:00'
  166. }
  167. begin
  168.   hours := copy(timestr,1,2);
  169.   mins  := copy(timestr,4,2);
  170.  
  171.   val(hours,H,code);
  172.   val(mins,M,code);
  173.  
  174.   if H = 0 then
  175.     hours := '00';
  176.   if M = 0 then
  177.     mins := '00';
  178.  
  179.   timestr := hours + ':' + mins;
  180. end;
  181.  
  182.  
  183. procedure display_time;
  184. var OldPainting: boolean;
  185.     fattr:       byte;
  186.  
  187. begin
  188.   assemble_time;
  189.  
  190.   if PaintingFields or finished then
  191.     fattr := tattr
  192.   else fattr := separator_attr;
  193.  
  194.   OldPainting := PaintingFields;
  195.   PaintingFields := true;
  196.  
  197. { paint field }
  198.  
  199.   get_subfield(1);
  200.   Qwrite(atr,atc + 2,fattr,':');
  201.   get_subfield(2);
  202.  
  203.   PaintingFields := OldPainting;
  204. end;
  205.  
  206.  
  207. procedure get_input;
  208. begin
  209.   finished := false;
  210.   get_subfield(field); {getstring takes care of clearerror}
  211.  
  212.   action := get_edit(command);
  213.   case action of
  214.  
  215.     leftchar : if (field > 1) or OldStringFieldWrap then
  216.                  begin
  217.                    dec(field);
  218.                    cp := 255; {move cursor to end of previous field}
  219.                  end;
  220.  
  221.     rightchar: if (field < 3) or OldStringFieldWrap then
  222.                  begin
  223.                    inc(field);
  224.                    cp := 1;
  225.                  end;
  226.  
  227.     del_block: begin {^Y for time field}
  228.                  timestr := '00:00';
  229.                  field := 1;
  230.                  cp := 1;
  231.                  display_time;
  232.                end;
  233.  
  234.     restore_block: {^U for time field}
  235.  
  236.                begin
  237.                  timestr := old_timestr;
  238.                  display_time;
  239.                end;
  240.  
  241.     enter_default:
  242.  
  243.                get_system_time;
  244.  
  245.     carriage_return:
  246.  
  247.                begin
  248.                  if SystemDefault and (timestr = '00:00') then
  249.                    get_system_time;
  250.  
  251.                  finished := true;
  252.                end;
  253.  
  254.     tabback:   begin
  255.                  dec(field);
  256.                  cp := 255;
  257.                end;
  258.  
  259.     tabover,
  260.     post_letter: begin
  261.                    inc(field);
  262.                    cp := 1;
  263.                  end;
  264.  
  265.     escapefrom,
  266.     help,
  267.     upchar,
  268.     downchar,
  269.     pageup,
  270.     pagedown,
  271.     scrollup,
  272.     scrolldown,
  273.     goto_top,
  274.     goto_bottom,
  275.     abort,
  276.     exit_screen,
  277.     quit:
  278.                 begin
  279.                   timestr := old_timestr;           {restore timestr}
  280.                   assemble_time;
  281.                   escaped := (get_edit(command) = escapefrom);
  282.                   finished := not escaped;       {1 or other is true}
  283.                 end;
  284.  
  285.   else
  286.     error2(Invalid_key);
  287.   end; {case}
  288.  
  289.   timestr := hours + ':' + mins;
  290.   finished := finished or (field in [0,3]);
  291. end;
  292.  
  293.  
  294. begin {gettimestring}
  295.  
  296.   display_prompt(time_prompt,atr,atc,pattr,5); {field size = 5 (hh:mm)}
  297.  
  298.   if not PaintingFields then
  299.     begin
  300.  
  301.      finished           := false; {tested in display_time}
  302.      display_time;
  303.  
  304.      old_timestr := timestr;
  305.  
  306.      SystemDefault      := (status and $02) > 0;
  307.      OldTop_n_tail      := Top_n_tail;
  308.      Top_n_tail         := false;   {don't turn '00' into '' in getday etc}
  309.      OldInsLock         := Inslock;
  310.      InsLock            := true;    {disable}
  311.      OldAutoTab         := AutoTab;
  312.      AutoTab            := true;
  313.      OldStringFieldWrap := StringFieldWrap;
  314.      StringFieldWrap    := true;
  315.      OldTabsize         := tabsize; {disable <tab> within date field}
  316.      tabsize            := 0;
  317.  
  318.      escaped := false;
  319.      field   := 1;
  320.  
  321.      repeat
  322.        cp := 1;
  323.        repeat
  324.          get_input
  325.        until escaped or finished;
  326.        TimeOk := not ((H = 24) and (M <> 0)); {No error msg if time > 24:00}
  327.      until TimeOk or escaped;
  328.  
  329.      InsLock         := OldInslock;
  330.      AutoTab         := OldAutoTab;
  331.      StringFieldWrap := OldStringFieldWrap;
  332.      Tabsize         := OldTabsize;
  333.      Top_n_tail      := OldTop_n_tail;
  334.    end;
  335.  
  336.   display_time; {ALWAYS (to reset separator attribute) even if unchanged}
  337. end;
  338.  
  339.