File: scale.C

package info (click to toggle)
mixviews 1.20-10.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,928 kB
  • ctags: 5,960
  • sloc: cpp: 32,879; ansic: 2,110; makefile: 445; sh: 17
file content (651 lines) | stat: -rw-r--r-- 16,220 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
// scale.C

/******************************************************************************
 *
 *  MiXViews - an X window system based sound & data editor/processor
 *
 *  Copyright (c) 1993, 1994 Regents of the University of California
 *
 *  Author:     Douglas Scott
 *  Date:       December 13, 1994
 *
 *  Permission to use, copy and modify this software and its documentation
 *  for research and/or educational purposes 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. The author reserves the right to distribute this
 *  software and its documentation.  The University of California and the author
 *  make no representations about the suitability of this software for any 
 *  purpose, and in no event shall University of California be liable for any
 *  damage, loss of data, or profits resulting from its use.
 *  It is provided "as is" without express or implied warranty.
 *
 ******************************************************************************/


#ifdef __GNUG__
#pragma implementation
#endif

#include <InterViews/shape.h>
#include <InterViews/message.h>
#include <InterViews/painter.h>
#include <InterViews/scene.h>
#include <InterViews/font.h>
#include <InterViews/canvas.h>
#include <InterViews/box.h>
#include <InterViews/glue.h>
#include <InterViews/perspective.h>
#include "localdefs.h"
#include "scale.h"
#include "vmessage.h"
extern "C" {
#include <math.h>
}
#ifdef NeXT
#include <m68k/math.h>
#else
#include <values.h>
#endif

char *
ScaleMarks::Tick::setString(char *str) {
	delete [] string;
	return string = newstr(str);
}

ScaleMarks::TickList::~TickList() { 
	while(currentsize > 0) delete list[--currentsize]; 
	delete list;
}

// *************

const int ScaleMarks::defaultLabelPad = 2;
const int ScaleMarks::defaultBorderWidth = 0;

ScaleMarks::ScaleMarks(const Range &r, Scale::NumberDisplay nd)
		: scaleRange(r), displayIntegers(nd == Scale::AsInteger) {
	Init();
}

ScaleMarks::~ScaleMarks() {
	setViewPerspective(nil);
	Resource::unref(shown);
	delete tickList;
}

void
ScaleMarks::Init() {
	tickList = nil;
	view = nil;
	shown = new Perspective;
	setVertMargins(defaultBorderWidth, defaultBorderWidth);
	setHorizMargins(defaultBorderWidth, defaultBorderWidth);
	topSpacer = 0;
	bottomSpacer = 0;
	rangeChanged = true;
}

// These are redefined InterViews virtual functions.

void
ScaleMarks::Resize() {
	doAdjust();
}

void
ScaleMarks::Update() {
	if(viewChanged()) {
		newRange();
		if(view != nil) *shown = *view;
		BorderedArea::Update();
	}
}

// new public functions

void
ScaleMarks::setRange(const Range& range) {
	Range newRange = range;		// copy to avoid const
	newRange.check();
	if(newRange != scaleRange) {
		scaleRange = newRange; 
		rangeChanged = true;
	}
}

void
ScaleMarks::setSpacers(int top, int bottom) {
	if(topSpacer != top || bottomSpacer != bottom) {
		topSpacer = top;
		bottomSpacer = bottom;
		BorderedArea::Update();
	}
}

// internal functions

