Abacus.c 186 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * @(#)Abacus.c
 *
 * Copyright 1994 - 2009  David A. Bagley, bagleyd@tux.org
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby granted,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of the author not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.
 *
 * This program is distributed in the hope that it will be "useful",
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
20 21 22

/* Methods file for Abacus */

23
#include "file.h"
24
#include "rngs.h"
25 26
#include "sound.h"
#include "AbacusP.h"
27 28
static Boolean deleteSpecialRail(AbacusWidget w,
	Boolean sign, Boolean piece, Boolean piecePercent);
29

30
#ifdef WINVER
31 32
#ifndef SCRIPTFILE
#define SCRIPTFILE "Abacus.les"
33
#endif
34 35 36 37 38 39

#ifndef INIFILE
#define INIFILE "wabacus.ini"
#endif
#define SECTION "setup"
#else
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
#if defined( USE_SOUND ) && defined( USE_NAS )
Display *dsp;
#endif

static Boolean setValuesAbacus(Widget current, Widget request, Widget renew);
static void quitAbacus(AbacusWidget w, XEvent *event, char **args, int nArgs);
static void destroyAbacus(Widget old);
static void resizeAbacus(AbacusWidget w);
static void initializeAbacus(Widget request, Widget renew);
static void exposeAbacus(Widget renew, XEvent *event, Region region);
static void hideAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void selectAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void releaseAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void clearAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void complementAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void clearWithQueryAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void clearWithDoubleClickAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void incrementAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void decrementAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void speedUpAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void slowDownAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleSoundAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void changeFormatAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void changeMuseumAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleRomanNumeralsAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleGroupingAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleNegativeSignAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleQuartersAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleQuarterPercentsAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleTwelfthsAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleSubdecksAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleEighthsAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleAnomalyAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleWatchAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleDemoAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void showNextAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void showRepeatAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void showMoreAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleTeachAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleRightToLeftAddAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void toggleRightToLeftMultAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void enterAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);
static void leaveAbacus(AbacusWidget w,
	XEvent *event, char **args, int nArgs);

static char translations[] =
119 120 121 122 123 124 125
"<KeyPress>q: Quit()\n\
 Ctrl<KeyPress>C: Quit()\n\
 <KeyPress>osfCancel: Hide()\n\
 <KeyPress>Escape: Hide()\n\
 <KeyPress>osfEscape: Hide()\n\
 Ctrl<KeyPress>[: Hide()\n\
 <KeyPress>0x1B: Hide()\n\
126 127 128 129 130
 <KeyPress>0x2E: Speed()\n\
 <KeyPress>0x3E: Speed()\n\
 <KeyPress>0x3C: Slow()\n\
 <KeyPress>0x2C: Slow()\n\
 Shift<KeyPress>2: Sound()\n\
131 132 133
 <Btn1Down>: Select()\n\
 <Btn1Up>: Release()\n\
 <KeyPress>c: Clear()\n\
