home *** CD-ROM | disk | FTP | other *** search
- PROGRAM calc;
- (*****************************************************************************
- Name: CALC.PAS
- Version: 1.0
- Edit Datum: 1.4.1992
- Autor: Andreas Schumm
- Kurzbeschreibung: Nichtmodaler Taschenrechner
- *****************************************************************************)
-
- USES Kernel, API, Dialogs;
-
- TYPE Ops = (divide, multiply, add, subtract, none);
-
- VAR LaunchResult : integer;
- MyEvent : EventTyp;
- StillRunning : boolean;
-
- Calculator : Dialog;
- CalcOn : boolean;
- CalcID : Integer; { ID of calc Subapplication }
-
- Accu : real;
- Op1 : real;
- operation : Ops;
- ToBeCleared : boolean;
- errorlock : boolean;
-
-
-
- procedure CalcEventProc(TheEvent: EventTyp); far;
- var MyEditor : PEditField;
- begin
- SetTheViewPort(CalcID);
- MyEditor := Calculator.FindDlgItem(999);
- if (TheEvent.Class = NormKey) then With TheEvent Do
- begin
- Class := DialogEvent;
- MSG := DLG_BUTTON;
- if (Attrib in ['0'..'9']) then ID := 100 + ord(Attrib) - ord('0');
- if (Attrib = '/') then ID := 111;
- if (Attrib = '*') then ID := 112;
- if (Attrib = '+') then ID := 113;
- if (Attrib = '-') then ID := 114;
- { convert keyboard-events to dialog-events }
- end;
-
- if (TheEvent.Class = DialogEvent) and (TheEvent.MSG = DLG_OK) then
- begin
- TheEvent.MSG := DLG_BUTTON;
- TheEvent.ID := 115;
- { convert carriage return }
- end;
-
- If (TheEvent.Class = DialogEvent) and (TheEvent.MSG = DLG_BUTTON) then
- begin
- SetTheViewPort(CalcID);
- if errorlock and (TheEvent.ID <> 110) then exit;
- if ToBeCleared and (TheEvent.ID >= 100) and (TheEvent.ID <= 109) then
- begin
- MyEditor^.SetString(' ');
- ToBeCleared := false;
- Accu := 0;
- end;
- Case TheEvent.ID of
- 100: if Accu <> 0 then Accu := Accu * 10;
- 101: Accu := Accu * 10 + 1;
- 102: Accu := Accu * 10 + 2;
- 103: Accu := Accu * 10 + 3;
- 104: Accu := Accu * 10 + 4;
- 105: Accu := Accu * 10 + 5;
- 106: Accu := Accu * 10 + 6;
- 107: Accu := Accu * 10 + 7;
- 108: Accu := Accu * 10 + 8;
- 109: Accu := Accu * 10 + 9;
- 110: begin
- Accu := 0;
- Op1 := 0;
- operation := none;
- ToBeCleared := false;
- errorlock := false;
- end;
- 111: begin
- Op1 := Accu;
- Accu := 0;
- operation := divide;
- ToBeCleared := true;
- end;
- 112: begin
- Op1 := Accu;
- Accu := 0;
- operation := multiply;
- ToBeCleared := true;
- end;
- 113: begin
- Op1 := Accu;
- Accu := 0;
- operation := add;
- ToBeCleared := true;
- end;
- 114: begin
- Op1 := Accu;
- Accu := 0;
- operation := subtract;
- ToBeCleared := true;
- end;
- 115: begin
- Case Operation of
- multiply : Accu := Op1 * Accu;
- divide : begin
- if Accu <> 0 then
- Accu := Op1 / Accu
- else
- begin
- errorlock := true;
- MyEditor^.SetString('Error');
- end;
- end;
- add : Accu := Accu + Op1;
- subtract : Accu := Op1 - Accu;
- end;
- ToBeCleared := true;
- if not errorlock then MyEditor^.SetReal(Accu);
- Op1 := 0;
- end;
- end; { Case }
- if not ToBeCleared then MyEditor^.SetReal(Accu);
- end; { If }
- end;
-
- procedure RouteSubAppEvents(TheEvent: EventTyp); far;
- begin
- Calculator.HandleEvent(TheEvent); { route events to dialog }
- end;
-
- procedure ShowCalc;
- var MyButton: PButton;
- MyEditor: PEditField;
- hght : integer; { vertical size }
- begin
- if CalcOn then exit;
- EnableMenuItem(GetMenu, 101, MF_DISABLED);
- EnableMenuItem(GetMenu, 102, MF_ENABLED);
- Accu := 0;
- Op1 := 0;
- tobecleared := false;
- errorlock := false;
- operation := none;
- hght := 13*FontY;
- Calculator.Init(20*FontX, hght, MF_MOVEABLE, CalcEventProc);
- Calculator.SetCaption('Calculator');
- Calculator.Move(100,100);
- Calculator.BackGnd := lightgray;
-
- new(MyButton, Init(FontX,hght-2*FontY, 4*FontY+4, 2*FontY, 100,'0'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+4*FontY+8, hght-2*FontY, 2*FontY, 2*FontY,99,'.'));
- Calculator.AddItem(MyButton);
- MyButton^.DisableItem; { Sorry ! }
-
- new(MyButton, Init(FontX, hght-4*FontY-4, 2*FontY, 2*FontY, 101,'1'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+2*FontY+4, hght-4*FontY-4, 2*FontY, 2*FontY, 102,'2'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+4*FontY+8, hght-4*FontY-4, 2*FontY, 2*FontY, 103,'3'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX, hght-6*FontY-8, 2*FontY, 2*FontY, 104,'4'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+2*FontY+4, hght-6*FontY-8, 2*FontY, 2*FontY, 105,'5'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+4*FontY+8, hght-6*FontY-8, 2*FontY, 2*FontY, 106,'6'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX, hght-8*FontY-12, 2*FontY, 2*FontY, 107,'7'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+2*FontY+4, hght-8*FontY-12, 2*FontY, 2*FontY, 108,'8'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+4*FontY+8, hght-8*FontY-12, 2*FontY, 2*FontY, 109,'9'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX, hght-10*FontY-16, 4*FontY+4, 2*FontY, 110,'C'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+4*FontY+8, hght-10*FontY-16, 2*FontY, 2*FontY, 111,'/'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+6*FontY+12, hght-10*FontY-16, 2*FontY, 2*FontY, 112,'*'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+6*FontY+12, hght-8*FontY-12, 2*FontY, 2*FontY, 113,'+'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+6*FontY+12, hght-6*FontY-8, 2*FontY, 2*FontY, 114,'-'));
- Calculator.AddItem(MyButton);
-
- new(MyButton, Init(FontX+6*FontY+12, hght-4*FontY-4, 2*FontY, 4*FontY+4, 115,'='));
- Calculator.AddItem(MyButton);
-
- new(MyEditor, Init(3*FontX, hght-13*FontY+6, 13,13,999,'0'));
- MyEditor^.status := MyEditor^.status or sfBlind or sfNofocus;
- MyEditor^.SetDigits(13);
- Calculator.AddItem(MyEditor);
-
- SuspendApplication(GetMainID);
-
- CalcID := OpenSubApplication(RouteSubAppEvents, APP_NOFRAME, 'Calc',
- Calculator.Port.x1,
- Calculator.Port.y1,
- Calculator.Port.x2,
- Calculator.Port.y2);
- Calculator.Show;
- CalcOn := true;
- end;
-
- procedure HideCalc;
- begin
- if not CalcOn then exit;
- EnableMenuItem(GetMenu, 101, MF_ENABLED);
- EnableMenuItem(GetMenu, 102, MF_DISABLED);
- Calculator.Done;
- CalcOn := false;
- CloseSubApplication(CalcId);
- ActivateApplication(GetMainID);
- end;
-
-
- Procedure HandleMsg(MyMessage: EventTyp); far;
- Begin
- With MyMessage Do
- Case Class Of
- NormKey : Calculator.HandleEvent(MyMessage);
- CTRLKey : Calculator.HandleEvent(MyMessage);
- Menu : begin
- Case MenuItemID of
- 0 : begin
- if CalcOn then HideCalc;
- StillRunning := false; { Ende }
- end;
- 101 : ShowCalc;
- 102 : HideCalc;
- end;
- end;
- end; { Case Class }
- End;
-
-
- Begin
- StillRunning := true;
- CalcOn := false;
- LaunchResult := OpenMainApplication(HandleMsg, APP_NOFONT, 'CALC');
-
- If LaunchResult = 0 then { App launched ! }
- begin
- EnableMenuItem(GetMenu,102,MF_DISABLED);
- while StillRunning Do
- begin
- GetEvent(MyEvent);
- DispatchMessage(MyEvent);
- end;
- CloseMainApplication;
- end
- Else
- Writeln('Error #',LaunchResult,' occured. Cannot start program.');
- End.
-
-