home *** CD-ROM | disk | FTP | other *** search
- unit clocks;
- {$X+} {allow discardable function results}
-
- { Clock-on-a-menubar OOP extension to Turbo Vision apps
-
- Copyright (c) 1990 by Danny Thorpe
-
- Alarms have not been implemented.
- }
-
- interface
- uses dos, objects, drivers, views, menus, dialogs, app, msgbox;
-
- const cmClockChangeDisplay = 1001;
- cmClockSetAlarm = 1002;
-
- ClockNoSecs = 0;
- ClockDispSecs = 1;
- Clock12hour = 0;
- Clock24hour = 1;
-
- type
-
- ClockDataRec = record
- Format: word;
- Seconds: word;
- RefreshStr: String[2];
- end;
-
-
- PClockMenu = ^TClockMenu;
- TClockMenu = object(TMenuBar)
- ClockOptions: ClockDataRec;
- Refresh: byte;
- LastTime: DateTime;
- TimeStr: string[10];
- constructor Init(var Bounds: TRect; Amenu: PMenu);
- procedure Draw; virtual;
- procedure Update; virtual;
- procedure SetRefresh(Secs: integer); virtual;
- procedure SetRefreshStr( Secs: string); virtual;
- procedure ClockChangeDisplay; virtual;
- procedure HandleEvent( var Event: TEvent); virtual;
- function FormatTimeStr(h,m,s:word):string; virtual;
- end;
-
-
-
-
- implementation
-
-
- function LeadingZero(w : Word) : String;
- var
- s : String;
- begin
- Str(w:0,s);
- if Length(s) = 1 then
- s := '0' + s;
- LeadingZero := s;
- end;
-
-
-
- constructor TClockMenu.Init(var Bounds: TRect; AMenu: PMenu);
- var Temp: PMenuBar;
- ClockMenu: PMenu;
- R: TRect;
- begin
- ClockMenu:= NewMenu(NewSubMenu('~'#0'~Clock ', hcNoContext, NewMenu(
- NewItem('~C~hange display','',0,cmClockChangeDisplay, hcNoContext,
- NewItem('Set ~A~larm','', 0, cmClockSetAlarm, hcNoContext,
- nil))),
- AMenu^.Items));
- { ^^ tack passed menubar on end of new clock menu }
- ClockMenu^.Default:= AMenu^.Default;
-
- TMenuBar.Init(Bounds, ClockMenu);
-
- fillchar(LastTime,sizeof(LastTime),#$FF); {fill with 65000's}
- TimeStr:='';
- ClockOptions.Format:= Clock24Hour;
- ClockOptions.Seconds:= ClockDispSecs;
- SetRefresh(1);
- end;
-
-
-
- procedure TClockMenu.Draw;
- var P: PMenuItem;
- begin
- P:= FindItem(#0);
- if P <> nil then
- begin
- DisposeStr(P^.Name);
- P^.Name:= NewStr('~'#0'~'+TimeStr);
- end;
- TMenuBar.Draw;
- end;
-
-
-
- procedure TClockMenu.Update;
- var h,m,s,hund: word;
- begin
- GetTime(h,m,s,hund);
- if abs(s-LastTime.sec) >= Refresh then
- begin
- with LastTime do
- begin
- Hour:=h;
- Min:=m;
- Sec:=s;
- end;
- TimeStr:= FormatTimeStr(h,m,s);
- DrawView;
- end;
- end;
-
-
-
-
- procedure TClockMenu.SetRefresh(Secs: integer);
- begin
- if Secs > 59 then
- Secs := 59;
- if Secs < 0 then
- Secs := 0;
- Refresh:= Secs;
- Str(Refresh:2,ClockOptions.RefreshStr);
- end;
-
-
-
- procedure TClockMenu.SetRefreshStr( Secs: string);
- var temp,code: integer;
- begin
- val(Secs, temp, code);
- if code = 0 then
- SetRefresh(temp);
- end;
-
-
-
-
- procedure TClockMenu.ClockChangeDisplay;
-
- var
- D: PDialog;
- Control: PView;
- Command: word;
- temp,code: integer;
- R: TRect;
- ClockData : ClockDataRec;
-
- begin
-
- ClockData := ClockOptions;
-
- R.Assign(14,3,48,15);
- D:= new(PDialog, Init(R, 'Clock Display'));
-
- R.Assign(3,3,20,5);
- Control:= new(PRadioButtons, Init(R,
- NewSItem('~1~2 hour',
- NewSItem('~2~4 hour',
- nil))));
- D^.Insert(Control);
-
- R.Assign(3,2,20,3);
- Control:= new(Plabel, Init(R, '~F~ormat', Control));
- D^.Insert(Control);
-
- R.Assign(3,6,20,7);
- Control:= new(PCheckBoxes, Init(R,
- NewSItem('~S~econds',
- nil)));
- D^.Insert(Control);
-
- R.Assign(16,9,20,10);
- Control:= new(PInputLine, Init(R, 2));
- D^.Insert(Control);
-
- R.Assign(2,8,20,9);
- Control:= new(PLabel, Init(R, '~R~efresh Rate', Control));
- D^.Insert(Control);
-
- R.Assign(2,9,15,10);
- Control:= new(PLabel, Init(R, '0-59 seconds', PLabel(Control)^.Link));
- D^.Insert(Control);
-
- R.Assign(21,3,31,5);
- Control:= new(PButton, Init(R, '~O~k', cmOk, bfDefault));
- D^.Insert(Control);
-
- R.Assign(21,6,31,8);
- Control:= new(PButton, Init(R, '~C~ancel', cmCancel, bfNormal));
- D^.Insert(Control);
-
-
- D^.SelectNext(False);
- D^.SetData(ClockData);
- repeat
- Command:= Desktop^.ExecView(D);
- if Command = cmOK then
- begin
- D^.GetData(ClockData);
- val(ClockData.RefreshStr,temp,code);
- if (code <> 0) or ((temp<0) or (temp>59)) then
- MessageBox('Refresh rate must be between 0 and 59 seconds.',nil,
- mfOKButton+mfError);
- end;
- until (Command = cmCancel)
- or ((code=0) and ((temp>=0) and (temp<=59)));
-
- Dispose(D, Done);
-
- if Command = cmOk then
- begin
- ClockOptions:= ClockData;
- SetRefreshStr(ClockData.RefreshStr);
- end;
-
- { update display to reflect changes immediately }
- TimeStr:= FormatTimeStr(LastTime.hour, LastTime.min, LastTime.sec);
- DrawView;
- end;
-
-
-
-
-
- procedure TClockMenu.HandleEvent( var Event: TEvent);
- begin
- TMenuBar.HandleEvent( Event);
- if Event.What = evCommand then
- begin
- case Event.Command of
- cmClockChangeDisplay: ClockChangeDisplay;
- cmClockSetAlarm: ;
- end;
- end;
- end;
-
-
-
-
- function TClockMenu.FormatTimeStr(h,m,s: word): string;
- var st, tail: string;
- begin
- tail:='';
- if ClockOptions.Format = Clock24Hour then
- st:= LeadingZero(h)
- else
- begin
- if h >= 12 then
- begin
- tail:= 'pm';
- if h>12 then
- dec(h,12);
- end
- else
- tail:= 'am';
- if h=0 then h:=12; {12 am}
- str(h:0,st); { no leading space on hours }
- end;
-
- st:=st+':'+ LeadingZero(m);
-
-
- if ClockOptions.Seconds = ClockDispSecs then
- st:= st+':'+LeadingZero(s);
-
- FormatTimeStr:= st + tail;
- end;
-
-
-
-
- end.