void
ScaleMarks::doAdjust() {
	Range currentRange = getCurrentRange();
	currentRange.limit(-MAXDOUBLE, MAXDOUBLE);	// protect against NaN
	double minval = currentRange.min();
	double range, primeIncr;
	const int minTicks = 2;
	const int maxTicks = 11;
	int ztick;
	if(currentRange.includesZero()) {
		range = currentRange.absoluteMax();
		ztick = 1;	/* origin counts as tick mark */
	}
	else	{		/* scale all pos or neg */
		range = currentRange.spread();
		ztick = 0;
	}
	int zeroRange = false;
	/* find max power of ten less than range on one side of origin */
	if (range > 0.0)
	    primeIncr = lowerPowerOfTen(range);
	else {
	    primeIncr = 1.0;
	    ztick = 1;	// add extra tick at end
	    zeroRange = true;
	}
	if(displayIntegers)
		primeIncr = max(1.0, primeIncr);	// must be >= 1
	double totalrange = currentRange.spread();
	int nPrimeTicks, nSecTicks = 10;
	// make sure there is no more than 11 and at least 2 prime ticks
	// other than the origin 
	while((nPrimeTicks = int(totalrange/primeIncr) + ztick) > maxTicks) {
		primeIncr *= 2; 		// 0, 10, 20 -> 0, 20, 40,
	}
	while((nPrimeTicks = int(totalrange/primeIncr) + ztick) <= minTicks) {
		if(displayIntegers && primeIncr == 1.0)
			break;				// cannot subdivide!
		else if(nSecTicks == 10) {
			primeIncr /= 2; 	// 10, 20, 30 -> 10, 15, 20, 25
			nSecTicks = 2;
		}
		else {
			primeIncr /= 5;		// 10, 15, 20 -> 10, 11, 12, 13
			nSecTicks = 10;
		}
	}
	int primeTicksOnly = displayIntegers && (primeIncr < 5.0);
	nPrimeTicks = max(1, int(totalrange/primeIncr) + ztick);	// recompute
        double tickIncrement, tickdist, baseval, currentTickLoc;
        double basePrimeVal;
	int ticknumber;
		
	if (!zeroRange) {
	    nSecTicks = (scaleLength()/nPrimeTicks < 31) ? 2 : nSecTicks;
	    tickIncrement = primeIncr / nSecTicks;
	    totalTicks = int(totalrange/tickIncrement) + ztick;
	    // actual fractional distance between smallest ticks
	    tickdist = scaleLength()/(totalrange/tickIncrement);
	    // bottom-most tick value (whether or not labeled)
	    baseval = roundUpToNearest(minval, tickIncrement);
	    currentTickLoc = abs((minval-baseval)/tickIncrement) * tickdist;
	    currentTickLoc += baseLocation();	// offset by borderwidth
	    basePrimeVal = roundUpToNearest(minval, primeIncr);
	    ticknumber = -int(abs(minval - basePrimeVal)/tickIncrement);
	}
	// special case for zoom-in such that start of screen and end of screen
	// are all the same frame
	else {
	    nSecTicks = 1;
	    tickIncrement = 0;
	    totalTicks = 2;
	    // ticks set at opposite ends of scale
	    tickdist = scaleLength();
	    // bottom-most tick value (whether or not labeled)
	    baseval = minval;
	    currentTickLoc = baseLocation();
	    basePrimeVal = baseval;
	    ticknumber = 0;
	}
	
	const int shortTickLength = 2, medTickLength = 4, MXINT32TickLength = 6;

	// add all ticks into list
	delete tickList;
	tickList = new TickList(totalTicks);
	for(int i = 0; i < totalTicks; i++) {
		int loc = int(currentTickLoc);
		int len = shortTickLength;
		char *string = NULL;
		if(!(ticknumber % nSecTicks)) { // every 2 or 10 ticks
			len = MXINT32TickLength;
			double currentVal = baseval + (i * tickIncrement);
			double increment = tickIncrement*nSecTicks;
			string = displayIntegers ? iFormat(currentVal, increment)
				: kFormat(currentVal, increment);
		}
		else if(nSecTicks == 10 && !(ticknumber % 5))
			len = medTickLength;
		if(!primeTicksOnly || len == MXINT32TickLength)
			tickList->addTick(loc, len, string);
		currentTickLoc += tickdist;
		ticknumber++;
	}
}

int
ScaleMarks::textMajorDimension(char *string) {
    return getTextMajorDimension(output->GetFont(), string) + labelPad;
}

int
ScaleMarks::textMinorDimension(char *string) {
    return getTextMinorDimension(output->GetFont(), string) + labelPad;
}

void
ScaleMarks::setViewPerspective(Perspective *np) {
	// this does what Init() does in other view classes
	if(np != view) {
		if(view != nil)
			view->Detach(this);
		view = np;
		if(view != nil) {
			view->Attach(this);
			*shown = *view;
		}
	}
}

// *************

void
VScaleMarks::Init() {
	SetClassName("VerticalScale");
}

void
VScaleMarks::Reconfig() {
	const char *a = GetAttribute("padding");
	if (a != nil) labelPad = atoi(a);
	else labelPad = defaultLabelPad;
	a = GetAttribute("borderWidth");
	int border = (a != nil) ? atoi(a) : defaultBorderWidth;
	// always leave room for labels on ends
	int textPad = textMajorDimension("0e+00")/2;
	setVertMargins(border + textPad, border + textPad);
	boolean allowShrink = AttributeIsSet("allowShrink");
	shape->height = textMajorDimension("0.00e+00") * 12 
		+ topBorder() + bottomBorder() + topSpacer + bottomSpacer;
	shape->width = textMinorDimension("0.00e+00") + 20;
	shape->Rigid(0, 0, (allowShrink ? shape->height/2 : 0), vfil);
}

void
VScaleMarks::Redraw(Coord l, Coord b, Coord r, Coord t) {
	output->Clip(canvas, l, b, r, t);
	output->ClearRect(canvas, l, b, r, t);
	drawTicks(b, t);
	output->Line(canvas, xmax, baseLocation(), xmax, 
		scaleLength() + baseLocation());
	output->NoClip();
}