134 135 136 137
 <Btn3Down>: ClearWithQuery()\n\
 <Btn3Down>(2+): ClearWithDoubleClick()\n\
 <KeyPress>~: Complement()\n\
 <KeyPress>`: Complement()\n\
138 139 140
 <KeyPress>i: Increment()\n\
 <KeyPress>d: Decrement()\n\
 <KeyPress>f: Format()\n\
141
 <KeyPress>e: Museum()\n\
142
 <KeyPress>m: RomanNumerals()\n\
143
 <KeyPress>g: Group()\n\
144 145 146
 <KeyPress>s: Sign()\n\
 <KeyPress>u: Quarter()\n\
 <KeyPress>p: QuarterPercent()\n\
147
 <KeyPress>t: Twelfth()\n\
148 149 150
 <KeyPress>b: Subdeck()\n\
 <KeyPress>h: Eighth()\n\
 <KeyPress>l: Anomaly()\n\
151
 <KeyPress>w: Watch()\n\
152 153 154 155 156 157
 <KeyPress>o: Demo()\n\
 <KeyPress>n: Next()\n\
 <KeyPress>r: Repeat()\n\
 <KeyPress>0x20: More()\n\
 <KeyPress>KP_Space: More()\n\
 <KeyPress>Return: More()\n\
158 159 160
 <KeyPress>$: Teach()\n\
 <KeyPress>+: RightToLeftAdd()\n\
 <KeyPress>*: RightToLeftMult()\n\
161 162
 <EnterWindow>: Enter()\n\
 <LeaveWindow>: Leave()";
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static XtActionsRec actionsList[] =
{
	{(char *) "Quit", (XtActionProc) quitAbacus},
	{(char *) "Hide", (XtActionProc) hideAbacus},
	{(char *) "Select", (XtActionProc) selectAbacus},
	{(char *) "Release", (XtActionProc) releaseAbacus},
	{(char *) "Clear", (XtActionProc) clearAbacus},
	{(char *) "ClearWithQuery", (XtActionProc) clearWithQueryAbacus},
	{(char *) "ClearWithDoubleClick", (XtActionProc) clearWithDoubleClickAbacus},
	{(char *) "Complement", (XtActionProc) complementAbacus},
	{(char *) "Increment", (XtActionProc) incrementAbacus},
	{(char *) "Decrement", (XtActionProc) decrementAbacus},
	{(char *) "Speed", (XtActionProc) speedUpAbacus},
	{(char *) "Slow", (XtActionProc) slowDownAbacus},
	{(char *) "Sound", (XtActionProc) toggleSoundAbacus},
	{(char *) "Format", (XtActionProc) changeFormatAbacus},
	{(char *) "Museum", (XtActionProc) changeMuseumAbacus},
	{(char *) "RomanNumerals", (XtActionProc) toggleRomanNumeralsAbacus},
	{(char *) "Group", (XtActionProc) toggleGroupingAbacus},
	{(char *) "Sign", (XtActionProc) toggleNegativeSignAbacus},
	{(char *) "Quarter", (XtActionProc) toggleQuartersAbacus},
	{(char *) "QuarterPercent", (XtActionProc) toggleQuarterPercentsAbacus},
	{(char *) "Twelfth", (XtActionProc) toggleTwelfthsAbacus},
	{(char *) "Subdeck", (XtActionProc) toggleSubdecksAbacus},
	{(char *) "Eighth", (XtActionProc) toggleEighthsAbacus},
	{(char *) "Anomaly", (XtActionProc) toggleAnomalyAbacus},
	{(char *) "Watch", (XtActionProc) toggleWatchAbacus},
	{(char *) "Demo", (XtActionProc) toggleDemoAbacus},
	{(char *) "Next", (XtActionProc) showNextAbacus},
	{(char *) "Repeat", (XtActionProc) showRepeatAbacus},
	{(char *) "More", (XtActionProc) showMoreAbacus},
	{(char *) "Teach", (XtActionProc) toggleTeachAbacus},
	{(char *) "RightToLeftAdd", (XtActionProc) toggleRightToLeftAddAbacus},
	{(char *) "RightToLeftMult", (XtActionProc) toggleRightToLeftMultAbacus},
	{(char *) "Enter", (XtActionProc) enterAbacus},
	{(char *) "Leave", (XtActionProc) leaveAbacus}
200 201
};

202
static XtResource resources[] =
203 204
{
	{XtNwidth, XtCWidth, XtRDimension, sizeof (Dimension),
205 206
	 XtOffset(AbacusWidget, core.width),
	 XtRString, (caddr_t) "234"},
207
	{XtNheight, XtCHeight, XtRDimension, sizeof (Dimension),
208 209
	 XtOffset(AbacusWidget, core.height),
	 XtRString, (caddr_t) "123"},
210 211
	{XtNmono, XtCMono, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.mono),
212
	 XtRString, (caddr_t) "FALSE"},
213
	{XtNreverseVideo, XtCReverseVideo, XtRBoolean, sizeof (Boolean),
214
	 XtOffset(AbacusWidget, abacus.reverse),
215
	 XtRString, (caddr_t) "FALSE"},
216
	{XtNforeground, XtCForeground, XtRPixel, sizeof (Pixel),
217 218 219 220
	 XtOffset(AbacusWidget, abacus.foreground),
	 XtRString, (caddr_t) XtDefaultForeground},
	{XtNbackground, XtCBackground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.background),
221 222 223 224
	 XtRString, (caddr_t) "#AEB2C3" /*XtDefaultBackground*/},
	{XtNborderColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.borderColor),
	 XtRString, (caddr_t) "gray25" /*XtDefaultForeground*/},
225
	{XtNframeColor, XtCColor, XtRPixel, sizeof (Pixel),
226
	 XtOffset(AbacusWidget, abacus.frameColor),
227 228 229
	 XtRString, (caddr_t) "wheat4" /*XtDefaultForeground*/},
	{XtNprimaryBeadColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.beadColor[0]),
230
	 XtRString, (caddr_t) "DarkRed" /*XtDefaultForeground*/},
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	{XtNleftAuxBeadColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.leftAuxColor),
	 XtRString, (caddr_t) "LimeGreen" /*XtDefaultForeground*/},
	{XtNrightAuxBeadColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.rightAuxColor),
	 XtRString, (caddr_t) "gainsboro" /*XtDefaultForeground*/},
	{XtNsecondaryBeadColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.beadColor[1]),
	 XtRString, (caddr_t) "wheat4" /*XtDefaultForeground*/},
	{XtNhighlightBeadColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.beadColor[2]),
	 XtRString, (caddr_t) "magenta" /*XtDefaultForeground*/},
	{XtNprimaryRailColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.railColor[0]),
	 XtRString, (caddr_t) "gold" /*XtDefaultForeground*/},
	{XtNsecondaryRailColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.railColor[1]),
	 XtRString, (caddr_t) "LightSteelBlue1" /*XtDefaultForeground*/},
	{XtNhighlightRailColor, XtCForeground, XtRPixel, sizeof (Pixel),
	 XtOffset(AbacusWidget, abacus.railColor[2]),
	 XtRString, (caddr_t) "Purple" /*XtDefaultForeground*/},
252 253 254 255 256 257
	{XtNbumpSound, XtCBumpSound, XtRString, sizeof (String),
	 XtOffset(AbacusWidget, abacus.bumpSound),
	 XtRString, (caddr_t) BUMPSOUND},
	{XtNmoveSound, XtCMoveSound, XtRString, sizeof (String),
	 XtOffset(AbacusWidget, abacus.moveSound),
	 XtRString, (caddr_t) MOVESOUND},
258 259 260 261 262 263
	{XtNsound, XtCSound, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.sound),
	 XtRString, (caddr_t) "FALSE"},
	{XtNdelay, XtCDelay, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.delay),
	 XtRString, (caddr_t) "50"},
264
	{XtNscript, XtCScript, XtRBoolean, sizeof (Boolean),
265 266
	 XtOffset(AbacusWidget, abacus.script),
	 XtRString, (caddr_t) "FALSE"},
267
	{XtNdemo, XtCDemo, XtRBoolean, sizeof (Boolean),
268 269
	 XtOffset(AbacusWidget, abacus.demo),
	 XtRString, (caddr_t) "FALSE"},
270
	{XtNdemoPath, XtCDemoPath, XtRString, sizeof (String),
271 272
	 XtOffset(AbacusWidget, abacusDemo.path),
	 XtRString, (caddr_t) DEMOPATH},
273
	{XtNdemoFont, XtCDemoFont, XtRString, sizeof (Font),
274 275
	 XtOffset(AbacusWidget, abacusDemo.font),
	 XtRString, (caddr_t) "-*-times-*-r-*-*-*-180-*-*-*-*"},
276
	{XtNdemoForeground, XtCForeground, XtRPixel, sizeof (Pixel),
277 278
	 XtOffset(AbacusWidget, abacusDemo.foreground),
	 XtRString, (caddr_t) XtDefaultForeground},
279
	{XtNdemoBackground, XtCBackground, XtRPixel, sizeof (Pixel),
280 281
	 XtOffset(AbacusWidget, abacusDemo.background),
	 XtRString, (caddr_t) XtDefaultBackground},
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
	{XtNteach, XtCTeach, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.teach),
	 XtRString, (caddr_t) "FALSE"},
	{XtNteachBuffer, XtCTeachBuffer, XtRString, sizeof (String),
	 XtOffset(AbacusWidget, abacus.teachBuffer),
	 XtRString, (caddr_t) ""},
	{XtNrightToLeftAdd, XtCRightToLeftAdd, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.rightToLeftAdd),
	 XtRString, (caddr_t) "FALSE"},
	{XtNrightToLeftMult, XtCRightToLeftMult, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.rightToLeftMult),
	 XtRString, (caddr_t) "FALSE"},
	{XtNlee, XtCLee, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.lee),
	 XtRString, (caddr_t) "TRUE"},
	{XtNrails, XtCRails, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.rails),
	 XtRString, (caddr_t) "13"},
	{XtNleftAuxRails, XtCLeftAuxRails, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.leftAuxRails),
	 XtRString, (caddr_t) "9"},
	{XtNrightAuxRails, XtCRightAuxRails, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.rightAuxRails),
	 XtRString, (caddr_t) "9"},
306 307 308
	{XtNvertical, XtCVertical, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.vertical),
	 XtRString, (caddr_t) "FALSE"},
309 310 311
	{XtNcolorScheme, XtCColorScheme, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.colorScheme),
	 XtRString, (caddr_t) "0"},
312 313 314 315 316 317
	{XtNslot, XtCSlot, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.slot),
	 XtRString, (caddr_t) "FALSE"},
	{XtNdiamond, XtCDiamond, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.diamond),
	 XtRString, (caddr_t) "FALSE"},
318 319 320
	{XtNrailIndex, XtCRailIndex, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.railIndex),
	 XtRString, (caddr_t) "0"},
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	{XtNtopOrient, XtCTopOrient, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.decks[TOP].orientation),
	 XtRString, (caddr_t) "TRUE"},
	{XtNbottomOrient, XtCBottomOrient, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.decks[BOTTOM].orientation),
	 XtRString, (caddr_t) "FALSE"},
	{XtNtopNumber, XtCTopNumber, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[TOP].number),
	 XtRString, (caddr_t) "2"},
	{XtNbottomNumber, XtCBottomNumber, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[BOTTOM].number),
	 XtRString, (caddr_t) "5"},
	{XtNtopFactor, XtCTopFactor, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[TOP].factor),
	 XtRString, (caddr_t) "5"},
	{XtNbottomFactor, XtCBottomFactor, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[BOTTOM].factor),
	 XtRString, (caddr_t) "1"},
	{XtNtopSpaces, XtCTopSpaces, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[TOP].spaces),
	 XtRString, (caddr_t) "2"},
	{XtNbottomSpaces, XtCBottomSpaces, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[BOTTOM].spaces),
	 XtRString, (caddr_t) "3"},
	{XtNtopPiece, XtCTopPiece, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[TOP].piece),
	 XtRString, (caddr_t) "0"},
	{XtNbottomPiece, XtCBottomPiece, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[BOTTOM].piece),
	 XtRString, (caddr_t) "0"},
	{XtNtopPiecePercent, XtCTopPiecePercent, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[TOP].piecePercent),
	 XtRString, (caddr_t) "0"},
	{XtNbottomPiecePercent, XtCBottomPiecePercent, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decks[BOTTOM].piecePercent),
	 XtRString, (caddr_t) "0"},
	{XtNshiftPercent, XtCShiftPercent, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.shiftPercent),
	 XtRString, (caddr_t) "2"},
360 361 362 363 364 365
	{XtNsubdeck, XtCSubdeck, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.subdeck),
	 XtRString, (caddr_t) "0"},
	{XtNsubbead, XtCSubbead, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.subbead),
	 XtRString, (caddr_t) "4"},
366 367 368 369 370 371
	{XtNsign, XtCSign, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.sign),
	 XtRString, (caddr_t) "FALSE"},
	{XtNdecimalPosition, XtCDecimalPosition, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.decimalPosition),
	 XtRString, (caddr_t) "2"}, /* same as shiftPercent */
372 373 374
	{XtNgroup, XtCGroup, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.group),
	 XtRString, (caddr_t) "FALSE"},
375 376 377
	{XtNgroupSize, XtCGroupSize, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.groupSize),
	 XtRString, (caddr_t) "3"},
378 379 380
	{XtNdecimalComma, XtCDecimalComma, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.decimalComma),
	 XtRString, (caddr_t) "FALSE"},
381 382 383
	{XtNbase, XtCBase, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.base),
	 XtRString, (caddr_t) "10"},
384 385 386
	{XtNsubbase, XtCSubbase, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.subbase),
	 XtRString, (caddr_t) "12"},
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	{XtNanomaly, XtCAnomaly, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.anomaly),
	 XtRString, (caddr_t) "0"},
	{XtNshiftAnomaly, XtCShiftAnomaly, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.shiftAnomaly),
	 XtRString, (caddr_t) "2"},
	{XtNanomalySq, XtCAnomalySq, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.anomalySq),
	 XtRString, (caddr_t) "0"},
	{XtNshiftAnomalySq, XtCShiftAnomalySq, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.shiftAnomalySq),
	 XtRString, (caddr_t) "2"},
	{XtNdisplayBase, XtCDisplayBase, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.displayBase),
	 XtRString, (caddr_t) "10"},
402 403 404
	{XtNpressOffset, XtCPressOffset, XtRBoolean, sizeof (int),
	 XtOffset(AbacusWidget, abacus.pressOffsetY),
	 XtRString, (caddr_t) "0"},
405 406 407
	{XtNromanNumerals, XtCRomanNumerals, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.romanNumerals),
	 XtRString, (caddr_t) "FALSE"},
408 409 410 411 412 413 414 415 416
	{XtNlatin, XtCLatin, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.latin),
	 XtRString, (caddr_t) "FALSE"},
	{XtNancientRoman, XtCAncientRoman, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.ancientRoman),
	 XtRString, (caddr_t) "FALSE"},
	{XtNmodernRoman, XtCModernRoman, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.modernRoman),
	 XtRString, (caddr_t) "FALSE"},
417 418
	{XtNmode, XtCMode, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.mode),
419
	 XtRString, (caddr_t) "0"}, /* CHINESE */
420 421
	{XtNformat, XtCFormat, XtRString, sizeof (String),
	 XtOffset(AbacusWidget, abacus.format),
422 423 424 425 426 427 428
	 XtRString, (caddr_t) "Chinese"},
	{XtNmuseum, XtCMuseum, XtRString, sizeof (String),
	 XtOffset(AbacusWidget, abacus.museum),
	 XtRString, (caddr_t) "--"},
	{XtNversionOnly, XtCBoolean, XtRBoolean, sizeof (Boolean),
	 XtOffset(AbacusWidget, abacus.versionOnly),
	 XtRString, (caddr_t) "FALSE"},
429 430
	{XtNmenu, XtCMenu, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.menu),
431 432 433 434
	 XtRString, (caddr_t) "999"}, /* ACTION_IGNORE */
	{XtNaux, XtCAux, XtRBoolean, sizeof (int),
	 XtOffset(AbacusWidget, abacus.aux),
	 XtRString, (caddr_t) "0"},
435
	{XtNdeck, XtCDeck, XtRInt, sizeof (int),
436
	 XtOffset(AbacusWidget, abacus.deck),
437
	 XtRString, (caddr_t) "-1"}, /* IGNORE_DECK */
438
	{XtNrail, XtCRail, XtRInt, sizeof (int),
439 440
	 XtOffset(AbacusWidget, abacus.rail),
	 XtRString, (caddr_t) "0"},
441
	{XtNnumber, XtCNumber, XtRInt, sizeof (int),
442 443 444 445 446
	 XtOffset(AbacusWidget, abacus.number),
	 XtRString, (caddr_t) "0"},
	{XtNmathBuffer, XtCMathBuffer, XtRString, sizeof (String),
	 XtOffset(AbacusWidget, abacus.mathBuffer),
	 XtRString, (caddr_t) ""},
447 448 449 450 451 452 453 454 455
	{XtNpixmapSize, XtCPixmapSize, XtRInt, sizeof (int),
	 XtOffset(AbacusWidget, abacus.pixmapSize),
	 XtRString, (caddr_t) "64"},
	{XtNleftAuxAbacus, XtCLeftAuxAbacus, XtRPointer, sizeof (caddr_t),
	 XtOffset(AbacusWidget, abacus.leftAux),
	 XtRCallback, (caddr_t) NULL},
	{XtNrightAuxAbacus, XtCRightAuxAbacus, XtRPointer, sizeof (caddr_t),
	 XtOffset(AbacusWidget, abacus.rightAux),
	 XtRCallback, (caddr_t) NULL},
456
	{XtNselectCallback, XtCCallback, XtRCallback, sizeof (caddr_t),
457 458
	 XtOffset(AbacusWidget, abacus.select),
	 XtRCallback, (caddr_t) NULL}
459 460 461 462 463 464
};

AbacusClassRec abacusClassRec =
{
	{
		(WidgetClass) & widgetClassRec,		/* superclass */
465
		(char *) "Abacus",	/* class name */
466 467 468 469
		sizeof (AbacusRec),	/* widget size */
		NULL,		/* class initialize */
		NULL,		/* class part initialize */
		FALSE,		/* class inited */
470
		(XtInitProc) initializeAbacus,	/* initialize */
471 472
		NULL,		/* initialize hook */
		XtInheritRealize,	/* realize */
473 474 475 476
		actionsList,	/* actions */
		XtNumber(actionsList),	/* num actions */
		resources,	/* resources */
		XtNumber(resources),	/* num resources */
477 478 479 480 481
		NULLQUARK,	/* xrm class */
		TRUE,		/* compress motion */
		TRUE,		/* compress exposure */
		TRUE,		/* compress enterleave */
		TRUE,		/* visible interest */
482 483 484 485
		(XtWidgetProc) destroyAbacus,	/* destroy */
		(XtWidgetProc) resizeAbacus,	/* resize */
		(XtExposeProc) exposeAbacus,	/* expose */
		(XtSetValuesFunc) setValuesAbacus,	/* set values */
486 487 488 489 490 491
		NULL,		/* set values hook */
		XtInheritSetValuesAlmost,	/* set values almost */
		NULL,		/* get values hook */
		NULL,		/* accept focus */
		XtVersion,	/* version */
		NULL,		/* callback private */
492
		translations,	/* tm table */
493 494 495 496 497 498 499 500 501 502 503
		NULL,		/* query geometry */
		NULL,		/* display accelerator */
		NULL		/* extension */
	},
	{
		0		/* ignore */
	}
};

WidgetClass abacusWidgetClass = (WidgetClass) & abacusClassRec;

504
void
505
setAbacus(AbacusWidget w, int reason)
506
{
507 508 509 510
	abacusCallbackStruct cb;

	cb.reason = reason;
	XtCallCallbacks((Widget) w, (char *) XtNselectCallback, &cb);
511 512
}

513
void
514
setAbacusMove(AbacusWidget w, int reason, int aux, int deck,
515
		int rail, int number)
516
{
517
	abacusCallbackStruct cb;
518

519 520 521 522 523 524
	cb.reason = reason;
	cb.aux = aux;
	cb.deck = deck;
	cb.rail = rail;
	cb.number = number;
	XtCallCallbacks((Widget) w, (char *) XtNselectCallback, &cb);
525 526
}

527 528 529 530 531 532 533 534 535 536 537
void
setAbacusText(AbacusWidget w, int reason, char * text, int line)
{
        abacusCallbackStruct cb;

        cb.reason = reason;
        cb.line = line;
        cb.teachBuffer = text;
        XtCallCallbacks((Widget) w, (char *) XtNselectCallback, &cb);
}

538
static void
539
setAbacusString(AbacusWidget w, int reason, char * string)
540
{
541
	abacusCallbackStruct cb;
542

543 544 545
	cb.reason = reason;
	cb.buffer = string;
	XtCallCallbacks((Widget) w, (char *) XtNselectCallback, &cb);
546
}
547
#endif
548

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
static void resizeBead(AbacusWidget w);

static char
decimalChar(AbacusWidget w)
{
	return ((w->abacus.decimalComma) ? ',' : '.');
}

static char
groupChar(AbacusWidget w)
{
	return ((w->abacus.decimalComma) ? '.' : ',');
}

static void
setSubmodeFromMuseum(AbacusWidget w)
{
	if (strncasecmp("it", w->abacus.museum,
			COUNTRY_SIZE) == 0) {
		w->abacus.submode = IT;
	} else if (strncasecmp("uk", w->abacus.museum,
			COUNTRY_SIZE) == 0) {
		w->abacus.submode = UK;
	} else if (strncasecmp("fr", w->abacus.museum,
			COUNTRY_SIZE) == 0) {
		w->abacus.submode = FR;
	} else {
		w->abacus.submode = NRAND(MAX_MUSEUMS);
	}
}

static Boolean
submodeSlotsSeparate(int submode)
{
	return (submode == UK);
}
585 586

static void
587
setModeFromFormat(AbacusWidget w)
588 589
{
	if (strncasecmp("chinese", w->abacus.format,
590
			MAX_FORMAT_LENGTH) == 0) {
591 592
		w->abacus.mode = CHINESE;
	} else if (strncasecmp("japanese", w->abacus.format,
593
			MAX_FORMAT_LENGTH) == 0) {
594 595
		w->abacus.mode = JAPANESE;
	} else if (strncasecmp("korean", w->abacus.format,
596
			MAX_FORMAT_LENGTH) == 0) {
597 598
		w->abacus.mode = KOREAN;
	} else if (strncasecmp("roman", w->abacus.format,
599
			MAX_FORMAT_LENGTH) == 0) {
600 601
		w->abacus.mode = ROMAN;
	} else if (strncasecmp("italian", w->abacus.format,
602
			MAX_FORMAT_LENGTH) == 0) {
603 604
		w->abacus.mode = ROMAN;
	} else if (strncasecmp("russian", w->abacus.format,
605
			MAX_FORMAT_LENGTH) == 0) {
606
		w->abacus.mode = RUSSIAN;
607 608 609 610 611 612
	} else if (strncasecmp("danish", w->abacus.format,
			MAX_FORMAT_LENGTH) == 0) {
		w->abacus.mode = DANISH;
	} else if (strncasecmp("cn", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
		w->abacus.mode = CHINESE;
	} else if (strncasecmp("zh", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
613
		w->abacus.mode = CHINESE;
614
	} else if (strncasecmp("jp", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
615
		w->abacus.mode = JAPANESE;
616
	} else if (strncasecmp("ja", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
617
		w->abacus.mode = JAPANESE;
618
	} else if (strncasecmp("ko", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
619
		w->abacus.mode = KOREAN;
620
	} else if (strncasecmp("ro", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
621
		w->abacus.mode = ROMAN;
622
	} else if (strncasecmp("it", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
623
		w->abacus.mode = ROMAN;
624
	} else if (strncasecmp("ru", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
625
		w->abacus.mode = RUSSIAN;
626 627 628 629
	} else if (strncasecmp("dk", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
		w->abacus.mode = DANISH;
	} else if (strncasecmp("da", w->abacus.format, MAX_FORMAT_LENGTH) == 0) {
		w->abacus.mode = DANISH;
630
	} else {
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
		w->abacus.mode = GENERIC;
	}
}

/* This is setup for Roman abacus of 3 subdecks
 * deck == 0 => 1/12 * pieceFactor
 * deck == 1 => 1/4 * pieceFactor
 * deck == 2 => 1/2 * pieceFactor
 * For other subdecks its more whimsical. */
static int
convertRomanFactor(AbacusWidget w, int deck)
{
	int b = w->abacus.decks[BOTTOM].piece;

	if (w->abacus.decks[TOP].piece != 0)
		b *= w->abacus.decks[TOP].piece;
	if (w->abacus.subbase == EIGHTHS) {
		if (deck <= 1)
			return 1;
		else
			return deck * b / (w->abacus.subdeck + 2);
652
	}
653 654 655 656
	if (deck == 0)
		return 1;
	else
		return deck * b / (w->abacus.subdeck + 1);
657 658
}

659
static int
660
checkBottomSpace(AbacusWidget w)
661 662
{
	return w->abacus.decks[BOTTOM].spaces +
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
		convertBaseToBottom(w->abacus.base) - 1;
}

Boolean checkPiece(AbacusWidget w)
{
	return (w->abacus.decks[BOTTOM].piece != 0);
}

Boolean checkPiecePercent(AbacusWidget w)
{
	return (w->abacus.decks[BOTTOM].piece != 0 &&
			w->abacus.decks[BOTTOM].piecePercent != 0);
}

Boolean
checkSubdeck(AbacusWidget w, int position)
{
	return (w->abacus.decks[BOTTOM].piece != 0 &&
		w->abacus.decks[BOTTOM].piecePercent == 0 &&
		w->abacus.slot && w->abacus.subdeck != 0 &&
		w->abacus.decks[BOTTOM].room >= w->abacus.subbead +
		w->abacus.subdeck * SUBDECK_SPACE &&
		w->abacus.decimalPosition == position);
686 687 688
}

static Boolean
689 690 691 692
checkAnomaly(AbacusWidget w)
{
	/* Record carries from anomalies to add in later */
	return (w->abacus.anomaly != 0);
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
}

static int
numberPieces(AbacusWidget w, int deck)
{
	int pieces = 0;

	if (deck == BOTTOM) {
		pieces = w->abacus.decks[BOTTOM].piece;
		if (w->abacus.decks[TOP].number == 0 &&
				w->abacus.decks[TOP].piece != 0)
			pieces *= w->abacus.decks[TOP].piece;
		if (w->abacus.decks[BOTTOM].number ==
				w->abacus.decks[TOP].factor - 1)
			pieces -= 1;
	} else {
		if (w->abacus.decks[TOP].number != 0 &&
				w->abacus.decks[TOP].piece != 0) {
			pieces = w->abacus.decks[TOP].piece;
			if ((w->abacus.decks[TOP].number + 1) *
					w->abacus.decks[TOP].factor ==
					w->abacus.base)
				pieces -= 1;
		}
	}
	return pieces;
}

721 722
static int
numberPiecePercents(AbacusWidget w, int deck)
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746
{
	int piecePercents = 0;

	if (deck == BOTTOM) {
		piecePercents = w->abacus.decks[BOTTOM].piecePercent;
		if (w->abacus.decks[TOP].number == 0 &&
				w->abacus.decks[TOP].piecePercent != 0)
			piecePercents *= w->abacus.decks[TOP].piecePercent;
		if (w->abacus.decks[BOTTOM].number ==
				w->abacus.decks[TOP].factor - 1)
			piecePercents -= 1;
	} else {
		if (w->abacus.decks[TOP].number != 0 &&
				w->abacus.decks[TOP].piecePercent != 0) {
			piecePercents = w->abacus.decks[TOP].piecePercent;
			if ((w->abacus.decks[TOP].number + 1) *
					w->abacus.decks[TOP].factor ==
					w->abacus.base)
				piecePercents -= 1;
		}
	}
	return piecePercents;
}

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
static int valuePiece(AbacusWidget w) {
	int sum = 0, localBase = 1;
	int factor = 1, rail = w->abacus.decimalPosition - 1;

	if (w->abacus.decks[BOTTOM].piece != 0) {
		int deck;

		for (deck = BOTTOM; deck <= TOP; deck++) {
			int piecePosition = w->abacus.decks[deck].position[rail];

			localBase = w->abacus.decks[BOTTOM].piece;
			if (deck == TOP) {
				if (w->abacus.decks[TOP].piece == 0) {
					break;
				} else {
					factor = w->abacus.decks[BOTTOM].piece;
					localBase *= w->abacus.decks[TOP].piece;
				}
			}
			if (w->abacus.decks[deck].orientation) {
				sum += (numberPieces(w, deck) - piecePosition) *
					factor;
			} else {
				sum += piecePosition * factor;
			}
		}
	}
	return sum % localBase;
}

#if 0
static int valuePiecePercent(AbacusWidget w) {
	int sum = 0, localBase = 1;
	int factor = 1, rail = w->abacus.decimalPosition - w->abacus.shiftPercent - 1 -
		((w->abacus.decks[BOTTOM].piece == 0) ? 0 : 1);

	if (w->abacus.decks[BOTTOM].piece != 0 &&
			w->abacus.decks[BOTTOM].piecePercent != 0) {
		int deck;

		for (deck = BOTTOM; deck <= TOP; deck++) {
			int piecePercentPosition = w->abacus.decks[deck].position[rail];

			localBase = w->abacus.decks[BOTTOM].piecePercent;
			if (deck == TOP) {
				if (w->abacus.decks[TOP].piecePercent == 0) {
					break;
				} else {
					factor = w->abacus.decks[BOTTOM].piecePercent;
					localBase *= w->abacus.decks[TOP].piecePercent;
				}
			}
			if (w->abacus.decks[deck].orientation) {
				sum += (numberPiecePercents(w, deck) -
					piecePercentPosition) * factor;
			} else {
				sum += piecePercentPosition * factor;
			}
		}
	}
	return sum % localBase;
}
#endif

static int valueSubdeck(AbacusWidget w) {
	int sum = 0;

	if (checkSubdeck(w, 3)) {
		int deck;

		for (deck = 0; deck < w->abacus.subdeck; deck++) {
			if (w->abacus.decks[BOTTOM].orientation) {
				sum += (w->abacus.subdecks[deck].number -
					w->abacus.subdecks[deck].position) *
					convertRomanFactor(w, deck);
			} else {
				sum += w->abacus.subdecks[deck].position *
					convertRomanFactor(w, deck);
			}
		}
	}
	return sum;
}

static void
setSpace(AbacusWidget w, int deck)
{
	int room, number;

	room = w->abacus.decks[deck].number + w->abacus.decks[deck].spaces;
	if (w->abacus.decks[BOTTOM].piece != 0) {
		number = numberPieces(w, deck);
		if (room <= number)
			room = number + 1;
		if (w->abacus.decks[BOTTOM].piecePercent != 0) {
			number = numberPiecePercents(w, deck);
			if (room <= number)
				room = number + 1;
		}
		if (deck == BOTTOM && checkSubdeck(w, 3)) {
			number = w->abacus.subbead + w->abacus.subdeck *
				w->abacus.subdecks[deck].spaces - 1;
			if (room <= number)
				room = number + 1;
		}
	}
	if (w->abacus.decks[deck].number != 0) {
		if (room > w->abacus.decks[deck].number +
				w->abacus.decks[deck].spaces)
			w->abacus.decks[deck].spaces = room -
				w->abacus.decks[deck].number;
		w->abacus.decks[deck].room = w->abacus.decks[deck].number +
			w->abacus.decks[deck].spaces;
	}
}

static int numberSubbeads(AbacusWidget w, int subdecks)
{
	if (w->abacus.subdeck != 0)
		return w->abacus.subbead / w->abacus.subdeck +
			((w->abacus.subbead % w->abacus.subdeck -
			subdecks <= 0) ? 0 : 1);
	return 0;
}

static int numberSubbeadsOffset(AbacusWidget w, int subdecks)
{
	int deck, offset = 0;
	int space = 1;

	if (subdecks < 0)
		return w->abacus.subbead + w->abacus.subdeck *
			w->abacus.subdecks[0].spaces;
	for (deck = 0; deck < w->abacus.subdeck - 1 - subdecks; deck++) {
		offset += numberSubbeads(w, w->abacus.subdeck - 1 - deck) +
			space;
	}
	return offset;
}

static int positionSubdeck(AbacusWidget w, int j)
{
	int b1, b2, subj, d;

	subj = j;
	b1 = b2 = 0;
	for (d = w->abacus.subdeck - 1; d >= 0; d--) {
		b1 = b2;
		b2 = numberSubbeadsOffset(w, d - 1);
		if (subj <= b2) {
			subj = subj - b1;
			break;
		}
	}
	return subj;
}

static int subdeckPosition(AbacusWidget w, int j)
{
	int subj, d;

	subj = j;
	for (d = w->abacus.subdeck - 1; d >= 0; d--) {
		if (subj <= numberSubbeadsOffset(w, d - 1)) {
			break;
		}
	}
	return d;
}

#if 0
static int numberSubdeck(AbacusWidget w, int subbeads)
{
	int deck, offset = 0;
	int space = 1;

	for (deck = 0; deck < w->abacus.subdeck; deck++) {
		offset += numberSubbeads(w, deck) + space;
		if (offset >= subbeads)
			return deck;
	}
	return w->abacus.subdeck;
}
#endif

932
static void
933
checkBeads(AbacusWidget w)
934 935 936
{
	char *buf1, *buf2;

937
	if (w->abacus.aux != PRIMARY && w->abacus.vertical) {
938 939 940
		/* not allowed by default, but user can change later */
		w->abacus.vertical = False;
	}
941 942
	if (w->abacus.railIndex != 0)
		w->abacus.railIndex = 1;
943
	if (w->abacus.shiftPercent <= 0) {
944
		w->abacus.shiftPercent = DEFAULT_SHIFT_PERCENT;
945 946 947 948
	}
	if (w->abacus.decimalPosition < 0) {
		w->abacus.decimalPosition = 0;
	}
949 950 951 952 953 954 955 956
	if (w->abacus.decimalPosition >= w->abacus.rails) {
		w->abacus.decimalPosition = w->abacus.rails - 1;
	}
	if (w->abacus.shiftPercent >= w->abacus.rails) {
		w->abacus.shiftPercent = 2;
	}
	if (w->abacus.decks[BOTTOM].piece > MAX_BASE) {
		intCat(&buf1, "Bottom Piece must be less than or equal to ", MAX_BASE);
957 958 959
		DISPLAY_WARNING(buf1);
		free(buf1);
		w->abacus.decks[BOTTOM].piece = 0;
960 961 962
	} else if (w->abacus.decks[BOTTOM].piece != 0 &&
			w->abacus.decks[BOTTOM].piece < MIN_BASE) {
		intCat(&buf1, "Bottom Piece must be greater than or equal to ", MIN_BASE);
963 964 965 966 967 968
		stringCat(&buf2, buf1, ", or 0");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
		w->abacus.decks[BOTTOM].piece = 0;
	}
969 970 971
	if (w->abacus.decks[TOP].piece != 0 &&
			w->abacus.decks[TOP].piece < MIN_BASE) {
		intCat(&buf1, "Top Piece must be greater than or equal to ", MIN_BASE);
972 973 974 975 976 977
		stringCat(&buf2, buf1, ", or 0");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
		w->abacus.decks[TOP].piece = 0;
	}
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	if (w->abacus.decks[BOTTOM].piecePercent > MAX_BASE) {
		intCat(&buf1, "Bottom Piece Percent must be less than or equal to ", MAX_BASE);
		DISPLAY_WARNING(buf1);
		free(buf1);
		w->abacus.decks[BOTTOM].piecePercent = 0;
	} else if (w->abacus.decks[BOTTOM].piecePercent != 0 &&
			w->abacus.decks[BOTTOM].piecePercent < MIN_BASE) {
		intCat(&buf1, "Bottom Piece Percent must be greater than or equal to ", MIN_BASE);
		stringCat(&buf2, buf1, ", or 0");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
		w->abacus.decks[BOTTOM].piecePercent = 0;
	}
	if (w->abacus.decks[TOP].piecePercent != 0 &&
			w->abacus.decks[TOP].piecePercent < MIN_BASE) {
		intCat(&buf1, "Top Piece Percent must be greater than or equal to ", MIN_BASE);
		stringCat(&buf2, buf1, ", or 0");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
		w->abacus.decks[TOP].piecePercent = 0;
	}
	if (w->abacus.base > MAX_BASE) {
		intCat(&buf1, "Base must be less than or equal to ", MAX_BASE);
1003 1004
		DISPLAY_WARNING(buf1);
		free(buf1);
1005 1006 1007 1008
		w->abacus.base = DEFAULT_BASE;
		setAbacus(w, ACTION_BASE_DEFAULT);
	} else if (w->abacus.base < MIN_BASE) {
		intCat(&buf1, "Base must be greater than or equal to ", MIN_BASE);
1009 1010
		DISPLAY_WARNING(buf1);
		free(buf1);
1011 1012 1013 1014
		w->abacus.base = DEFAULT_BASE;
		setAbacus(w, ACTION_BASE_DEFAULT);
	} else if (w->abacus.base != DEFAULT_BASE && w->abacus.demo) {
		intCat(&buf1, "Base must be equal to ", DEFAULT_BASE);
1015 1016 1017 1018
		stringCat(&buf2, buf1, ", for demo");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
1019 1020
		w->abacus.base = DEFAULT_BASE;
		setAbacus(w, ACTION_BASE_DEFAULT);
1021 1022
	}
	if (w->abacus.decks[BOTTOM].piece != 0 &&
1023 1024
			(checkBottomSpace(w) < w->abacus.decks[BOTTOM].piece)) {
		/*DISPLAY_WARNING("Bottom Spaces must be large enough with base when piece set");*/
1025
		w->abacus.decks[BOTTOM].spaces -=
1026
			(checkBottomSpace(w) - w->abacus.decks[BOTTOM].piece);
1027 1028
	}
	if (w->abacus.decks[BOTTOM].piecePercent > 1 &&
1029 1030
			(checkBottomSpace(w) < w->abacus.decks[BOTTOM].piecePercent)) {
		/*DISPLAY_WARNING("Bottom Spaces must be large enough with base when piece percent set");*/
1031
		w->abacus.decks[BOTTOM].spaces -=
1032
			(checkBottomSpace(w) - w->abacus.decks[BOTTOM].piecePercent);
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	}
	if (w->abacus.anomaly < 0) {
		DISPLAY_WARNING("Anomaly must be greater than or equal to 0");
		w->abacus.anomaly = 0;
	}
	if (w->abacus.anomaly >= w->abacus.base) {
		intCat(&buf1, "Anomaly must be less than ", w->abacus.base);
		DISPLAY_WARNING(buf1);
		free(buf1);
		w->abacus.anomaly = 0;
	}
1044
	if (w->abacus.shiftAnomaly <= 0) {
1045
		DISPLAY_WARNING("Shift Anomaly must be greater than 0");
1046
		w->abacus.shiftAnomaly = DEFAULT_SHIFT_ANOMALY;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	}
	if (w->abacus.anomalySq < 0) {
		DISPLAY_WARNING("Anomaly Squared must be greater than or equal to 0");
		w->abacus.anomalySq = 0;
	}
	if (w->abacus.anomalySq >= w->abacus.base) {
		intCat(&buf1, "Anomaly Squared must be less than ", w->abacus.base);
		DISPLAY_WARNING(buf1);
		free(buf1);
		w->abacus.anomalySq = 0;
	}
1058
	if (w->abacus.shiftAnomalySq <= 0) {
1059
		DISPLAY_WARNING("Shift Anomaly Squared must be greater than 0");
1060
		w->abacus.shiftAnomalySq = DEFAULT_SHIFT_ANOMALY;
1061
	}
1062 1063
	if (w->abacus.displayBase > MAX_BASE) {
		intCat(&buf1, "Display Base must be less than or equal to ", MAX_BASE);
1064 1065
		DISPLAY_WARNING(buf1);
		free(buf1);
1066 1067
		w->abacus.displayBase = DEFAULT_BASE;
	} else if (w->abacus.displayBase < MIN_BASE) {
1068
		intCat(&buf1, "Display Base must be greater than or equal to ",
1069
			MIN_BASE);
1070 1071
		DISPLAY_WARNING(buf1);
		free(buf1);
1072 1073 1074
		w->abacus.displayBase = DEFAULT_BASE;
	} else if (w->abacus.displayBase != DEFAULT_BASE && w->abacus.demo) {
		intCat(&buf1, "Display Base must be equal to ", DEFAULT_BASE);
1075 1076 1077 1078
		stringCat(&buf2, buf1, ", for demo");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
1079
		w->abacus.displayBase = DEFAULT_BASE;
1080
	}
1081 1082 1083
	if (w->abacus.mode == GENERIC && w->abacus.demo &&
			w->abacus.aux == PRIMARY) {
		/*DISPLAY_WARNING("Format must not be \"Generic\", for demo");*/
1084
		w->abacus.mode = CHINESE;
1085
	}
1086
	/* If a particular mode, ignore improper settings */
1087 1088
	if (w->abacus.mode == DANISH) {
		w->abacus.colorScheme = COLOR_HALF;
1089 1090 1091 1092
		w->abacus.decks[BOTTOM].factor = 1;
		w->abacus.decks[TOP].factor = w->abacus.base;
		w->abacus.decks[BOTTOM].number = w->abacus.base;
		w->abacus.decks[TOP].number = 0;
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
		w->abacus.decks[BOTTOM].orientation = DEFAULT_BOTTOM_ORIENT;
		w->abacus.decks[TOP].orientation = DEFAULT_TOP_ORIENT;
		w->abacus.decks[BOTTOM].spaces = 4 * DEFAULT_BOTTOM_SPACES;
		w->abacus.decks[TOP].spaces = 0;
		w->abacus.vertical = True;
		w->abacus.slot = False;
		w->abacus.diamond = False;
		w->abacus.railIndex = 1;
		setSpace(w, BOTTOM);
		setSpace(w, TOP);
	} else if (w->abacus.mode == RUSSIAN) {
		w->abacus.colorScheme = COLOR_MIDDLE | COLOR_FIRST;
		w->abacus.decks[BOTTOM].factor = 1;
		w->abacus.decks[TOP].factor = w->abacus.base;
		w->abacus.decks[BOTTOM].number = w->abacus.base;
		w->abacus.decks[TOP].number = 0;
		w->abacus.decks[BOTTOM].orientation = DEFAULT_TOP_ORIENT;
		w->abacus.decks[TOP].orientation = DEFAULT_TOP_ORIENT;
		w->abacus.decks[BOTTOM].spaces = DEFAULT_BOTTOM_SPACES;
1112
		w->abacus.decks[TOP].spaces = 0;
1113
		w->abacus.vertical = True;
1114 1115
		w->abacus.slot = False;
		w->abacus.diamond = False;
1116 1117 1118
		w->abacus.railIndex = 1;
		setSpace(w, BOTTOM);
		setSpace(w, TOP);
1119
	} else if (w->abacus.mode == JAPANESE || w->abacus.mode == ROMAN) {
1120
		w->abacus.colorScheme = 0;
1121
		w->abacus.decks[BOTTOM].factor = 1;
1122
		w->abacus.decks[TOP].factor = convertBaseToBottom(w->abacus.base);
1123 1124 1125 1126
		w->abacus.decks[BOTTOM].number = w->abacus.decks[TOP].factor -
			1;
		w->abacus.decks[TOP].number = w->abacus.base /
			w->abacus.decks[TOP].factor - 1;
1127 1128 1129 1130
		w->abacus.decks[BOTTOM].orientation = DEFAULT_BOTTOM_ORIENT;
		w->abacus.decks[TOP].orientation = DEFAULT_TOP_ORIENT;
		if (w->abacus.subdeck < 0)
			w->abacus.subdeck = DEFAULT_SUBDECKS;
1131
		w->abacus.vertical = False;
1132 1133 1134
		if (w->abacus.mode == JAPANESE) {
			w->abacus.slot = False;
			w->abacus.diamond = True;
1135 1136 1137
			w->abacus.decks[BOTTOM].spaces = DEFAULT_BOTTOM_SPACES - 1;
			w->abacus.decks[TOP].spaces = DEFAULT_TOP_SPACES - 1;
		} else if (w->abacus.mode == ROMAN) {
1138 1139
			w->abacus.slot = True;
			w->abacus.diamond = False;
1140 1141
			w->abacus.decks[BOTTOM].spaces = DEFAULT_BOTTOM_SPACES + 1;
			w->abacus.decks[TOP].spaces = DEFAULT_TOP_SPACES + 1;
1142
		}
1143 1144 1145
		w->abacus.railIndex = 0;
		setSpace(w, BOTTOM);
		setSpace(w, TOP);
1146
	} else if (w->abacus.mode == KOREAN) {
1147
		w->abacus.colorScheme = 0;
1148
		w->abacus.decks[BOTTOM].factor = 1;
1149
		w->abacus.decks[TOP].factor = convertBaseToBottom(w->abacus.base);
1150 1151 1152
		w->abacus.decks[BOTTOM].number = w->abacus.decks[TOP].factor;
		w->abacus.decks[TOP].number = w->abacus.base /
			w->abacus.decks[TOP].factor - 1;
1153 1154 1155 1156
		w->abacus.decks[BOTTOM].orientation = DEFAULT_BOTTOM_ORIENT;
		w->abacus.decks[TOP].orientation = DEFAULT_TOP_ORIENT;
		w->abacus.decks[BOTTOM].spaces = DEFAULT_BOTTOM_SPACES - 1;
		w->abacus.decks[TOP].spaces = DEFAULT_TOP_SPACES - 1;
1157
		w->abacus.vertical = False;
1158 1159
		w->abacus.slot = False;
		w->abacus.diamond = True;
1160 1161 1162
		w->abacus.railIndex = 0;
		setSpace(w, BOTTOM);
		setSpace(w, TOP);
1163
	} else if (w->abacus.mode == CHINESE) {
1164
		w->abacus.colorScheme = 0;
1165
		w->abacus.decks[BOTTOM].factor = 1;
1166
		w->abacus.decks[TOP].factor = convertBaseToBottom(w->abacus.base);
1167 1168 1169
		w->abacus.decks[BOTTOM].number = w->abacus.decks[TOP].factor;
		w->abacus.decks[TOP].number = w->abacus.base /
			w->abacus.decks[TOP].factor;
1170 1171 1172 1173
		w->abacus.decks[BOTTOM].orientation = DEFAULT_BOTTOM_ORIENT;
		w->abacus.decks[TOP].orientation = DEFAULT_TOP_ORIENT;
		w->abacus.decks[BOTTOM].spaces = DEFAULT_BOTTOM_SPACES;
		w->abacus.decks[TOP].spaces = DEFAULT_TOP_SPACES;
1174
		w->abacus.vertical = False;
1175 1176
		w->abacus.slot = False;
		w->abacus.diamond = False;
1177 1178 1179
		w->abacus.railIndex = 0;
		setSpace(w, BOTTOM);
		setSpace(w, TOP);
1180
	}
1181
	if (w->abacus.demo && w->abacus.aux == PRIMARY) {
1182
		/* Trying to keep these at a minimum... */
1183
		if (w->abacus.rails < MIN_DEMO_RAILS) {
1184
			intCat(&buf1, "Number of rails must be at least ",
1185
				MIN_DEMO_RAILS);
1186 1187 1188 1189
			stringCat(&buf2, buf1, ", for demo");
			free(buf1);
			DISPLAY_WARNING(buf2);
			free(buf2);
1190
			w->abacus.rails = MIN_DEMO_RAILS;
1191
		}
1192
		if (w->abacus.rails - w->abacus.decimalPosition <
1193
				MIN_DEMO_RAILS) {
1194 1195
			if (w->abacus.decks[BOTTOM].piecePercent != 0) {
				w->abacus.decks[BOTTOM].piecePercent = 0;
1196
			}
1197 1198
			if (w->abacus.decks[BOTTOM].piece != 0) {
				w->abacus.decks[BOTTOM].piece = 0;
1199 1200 1201 1202
			}
			w->abacus.decimalPosition = 0;
		}
	} else {
1203
		if (w->abacus.rails < MIN_RAILS) {
1204
			intCat(&buf1, "Number of rails must be at least ",
1205
				MIN_RAILS);
1206 1207
			DISPLAY_WARNING(buf1);
			free(buf1);
1208
			w->abacus.rails = MIN_RAILS;
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
		}
		if (w->abacus.decks[TOP].factor < 1 ||
				w->abacus.decks[TOP].factor > w->abacus.base) {
			intCat(&buf1, "Factor of Top Beads out of bounds, use 1..",
				w->abacus.base);
			DISPLAY_WARNING(buf1);
			free(buf1);
			w->abacus.decks[TOP].factor = 5;
		}
		if (w->abacus.decks[BOTTOM].factor < 1 ||
				w->abacus.decks[BOTTOM].factor >
				w->abacus.base) {
			intCat(&buf1,
				"Factor of Bottom Beads out of bounds, use 1..",
				w->abacus.base);
			DISPLAY_WARNING(buf1);
			free(buf1);
			w->abacus.decks[BOTTOM].factor = 1;
1227
		}
1228
	}
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	if (w->abacus.decks[TOP].number < 0 ||
			w->abacus.decks[TOP].number > w->abacus.base) {
		intCat(&buf1, "Number of Top Beads out of bounds, use 1..",
			w->abacus.base);
		DISPLAY_WARNING(buf1);
		free(buf1);
		w->abacus.decks[TOP].number = 2;
	}
	if (w->abacus.decks[BOTTOM].number < 0 ||
			w->abacus.decks[BOTTOM].number > w->abacus.base) {
		intCat(&buf1, "Number of Bottom Beads out of bounds, use 1..",
			w->abacus.base);
		DISPLAY_WARNING(buf1);
		free(buf1);
		w->abacus.decks[BOTTOM].number = 5;
	}
	if (w->abacus.decks[TOP].spaces < 0) {
		DISPLAY_WARNING("Number of Top Spaces must be at least 0");
		w->abacus.decks[TOP].spaces = 2;
	}
	if (w->abacus.decks[BOTTOM].spaces < 0) {
		DISPLAY_WARNING("Number of Bottom Spaces must be at least 0");
		w->abacus.decks[BOTTOM].spaces = 2;
	}
	if (w->abacus.decks[TOP].spaces == 0 && w->abacus.decks[BOTTOM].spaces == 0) {
1254
		DISPLAY_WARNING("Number of Top plus Bottom Spaces must be at least 1");
1255 1256
		w->abacus.decks[BOTTOM].spaces = 2;
	}
1257 1258
	if (w->abacus.groupSize < 2) {
		DISPLAY_WARNING("Group Size must be at least 2");
1259
		w->abacus.groupSize = DEFAULT_GROUP_SIZE;
1260
	}
1261 1262 1263 1264
	if (w->abacus.delay < 0) {
		DISPLAY_WARNING("Delay must be at least 0");
		w->abacus.delay = -w->abacus.delay;
	}
1265 1266
}

1267
static Boolean
1268
checkMove(AbacusWidget w)
1269
{
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	int deck_number, deck_position;
	char *buf1, *buf2;

	if (w->abacus.deck < 0 || w->abacus.deck > 2) {
		intCat(&buf1, "Corrupted deck input value ", w->abacus.deck);
		stringCat(&buf2, buf1, " out of bounds, use 0..2, ignoring");
		free(buf1);
		DISPLAY_WARNING(buf2);
		free(buf2);
		return False;
	}
1281
	if (w->abacus.rail < -w->abacus.decimalPosition || w->abacus.rail >=
1282
				w->abacus.rails - w->abacus.decimalPosition) {
1283
		intCat(&buf1, "Number of rails too small for input, needs rail ",
1284 1285 1286 1287 1288
			w->abacus.rail);
		DISPLAY_WARNING(buf1);
		free(buf1);
		return False;
	}
1289
	if (w->abacus.deck == PLACE_SETTING) {
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		/* moving decimal point */
		if (w->abacus.number + w->abacus.decimalPosition >=
				w->abacus.rails || w->abacus.number +
				w->abacus.decimalPosition < 0) {
			intCat(&buf1, "Corrupted number for input value ",
				w->abacus.number);
			stringCat(&buf2, buf1, " out of bounds, use ");
			free(buf1);
			intCat(&buf1, buf2, -w->abacus.decimalPosition);
			free(buf2);
			stringCat(&buf2, buf1, "..");
			free(buf1);
			intCat(&buf1, buf2, w->abacus.rails -
				w->abacus.decimalPosition);
			free(buf2);
			DISPLAY_WARNING(buf1);
			free(buf1);
			return False;
		}
		return True;
	}
	deck_number = w->abacus.decks[w->abacus.deck].number;
	deck_position = w->abacus.decks[w->abacus.deck].position[w->abacus.rail
		+ w->abacus.decimalPosition];
	if (w->abacus.decks[w->abacus.deck].orientation &&
			(w->abacus.number < -deck_number + deck_position ||
			w->abacus.number > deck_position)) {
		intCat(&buf1, "Corrupted number for input value ",
			w->abacus.number);
		stringCat(&buf2, buf1, " out of bounds, use ");
		free(buf1);
		intCat(&buf1, buf2, -deck_number + deck_position);
		free(buf2);
		stringCat(&buf2, buf1, "..");
		free(buf1);
		intCat(&buf1, buf2, deck_position);
		free(buf2);
		DISPLAY_WARNING(buf1);
		free(buf1);
		return False;
	}
	if (!w->abacus.decks[w->abacus.deck].orientation &&
			(w->abacus.number < -deck_position ||
			w->abacus.number > deck_number - deck_position)) {
		intCat(&buf1, "Corrupted number for input value ",
			w->abacus.number);
		stringCat(&buf2, buf1, " out of bounds, use ");
		free(buf1);
		intCat(&buf1, buf2, -deck_position);
		free(buf2);
		stringCat(&buf2, buf1, "..");
		free(buf1);
		intCat(&buf1, buf2, deck_number - deck_position);
		free(buf2);
		DISPLAY_WARNING(buf1);
		free(buf1);
		return False;
	}
	return True;
1349 1350 1351
}

static void
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
resetSubdecks(AbacusWidget w)
{

	int deck, ndecks;

	if (w->abacus.subdecks)
		free(w->abacus.subdecks);
	ndecks = (w->abacus.subdeck <= 0) ? DEFAULT_SUBDECKS :
		w->abacus.subdeck;
	if (!(w->abacus.subdecks = (SubdeckPart *) malloc(
			sizeof (SubdeckPart) * ndecks))) {
		DISPLAY_ERROR("Not enough memory (resetSubdecks), exiting.");
	}
	for (deck = 0; deck < ndecks; deck++) {
		w->abacus.subdecks[deck].number =
			numberSubbeads(w, deck);
		w->abacus.subdecks[deck].spaces = SUBDECK_SPACE;
		w->abacus.subdecks[deck].room =
			w->abacus.subdecks[deck].number +
			w->abacus.subdecks[deck].spaces;
		w->abacus.subdecks[deck].position =
			(w->abacus.decks[BOTTOM].orientation) ?
			w->abacus.subdecks[deck].number : 0;
	}
}

static void
resetBeads(AbacusWidget w)
1380
{
1381
	int deck, rail;
1382

1383
	w->abacus.currentDeck = IGNORE_DECK;
1384 1385 1386
	w->abacus.numDigits = w->abacus.rails + CARRY + 1;
	for (deck = BOTTOM; deck <= TOP; deck++) {
		if (w->abacus.decks[deck].position)
1387
			free(w->abacus.decks[deck].position);
1388 1389
		if (!(w->abacus.decks[deck].position = (int *)
				malloc(sizeof (int) * w->abacus.rails))) {
1390
			DISPLAY_ERROR("Not enough memory (resetBeads), exiting.");
1391
		}
1392 1393
	}
	if (w->abacus.digits)
1394
		free(w->abacus.digits);
1395 1396
	if (!(w->abacus.digits = (char *)
			malloc(sizeof (char) * w->abacus.numDigits))) {
1397
		DISPLAY_ERROR("Not enough memory (resetBeads1), exiting.");
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
	}
	for (deck = BOTTOM; deck <= TOP; deck++) {
		w->abacus.decks[deck].room = w->abacus.decks[deck].number +
			w->abacus.decks[deck].spaces;
		for (rail = 0; rail < w->abacus.rails; rail++)
			w->abacus.decks[deck].position[rail] =
				(w->abacus.decks[deck].orientation) ?
				w->abacus.decks[deck].number : 0;
	}
	if (w->abacus.sign) {
		rail = w->abacus.rails - 1;
		w->abacus.decks[BOTTOM].position[rail] =
			(w->abacus.decks[BOTTOM].orientation) ? 1 : 0;
	}
1412
	if (w->abacus.decks[BOTTOM].piece != 0) {
1413 1414
		rail = w->abacus.decimalPosition - 1;
		w->abacus.decks[BOTTOM].position[rail] =
1415 1416 1417 1418 1419 1420 1421 1422
			(w->abacus.decks[BOTTOM].orientation) ?
			numberPieces(w, BOTTOM) : 0;
		if (w->abacus.decks[TOP].number != 0 &&
				w->abacus.decks[TOP].piece != 0) {
			w->abacus.decks[TOP].position[rail] =
				(w->abacus.decks[TOP].orientation) ?
				numberPieces(w, TOP) : 0;
		}
1423
	}
1424 1425
	if (w->abacus.decks[BOTTOM].piece != 0 &&
			w->abacus.decks[BOTTOM].piecePercent != 0) {
1426
		rail = w->abacus.decimalPosition - w->abacus.shiftPercent - 1 -
1427
			((w->abacus.decks[BOTTOM].piece == 0) ? 0 : 1);
1428 1429
		w->abacus.decks[BOTTOM].position[rail] =
			(w->abacus.decks[BOTTOM].orientation) ?
1430 1431 1432 1433 1434 1435 1436
			numberPiecePercents(w, BOTTOM) : 0;
		if (w->abacus.decks[TOP].number != 0 &&
				w->abacus.decks[TOP].piecePercent != 0) {
			w->abacus.decks[TOP].position[rail] =
				(w->abacus.decks[TOP].orientation) ?
				numberPiecePercents(w, TOP) : 0;
		}
1437
	}
1438
	resetSubdecks(w);
1439 1440 1441
	for (rail = 0; rail < w->abacus.numDigits - 1; rail++)
		w->abacus.digits[rail] = '0';
	w->abacus.digits[w->abacus.numDigits - 1] = '\0';
1442 1443
}

1444
#ifdef HAVE_MOTIF
1445
static Boolean
1446
isEmptyCounter(AbacusWidget w)
1447
{
1448 1449
	int n = 0;

1450
	while (n < w->abacus.numDigits - 1) {
1451
		if (w->abacus.digits[n] != '0') {
1452
			return False;
1453
		}
1454
		n++;
1455
	}
1456
	return True;
1457
}
1458
#endif
1459

1460 1461
static Boolean
emptyCounter(AbacusWidget w)
1462
{
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
	int n = 0;
	Boolean good = True;

	while (n < w->abacus.numDigits - 1) {
		if (w->abacus.digits[n] != '0') {
			good = False;
			w->abacus.digits[n] = '0';
		}
		n++;
	}
	return good;
}

static void
setCounter(AbacusWidget w, int deck, int rail, int number)
{
	int n = 0, s = 0, i;
	int m = 0, o = 0, half;
	char *buffer;
	Boolean anomalyActive;
1483

1484 1485
	/* n digits above decimal *
	 * m digits below decimal */
1486
	while (n < w->abacus.numDigits - 2 - w->abacus.decimalPosition &&
1487 1488 1489 1490 1491 1492 1493 1494
			w->abacus.digits[n] == '0')
		n++;
	while (m < w->abacus.decimalPosition - 1 &&
			w->abacus.digits[w->abacus.numDigits - CARRY - m] ==
			'0')
		m++;
	while (o < w->abacus.numDigits - 1 && w->abacus.digits[o] == '0')
		o++;
1495
	half = w->abacus.numDigits - w->abacus.decimalPosition - n - 1;
1496
	s = (w->abacus.sign &&
1497
		(((w->abacus.decks[BOTTOM].position[w->abacus.rails - 1] == 1 &&
1498 1499
		!w->abacus.decks[BOTTOM].orientation) ||
		(w->abacus.decks[BOTTOM].position[w->abacus.rails - 1] == 0 &&
1500 1501
		w->abacus.decks[BOTTOM].orientation)) ||
		(w->abacus.decks[TOP].number != 0 &&
1502
		w->abacus.decks[TOP].piecePercent != 0 &&
1503 1504 1505 1506
		((w->abacus.decks[TOP].position[w->abacus.rails - 1] == 1 &&
		!w->abacus.decks[TOP].orientation) ||
		(w->abacus.decks[TOP].position[w->abacus.rails - 1] == 0 &&
		w->abacus.decks[TOP].orientation)))) &&
1507 1508 1509
		o < w->abacus.numDigits - 1) ? 1 : 0;
	if (!(buffer = (char *) malloc(sizeof (char) *
			(w->abacus.numDigits + 12 + sizeofRoman(w->abacus.base,
1510 1511
			w->abacus.romanNumerals, w->abacus.ancientRoman))))) {
		DISPLAY_ERROR("Not enough memory (setCounter), exiting.");
1512 1513 1514 1515 1516
	}
	if (s == 1)
		buffer[0] = '-';
	for (i = 0; i < half; i++)
		buffer[s + i] = w->abacus.digits[n + i];
1517 1518
	buffer[s + half] = decimalChar(w);
	buffer[s + half + 1] = '\0';
1519 1520 1521 1522
	if (w->abacus.decks[BOTTOM].piece != 0) {
		char *stringBuf, *finalBuf, *midBuf;
		int pieces = w->abacus.decks[BOTTOM].piece;
		int precision;