1 /******************************************************************************** 2 * * 3 * F O X D e s k t o p C a l c u l a t o r * 4 * * 5 ********************************************************************************* 6 * Copyright (C) 2001,2021 by Jeroen van der Zijp. All Rights Reserved. * 7 ********************************************************************************* 8 * This program is free software: you can redistribute it and/or modify * 9 * it under the terms of the GNU General Public License as published by * 10 * the Free Software Foundation, either version 3 of the License, or * 11 * (at your option) any later version. * 12 * * 13 * This program is distributed in the hope that it will be useful, * 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 16 * GNU General Public License for more details. * 17 * * 18 * You should have received a copy of the GNU General Public License * 19 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 20 ********************************************************************************/ 21 #ifndef CALCULATOR_H 22 #define CALCULATOR_H 23 24 25 /*******************************************************************************/ 26 27 28 // Show exponent 29 enum { 30 EXPONENT_NEVER, 31 EXPONENT_ALWAYS, 32 EXPONENT_IFNEEDED 33 }; 34 35 36 // Mini application object 37 class Calculator : public FXMainWindow { 38 FXDECLARE(Calculator) 39 protected: 40 enum{STACKLEN=32}; 41 protected: 42 FXTextField *display; // Number display 43 FXFont *font; // Display font 44 FXButton *digit[16]; // Digit buttons 45 FXButton *memory[4]; // Memory buttons 46 FXButton *clearbtn; // Clear entry button 47 FXButton *clearallbtn; // Clear all button 48 FXButton *inverse; // Inverse function 49 FXButton *hyper2; // Hyper (for trig) button 50 FXButton *functions[16]; // Function buttons 51 FXButton *operators[14]; // Operator buttons 52 FXButton *numbase[4]; // Numeric base 53 FXButton *angmode[3]; // Angle mode (deg, rad, grad) 54 FXIcon *bigicon; // Big application icon 55 FXIcon *smallicon; // Small application icon 56 FXIcon *cmem; // Label 57 FXIcon *quest; // Question mark 58 FXdouble numstack[STACKLEN]; // Evaluation stack 59 FXint numsp; // Evaluation stack pointer 60 FXuchar opstack[STACKLEN]; // Operator stack 61 FXint opsp; // Operator stack pointer 62 FXdouble recall; // Memory cell 63 FXint limit; // Maximum displayed digits 64 FXint digits; // Number of digits entered 65 FXint base; // Number base 66 FXint angles; // Angle mode 67 FXint precision; // How many digits to show 68 FXuchar exponent; // Exponential notation mode 69 FXbool beep; // Beep on error 70 FXint parens; // Count of ( and ) 71 FXuchar modifiers; // Invert, hyperbolic, entry modifiers 72 static const char help[]; 73 protected: Calculator()74 Calculator(){} 75 private: 76 Calculator(const Calculator&); 77 Calculator &operator=(const Calculator&); 78 void setDisplayText(const FXString& txt); 79 FXString getDisplayText() const; 80 void setDisplayValue(FXdouble val); 81 FXdouble getDisplayValue() const; 82 FXdouble trigarg(FXdouble ang) const; 83 FXdouble trigres(FXdouble res) const; 84 FXdouble pushnum(FXdouble num); 85 FXdouble setnum(FXdouble num); 86 FXdouble popnum(); 87 FXdouble getnum(); 88 void dyop(FXuchar op); 89 void clear(); 90 void unary(FXuchar op); 91 void dyadic(FXuchar op); 92 void evaluate(); 93 void lparen(); 94 void rparen(); 95 public: 96 long onCmdAngle(FXObject*,FXSelector,void*); 97 long onUpdAngle(FXObject*,FXSelector,void*); 98 long onCmdBase(FXObject*,FXSelector,void*); 99 long onUpdBase(FXObject*,FXSelector,void*); 100 long onUpdDigit(FXObject*,FXSelector,void*); 101 long onCmdDigit(FXObject*,FXSelector,void*); 102 long onCmdPoint(FXObject*,FXSelector,void*); 103 long onUpdPoint(FXObject*,FXSelector,void*); 104 long onCmdExp(FXObject*,FXSelector,void*); 105 long onUpdExp(FXObject*,FXSelector,void*); 106 long onCmdDelete(FXObject*,FXSelector,void*); 107 long onCmdClear(FXObject*,FXSelector,void*); 108 long onCmdClearAll(FXObject*,FXSelector,void*); 109 long onCmdInverse(FXObject*,FXSelector,void*); 110 long onCmdHyper(FXObject*,FXSelector,void*); 111 long onCmdMemRec(FXObject*,FXSelector,void*); 112 long onCmdMemAdd(FXObject*,FXSelector,void*); 113 long onCmdMemSub(FXObject*,FXSelector,void*); 114 long onCmdMemClr(FXObject*,FXSelector,void*); 115 long onCmdSin(FXObject*,FXSelector,void*); 116 long onUpdSin(FXObject*,FXSelector,void*); 117 long onCmdCos(FXObject*,FXSelector,void*); 118 long onUpdCos(FXObject*,FXSelector,void*); 119 long onCmdTan(FXObject*,FXSelector,void*); 120 long onUpdTan(FXObject*,FXSelector,void*); 121 long onCmdLog(FXObject*,FXSelector,void*); 122 long onUpdLog(FXObject*,FXSelector,void*); 123 long onCmdLn(FXObject*,FXSelector,void*); 124 long onUpdLn(FXObject*,FXSelector,void*); 125 long onCmdPi(FXObject*,FXSelector,void*); 126 long onUpdPi(FXObject*,FXSelector,void*); 127 long onCmdFac(FXObject*,FXSelector,void*); 128 long onUpdFac(FXObject*,FXSelector,void*); 129 long onCmdPer(FXObject*,FXSelector,void*); 130 long onUpdPer(FXObject*,FXSelector,void*); 131 long onCmdCom(FXObject*,FXSelector,void*); 132 long onUpdCom(FXObject*,FXSelector,void*); 133 long onCmdRecip(FXObject*,FXSelector,void*); 134 long onUpdRecip(FXObject*,FXSelector,void*); 135 long onCmdPlusMin(FXObject*,FXSelector,void*); 136 long onCmdXToY(FXObject*,FXSelector,void*); 137 long onUpdXToY(FXObject*,FXSelector,void*); 138 long onCmdSqrt(FXObject*,FXSelector,void*); 139 long onUpdSqrt(FXObject*,FXSelector,void*); 140 long onCmdShl(FXObject*,FXSelector,void*); 141 long onUpdShl(FXObject*,FXSelector,void*); 142 long onCmdShr(FXObject*,FXSelector,void*); 143 long onUpdShr(FXObject*,FXSelector,void*); 144 long onCmd2Log(FXObject*,FXSelector,void*); 145 long onUpd2Log(FXObject*,FXSelector,void*); 146 long onCmdLPar(FXObject*,FXSelector,void*); 147 long onCmdRPar(FXObject*,FXSelector,void*); 148 long onCmdAnd(FXObject*,FXSelector,void*); 149 long onCmdOr(FXObject*,FXSelector,void*); 150 long onCmdXor(FXObject*,FXSelector,void*); 151 long onCmdNot(FXObject*,FXSelector,void*); 152 long onCmdMul(FXObject*,FXSelector,void*); 153 long onCmdDiv(FXObject*,FXSelector,void*); 154 long onCmdMod(FXObject*,FXSelector,void*); 155 long onUpdMod(FXObject*,FXSelector,void*); 156 long onCmdAdd(FXObject*,FXSelector,void*); 157 long onCmdSub(FXObject*,FXSelector,void*); 158 long onCmdEnter(FXObject*,FXSelector,void*); 159 long onCmdPreferences(FXObject*,FXSelector,void*); 160 long onUpdColor(FXObject*,FXSelector,void*); 161 long onCmdColor(FXObject*,FXSelector,void*); 162 long onCmdFont(FXObject*,FXSelector,void*); 163 long onCmdExponent(FXObject*,FXSelector,void*); 164 long onUpdExponent(FXObject*,FXSelector,void*); 165 long onCmdEngineeringMode(FXObject*,FXSelector,void*); 166 long onUpdEngineeringMode(FXObject*,FXSelector,void*); 167 long onCmdPrecision(FXObject*,FXSelector,void*); 168 long onUpdPrecision(FXObject*,FXSelector,void*); 169 long onCmdBeep(FXObject*,FXSelector,void*); 170 long onUpdBeep(FXObject*,FXSelector,void*); 171 long onCmdQuestion(FXObject*,FXSelector,void*); 172 public: 173 enum { 174 MOD_INV=1, // Modes 175 MOD_HYP=2, 176 MOD_ENT=4 177 }; 178 enum { 179 ANG_DEG, // Angle modes 180 ANG_RAD, 181 ANG_GRA 182 }; 183 enum { 184 NUM_BIN=2, // Number bases 185 NUM_OCT=8, 186 NUM_DEC=10, 187 NUM_HEX=16 188 }; 189 enum { 190 DY_OR, // Dyadics 191 DY_XOR, 192 DY_AND, 193 DY_SUB, 194 DY_ADD, 195 DY_MOD, 196 DY_IDIV, 197 DY_DIV, 198 DY_MUL, 199 DY_XTOY, 200 DY_XTOINVY, 201 DY_PER, 202 DY_COM, 203 DY_LPAR, 204 DY_RPAR 205 }; 206 enum { 207 UN_NOT, // Unaries 208 UN_NEG, 209 UN_SHL, 210 UN_SHR, 211 UN_SAR, 212 UN_RECIP, 213 UN_FAC, 214 UN_SQRT, 215 UN_QUAD, 216 UN_2LOG, 217 UN_2TOX, 218 UN_LOG, 219 UN_10TOX, 220 UN_LN, 221 UN_EXP, 222 UN_SIN, 223 UN_ASIN, 224 UN_SINH, 225 UN_ASINH, 226 UN_COS, 227 UN_ACOS, 228 UN_COSH, 229 UN_ACOSH, 230 UN_TAN, 231 UN_ATAN, 232 UN_TANH, 233 UN_ATANH 234 }; 235 public: 236 enum { 237 ID_TEXT=FXMainWindow::ID_LAST, 238 ID_PREFERENCES, 239 ID_COLOR_DISPLAY, 240 ID_COLOR_DISPLAYNUMBER, 241 ID_COLOR_DIGITS, 242 ID_COLOR_HEXDIGITS, 243 ID_COLOR_OPERATORS, 244 ID_COLOR_FUNCTIONS, 245 ID_COLOR_MEMORY, 246 ID_COLOR_BASE, 247 ID_COLOR_ANGLES, 248 ID_COLOR_INVERT, 249 ID_COLOR_HYPER, 250 ID_COLOR_CLEARALL, 251 ID_COLOR_CLEAR, 252 ID_EXPONENT_ALWAYS, 253 ID_EXPONENT_NEVER, 254 ID_ENGINEERING_MODE, 255 ID_PRECISION, 256 ID_QUESTION, 257 ID_BEEP, 258 ID_FONT, 259 ID_BASE, 260 ID_BIN=ID_BASE+NUM_BIN, 261 ID_OCT=ID_BASE+NUM_OCT, 262 ID_DEC=ID_BASE+NUM_DEC, 263 ID_HEX=ID_BASE+NUM_HEX, 264 ID_MODE, 265 ID_DEG=ID_MODE+ANG_DEG, 266 ID_RAD=ID_MODE+ANG_RAD, 267 ID_GRA=ID_MODE+ANG_GRA, 268 ID_MEM_REC, 269 ID_MEM_ADD, 270 ID_MEM_SUB, 271 ID_MEM_CLR, 272 ID_PLUSMIN, 273 ID_ENTER, 274 ID_CLEAR, 275 ID_CLEARALL, 276 ID_DELETE, 277 ID_INV, 278 ID_HYP, 279 ID_EXP, 280 ID_PNT, 281 ID_PI, 282 ID_0, 283 ID_1, 284 ID_2, 285 ID_3, 286 ID_4, 287 ID_5, 288 ID_6, 289 ID_7, 290 ID_8, 291 ID_9, 292 ID_A, 293 ID_B, 294 ID_C, 295 ID_D, 296 ID_E, 297 ID_F, 298 ID_OR, 299 ID_XOR, 300 ID_AND, 301 ID_SUB, 302 ID_ADD, 303 ID_MOD, 304 ID_DIV, 305 ID_MUL, 306 ID_XTOY, 307 ID_PER, 308 ID_COM, 309 ID_LPAR, 310 ID_RPAR, 311 ID_NOT, 312 ID_SHL, 313 ID_SHR, 314 ID_RECIP, 315 ID_FAC, 316 ID_SQRT, 317 ID_2LOG, 318 ID_LOG, 319 ID_LN, 320 ID_SIN, 321 ID_COS, 322 ID_TAN, 323 ID_LAST 324 }; 325 public: 326 327 /// Construct calculator dialog 328 Calculator(FXApp* a); 329 330 // Close the window and save registry 331 virtual FXbool close(FXbool notify=false); 332 333 /// Create 334 virtual void create(); 335 336 /// Set digit color 337 void setDigitColor(FXColor clr); 338 FXColor getDigitColor() const; 339 340 /// Set digit color 341 void setHexDigitColor(FXColor clr); 342 FXColor getHexDigitColor() const; 343 344 /// Set operator color 345 void setOperatorColor(FXColor clr); 346 FXColor getOperatorColor() const; 347 348 /// Set function color 349 void setFunctionColor(FXColor clr); 350 FXColor getFunctionColor() const; 351 352 /// Set memory color 353 void setMemoryColor(FXColor clr); 354 FXColor getMemoryColor() const; 355 356 /// Set inverse color 357 void setInverseColor(FXColor clr); 358 FXColor getInverseColor() const; 359 360 /// Set hyp color 361 void setHyperColor(FXColor clr); 362 FXColor getHyperColor() const; 363 364 /// Set clear color 365 void setClearColor(FXColor clr); 366 FXColor getClearColor() const; 367 368 /// Set clear all color 369 void setClearAllColor(FXColor clr); 370 FXColor getClearAllColor() const; 371 372 /// Set display color 373 void setDisplayColor(FXColor clr); 374 FXColor getDisplayColor() const; 375 376 /// Set display number color 377 void setDisplayNumberColor(FXColor clr); 378 FXColor getDisplayNumberColor() const; 379 380 /// Set numeric base color 381 void setBaseColor(FXColor clr); 382 FXColor getBaseColor() const; 383 384 /// Set numeric base color 385 void setAngleColor(FXColor clr); 386 FXColor getAngleColor() const; 387 388 /// Set number base 389 void setBase(FXint base); getBase()390 FXint getBase() const { return base; } 391 392 /// Set angle mode setAngles(FXint ang)393 void setAngles(FXint ang){ angles=ang; } getAngles()394 FXint getAngles() const { return angles; } 395 396 /// Set exponent mode 397 void setExponentMode(FXuchar expmode); getExponentMode()398 FXuchar getExponentMode() const { return (exponent&3); } 399 400 /// Set exponent mode 401 void setEngineeringMode(FXbool engmode); getEngineeringMode()402 FXbool getEngineeringMode() const { return !!(exponent&4); } 403 404 /// Set precision 405 void setPrecision(FXint prec); getPrecision()406 FXint getPrecision() const { return precision; } 407 408 /// Beep on error setBeep(FXbool on)409 void setBeep(FXbool on){ beep=on; } getBeep()410 FXbool getBeep() const { return beep; } 411 412 /// Set display font 413 void setDisplayFont(FXFont* font); 414 FXFont* getDisplayFont() const; 415 416 /// Clear the calculator 417 void clearAll(); 418 419 /// Read/write registry 420 void readRegistry(); 421 void writeRegistry(); 422 423 /// Destroy calculator 424 virtual ~Calculator(); 425 }; 426 427 #endif 428