From 411b0e9e319550a394b0a5945543f39a5affc6a6 Mon Sep 17 00:00:00 2001 From: Kaleb Keithley Date: Fri, 14 Nov 2003 16:48:58 +0000 Subject: Initial revision --- XCalc-color.ad | 102 ++++++ XCalc.ad | 528 +++++++++++++++++++++++++++++ actions.c | 551 +++++++++++++++++++++++++++++++ actions.h | 94 ++++++ math.c | 1007 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xcalc.c | 402 ++++++++++++++++++++++ xcalc.h | 123 +++++++ xcalc.man | 439 ++++++++++++++++++++++++ 8 files changed, 3246 insertions(+) create mode 100644 XCalc-color.ad create mode 100644 XCalc.ad create mode 100644 actions.c create mode 100644 actions.h create mode 100644 math.c create mode 100644 xcalc.c create mode 100644 xcalc.h create mode 100644 xcalc.man diff --git a/XCalc-color.ad b/XCalc-color.ad new file mode 100644 index 0000000..1335596 --- /dev/null +++ b/XCalc-color.ad @@ -0,0 +1,102 @@ +#include "XCalc" + +XCalc*ti.backgroundPixmap: gray3?foreground=gray70&background=gray85 +XCalc*ti.bevel.background: gray80 +XCalc*ti.bevel.displayList: foreground gray90;lines -1,0,0,0,0,-1;lines 3,-4,-4,-4,-4,3 + +XCalc*ti.bevel.borderColor: gray60 +XCalc*ti.bevel.borderWidth: 1 +XCalc*ti.bevel.vertDistance: 4 +XCalc*ti.bevel.defaultDistance: 4 +XCalc*ti.bevel.screen.vertDistance: 4 +XCalc*ti.bevel.screen.horizDistance: 4 +XCalc*ti.bevel.screen*left: chainLeft +XCalc*ti.bevel.screen*right: chainRight +XCalc*ti.bevel.screen*top: chainTop +XCalc*ti.bevel.screen*bottom: chainBottom +XCalc*ti.bevel.screen*background: rgb:9/a/9 +XCalc*ti.bevel.screen.borderColor: gray50 +XCalc*ti.bevel.screen*LCD.foreground: gray20 +XCalc*ti.bevel.screen*INV.vertDistance: 2 + +XCalc*ti.Command.shapeStyle: roundedRectangle +XCalc*ti.Command.displayList: foreground rgb:a/b/c;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 + +XCalc*ti.Command.borderColor: rgb:8/9/a +XCalc*ti.Command.background: rgb:c/d/e +XCalc*ti.Command.foreground: gray5 + +XCalc*ti.button20.foreground: gray5 +XCalc*ti.button20.background: rgb:e/d/c +XCalc*ti.button20.displayList: foreground rgb:a/9/8;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button20.borderColor: rgb:9/8/7 + +XCalc*ti.button25.foreground: gray5 +XCalc*ti.button25.background: rgb:e/d/c +XCalc*ti.button25.displayList: foreground rgb:a/9/8;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button25.borderColor: rgb:9/8/7 + +XCalc*ti.button30.foreground: gray5 +XCalc*ti.button30.background: rgb:e/d/c +XCalc*ti.button30.displayList: foreground rgb:a/9/8;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button30.borderColor: rgb:9/8/7 + +XCalc*ti.button35.foreground: gray5 +XCalc*ti.button35.background: rgb:e/d/c +XCalc*ti.button35.displayList: foreground rgb:a/9/8;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button35.borderColor: rgb:9/8/7 + +XCalc*ti.button40.foreground: gray5 +XCalc*ti.button40.background: rgb:e/d/c +XCalc*ti.button40.displayList: foreground rgb:a/9/8;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button40.borderColor: rgb:9/8/7 + + + +XCalc*ti.button22.background: gray95 +XCalc*ti.button22.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button22.borderColor: gray65 + +XCalc*ti.button23.background: gray95 +XCalc*ti.button23.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button23.borderColor: gray65 + +XCalc*ti.button24.background: gray95 +XCalc*ti.button24.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button24.borderColor: gray65 + +XCalc*ti.button27.background: gray95 +XCalc*ti.button27.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button27.borderColor: gray65 + +XCalc*ti.button28.background: gray95 +XCalc*ti.button28.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button28.borderColor: gray65 + +XCalc*ti.button29.background: gray95 +XCalc*ti.button29.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button29.borderColor: gray65 + +XCalc*ti.button32.background: gray95 +XCalc*ti.button32.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button32.borderColor: gray65 + +XCalc*ti.button33.background: gray95 +XCalc*ti.button33.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button33.borderColor: gray65 + +XCalc*ti.button34.background: gray95 +XCalc*ti.button34.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button34.borderColor: gray65 + +XCalc*ti.button37.background: gray95 +XCalc*ti.button37.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button37.borderColor: gray65 + +XCalc*ti.button38.background: gray95 +XCalc*ti.button38.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button38.borderColor: gray65 + +XCalc*ti.button39.background: gray95 +XCalc*ti.button39.displayList: foreground gray75;segments 8,-4,-9,-4,-4,-9,-4,8;draw-arc -14,-14,-4,-4,270,90 +XCalc*ti.button39.borderColor: gray65 diff --git a/XCalc.ad b/XCalc.ad new file mode 100644 index 0000000..0107978 --- /dev/null +++ b/XCalc.ad @@ -0,0 +1,528 @@ +! $XConsortium: XCalc.ad,v 1.13 94/03/03 19:19:32 converse Exp $ +! XCalc application class resource file + +XCalc.Title: Calculator +XCalc.IconName: Calc +XCalc.IconPixmap: calculator + +XCalc*Cursor: hand2 +XCalc*Font: 8x13 +XCalc*ShapeStyle: rectangle +XCalc*Command.horizDistance: 2 +XCalc*Command.vertDistance: 2 +XCalc*Command.width: 40 +XCalc*Command.height: 26 +XCalc*Command.internalWidth: 1 + +XCalc*bevel.background: black +XCalc*bevel.horizDistance: 4 +XCalc*bevel.vertDistance: 2 + +XCalc*bevel.screen.horizDistance: 6 +XCalc*bevel.screen.vertDistance: 2 +XCalc*bevel.screen.defaultDistance: 0 + +XCalc*bevel.screen.Label.horizDistance: 4 +XCalc*bevel.screen.Label.vertDistance: 2 +XCalc*bevel.screen.Label.internalHeight: 1 +XCalc*bevel.screen.Label.internalWidth: 1 +XCalc*bevel.screen.LCD.label: 88888888888 +XCalc*bevel.screen.LCD.fromHoriz: M +XCalc*bevel.screen.LCD.horizDistance: 4 +XCalc*bevel.screen.LCD.vertDistance: 2 +XCalc*bevel.screen.INV.fromVert: LCD +XCalc*bevel.screen.INV.vertDistance: 4 +XCalc*bevel.screen.DEG.fromHoriz: INV +XCalc*bevel.screen.DEG.fromVert: LCD +XCalc*bevel.screen.DEG.horizDistance: 1 +XCalc*bevel.screen.RAD.fromHoriz: DEG +XCalc*bevel.screen.RAD.fromVert: LCD +XCalc*bevel.screen.GRAD.fromHoriz: RAD +XCalc*bevel.screen.GRAD.fromVert: LCD +XCalc*bevel.screen.P.label: () +XCalc*bevel.screen.P.fromHoriz: GRAD +XCalc*bevel.screen.P.fromVert: LCD +XCalc*bevel.screen.P.horizDistance: 2 + +!XCalc*ti.Geometry: 171x252 +XCalc*ti.bevel.screen.LCD.width: 186 + +XCalc*ti.bevel.screen.LCD.translations: #replace\n\ + Ctrlc:quit()\n\ + Ctrlh:clear()\n\ + None0:digit(0)\n\ + None1:digit(1)\n\ + None2:digit(2)\n\ + None3:digit(3)\n\ + None4:digit(4)\n\ + None5:digit(5)\n\ + None6:digit(6)\n\ + None7:digit(7)\n\ + None8:digit(8)\n\ + None9:digit(9)\n\ + KP_0:digit(0)\n\ + KP_1:digit(1)\n\ + KP_2:digit(2)\n\ + KP_3:digit(3)\n\ + KP_4:digit(4)\n\ + KP_5:digit(5)\n\ + KP_6:digit(6)\n\ + KP_7:digit(7)\n\ + KP_8:digit(8)\n\ + KP_9:digit(9)\n\ + KP_Enter:equal()\n\ + KP_Equal:equal()\n\ + KP_Multiply:multiply()\n\ + KP_Add:add()\n\ + KP_Subtract:subtract()\n\ + KP_Decimal:decimal()\n\ + KP_Divide:divide()\n\ + KP_Tab:equal()\n\ + Clear:clear()\n\ + :.:decimal()\n\ + :+:add()\n\ + :-:subtract()\n\ + :*:multiply()\n\ + :/:divide()\n\ + :(:leftParen()\n\ + :):rightParen()\n\ + :!:factorial()\n\ + e:e()\n\ + :^:power()\n\ + p:pi()\n\ + i:inverse()\n\ + s:sine()\n\ + c:cosine()\n\ + t:tangent()\n\ + d:degree()\n\ + l:naturalLog()\n\ + :=:equal()\n\ + n:negate()\n\ + r:squareRoot()\n\ + space:clear()\n\ + q:quit()\n\ + Delete:clear()\n\ + BackSpace:clear()\n\ + ,:toggle()selection()\n + +XCalc*ti.button1.label: 1/x +XCalc*ti.button1.translations: #override,:reciprocal()unset() +XCalc*ti.button2.label: x\262 +XCalc*ti.button2.translations: #override,:square()unset() +XCalc*ti.button3.font: -adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-* +XCalc*ti.button3.label: \326\140 +XCalc*ti.button3.translations: #override,:squareRoot()unset() +XCalc*ti.button4.label: CE/C +XCalc*ti.button4.translations: #override,:clear()unset() +XCalc*ti.button5.label: AC +XCalc*ti.button5.translations: #override,:off()unset()\n\ + ,:quit() + +XCalc*ti.button6.label: INV +XCalc*ti.button6.translations: #override,:inverse()unset() +XCalc*ti.button7.label: sin +XCalc*ti.button7.translations: #override,:sine()unset() +XCalc*ti.button8.label: cos +XCalc*ti.button8.translations: #override,:cosine()unset() +XCalc*ti.button9.label: tan +XCalc*ti.button9.translations: #override,:tangent()unset() +XCalc*ti.button10.label: DRG +XCalc*ti.button10.translations: #override,:degree()unset() + +XCalc*ti.button11.label: e +XCalc*ti.button11.translations: #override,:e()unset() +XCalc*ti.button12.label: EE +XCalc*ti.button12.translations: #override,:scientific()unset() +XCalc*ti.button13.label: log +XCalc*ti.button13.translations: #override,:logarithm()unset() +XCalc*ti.button14.label: ln +XCalc*ti.button14.translations: #override,:naturalLog()unset() +XCalc*ti.button15.label: y^x +XCalc*ti.button15.translations: #override,:power()unset() + +XCalc*ti.button16.font: -adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-* +XCalc*ti.button16.label: \160 +XCalc*ti.button16.translations: #override,:pi()unset() +XCalc*ti.button17.label: x! +XCalc*ti.button17.translations: #override,:factorial()unset() +XCalc*ti.button18.label: ( +XCalc*ti.button18.translations: #override,:leftParen()unset() +XCalc*ti.button19.label: ) +XCalc*ti.button19.translations: #override,:rightParen()unset() +XCalc*ti.button20.font: -adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-* +XCalc*ti.button20.label: \270 +XCalc*ti.button20.translations: #override,:divide()unset() + +XCalc*ti.button21.label: STO +XCalc*ti.button21.translations: #override,:store()unset() +XCalc*ti.button22.label: 7 +XCalc*ti.button22.translations: #override,:digit(7)unset() +XCalc*ti.button23.label: 8 +XCalc*ti.button23.translations: #override,:digit(8)unset() +XCalc*ti.button24.label: 9 +XCalc*ti.button24.translations: #override,:digit(9)unset() +XCalc*ti.button25.label: * +XCalc*ti.button25.translations: #override,:multiply()unset() + +XCalc*ti.button26.label: RCL +XCalc*ti.button26.translations: #override,:recall()unset() +XCalc*ti.button27.label: 4 +XCalc*ti.button27.translations: #override,:digit(4)unset() +XCalc*ti.button28.label: 5 +XCalc*ti.button28.translations: #override,:digit(5)unset() +XCalc*ti.button29.label: 6 +XCalc*ti.button29.translations: #override,:digit(6)unset() +XCalc*ti.button30.label: - +XCalc*ti.button30.translations: #override,:subtract()unset() + +XCalc*ti.button31.label: SUM +XCalc*ti.button31.translations: #override,:sum()unset() +XCalc*ti.button32.label: 1 +XCalc*ti.button32.translations: #override,:digit(1)unset() +XCalc*ti.button33.label: 2 +XCalc*ti.button33.translations: #override,:digit(2)unset() +XCalc*ti.button34.label: 3 +XCalc*ti.button34.translations: #override,:digit(3)unset() +XCalc*ti.button35.label: + +XCalc*ti.button35.translations: #override,:add()unset() + +XCalc*ti.button36.label: EXC +XCalc*ti.button36.translations: #override,:exchange()unset() +XCalc*ti.button37.label: 0 +XCalc*ti.button37.translations: #override,:digit(0)unset() +XCalc*ti.button38.label: . +XCalc*ti.button38.translations: #override,:decimal()unset() +XCalc*ti.button39.label: +/- +XCalc*ti.button39.translations: #override,:negate()unset() +XCalc*ti.button40.label: = +XCalc*ti.button40.translations: #override,:equal()unset() + +XCalc*ti.button1.horizDistance: 4 +XCalc*ti.button1.vertDistance: 12 +XCalc*ti.button1.fromVert: bevel +XCalc*ti.button2.fromHoriz: button1 +XCalc*ti.button2.fromVert: bevel +XCalc*ti.button2.vertDistance: 12 +XCalc*ti.button3.fromHoriz: button2 +XCalc*ti.button3.fromVert: bevel +XCalc*ti.button3.vertDistance: 12 +XCalc*ti.button4.fromHoriz: button3 +XCalc*ti.button4.fromVert: bevel +XCalc*ti.button4.vertDistance: 12 +XCalc*ti.button5.fromHoriz: button4 +XCalc*ti.button5.fromVert: bevel +XCalc*ti.button5.vertDistance: 12 + +XCalc*ti.button6.horizDistance: 4 +XCalc*ti.button6.fromVert: button1 +XCalc*ti.button7.fromHoriz: button6 +XCalc*ti.button7.fromVert: button2 +XCalc*ti.button8.fromHoriz: button7 +XCalc*ti.button8.fromVert: button3 +XCalc*ti.button9.fromHoriz: button8 +XCalc*ti.button9.fromVert: button4 +XCalc*ti.button10.fromHoriz: button9 +XCalc*ti.button10.fromVert: button5 + +XCalc*ti.button11.horizDistance: 4 +XCalc*ti.button11.fromVert: button6 +XCalc*ti.button12.fromHoriz: button11 +XCalc*ti.button12.fromVert: button7 +XCalc*ti.button13.fromHoriz: button12 +XCalc*ti.button13.fromVert: button8 +XCalc*ti.button14.fromHoriz: button13 +XCalc*ti.button14.fromVert: button9 +XCalc*ti.button15.fromHoriz: button14 +XCalc*ti.button15.fromVert: button10 + +XCalc*ti.button16.horizDistance: 4 +XCalc*ti.button16.fromVert: button11 +XCalc*ti.button17.fromHoriz: button16 +XCalc*ti.button17.fromVert: button12 +XCalc*ti.button18.fromHoriz: button17 +XCalc*ti.button18.fromVert: button13 +XCalc*ti.button19.fromHoriz: button18 +XCalc*ti.button19.fromVert: button14 +XCalc*ti.button20.fromHoriz: button19 +XCalc*ti.button20.fromVert: button15 + +XCalc*ti.button21.horizDistance: 4 +XCalc*ti.button21.fromVert: button16 +XCalc*ti.button22.fromHoriz: button21 +XCalc*ti.button22.fromVert: button17 +XCalc*ti.button23.fromHoriz: button22 +XCalc*ti.button23.fromVert: button18 +XCalc*ti.button24.fromHoriz: button23 +XCalc*ti.button24.fromVert: button19 +XCalc*ti.button25.fromHoriz: button24 +XCalc*ti.button25.fromVert: button20 + +XCalc*ti.button26.horizDistance: 4 +XCalc*ti.button26.fromVert: button21 +XCalc*ti.button27.fromHoriz: button26 +XCalc*ti.button27.fromVert: button22 +XCalc*ti.button28.fromHoriz: button27 +XCalc*ti.button28.fromVert: button23 +XCalc*ti.button29.fromHoriz: button28 +XCalc*ti.button29.fromVert: button24 +XCalc*ti.button30.fromHoriz: button29 +XCalc*ti.button30.fromVert: button25 + +XCalc*ti.button31.horizDistance: 4 +XCalc*ti.button31.fromVert: button26 +XCalc*ti.button32.fromHoriz: button31 +XCalc*ti.button32.fromVert: button27 +XCalc*ti.button33.fromHoriz: button32 +XCalc*ti.button33.fromVert: button28 +XCalc*ti.button34.fromHoriz: button33 +XCalc*ti.button34.fromVert: button29 +XCalc*ti.button35.fromHoriz: button34 +XCalc*ti.button35.fromVert: button30 + +XCalc*ti.button36.horizDistance: 4 +XCalc*ti.button36.fromVert: button31 +XCalc*ti.button37.fromHoriz: button36 +XCalc*ti.button37.fromVert: button32 +XCalc*ti.button38.fromHoriz: button37 +XCalc*ti.button38.fromVert: button33 +XCalc*ti.button39.fromHoriz: button38 +XCalc*ti.button39.fromVert: button34 +XCalc*ti.button40.fromHoriz: button39 +XCalc*ti.button40.fromVert: button35 + + +!XCalc*hp.Geometry: 336x164 +XCalc*hp.bevel.screen.LCD.width: 186 + +XCalc*hp.bevel.screen.LCD.translations: #replace\n\ + Ctrlc:quit()\n\ + Ctrlh:back()\n\ + None0:digit(0)\n\ + None1:digit(1)\n\ + None2:digit(2)\n\ + None3:digit(3)\n\ + None4:digit(4)\n\ + None5:digit(5)\n\ + None6:digit(6)\n\ + None7:digit(7)\n\ + None8:digit(8)\n\ + None9:digit(9)\n\ + KP_0:digit(0)\n\ + KP_1:digit(1)\n\ + KP_2:digit(2)\n\ + KP_3:digit(3)\n\ + KP_4:digit(4)\n\ + KP_5:digit(5)\n\ + KP_6:digit(6)\n\ + KP_7:digit(7)\n\ + KP_8:digit(8)\n\ + KP_9:digit(9)\n\ + KP_Enter:enter()\n\ + KP_Multiply:multiply()\n\ + KP_Add:add()\n\ + KP_Subtract:subtract()\n\ + KP_Decimal:decimal()\n\ + KP_Divide:divide()\n\ + :.:decimal()\n\ + :+:add()\n\ + :-:subtract()\n\ + :*:multiply()\n\ + :/:divide()\n\ + :!:factorial()\n\ + e:e()\n\ + :^:power()\n\ + p:pi()\n\ + i:inverse()\n\ + s:sine()\n\ + c:cosine()\n\ + t:tangent()\n\ + d:degree()\n\ + l:naturalLog()\n\ + n:negate()\n\ + r:squareRoot()\n\ + space:clear()\n\ + q:quit()\n\ + Delete:back()\n\ + Return:enter()\n\ + Linefeed:enter()\n\ + x:XexchangeY()\n\ + BackSpace:back()\n\ + ,:toggle()selection()\n + +XCalc*hp.button1.font: -adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-* +XCalc*hp.button1.label: \326\140 +XCalc*hp.button1.translations: #override,:squareRoot()unset() +XCalc*hp.button2.label: e^x +XCalc*hp.button2.translations: #override,:epower()unset() +XCalc*hp.button3.label: 10^x +XCalc*hp.button3.translations: #override,:tenpower()unset() +XCalc*hp.button4.label: y^x +XCalc*hp.button4.translations: #override,:power()unset() +XCalc*hp.button5.label: 1/x +XCalc*hp.button5.translations: #override,:reciprocal()unset() +XCalc*hp.button6.label: CHS +XCalc*hp.button6.translations: #override,:negate()unset() +XCalc*hp.button7.label: 7 +XCalc*hp.button7.translations: #override,:digit(7)unset() +XCalc*hp.button8.label: 8 +XCalc*hp.button8.translations: #override,:digit(8)unset() +XCalc*hp.button9.label: 9 +XCalc*hp.button9.translations: #override,:digit(9)unset() +XCalc*hp.button10.font: -adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-* +XCalc*hp.button10.label: \270 +XCalc*hp.button10.translations: #override,:divide()unset() + +XCalc*hp.button11.label: x! +XCalc*hp.button11.translations: #override,:factorial()unset() +XCalc*hp.button12.font: -adobe-symbol-*-*-*-*-*-120-*-*-*-*-*-* +XCalc*hp.button12.label: \160 +XCalc*hp.button12.translations: #override,:pi()unset() +XCalc*hp.button13.label: sin +XCalc*hp.button13.translations: #override,:sine()unset() +XCalc*hp.button14.label: cos +XCalc*hp.button14.translations: #override,:cosine()unset() +XCalc*hp.button15.label: tan +XCalc*hp.button15.translations: #override,:tangent()unset() +XCalc*hp.button16.label: EEX +XCalc*hp.button16.translations: #override,:scientific()unset() +XCalc*hp.button17.label: 4 +XCalc*hp.button17.translations: #override,:digit(4)unset() +XCalc*hp.button18.label: 5 +XCalc*hp.button18.translations: #override,:digit(5)unset() +XCalc*hp.button19.label: 6 +XCalc*hp.button19.translations: #override,:digit(6)unset() +XCalc*hp.button20.label: * +XCalc*hp.button20.translations: #override,:multiply()unset() + +XCalc*hp.button21.mappedWhenManaged: False +XCalc*hp.button22.mappedWhenManaged: False +XCalc*hp.button23.label: Rv +XCalc*hp.button23.translations: #override,:roll()unset() +XCalc*hp.button24.label: x:y +XCalc*hp.button24.translations: #override,:XexchangeY()unset() +XCalc*hp.button25.label: <- +XCalc*hp.button25.translations: #override,:back()unset() +XCalc*hp.button26.label: E\nN\nT\nE\nR +XCalc*hp.button26.translations: #override,:enter()unset() +XCalc*hp.button27.label: 1 +XCalc*hp.button27.translations: #override,:digit(1)unset() +XCalc*hp.button28.label: 2 +XCalc*hp.button28.translations: #override,:digit(2)unset() +XCalc*hp.button29.label: 3 +XCalc*hp.button29.translations: #override,:digit(3)unset() +XCalc*hp.button30.label: - +XCalc*hp.button30.translations: #override,:subtract()unset() + +XCalc*hp.button31.label: ON +XCalc*hp.button31.translations: #override,:off()unset()\n\ + ,:quit() +XCalc*hp.button32.label: DRG +XCalc*hp.button32.translations: #override,:degree()unset() +XCalc*hp.button33.label: INV +XCalc*hp.button33.translations: #override,:inverse()unset() +XCalc*hp.button34.label: STO +XCalc*hp.button34.translations: #override,:store()unset() +XCalc*hp.button35.label: RCL +XCalc*hp.button35.translations: #override,:recall()unset() +XCalc*hp.button36.label: 0 +XCalc*hp.button36.translations: #override,:digit(0)unset() +XCalc*hp.button37.label: . +XCalc*hp.button37.translations: #override,:decimal()unset() +XCalc*hp.button38.label: SUM +XCalc*hp.button38.translations: #override,:sum()unset() +XCalc*hp.button39.label: + +XCalc*hp.button39.translations: #override,:add()unset() + +XCalc*hp.button1.horizDistance: 4 +XCalc*hp.button1.vertDistance: 12 +XCalc*hp.button1.fromVert: bevel +XCalc*hp.button2.fromHoriz: button1 +XCalc*hp.button2.fromVert: bevel +XCalc*hp.button2.vertDistance: 12 +XCalc*hp.button3.fromHoriz: button2 +XCalc*hp.button3.fromVert: bevel +XCalc*hp.button3.vertDistance: 12 +XCalc*hp.button4.fromHoriz: button3 +XCalc*hp.button4.fromVert: bevel +XCalc*hp.button4.vertDistance: 12 +XCalc*hp.button5.fromHoriz: button4 +XCalc*hp.button5.fromVert: bevel +XCalc*hp.button5.vertDistance: 12 +XCalc*hp.button6.fromHoriz: button5 +XCalc*hp.button6.fromVert: bevel +XCalc*hp.button6.vertDistance: 12 +XCalc*hp.button7.fromHoriz: button6 +XCalc*hp.button7.fromVert: bevel +XCalc*hp.button7.vertDistance: 12 +XCalc*hp.button8.fromHoriz: button7 +XCalc*hp.button8.fromVert: bevel +XCalc*hp.button8.vertDistance: 12 +XCalc*hp.button9.fromHoriz: button8 +XCalc*hp.button9.fromVert: bevel +XCalc*hp.button9.vertDistance: 12 +XCalc*hp.button10.fromHoriz: button9 +XCalc*hp.button10.fromVert: bevel +XCalc*hp.button10.vertDistance: 12 + +XCalc*hp.button11.horizDistance: 4 +XCalc*hp.button11.fromVert: button1 +XCalc*hp.button12.fromHoriz: button11 +XCalc*hp.button12.fromVert: button2 +XCalc*hp.button13.fromHoriz: button12 +XCalc*hp.button13.fromVert: button3 +XCalc*hp.button14.fromHoriz: button13 +XCalc*hp.button14.fromVert: button4 +XCalc*hp.button15.fromHoriz: button14 +XCalc*hp.button15.fromVert: button5 +XCalc*hp.button16.fromHoriz: button15 +XCalc*hp.button16.fromVert: button6 +XCalc*hp.button17.fromHoriz: button16 +XCalc*hp.button17.fromVert: button7 +XCalc*hp.button18.fromHoriz: button17 +XCalc*hp.button18.fromVert: button8 +XCalc*hp.button19.fromHoriz: button18 +XCalc*hp.button19.fromVert: button9 +XCalc*hp.button20.fromHoriz: button19 +XCalc*hp.button20.fromVert: button10 + +XCalc*hp.button21.horizDistance: 4 +XCalc*hp.button21.fromVert: button11 +XCalc*hp.button22.fromHoriz: button21 +XCalc*hp.button22.fromVert: button12 +XCalc*hp.button23.fromHoriz: button22 +XCalc*hp.button23.fromVert: button13 +XCalc*hp.button24.fromHoriz: button23 +XCalc*hp.button24.fromVert: button14 +XCalc*hp.button25.fromHoriz: button24 +XCalc*hp.button25.fromVert: button15 +XCalc*hp.button26.fromHoriz: button25 +XCalc*hp.button26.fromVert: button16 +XCalc*hp.button26.font: 6x12 +XCalc*hp.button26.height: 56 +XCalc*hp.button27.fromHoriz: button26 +XCalc*hp.button27.fromVert: button17 +XCalc*hp.button28.fromHoriz: button27 +XCalc*hp.button28.fromVert: button18 +XCalc*hp.button29.fromHoriz: button28 +XCalc*hp.button29.fromVert: button19 +XCalc*hp.button30.fromHoriz: button29 +XCalc*hp.button30.fromVert: button20 + +XCalc*hp.button31.horizDistance: 4 +XCalc*hp.button31.fromVert: button21 +XCalc*hp.button32.fromHoriz: button31 +XCalc*hp.button32.fromVert: button22 +XCalc*hp.button33.fromHoriz: button32 +XCalc*hp.button33.fromVert: button23 +XCalc*hp.button34.fromHoriz: button33 +XCalc*hp.button34.fromVert: button24 +XCalc*hp.button35.fromHoriz: button34 +XCalc*hp.button35.fromVert: button25 +XCalc*hp.button36.fromHoriz: button26 +XCalc*hp.button36.fromVert: button27 +XCalc*hp.button37.fromHoriz: button36 +XCalc*hp.button37.fromVert: button28 +XCalc*hp.button38.fromHoriz: button37 +XCalc*hp.button38.fromVert: button29 +XCalc*hp.button39.fromHoriz: button38 +XCalc*hp.button39.fromVert: button30 diff --git a/actions.c b/actions.c new file mode 100644 index 0000000..37c5736 --- /dev/null +++ b/actions.c @@ -0,0 +1,551 @@ +/* $XConsortium: actions.c,v 1.9 94/04/17 20:43:30 converse Exp $ */ +/* + +Copyright (c) 1989 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall +not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization +from the X Consortium. + +*/ + +/* + * actions.c - externally available procedures for xcalc + * + * Author: Donna Converse, MIT X Consortium + */ + +#include +#include +#include "xcalc.h" +extern int rpn; +extern Atom wm_delete_window; +extern int pre_op(); +extern void post_op(), Quit(), ringbell(), do_select(); + +#ifndef IEEE +extern jmp_buf env; +extern void fail_op(); +#define XCALC_PRE_OP(keynum) { if (pre_op(keynum)) return; \ + if (setjmp (env)) {fail_op(); return;}} +#else +#define XCALC_PRE_OP(keynum) if (pre_op(keynum)) return; +#endif + +/*ARGSUSED*/ +void add(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kADD); + rpn ? twof(kADD) : twoop(kADD); + post_op(); +} + +/*ARGSUSED*/ +void back(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kBKSP); + bkspf(); + post_op(); +} + +/*ARGSUSED*/ +void bell(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + ringbell(); +} + +/*ARGSUSED*/ +void clearit(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kCLR); + clearf(); + post_op(); +} + +/*ARGSUSED*/ +void cosine(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kCOS); + oneop(kCOS); + post_op(); +} + +/*ARGSUSED*/ +void decimal(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kDEC); + decf(); + post_op(); +} + +/*ARGSUSED*/ +void degree(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kDRG); + drgf(); + post_op(); +} + +/*ARGSUSED*/ +void digit(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + switch (vector[0][0]) + { + case '1': XCALC_PRE_OP(kONE); numeric(kONE); break; + case '2': XCALC_PRE_OP(kTWO); numeric(kTWO); break; + case '3': XCALC_PRE_OP(kTHREE); numeric(kTHREE); break; + case '4': XCALC_PRE_OP(kFOUR); numeric(kFOUR); break; + case '5': XCALC_PRE_OP(kFIVE); numeric(kFIVE); break; + case '6': XCALC_PRE_OP(kSIX); numeric(kSIX); break; + case '7': XCALC_PRE_OP(kSEVEN); numeric(kSEVEN); break; + case '8': XCALC_PRE_OP(kEIGHT); numeric(kEIGHT); break; + case '9': XCALC_PRE_OP(kNINE); numeric(kNINE); break; + case '0': XCALC_PRE_OP(kZERO); numeric(kZERO); break; + } + post_op(); +} + +/*ARGSUSED*/ +void divide(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kDIV); + rpn ? twof(kDIV) : twoop(kDIV); + post_op(); +} + +/*ARGSUSED*/ +void e(w, ev, vector, count) + Widget w; + XEvent *ev; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kE); + oneop(kE); + post_op(); +} + +/*ARGSUSED*/ +void enter(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kENTR); + entrf(); + post_op(); +} + +/*ARGSUSED*/ +void epower(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kEXP); + oneop(kEXP); + post_op(); +} + +/*ARGSUSED*/ +void equal(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kEQU); + equf(); + post_op(); +} + +/*ARGSUSED*/ +void exchange(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kEXC); + oneop(kEXC); + post_op(); +} + +/*ARGSUSED*/ +void factorial(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kFACT); + oneop(kFACT); + post_op(); +} + +/*ARGSUSED*/ +void inverse(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kINV); + invf(); + post_op(); +} + +/*ARGSUSED*/ +void leftParen(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kLPAR); + lparf(); + post_op(); +} + +/*ARGSUSED*/ +void logarithm(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kLOG); + oneop(kLOG); + post_op(); +} + +/*ARGSUSED*/ +void multiply(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kMUL); + rpn ? twof(kMUL) : twoop(kMUL); + post_op(); +} + +/*ARGSUSED*/ +void naturalLog(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kLN); + oneop(kLN); + post_op(); +} + +/*ARGSUSED*/ +void negate(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kNEG); + negf(); + post_op(); +} + +/*ARGSUSED*/ +void nop(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + ringbell(); +} + +/*ARGSUSED*/ +void off(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kOFF); + offf(); + post_op(); +} + +/*ARGSUSED*/ +void pi(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kPI); + oneop(kPI); + post_op(); +} + +/*ARGSUSED*/ +void power(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kPOW); + rpn ? twof(kPOW) : twoop(kPOW); + post_op(); +} + +/*ARGSUSED*/ +void quit(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + if (e->type == ClientMessage && e->xclient.data.l[0] != wm_delete_window) + ringbell(); + else + Quit(); +} + +/*ARGSUSED*/ +void recall(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kRCL); + rpn ? memf(kRCL) : oneop(kRCL); + post_op(); +} + +/*ARGSUSED*/ +void reciprocal(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kRECIP); + oneop(kRECIP); + post_op(); +} + +/*ARGSUSED*/ +void rightParen(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kRPAR); + rparf(); + post_op(); +} + +/*ARGSUSED*/ +void roll(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kROLL); + rollf(); + post_op(); +} + +/*ARGSUSED*/ +void scientific(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kEE); + eef(); + post_op(); +} + +/*ARGSUSED*/ +void selection(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + do_select(((XButtonReleasedEvent *)e)->time); +} + +/*ARGSUSED*/ +void sine(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kSIN); + oneop(kSIN); + post_op(); +} + +/*ARGSUSED*/ +void square(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kSQR); + oneop(kSQR); + post_op(); +} + +/*ARGSUSED*/ +void squareRoot(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kSQRT); + oneop(kSQRT); + post_op(); +} + +/*ARGSUSED*/ +void store(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kSTO); + rpn ? memf(kSTO) : oneop(kSTO); + post_op(); +} + +/*ARGSUSED*/ +void subtract(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kSUB); + rpn ? twof(kSUB) : twoop(kSUB); + post_op(); +} + +/*ARGSUSED*/ +void sum(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kSUM); + rpn ? memf(kSUM) : oneop(kSUM); + post_op(); +} + +/*ARGSUSED*/ +void tangent(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kTAN); + oneop(kTAN); + post_op(); +} + +/*ARGSUSED*/ +void tenpower(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(k10X); + oneop(k10X); + post_op(); +} + +/*ARGSUSED*/ +void XexchangeY(w, e, vector, count) + Widget w; + XEvent *e; + String *vector; + Cardinal *count; +{ + XCALC_PRE_OP(kXXY); + twof(kXXY); + post_op(); +} diff --git a/actions.h b/actions.h new file mode 100644 index 0000000..69f1983 --- /dev/null +++ b/actions.h @@ -0,0 +1,94 @@ +/* $XConsortium: actions.h,v 1.6 94/04/17 20:43:31 rws Exp $ */ +/* + +Copyright (c) 1989 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall +not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization +from the X Consortium. + +*/ + +/* + * actions.h - action table declaring externally available procedures for xcalc + * + * Author: Donna Converse, MIT X Consortium + */ + +extern void + add(), back(), bell(), clearit(), cosine(), decimal(), + degree(), digit(), divide(), e(), enter(), epower(), equal(), + exchange(), factorial(), + inverse(), leftParen(), logarithm(), multiply(), naturalLog(), + negate(), nop(), off(), pi(), power(), quit(), recall(), + reciprocal(), rightParen(), roll(), scientific(), selection(), sine(), + square(), squareRoot(), store(), subtract(), sum(), + tangent(), tenpower(), XexchangeY(); + +/* + * calculator action table + */ + +XtActionsRec Actions[] = { +{"add", add}, /* addition */ +{"back", back}, /* HP-specific backspace */ +{"bell", bell}, /* ring bell */ +{"clear", clearit}, /* TI-specific clear calculator state */ +{"cosine", cosine}, /* trigonometric function cosine */ +{"decimal", decimal}, /* decimal point */ +{"degree", degree}, /* degree, radian, grad switch */ +{"digit", digit}, /* numeric key */ +{"divide", divide}, /* division */ +{"e", e}, /* the natural number e */ +{"enter", enter}, /* HP-specific enter */ +{"epower", epower}, /* e raised to a power */ +{"equal", equal}, /* TI-specific = */ +{"exchange", exchange}, /* TI-specific exchange memory and display */ +{"factorial", factorial}, /* factorial function */ +{"inverse", inverse}, /* inverse */ +{"leftParen", leftParen}, /* TI-specific left parenthesis */ +{"logarithm", logarithm}, /* logarithm base 10 */ +{"multiply", multiply}, /* multiplication */ +{"naturalLog", naturalLog}, /* natural logarithm base e */ +{"negate", negate}, /* change sign */ +{"nop", nop}, /* no operation, rings bell */ +{"off", off}, /* clear state */ +{"pi", pi}, /* the number pi */ +{"power", power}, /* raise to an arbitrary power */ +{"quit", quit}, /* quit */ +{"recall", recall}, /* memory recall */ +{"reciprocal", reciprocal}, /* reciprocal function */ +{"rightParen", rightParen}, /* TI-specific left parenthesis */ +{"roll", roll}, /* HP-specific roll stack */ +{"scientific", scientific}, /* scientfic notation (EE) */ +{"selection", selection}, /* copy selection */ +{"sine", sine}, /* trigonometric function sine */ +{"square", square}, /* square */ +{"squareRoot", squareRoot}, /* square root */ +{"store", store}, /* memory store */ +{"subtract", subtract}, /* subtraction */ +{"sum", sum}, /* memory summation */ +{"tangent", tangent}, /* trigonometric function tangent */ +{"tenpower", tenpower}, /* 10 raised to to an arbitrary power */ +{"XexchangeY", XexchangeY} /* HP-specific exchange X and Y registers */ +}; diff --git a/math.c b/math.c new file mode 100644 index 0000000..5823aee --- /dev/null +++ b/math.c @@ -0,0 +1,1007 @@ +/* $XConsortium: math.c,v 1.17 91/07/25 17:51:34 rws Exp $ + * $XFree86: xc/programs/xcalc/math.c,v 1.5 2001/10/28 03:34:26 tsi Exp $ + * + * math.c - mathematics functions for a hand calculator under X + * + * Author: John H. Bradley, University of Pennsylvania + * (bradley@cis.upenn.edu) + * March, 1987 + * + * RPN mode added and port to X11 by Mark Rosenstein, MIT Project Athena + * + * Modified to be a client of the Xt toolkit and the Athena widget set by + * Donna Converse, MIT X Consortium. This is all that remains of the + * original calculator, and it still needs to be rewritten. The HP + * functionality should be separated from the TI functionality. + * Beware the HP functions: there are still errors here. + */ + +#include +#include +#include +#include +#if !defined(IEEE) && defined(SVR4) +#include +#endif +#include +#include "xcalc.h" +#include +#include + +#ifdef _CRAY /* kludge around Cray STDC compiler */ +double (*log_p)() = log; +#define log ((*log_p)) +double (*exp_p)() = exp; +#define exp ((*exp_p)) +double (*sqrt_p)() = sqrt; +#define sqrt ((*sqrt_p)) +double (*log10_p)() = log10; +#define log10 ((*log10_p)) +double (*atan2_p)() = atan2; +#define atan2 ((*atan2_p)) +double (*asin_p)() = asin; +#define asin ((*asin_p)) +double (*acos_p)() = acos; +#define acos ((*acos_p)) +double (*atan_p)() = atan; +#define atan ((*atan_p)) +double (*sin_p)() = sin; +#define sin ((*sin_p)) +double (*cos_p)() = cos; +#define cos ((*cos_p)) +double (*tan_p)() = tan; +#define tan ((*tan_p)) +double (*pow_p)() = pow; +#define pow ((*pow_p)) +#endif /* _CRAY */ + +#ifndef PI /* sometimes defined in math.h */ +#define PI 3.14159265358979 +#endif +#define E 2.71828182845904 +#define MAXDISP 11 +#define DEG 0 /* DRG mode. used for trig calculations */ +#define RAD 1 +#define GRAD 2 +#define min(a,b) ((a) < (b) ? (a) : (b)) +#define max(a,b) ((a) > (b) ? (a) : (b)) +#define True 1 +#define False 0 + +extern int rpn; +extern char dispstr[]; +extern void draw(); +extern void ringbell(); +extern void setflag(); +extern void Quit(); + +#ifndef IEEE + jmp_buf env; +#endif + + +/* This section is all of the state machine that implements the calculator + * functions. Much of it is shared between the infix and rpn modes. + */ + +int flagINV, flagPAREN, flagM, drgmode; /* display flags */ + +static double drg2rad=PI/180.0; /* Conversion factors for trig funcs */ +static double rad2drg=180.0/PI; +static int entered=1; /* true if display contains a valid number. + if==2, then use 'dnum', rather than the string + stored in the display. (for accuracy) + if==3, then error occurred, only CLR & AC work */ +/* entered seems to be overloaded - dmc */ +static int lift_enabled = 0; /* for rpn mode only */ + +static int CLR =0; /* CLR clears display. if 1, clears acc, also */ +static int OFF =0; /* once clears mem, twice quits */ +static int Dpoint=0; /* to prevent using decimal pt twice in a # */ +static int clrdisp=1; /* if true clears display before entering # */ +static int accset =0; +static int lastop =kCLR; +static int memop =kCLR; +static int exponent=0; +static double acc =0.0; +static double dnum=0.0; +#define XCALC_MEMORY 10 +static double mem[XCALC_MEMORY] = { 0.0 }; + +static void DrawDisplay(void); +static void PushOp(int op); +static int PopOp(void); +static int isopempty(void); +#ifdef DEBUG +static void showstack(char *string); +#endif +static void PushNum(double num); +static double PopNum(void); +static void RollNum(int dir); +static void ClearStacks(void); +static int priority(int op); + +/* + * The following is to deal with the unfortunate assumption that if errno + * is non-zero then an error has occurred. On some systems (e.g. Ultrix), + * sscanf will call lower level routines that will set errno. + */ + +static void +parse_double (src, fmt, dp) + char *src; + char *fmt; + double *dp; +{ + int olderrno = errno; + + (void) sscanf (src, fmt, dp); + errno = olderrno; + return; +} + + +/*********************************/ +int pre_op(keynum) + int keynum; +{ + if (keynum==-1) return(0); + + errno = 0; /* for non-IEEE machines */ + + if ( (entered==3) && !(keynum==kCLR || keynum==kOFF)) { + if (rpn) { + clrdisp++; + } else { + ringbell(); + return(1); /* the intent was probably not to do the operation */ + } + } + + if (keynum != kCLR) CLR=0; + if (keynum != kOFF) OFF=0; + return(0); +} + +#ifndef IEEE + +/* cannot assign result of setjmp under ANSI C, use global instead */ +static int SignalKind; +static int SignalCode; + +void fail_op() +{ + if (SignalKind == SIGFPE) + switch (SignalCode) { +#ifdef SVR4 + case FPE_INTDIV: /* integer divide by zero */ + case FPE_FLTDIV: /* floating point divide by zero */ + strcpy(dispstr, "divide by 0"); + break; + case FPE_INTOVF: /* integer overflow */ + case FPE_FLTOVF: /* floating point overflow */ + strcpy(dispstr, "overflow"); + break; + case FPE_FLTUND: /* floating point underflow */ + strcpy(dispstr, "underflow"); + break; + case FPE_FLTRES: /* floating point inexact result */ + strcpy(dispstr, "inexact result"); + break; + case FPE_FLTINV: /* invalid floating point operation */ + strcpy(dispstr, "invalid op"); + break; + case FPE_FLTSUB: /* subscript out of range */ + strcpy(dispstr, "out of range"); + break; + +#endif /*SVR4*/ + +#ifdef FPE_FLTDIV_TRAP + case FPE_FLTDIV_TRAP: strcpy(dispstr,"div by zero"); break; +#endif +#ifdef FPE_FLTDIV_FAULT + case FPE_FLTDIV_FAULT: strcpy(dispstr,"div by zero"); break; +#endif +#ifdef FPE_FLTOVF_TRAP + case FPE_FLTOVF_TRAP: strcpy(dispstr,"overflow"); break; +#endif +#ifdef FPE_FLTOVF_FAULT + case FPE_FLTOVF_FAULT: strcpy(dispstr,"overflow"); break; +#endif +#ifdef FPE_FLTUND_TRAP + case FPE_FLTUND_TRAP: strcpy(dispstr,"underflow"); break; +#endif +#ifdef FPE_FLTUND_FAULT + case FPE_FLTUND_FAULT: strcpy(dispstr,"underflow"); break; +#endif + default: strcpy(dispstr,"error"); + } + else + if (SignalKind == SIGILL) + strcpy(dispstr, "illegal operand"); + + entered=3; + DrawDisplay(); + return; +} + + +/* keep SVR4 compiler from complaining about scope of arg declaration below */ +typedef struct sigcontext * sigcontextstructp; +/*ARGSUSED*/ +signal_t fperr(sig,code,scp) + int sig,code; + sigcontextstructp scp; +{ +#if defined(SYSV) || defined(SVR4) || defined(linux) + signal(SIGFPE,(signal_t (*)())fperr); +#endif + SignalKind = sig; + SignalCode = code; + longjmp(env,1); +} + +/* for VAX BSD4.3 */ +/*ARGSUSED*/ +signal_t illerr(sig,code,scp) + int sig,code; + sigcontextstructp scp; +{ + /* not reset when caught? */ + signal(SIGILL,(signal_t (*)())illerr); + + SignalKind = sig; + SignalCode = code; + longjmp(env,1); +} + +#endif /* not IEEE */ + + +void post_op() +{ +#ifdef DEBUG + showstack("\0"); +#endif +#ifndef IEEE + if (errno) { + strcpy(dispstr,"error"); + DrawDisplay(); + entered=3; + errno=0; + } +#endif +} +/*-------------------------------------------------------------------------*/ +static void +DrawDisplay(void) +{ + if ((int) strlen(dispstr) > 12) { /* strip out some decimal digits */ + char tmp[32]; + char *estr = index(dispstr,'e'); /* search for exponent part */ + if (!estr) dispstr[12]='\0'; /* no exp, just trunc. */ + else { + if ((int) strlen(estr) <= 4) + sprintf(tmp,"%.8s",dispstr); /* leftmost 8 chars */ + else + sprintf(tmp,"%.7s",dispstr); /* leftmost 7 chars */ + strcat (tmp,estr); /* plus exponent */ + strcpy (dispstr,tmp); + } + } + draw(dispstr); + setflag(XCalc_MEMORY, (flagM)); + setflag(XCalc_INVERSE, (flagINV)); + setflag(XCalc_DEGREE, (drgmode==DEG)); + setflag(XCalc_RADIAN, (drgmode==RAD)); + setflag(XCalc_GRADAM, (drgmode==GRAD)); + setflag(XCalc_PAREN, (flagPAREN)); +} + +/*-------------------------------------------------------------------------*/ +void +numeric(keynum) + int keynum; +{ + char st[2]; + int cell = 0; + + flagINV=0; + + if (rpn && (memop == kSTO || memop == kRCL || memop == kSUM)) { + switch (keynum) { + case kONE: cell = 1; break; + case kTWO: cell = 2; break; + case kTHREE: cell = 3; break; + case kFOUR: cell = 4; break; + case kFIVE: cell = 5; break; + case kSIX: cell = 6; break; + case kSEVEN: cell = 7; break; + case kEIGHT: cell = 8; break; + case kNINE: cell = 9; break; + case kZERO: cell = 0; break; + } + switch (memop) { + case kSTO: + mem[cell] = dnum; + lift_enabled = 1; + entered = 2; + clrdisp++; + break; + case kRCL: + PushNum(dnum); + dnum = mem[cell]; + sprintf(dispstr, "%.8g", dnum); + lift_enabled = 1; + entered = 1; + clrdisp = 0; + break; + case kSUM: + mem[cell] += dnum; + lift_enabled = 1; + entered = 2; + clrdisp++; + break; + } + memop = kCLR; + DrawDisplay(); + return; + } + + if (clrdisp) { + dispstr[0]='\0'; + exponent=Dpoint=0; +/* if (rpn && entered==2) + PushNum(dnum); + */ + if (rpn & lift_enabled) + PushNum(dnum); + } + if ((int) strlen(dispstr) >= MAXDISP) + return; + + switch (keynum){ + case kONE: st[0] = '1'; break; + case kTWO: st[0] = '2'; break; + case kTHREE: st[0] = '3'; break; + case kFOUR: st[0] = '4'; break; + case kFIVE: st[0] = '5'; break; + case kSIX: st[0] = '6'; break; + case kSEVEN: st[0] = '7'; break; + case kEIGHT: st[0] = '8'; break; + case kNINE: st[0] = '9'; break; + case kZERO: st[0] = '0'; break; + } + st[1] = '\0'; + strcat(dispstr,st); + + DrawDisplay(); + if (clrdisp && keynum != kZERO) + clrdisp=0; /*no leading 0s*/ + entered=1; + lift_enabled = 0; +} + +void +bkspf(void) +{ + + lift_enabled = 0; + + if (! flagINV) + { + if (entered!=1 || clrdisp) + return; + if ((int) strlen(dispstr) > 0) + dispstr[strlen(dispstr)-1] = 0; + if (strlen(dispstr) == 0) { + strcat(dispstr, "0"); + clrdisp++; + } + } + else + { + strcpy(dispstr, "0"); + dnum = 0.0; + clrdisp++; + flagINV = 0; + } + DrawDisplay(); +} + +void +decf(void) +{ + flagINV=0; + if (clrdisp) { + if (rpn) + PushNum(dnum); + strcpy(dispstr,"0"); + } + if (!Dpoint) { +#ifndef X_LOCALE + strcat(dispstr, localeconv()->decimal_point); +#else + strcat(dispstr, "."); +#endif + DrawDisplay(); + Dpoint++; + } + clrdisp=0; + entered=1; +} + +void +eef(void) +{ + flagINV=0; + if (clrdisp) { + if (rpn && lift_enabled) + PushNum(dnum); + strcpy(dispstr, rpn ? "1" : "0"); + } + if (!exponent) { + strcat(dispstr,"E+"); + DrawDisplay(); + exponent=strlen(dispstr)-1; /* where the '-' goes */ + } + clrdisp=0; + entered=1; +} + +void +clearf(void) +{ + flagINV=0; + if (CLR && !rpn) { /* clear all */ + ClearStacks(); + flagPAREN=0; + } + CLR++; + exponent=Dpoint=0; + clrdisp=1; + entered=1; + strcpy(dispstr,"0"); + DrawDisplay(); +} + +void +negf(void) +{ + flagINV=0; + if (exponent) { /* neg the exponent */ + if (dispstr[exponent]=='-') + dispstr[exponent]='+'; + else + dispstr[exponent]='-'; + DrawDisplay(); + return; + } + + if (strcmp("0",dispstr)==0) + return; /* don't neg a zero */ + if (dispstr[0]=='-') /* already neg-ed */ + strcpy(dispstr,dispstr+1); /* move str left once */ + else { /* not neg-ed. add a '-' */ + char tmp[32]; + sprintf(tmp,"-%s",dispstr); + strcpy(dispstr,tmp); + } + if (entered==2) + dnum = -1.0 * dnum; + DrawDisplay(); +} + +/* Two operand functions for infix calc */ +void +twoop(int keynum) +{ + if (flagINV) { + flagINV=0; + DrawDisplay(); + } + + if (!entered) { /* something like "5+*" */ + if (!isopempty()) + (void) PopOp(); /* replace the prev op */ + PushOp(keynum); /* with the new one */ + return; + } + + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + + clrdisp=CLR=1; + entered=Dpoint=exponent=0; + + if (!isopempty()) { /* there was a previous op */ + lastop=PopOp(); /* get it */ + + if (lastop==kLPAR) { /* put it back */ + PushOp(kLPAR); + PushOp(keynum); + PushNum(dnum); + return; + } + + /* now, if the current op (keynum) is of + higher priority than the lastop, the current + op and number are just pushed on top + Priorities: (Y^X) > *,/ > +,- */ + + if (priority(keynum) > priority(lastop)) { + PushNum(dnum); + PushOp(lastop); + PushOp(keynum); + } else { /* execute lastop on lastnum and dnum, push + result and current op on stack */ + acc=PopNum(); + switch (lastop) { /* perform the operation */ + case kADD: acc += dnum; break; + case kSUB: acc -= dnum; break; + case kMUL: acc *= dnum; break; + case kDIV: acc /= dnum; break; + case kPOW: acc = pow(acc,dnum); break; + } + PushNum(acc); + PushOp(keynum); + sprintf(dispstr,"%.8g",acc); + DrawDisplay(); + dnum=acc; + } + } + else { /* op stack is empty, push op and num */ + PushOp(keynum); + PushNum(dnum); + } +} + +/* Two operand functions for rpn calc */ +void +twof(int keynum) +{ + if (flagINV) { + flagINV=0; + DrawDisplay(); + } + if (!entered) + return; + if (entered==1) + parse_double(dispstr, "%lf", &dnum); + acc = PopNum(); + switch(keynum) { + case kADD: acc += dnum; break; + case kSUB: acc -= dnum; break; + case kMUL: acc *= dnum; break; + case kDIV: acc /= dnum; break; + case kPOW: acc = pow(acc,dnum); break; + case kXXY: PushNum(dnum); + } + sprintf(dispstr, "%.8g", acc); + DrawDisplay(); + clrdisp++; + Dpoint = exponent = 0; + entered = 2; + lift_enabled = 1; + dnum = acc; +} + +void +entrf(void) +{ + flagINV=0; + if (!entered) + return; + + clrdisp=CLR=1; + Dpoint=exponent=0; + + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + entered=2; + memop = kENTR; + PushNum(dnum); + lift_enabled = 0; +} + +void +equf(void) +{ + flagINV=0; + if (!entered) + return; + + clrdisp=CLR=1; + Dpoint=exponent=0; + + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + entered=2; + + PushNum(dnum); + + while (!isopempty()) { /* do all pending ops */ + dnum=PopNum(); + acc=PopNum(); + lastop=PopOp(); + switch (lastop) { + case kADD: acc += dnum; + break; + case kSUB: acc -= dnum; + break; + case kMUL: acc *= dnum; + break; + case kDIV: acc /= dnum; + break; + case kPOW: acc = pow(acc,dnum); + break; + case kLPAR: flagPAREN--; + PushNum(acc); + break; + } + dnum=acc; + PushNum(dnum); + } + + sprintf(dispstr,"%.8g",dnum); + DrawDisplay(); +} + +void +lparf(void) +{ + flagINV=0; + PushOp(kLPAR); + flagPAREN++; + DrawDisplay(); +} + +void +rollf(void) +{ + if (!entered) + return; + if (entered==1) + parse_double(dispstr, "%lf", &dnum); + entered = 2; + lift_enabled = 1; + RollNum(flagINV); + flagINV=0; + clrdisp++; + sprintf(dispstr, "%.8g", dnum); + DrawDisplay(); +} + +void +rparf(void) +{ + flagINV=0; + if (!entered) + return; + + if (!flagPAREN) + return; + + clrdisp++; + Dpoint=exponent=0; + + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + entered=2; + + PushNum(dnum); + while (!isopempty() && (lastop=PopOp())!=kLPAR) { + /* do all pending ops, back to left paren */ + dnum=PopNum(); + acc=PopNum(); + switch (lastop) { + case kADD: acc += dnum; + break; + case kSUB: acc -= dnum; + break; + case kMUL: acc *= dnum; + break; + case kDIV: acc /= dnum; + break; + case kPOW: acc = pow(acc,dnum); + break; + } + dnum=acc; + PushNum(dnum); + } + (void) PopNum(); + flagPAREN--; + entered=2; + sprintf(dispstr,"%.8g",dnum); + DrawDisplay(); +} + +void +drgf(void) +{ + if (flagINV) { + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + switch (drgmode) { + case DEG: dnum=dnum*PI/180.0; break; + case RAD: dnum=dnum*200.0/PI; break; + case GRAD: dnum=dnum*90.0/100.0; break; + } + entered=2; + clrdisp=1; + flagINV=0; + sprintf(dispstr,"%.8g",dnum); + } + + flagINV=0; + drgmode = (drgmode + 1) % 3; + switch (drgmode) { + case DEG: drg2rad=PI / 180.0; + rad2drg=180.0 / PI; + break; + case RAD: drg2rad=1.0; + rad2drg=1.0; + break; + case GRAD: drg2rad=PI / 200.0; + rad2drg=200.0 / PI; + break; + } + DrawDisplay(); +} + +void +invf(void) +{ + flagINV = ~flagINV; + DrawDisplay(); +} + +void +memf(int keynum) +{ + memop = keynum; + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + entered = 2; + clrdisp++; + lift_enabled = 0; +} + +void +oneop(int keynum) +{ + int i,j; + double dtmp; + + if (entered==1) + parse_double(dispstr,"%lf",&dnum); + entered = 2; + + switch (keynum) { /* do the actual math fn. */ + case kE: if (rpn && memop != kENTR) PushNum(dnum); dnum=E; break; + case kPI: if (rpn && memop != kENTR) PushNum(dnum); dnum=PI; break; + case kRECIP: dnum=1.0/dnum; break; + case kSQR: flagINV = !flagINV; /* fall through to */ + case kSQRT: if (flagINV) dnum=dnum*dnum; + else dnum=sqrt(dnum); + break; + case k10X: flagINV = !flagINV; /* fall through to */ + case kLOG: if (flagINV) dnum=pow(10.0,dnum); + else dnum=log10(dnum); + break; + case kEXP: flagINV = !flagINV; /* fall through to */ + case kLN: if (flagINV) dnum=exp(dnum); + else dnum=log(dnum); + break; + case kSIN: if (flagINV) dnum=asin(dnum)*rad2drg; + else dnum=sin(dnum*drg2rad); + break; + case kCOS: if (flagINV) dnum=acos(dnum)*rad2drg; + else dnum=cos(dnum*drg2rad); + break; + case kTAN: if (flagINV) dnum=atan(dnum)*rad2drg; + else dnum=tan(dnum*drg2rad); + break; + case kSTO: mem[0]=dnum; flagM=!(mem[0]==0.0); break; + case kRCL: if (rpn && lift_enabled) PushNum(dnum); + dnum=mem[0]; flagM=!(mem[0]==0.0); break; + case kSUM: mem[0]+=dnum; flagM=!(mem[0]==0.0); break; + case kEXC: dtmp=dnum; dnum=mem[0]; mem[0]=dtmp; + flagM=!(mem[0]==0.0); break; + case kFACT: if (floor(dnum)!=dnum || dnum<0.0 || dnum>500.0) { + strcpy(dispstr,"error"); + entered=3; + break; + } + i=(int) (floor(dnum)); + for (j=1,dnum=1.0; j<=i; j++) + dnum*=(float) j; + break; + } + + if (entered==3) { /* error */ + DrawDisplay(); + return; + } + + entered=2; + clrdisp=1; + flagINV=0; + lift_enabled = 1; + sprintf(dispstr,"%.8g",dnum); + DrawDisplay(); +} + +void +offf(void) +{ + /* full reset */ + int i; + ResetCalc(); + entered=clrdisp=1; + lift_enabled = 0; + dnum=mem[0]=0.0; + if (rpn) + for (i=1; i < XCALC_MEMORY; i++) + mem[i]=0.0; + accset=exponent=Dpoint=0; + DrawDisplay(); +} + + +#define STACKMAX 32 +static int opstack[STACKMAX]; +static int opsp; +static double numstack[STACKMAX]; +static int numsp; + + +/*******/ +static void +PushOp(op) + int op; +/*******/ +{ + if (opsp==STACKMAX) {strcpy(dispstr,"stack error"); entered=3;} + else opstack[opsp++]=op; +} + +/*******/ +static int +PopOp(void) +/*******/ +{ + if (opsp==0) { + strcpy(dispstr,"stack error"); + entered=3; + return(kNOP); + } else + return(opstack[--opsp]); +} + +/*******/ +static int +isopempty(void) +/*******/ +{ + return( opsp ? 0 : 1 ); +} + +#ifdef DEBUG +static void +showstack(string) + char *string; +{ + fprintf(stderr, "%s: %lf %lf %lf\n", string, numstack[0], numstack[1], + numstack[2]); +} +#endif + +/*******/ +static void +PushNum(num) + double num; +/*******/ +{ + if (rpn) { + numstack[2] = numstack[1]; + numstack[1] = numstack[0]; + numstack[0] = num; + return; + } + if (numsp==STACKMAX) { + strcpy(dispstr,"stack error"); + entered=3; + } else + numstack[numsp++]=num; +} + +/*******/ +static double +PopNum(void) +/*******/ +{ + if (rpn) { + double tmp = numstack[0]; + numstack[0] = numstack[1]; + numstack[1] = numstack[2]; + return(tmp); + } + if (numsp==0) { + strcpy(dispstr,"stack error"); + entered=3; + return 0.0; + } else + return(numstack[--numsp]); +} + +/*******/ +static void +RollNum(int dir) +/*******/ +{ + double tmp; + + if (dir) { /* roll up */ + tmp = dnum; + dnum = numstack[2]; + numstack[2] = numstack[1]; + numstack[1] = numstack[0]; + numstack[0] = tmp; + } else { /* roll down */ + tmp = dnum; + dnum = numstack[0]; + numstack[0] = numstack[1]; + numstack[1] = numstack[2]; + numstack[2] = tmp; + } +} + + +/*******/ +static void +ClearStacks(void) +/*******/ +{ + if (rpn) + numstack[0] = numstack[1] = numstack[2] = 0.; + opsp=numsp=0; +} + + +/*******/ +static int +priority(op) + int op; +/*******/ +{ + switch (op) { + case kPOW: return(2); + case kMUL: + case kDIV: return(1); + case kADD: + case kSUB: return(0); + } + return 0; +} + + +/********/ +void +ResetCalc(void) +/********/ +{ + flagM=flagINV=flagPAREN=0; drgmode=DEG; + setflag(XCalc_MEMORY, False); + setflag(XCalc_INVERSE, False); + setflag(XCalc_PAREN, False); + setflag(XCalc_RADIAN, False); + setflag(XCalc_GRADAM, False); + setflag(XCalc_DEGREE, True); + strcpy(dispstr,"0"); + draw(dispstr); + ClearStacks(); + drg2rad=PI/180.0; + rad2drg=180.0/PI; +} diff --git a/xcalc.c b/xcalc.c new file mode 100644 index 0000000..775ab98 --- /dev/null +++ b/xcalc.c @@ -0,0 +1,402 @@ +/* $XConsortium: xcalc.c,v 1.16 94/04/17 20:43:31 converse Exp $ */ +/* + +Copyright (c) 1989 X Consortium + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +Except as contained in this notice, the name of the X Consortium shall +not be used in advertising or otherwise to promote the sale, use or +other dealings in this Software without prior written authorization +from the X Consortium. + +*/ +/* $XFree86: xc/programs/xcalc/xcalc.c,v 1.2 2000/05/11 18:14:41 tsi Exp $ */ + +/* + * xcalc.c - a hand calculator for the X Window system + * + * Original Author: John H. Bradley, University of Pennsylvania + * (bradley@cis.upenn.edu) March, 1987 + * RPN mode added and port to X11 by Mark Rosenstein, MIT Project Athena + * Rewritten to be an Xaw and Xt client by Donna Converse, MIT X Consortium + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "xcalc.h" +#include "actions.h" + +#ifndef IEEE +extern signal_t fperr(); +extern signal_t illerr(); +#endif + +/* + * global data + */ +int rpn = 0; /* Reverse Polish Notation (HP mode) flag */ +#define LCD_STR_LEN 32 +char dispstr[LCD_STR_LEN]; /* string to show up in the LCD */ +Atom wm_delete_window; /* see ICCCM section 5.2.2 */ + +/* + * local data + */ +static Display *dpy = NULL; /* connection to the X server */ +static Widget toplevel=NULL; /* top level shell widget */ +static Widget calculator=NULL; /* an underlying form widget */ +static Widget LCD = NULL; /* liquid crystal display */ +static Widget ind[6]; /* mode indicators in the screen */ +static char selstr[LCD_STR_LEN]; /* storage for selections from the LCD */ + /* checkerboard used in mono mode */ +static XtAppContext xtcontext; /* Toolkit application context */ +#define check_width 16 +#define check_height 16 +static unsigned char check_bits[] = { + 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, + 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa, + 0x55, 0x55, 0xaa, 0xaa, 0x55, 0x55, 0xaa, 0xaa}; + +/* command line options specific to the application */ +static XrmOptionDescRec Options[] = { +{"-rpn", "rpn", XrmoptionNoArg, (XtPointer)"on"}, +{"-stipple", "stipple", XrmoptionNoArg, (XtPointer)"on"} +}; + +/* resources specific to the application */ +static struct resources { + Boolean rpn; /* reverse polish notation (HP mode) */ + Boolean stipple; /* background stipple */ + Cursor cursor; +} appResources; + +#define offset(field) XtOffsetOf(struct resources, field) +static XtResource Resources[] = { +{"rpn", "Rpn", XtRBoolean, sizeof(Boolean), + offset(rpn), XtRImmediate, (XtPointer) False}, +{"stipple", "Stipple", XtRBoolean, sizeof(Boolean), + offset(stipple), XtRImmediate, (XtPointer) False}, +{"cursor", "Cursor", XtRCursor, sizeof(Cursor), + offset(cursor), XtRCursor, (XtPointer)NULL} +}; +#undef offset + + +int +main(argc, argv) + int argc; + char **argv; +{ + Arg args[3]; + + void create_calculator(); + void Quit(), Syntax(); + + + XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL); + + toplevel = XtAppInitialize(&xtcontext, "XCalc", Options, XtNumber(Options), + &argc, argv, NULL, NULL, 0); + if (argc != 1) Syntax(argc, argv); + + XtSetArg(args[0], XtNinput, True); + XtSetValues(toplevel, args, ONE); + + XtGetApplicationResources(toplevel, (XtPointer)&appResources, Resources, + XtNumber(Resources), (ArgList) NULL, ZERO); + + create_calculator(toplevel); + + XtAppAddActions(xtcontext, Actions, XtNumber(Actions)); + + XtOverrideTranslations(toplevel, + XtParseTranslationTable("WM_PROTOCOLS: quit()\n")); + + XtRealizeWidget(toplevel); + + dpy = XtDisplay(toplevel); + wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False); + (void) XSetWMProtocols(dpy, XtWindow(toplevel), &wm_delete_window, 1); + XDefineCursor(dpy, XtWindow(toplevel), appResources.cursor); + + if (appResources.stipple || (CellsOfScreen(XtScreen(toplevel)) <= 2)) + { + Screen *screen = XtScreen(toplevel); + Pixmap backgroundPix; + + backgroundPix = XCreatePixmapFromBitmapData + (dpy, XtWindow(toplevel), + (char *)check_bits, check_width, check_height, + WhitePixelOfScreen(screen), BlackPixelOfScreen(screen), + DefaultDepthOfScreen(screen)); + XtSetArg(args[0], XtNbackgroundPixmap, backgroundPix); + XtSetValues(calculator, args, ONE); + } + +#ifndef IEEE + signal(SIGFPE,fperr); + signal(SIGILL,illerr); +#endif + ResetCalc(); + XtAppMainLoop(xtcontext); + + return 0; +} + +void create_calculator(shell) + Widget shell; +{ + void create_display(); + void create_keypad(); + + rpn = appResources.rpn; + calculator = XtCreateManagedWidget(rpn ? "hp" : "ti", formWidgetClass, + shell, (ArgList) NULL, ZERO); + create_display(calculator); + create_keypad(calculator); + XtSetKeyboardFocus(calculator, LCD); +} + +/* + * Do the calculator data display widgets. + */ +void create_display(parent) + Widget parent; +{ + Widget bevel, screen; + static Arg args[] = { + {XtNborderWidth, (XtArgVal)0}, + {XtNjustify, (XtArgVal)XtJustifyRight} + }; + + /* the frame surrounding the calculator display */ + bevel = XtCreateManagedWidget("bevel", formWidgetClass, parent, + (ArgList) NULL, ZERO); + + /* the screen of the calculator */ + screen = XtCreateManagedWidget("screen", formWidgetClass, bevel, + (ArgList) NULL, ZERO); + + /* M - the memory indicator */ + ind[XCalc_MEMORY] = XtCreateManagedWidget("M", labelWidgetClass, screen, + args, XtNumber(args)); + + /* liquid crystal display */ + LCD = XtCreateManagedWidget("LCD", toggleWidgetClass, screen, args, + XtNumber(args)); + + /* INV - the inverse function indicator */ + ind[XCalc_INVERSE] = XtCreateManagedWidget("INV", labelWidgetClass, + screen, args, XtNumber(args)); + + /* DEG - the degrees switch indicator */ + ind[XCalc_DEGREE] = XtCreateManagedWidget("DEG", labelWidgetClass, screen, + args, XtNumber(args)); + + /* RAD - the radian switch indicator */ + ind[XCalc_RADIAN] = XtCreateManagedWidget("RAD", labelWidgetClass, screen, + args, XtNumber(args)); + + /* GRAD - the grad switch indicator */ + ind[XCalc_GRADAM] = XtCreateManagedWidget("GRAD", labelWidgetClass, screen, + args, XtNumber(args)); + + /* () - the parenthesis indicator */ + ind[XCalc_PAREN] = XtCreateManagedWidget("P", labelWidgetClass, screen, + args, XtNumber(args)); +} + +/* + * Do all the buttons. The application defaults file will give the + * default button placement, default button labels, and default + * actions connected to the buttons. The user can change any of + * these defaults in an environment-specific resource file. + */ + +void create_keypad(parent) + Widget parent; +{ + static char *Keyboard[] = { + "button1", "button2", "button3", "button4", "button5", + "button6", "button7", "button8", "button9", "button10", + "button11","button12","button13","button14","button15", + "button16","button17","button18","button19","button20", + "button21","button22","button23","button24","button25", + "button26","button27","button28","button29","button30", + "button31","button32","button33","button34","button35", + "button36","button37","button38","button39","button40" + }; + register int i; + int n = XtNumber(Keyboard); + + if (appResources.rpn) n--; /* HP has 39 buttons, TI has 40 */ + + for (i=0; i < n; i++) + XtCreateManagedWidget(Keyboard[i], commandWidgetClass, parent, + (ArgList) NULL, ZERO); +} + +/* + * Miscellaneous utility routines that interact with the widgets. + */ + +/* + * called by math routines to write to the liquid crystal display. + */ +void draw(string) + char *string; +{ + Arg args[1]; + + XtSetArg(args[0], XtNlabel, string); + XtSetValues(LCD, args, ONE); +} +/* + * called by math routines to turn on and off the display indicators. + */ +void setflag(indicator, on) + int indicator; + Boolean on; +{ + if (on) XtMapWidget(ind[indicator]); + else XtUnmapWidget(ind[indicator]); +} + +/* + * ring the bell. + */ +void ringbell() +{ + XBell(dpy, 0); +} + +/* + * die. + */ +void Quit() +{ + extern void exit(); + XtDestroyApplicationContext(xtcontext); + exit(0); +} + +/* + * recite and die. + */ +void Syntax(argc, argv) + int argc; + char **argv; +{ + register int i; + extern void exit(); + (void) fprintf(stderr, "%s: unknown options:", argv[0]); + for (i=1; i