void
VScaleMarks::drawTicks(Coord begin, Coord end) {
	int averageOffset = textMajorDimension("+000.00e+00")/2;
	int bottom = begin - averageOffset, top = end + averageOffset;
	for(int i = 0; i < tickList->currentSize(); i++) {
		Tick *currentTick = tickList->get(i);
		int loc = currentTick->loc;
		if(loc < bottom || loc > top)
			continue; // skip unneeded redraws
		int len = currentTick->length;
		char *string = currentTick->string;
		output->Line(canvas, xmax - len, loc, xmax, loc);
		if(string) {
			int textOffset = textMajorDimension(string)/2;
			output->Text(canvas, string, labelPad,
				loc - textOffset);
		}
	}
}

Range
VScaleMarks::getCurrentRange() {
	register Perspective *v = view;
	Range newrange = scaleRange;
	if(v != nil) {
		double factor = v->curheight/double(v->height);
		newrange.scaleBy(factor).check();
	}
	return newrange;
}

int
VScaleMarks::getTextMajorDimension(const Font *f, char *) {
	return f->Height();
}

int
VScaleMarks::getTextMinorDimension(const Font *f, char *string) {
	return f->Width(string);
}

int
VScaleMarks::scaleLength() {
	return ymax + 1 - topBorder() - bottomBorder() - topSpacer - bottomSpacer;
}

void
VScaleMarks::newRange() {
	if(view != nil) {
		double factor = double(view->height) / shown->height;
		scaleRange *= factor;
	}
}

int
VScaleMarks::viewChanged() {
	register Perspective *v = view;
	int status = 0;
	// only converned with changes in the y (height) dimension here
	if(v != nil && (v->cury != shown->cury
			|| v->curheight != shown->curheight
			|| v->height != shown->height)) {
		status = 1;
	}
	if(rangeChanged) {
		status = 1;
		rangeChanged = false;	// reset
	}
	return status;
}
// *************

void
HScaleMarks::Init() {
	SetClassName("HorizontalScale");
}

void
HScaleMarks::Reconfig() {
	const char *a = GetAttribute("padding");
	if (a != nil) labelPad = atoi(a);
	else labelPad = defaultLabelPad;
	a = GetAttribute("borderWidth");
	int border = (a != nil) ? atoi(a) : defaultBorderWidth;
	// always leave room for labels on ends
	int textPad = textMajorDimension("0e+00")/2;
	setHorizMargins(border + textPad, border + textPad);
	boolean allowShrink = AttributeIsSet("allowShrink");
	shape->height = textMinorDimension("0.00e+00") + 8;
	shape->width = textMajorDimension("0.00e+00") * 12 
		+ topBorder() + bottomBorder() + topSpacer + bottomSpacer;
	shape->Rigid((allowShrink ? shape->width/2 : 0), hfil, 0, 0);
}

void
HScaleMarks::Redraw(Coord l, Coord b, Coord r, Coord t) {
	output->Clip(canvas, l, b, r, t);
	output->ClearRect(canvas, l, b, r, t);
	drawTicks(l, r);
	int edge = isAbove ? 0 : ymax;
	output->Line(canvas, baseLocation(), edge,
		scaleLength() + baseLocation(), edge);
	output->NoClip();
}

void
HScaleMarks::drawTicks(Coord begin, Coord end) {
	int averageOffset = textMajorDimension("+000.00e+00")/2;
	int textSpacer = textMinorDimension("+000.00e+00") + labelPad;
	int textVLoc = isAbove ? ymax - textSpacer : textSpacer;
	int left = begin - averageOffset, right = end + averageOffset;
	int tickBase = isAbove ? 0 : ymax;
	for(int i = 0; i < tickList->currentSize(); i++) {
		Tick *currentTick = tickList->get(i);
		int loc = currentTick->loc;
		if(loc < left || loc > right)
			continue; // skip unneeded redraws
		int len = currentTick->length;
		char *string = currentTick->string;
		output->Line(canvas, loc, tickBase,
			loc, isAbove ? len : tickBase - len);
		if(string) {
			int textOffset = textMajorDimension(string)/2;
			output->Text(canvas, string, loc - textOffset, textVLoc);
		}
	}
}

Range
HScaleMarks::getCurrentRange() {
	register Perspective *v = view;
	Range newrange = scaleRange;
	if(v != nil) {
		double rmin = newrange.min();
		double rmax = newrange.max();
		double spread = displayIntegers ? newrange.size() : newrange.spread();
		double fstart = (v->curx - v->x0) / double(v->width);
		double fwidth = v->curwidth/double(v->width);
		rmin = rmin + (fstart * spread);
		rmax = rmin + (fwidth * spread);
		if(displayIntegers) {
			rmin = round(rmin);
			rmax = round(rmax);
		}
		newrange.set(rmin, rmax);
	}
	return newrange;
}

