home *** CD-ROM | disk | FTP | other *** search
- (* ------------------------------------------------------ *)
- (* HEXCALC.PAS *)
- (* Nachbau von Charles Petzolds Windows-Hexcalc *)
- (* (Programming Windows, Microsoft Press, 1990) *)
- (* (c) 1993 te-wi Verlag, München *)
- (* ------------------------------------------------------ *)
- UNIT HexCalc;
-
- {$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q+,R+,S+,T-,V+,X+,Y+}
- {$M 16384,0,655360}
-
- INTERFACE
-
- USES Objects, Drivers, Views, Menus, MsgBox, Dialogs, App;
-
- CONST
- cmCalc = 10000;
-
- (* --- tPetzCalcDisplay Object ------------------------------ *)
-
- TYPE
- tCalcState = (csFirst, csValid, csError);
-
- pPetzCalcDisplay = ^tPetzCalcDisplay;
- tPetzCalcDisplay = OBJECT (tView)
- Status : tCalcState;
- Number : STRING [20];
- Operator : CHAR;
- Operand : LONGINT;
-
- CONSTRUCTOR Init(VAR Bounds : tRect);
- CONSTRUCTOR Load(VAR S : tStream);
- PROCEDURE HandleEvent(VAR Event : tEvent); VIRTUAL;
- FUNCTION GetPalette : pPalette; VIRTUAL;
- PROCEDURE Draw; VIRTUAL;
- PROCEDURE Clear;
- PROCEDURE CalcKey(Key : CHAR);
- PROCEDURE Store(VAR S : tStream);
- END;
-
- TYPE
- pPetzCalc = ^tPetzCalc;
- tPetzCalc = OBJECT (tDialog)
- CONSTRUCTOR Init;
- END;
-
- (* --- HexCalc Registration Record ---------------------- *)
-
- CONST
- rPetzCalcDisplay : tStreamRec = (
- ObjType : 10180; (* Visions CALC.PAS hat 10040 *)
- VmtLink : Ofs(TypeOf(tPetzCalcDisplay)^);
- Load : @tPetzCalcDisplay.Load;
- Store : @tPetzCalcDisplay.Store);
-
- rPetzCalc : tStreamRec = (
- ObjType : 10181;
- VmtLink : Ofs(TypeOf(tPetzCalc)^);
- Load : @tPetzCalc.Load;
- Store : @tPetzCalc.Store);
-
- PROCEDURE RegisterPetzCalc;
-
- IMPLEMENTATION
-
- CONST
- cmCalcButton = cmCalc + 1;
-
- CONSTRUCTOR tPetzCalcDisplay.Init(VAR Bounds : tRect);
- BEGIN
- inherited Init(Bounds);
- Options := Options OR ofSelectable;
- EventMask := evKeyDown OR evBroadCast;
- Clear;
- END;
-
- CONSTRUCTOR tPetzCalcDisplay.Load(VAR S : tStream);
- BEGIN
- inherited Load(S);
- S.Read(Status, SizeOf(Status) + SizeOf(Number) +
- SizeOf(Operator) + SizeOf(Operand));
- END;
-
- FUNCTION tPetzCalcDisplay.GetPalette : pPalette;
- CONST
- P : STRING [1] = #19;
- BEGIN
- GetPalette := @P;
- END;
-
- PROCEDURE tPetzCalcDisplay.Draw;
- (* aktualisiert das Display. Wird z.B. auch von Clear
- benötigt, sonst passiert schon bei der Initialisierung
- gar nichts. *)
- VAR
- Color : BYTE;
- i : INTEGER;
- B : tDrawBuffer;
- BEGIN
- Color := GetColor(1);
- i := Size.X - Length(Number) - 2;
- MoveChar(B, ' ', Color, Size.X);
- MoveStr(B[i], Number, Color);
- WriteBuf(0, 0, Size.X, 1, B);
- END;
-
- PROCEDURE tPetzCalcDisplay.Clear;
- BEGIN
- Status := csFirst;
- Number := '0';
- END;
-
- PROCEDURE tPetzCalcDisplay.CalcKey(Key : CHAR);
- VAR
- R : LONGINT;
-
- PROCEDURE Error(s : STRING);
- BEGIN
- Status := csError;
- Number := s;
- END;
-
- PROCEDURE CheckFirst;
- BEGIN
- IF Status = csFirst THEN BEGIN
- Status := csValid;
- Number := '0';
- END;
- END;
-
- PROCEDURE GetDisplay(VAR LI : LONGINT);
- VAR
- E : INTEGER;
- BEGIN
- Val('$' + Number, LI, E); (* Hex -> Dezimal *)
- END;
-
- FUNCTION HexByte(b : BYTE) : STRING;
- CONST
- H : ARRAY [0..15] OF CHAR = '0123456789ABCDEF';
- BEGIN
- HexByte[0] := #2;
- HexByte[1] := H[b DIV 16];
- HexByte[2] := H[b MOD 16];
- END;
-
- FUNCTION HexWord(w : WORD) : STRING;
- BEGIN
- HexWord := HexByte(w DIV 256) + HexByte(w MOD 256);
- END;
-
- PROCEDURE SetDisplay(Li : LONGINT);
- VAR
- L : RECORD Hi, Lo : WORD; END ABSOLUTE Li;
- BEGIN
- Number := HexWord(L.Lo) + HexWord(L.Hi);
- END;
-
- BEGIN
- Key := UpCase(Key);
- IF (Status = csError) AND (Key <> 'L') THEN Key := ' ';
- CASE Key OF
- '0'..'9',
- 'A'..'F' : BEGIN
- CheckFirst;
- IF Number = '0' THEN Number := '';
- Number := Number + Key;
- END;
- #8, #27 : BEGIN
- CheckFirst;
- IF Length(Number) = 1 THEN
- Number := '0'
- ELSE
- Dec(Number[0]);
- END;
- 'L' : Clear;
- '+', '-',
- '*', '/',
- '%', '&',
- '|', '^',
- '<', '>',
- '=', #13 : BEGIN
- IF Status = csValid THEN BEGIN
- Status := csFirst;
- GetDisplay(R);
- CASE Operator OF
- '+' : SetDisplay(Operand + R);
- '-' : SetDisplay(Operand - R);
- '*' : SetDisplay(Operand * R);
- '/' : IF R <> 0 THEN
- SetDisplay(Operand DIV R)
- ELSE
- Error('division by zero');
- '%' : IF R <> 0 THEN
- SetDisplay(Operand MOD R)
- ELSE
- Error('division by zero');
- '&' : SetDisplay(Operand AND R);
- '|' : SetDisplay(Operand OR R);
- '<' : SetDisplay(Operand SHL R);
- '>' : SetDisplay(Operand SHR R);
-
- (* --- Baustelle ------- *)
- '^' : ; (* aktivieren der zweiten display-zeile *)
- (* --------------------- *)
-
- '=', #13 : (* do nothing *) ;
- END;
- END;
- Operator := Key;
- GetDisplay(Operand);
- END;
- END;
- DrawView;
- END;
-
- PROCEDURE tPetzCalcDisplay.HandleEvent(VAR Event : tEvent);
- BEGIN
- inherited HandleEvent(Event);
-
- CASE Event.What OF
- evKeyDown : CalcKey(Event.CharCode);
- evBroadCast : IF Event.Command = cmCalcButton THEN BEGIN
- CalcKey(pButton(Event.InfoPtr)^.Title^[1]);
- END;
- ELSE
- Exit;
- END;
- ClearEvent(Event);
- END;
-
- PROCEDURE tPetzCalcDisplay.Store(VAR S : tStream);
- BEGIN
- inherited Store(S);
- S.Write(Status, SizeOf(Status) + SizeOf(Number) +
- SizeOf(Operator) + SizeOf(Operand));
- END;
-
- (* --- tPetzCalc Object --------------------------------- *)
-
- CONSTRUCTOR tPetzCalc.Init;
- VAR
- i : INTEGER;
- P : pView;
- R : tRect;
- BEGIN
- R.Assign(5, 3, 35, 21);
- inherited Init(R, 'Hex Calculator');
- Options := Options OR ofFirstClick;
-
- (* --- Knopfzeile 1 ------------------------------------- *)
-
- R.A.X := 2; R.A.Y := 5;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'D', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 7; R.A.Y := 5;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'E', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 12; R.A.Y := 5;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'F', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 17; R.A.Y := 5;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '+', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 22; R.A.Y := 5;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '&', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* --- Knopfzeile 2 ------------------------------------- *)
-
- R.A.X := 2; R.A.Y := 7;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'A', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 7; R.A.Y := 7;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'B', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 12; R.A.Y := 7;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'C', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 17; R.A.Y := 7;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '-', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 22; R.A.Y := 7;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '|', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* --- Knopfzeile 3 ------------------------------------- *)
-
- R.A.X := 2; R.A.Y := 9;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '7', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 7; R.A.Y := 9;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '8', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 12; R.A.Y := 9;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '9', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 17; R.A.Y := 9;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '*', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 22; R.A.Y := 9;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '^', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* --- Knopfzeile 4 ------------------------------------- *)
-
- R.A.X := 2; R.A.Y := 11;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '4', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 7; R.A.Y := 11;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '5', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 12; R.A.Y := 11;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '6', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 17; R.A.Y := 11;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '/', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 22; R.A.Y := 11;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '<', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* --- Knopfzeile 5 ------------------------------------- *)
-
- R.A.X := 2; R.A.Y := 13;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '1', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 7; R.A.Y := 13;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '2', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 12; R.A.Y := 13;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '3', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 17; R.A.Y := 13;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '%', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 22; R.A.Y := 13;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '>', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* --- Knopfzeile 6 ------------------------------------- *)
-
- R.A.X := 2; R.A.Y := 15;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '0', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 7; R.A.Y := 15;
- R.B.X := R.A.X + 10; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, #27, cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- R.A.X := 17; R.A.Y := 15;
- R.B.X := R.A.X + 10; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, '=', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* Clear *)
- R.A.X := 2; R.A.Y := 3;
- R.B.X := R.A.X + 5; R.B.Y := R.A.Y + 2;
- P := New(pButton, Init(R, 'L', cmCalcButton, bfNormal + bfBroadcast));
- P^.Options := P^.Options AND NOT ofSelectable;
- Insert(P);
-
- (* Display *)
- R.Assign(8, 3, 27, 4);
- Insert(New(pPetzCalcDisplay, Init(R)));
- END;
-
- PROCEDURE RegisterPetzCalc;
- BEGIN
- RegisterType(rPetzCalcDisplay);
- RegisterType(rPetzCalc);
- END;
-
- END.
- (* ------------------------------------------------------ *)
- (* Ende von HEXCALC.PAS *)
-
-
-