int
HScaleMarks::getTextMajorDimension(const Font *f, char *string) {
	return f->Width(string);
}

int
HScaleMarks::getTextMinorDimension(const Font *f, char *) {
	return f->Height();
}

int
HScaleMarks::scaleLength() {
	return xmax + 1 - topBorder() - bottomBorder() - topSpacer - bottomSpacer;
}

void
HScaleMarks::newRange() {
	if(view != nil) {
		double factor = double(view->width) / shown->width;
		scaleRange *= factor;
	}
}

int
HScaleMarks::viewChanged() {
	register Perspective *v = view;
	int status = 0;
	// only converned with changes in the x (width) dimension here
	if(v != nil && (v->curx != shown->curx
			|| v->curwidth != shown->curwidth
			|| v->width != shown->width)) {
		status = 1;
	}
	if(rangeChanged) {
		status = 1;
		rangeChanged = false;	// reset
	}
	return status;
}

// ********

Scale::Scale()
	: scalemarks(nil), placeHolder(nil), label(nil), scaleShown(true), pad(0) {
}

Scale::~Scale() {
	Resource::unref(
		scaleShown ? (Resource *) placeHolder : (Resource *) scalemarks
	);
}

void
Scale::Update() {
	MonoScene::Update();
	label->Update();
	scalemarks->Update();
}

void
Scale::Reconfig() {
	MonoScene::Reconfig();
}

void
Scale::setLabel(const char *name) {
	label->setText(name);
}
 
const char *
Scale::getLabel() const {
	return label->getText();
}

void
Scale::setNumberDisplay(Scale::NumberDisplay nd) {
	 scalemarks->setNumberDisplay(nd);
}

void
Scale::setRange(const Range &range) {
	scalemarks->setRange(range);
}

Range
Scale::getRange() {
	return scalemarks->getRange();
}

Scale::NumberDisplay
Scale::numberDisplay() { return scalemarks->numberDisplay(); }

void
Scale::setSpacers(int top, int bottom) {
	scalemarks->setSpacers(top, bottom);
}

void
Scale::setViewPerspective(Perspective *np) {
	scalemarks->setViewPerspective(np);
}

void
Scale::showScale(boolean show) {
	if(show != scaleShown) {
		if(show) {
			if(placeHolder != nil)
				interior->Remove(placeHolder);
			interior->Insert(scalemarks);
		}
		else {
			interior->Remove(scalemarks);
			interior->Insert(placeHolder = getPlaceHolder());
		}
		interior->Change(scalemarks);
		scaleShown = show;
	}
}

int
Scale::Height() {
	Canvas *c = GetCanvas();
	return (c != nil) ? c->Height() : 0;
}

int
Scale::Width() {
	Canvas *c = GetCanvas();
	return (c != nil) ? c->Width() : 0;
}

// **********

VScale::VScale(const char *name, const Range& range, NumberDisplay nd, Position pos) {
	Init(name, range, nd, pos);
}

void
VScale::Init(const char *name, const Range& range,
		NumberDisplay nd, Position pos) {
	SetClassName("VScale");
	label = new VMessage("VerticalScaleLabel", "MMMMMMMM", Center, pad);
	label->setText(name);
	scalemarks = new VScaleMarks(range, nd, pos);
	Insert(interior = new VBox(label,scalemarks));		
}

void
VScale::Reconfig() {
	Shape* s = label->GetShape();
	s->Rigid();
	Scale::Reconfig();
	s = scalemarks->GetShape();
	s->width = shape->width;	// set width of marks to width of container
}

int 
VScale::topMargin() {
	Canvas *c = label->GetCanvas();
	int labelheight = (c != nil) ? c->Height() : 0;
	return scalemarks->topBorder() + labelheight;
}

int
VScale::bottomMargin() { return scalemarks->bottomBorder(); }

Interactor *
VScale::getPlaceHolder() {
	return (placeHolder == nil) ? new VGlue(3,0,vfil) : placeHolder;
}

// **********

HScale::HScale(const char *name, const Range& range,
		NumberDisplay nd, Position pos) {
	Init(name, range, nd, pos);
}

void
HScale::Init(const char *name, const Range& range, NumberDisplay nd, Position pos) {
	SetClassName("HScale");
	label = new VMessage("HorizontalScaleLabel", "0000000000000000",
		Center, pad);
	label->setText(name);
	scalemarks = new HScaleMarks(range, nd, pos);
	Insert(interior = new VBox(
		new HBox(
			new HGlue,
			label,
			new HGlue
		),
		scalemarks)
	);
}

int 
HScale::topMargin() {
	return scalemarks->topBorder();
}

int 
HScale::bottomMargin() {
	return scalemarks->bottomBorder();
}

Interactor *
HScale::getPlaceHolder() {
	return (placeHolder == nil) ? new HGlue(3,0,hfil) : placeHolder;
}