File: dataview.C

package info (click to toggle)
mixviews 1.20-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 2,920 kB
  • ctags: 5,958
  • sloc: cpp: 32,873; ansic: 2,110; makefile: 411; sh: 17
file content (593 lines) | stat: -rw-r--r-- 14,355 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
// dataview.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 "localdefs.h"
#include <InterViews/border.h>
#include <InterViews/box.h>
#include <InterViews/canvas.h>
#include <InterViews/cursor.h>
#include <InterViews/event.h>
#include <InterViews/glue.h>
#include <InterViews/message.h>
#include <InterViews/perspective.h>
#include "application.h"
#include "block.h"
#include "controller.h"
#include "data.h"
#include "dataview.h"
#include "graph.h"
#include "range.h"
#include "scale.h"
#include "scroller.h"
#include "statuspanel.h"
#include "viewchanger.h"
#include <X11/keysym.h>

// if these are not defined, this program cannot be run correctly on R6 displays
// I assume that if one is not defined, then none are

#ifndef XK_KP_Left
#define XK_KP_Left      0xFF96
#define XK_KP_Right     0xFF98
#define XK_KP_Up        0xFF97
#define XK_KP_Down      0xFF99
#endif

ViewInfo::ViewInfo() 
	: rangeUnits(FrameUnit), frameRange(-1, -1), channelRange(-1, -1) {
}

ViewInfo::ViewInfo(RangeUnit units)
	: rangeUnits(units), frameRange(-1, -1), channelRange(-1, -1) {
}

ViewInfo::ViewInfo(RangeUnit units, const Range& frange, const Range& chrange)
	: rangeUnits(units), frameRange(frange), channelRange(chrange) {
}

//********

DataView::DataView(Controller *c, ViewInfo& info)
		: dataptr(c->model()), controller(c), 
		cursorLoc(0), graphsShown(0), maxGraphs(0), perspectiveSet(false) {
	Init(info);
}

void
DataView::Init(ViewInfo &info) {
	perspective = new Perspective;
	hscale = new HScale(
		dataptr->frameRangeLabel(info.rangeUnits),
		dataptr->frameRange(info.rangeUnits),
		dataptr->frameRangeDisplay(info.rangeUnits),
		Scale::IsAbove
	);
	setHorizRangeUnits(info.rangeUnits);
	graphs = nil;
	expandGraphArray();
	dataptr->Attach(this);	// this is view of Data
	interior = new VBox(
			hscale,
			graphBox = new VBox,		// box for scales and graphs
			new HBorder,
			new HBox(
				new ViewScaler(this),
				new VBorder,
				makeEditStatusPanel()
			),
			new HBorder,
			viewScroller = new HorizontalViewScroller(this)
	);
	Insert(interior);
	// initialize internal copy of perspective
	shown = new Perspective;
}

Interactor *
DataView::makeEditStatusPanel() {
	StatusPanel* start = nil;
	StatusPanel* end = nil;
	StatusPanel* duration = nil;
	VBox* panel = new VBox(
		new VGlue(3, 0, vfil),
		new HBox(
			start = new StatusPanel("Edit Start: ",
				"                         "),
			new HGlue(10, 0, hfil),
			end = new StatusPanel("Edit End: ",
				"                         ")
		),
		new VGlue(0, 0),
		new HBox(
			duration = new StatusPanel("Edit Dur: ",
				"                         "),
			new HGlue(20, 0, hfil)
		),
		new VGlue(3, 0, vfil)
	);
	// this delegate is passed to the controller
	editDelegate = new EditStatusDelegate(this, start, end, duration);
	return panel;
}

DataView::~DataView() {
	// all graphs and scales are destroyed by the MonoScene subclass
	delete [] graphs;
	dataptr->Detach(this);	// detach this view from Data
	Resource::unref(shown);
	Resource::unref(perspective);
}

void
DataView::Adjust(Perspective &p) {
	BUG("DataView::Adjust()");
	if(p != *perspective) {
		Application::busy(true);
		*perspective = constrainView(p);
		struct DoAdjuster : public Block {
			Perspective *newView;
			DoAdjuster(Perspective *p) : newView(p) {}
			redefined void doIt( Interactor *i ) {
				i->Adjust(*newView);
			}
		} adjuster(perspective);
		doForGraphs(adjuster);
		p = *perspective;
		perspective->Update();	// updates scales and sliders
		Application::busy(false);
	}
}

// all Events handled by the DataView are passed to each of the Graph objects
// to see if it was generated by it.  If the shift key is down, the event is
// only handed to the Graph that generated it.  If not, the event is passed to
// all the Graphs.  This allows the Graphs to react in sync to events only
// generated in one.

void
DataView::Handle(Event& e) {
	graphEvent(e.shift ? e.target : nil, e);
}

void
DataView::graphEvent(Interactor *target, Event &e) {
	if(e.shift_is_down()) {		// if selecting single channel
		switch(e.eventType) {
		case DownEvent:
			unselectOthers(target);
			cursorLoc = e.x;		
			break;
		case UpEvent:
			if(e.leftmouse || e.middlemouse)
				cursorLoc = e.x;
			break;
		case MotionEvent:
			if(e.leftmouse)		// if tracking insert point
				cursorLoc = e.x;
			break;
		case EnterEvent:		// if tracking insert or region between graphs
			if(e.leftmouse  || e.middlemouse) {
				unselectOthers(target);
				((Graph *) e.target)->setInsertCoord(cursorLoc);
			}
			break;
		default:
			break;
		}
	}
    struct EventSender : public TargetBlock {
		Event &event;
		EventSender(Interactor *target, Event &e) 
			: TargetBlock(target), event(e) {}
		redefined void doThis( Interactor *i ) {
			Graph *g = (Graph *) i;
			g->handleEvent(event);
		}
    } sender(target, e);
    doForGraphs( sender );
}

void
DataView::Reconfig() {
	BUG("DataView::Reconfig()");
	*shown = *perspective;
	hscale->setViewPerspective(perspective);	// ??? OK HERE ???
	viewScroller->Update();
	// set each graph's plot edges to match horiz scale's borders
    struct Margins : public Block {
		int left, right;
		Margins(int l, int r) : left(l), right(r) {}
		redefined void doIt( Interactor *i ) {
			BorderedArea *b = (BorderedArea *) i;
			b->setHorizMargins(left, right);
		}
    } margins(hscale->topMargin(), hscale->bottomMargin());
    doForGraphs( margins );
	MonoScene::Reconfig();
}

void
DataView::Resize() {
	hscale->setSpacers(horizScaleLeftSpacer(), horizScaleRightSpacer());
	MonoScene::Resize();	// this resizes all interior Interactors
}

void
DataView::Update() {
	BUG("DataView::Update()");
	Application::busy(true);
	checkPerspective();
	updateGraphs();
	perspective->Update();
	Application::busy(false);
}

boolean
DataView::keyCommand(unsigned MXINT32 sym) {
	boolean interested = true;
	switch (sym) {
	// map arrow keys from keypad and non-keypad to same methods
	case XK_Left:
	case XK_KP_Left:
		horizontalZoom();
		break;
	case XK_Up:
	case XK_KP_Up:
		verticalUnZoom();
		break;
	case XK_Right:
	case XK_KP_Right:
		horizontalUnZoom();
		break;
	case XK_Down:
	case XK_KP_Down:
		verticalZoom();
		break;
	case XK_f:
		setVisibleFrameRange();
		break;
	case XK_V:
		resetVerticalRange();
		break;
	case XK_equal:
		setChannelRange();
		break;
	case XK_greater:
		shiftChannelViewUp();
		break;
	case XK_less:
		shiftChannelViewDown();
		break;
	case XK_j:
		setInsertPoint();
		break;
	case XK_J:
		setEditRegion();
		break;
	case XK_percent:
	case XK_asciicircum:
		toggleGraphMode();
		break;
	case XK_KP_Add:
		showVerticalScales(true);
		break;
	case XK_KP_Subtract:
		showVerticalScales(false);
		break;
	default:
		interested = false;
		break;
	}
	return interested;
}

boolean
DataView::doForGraphs(Block &block) {
    int i = 0;
	while(1)
		if(block.doAndDone( graphs[i++] ))
			return true;
	return false;
}

void
DataView::updateGraphs() {
    struct Updater : public Block {
		redefined void doIt( Interactor *i ) {
			i->Update();
		}
    } updater;
    doForGraphs( updater );
}

void
DataView::unselect() {
	unselectOthers(nil);	// unselects all channels
}

void
DataView::unselectOthers(Interactor *target) {
    struct Unselector : public DoExceptBlock {
		Unselector(Interactor *notThis) : DoExceptBlock(notThis) {}
		redefined void doThis( Interactor *i ) {
			Graph *g = (Graph *) i;
			g->unSelect();
		}
    } unselector(target);
    doForGraphs( unselector );
}

Perspective &
DataView::constrainView(Perspective& np) {
	BUG("DataView::constrainView()");
	if(np == *shown) return np;
	*shown = np;
	constrainHorizontalView(*shown);
	constrainVerticalView(*shown);
	return *shown;
}

void
DataView::constrainHorizontalView(Perspective& np) {
	const int minwidth = 8;
	register Perspective* p = perspective;
	np.height = p->height;
	np.curx = max(np.curx, 0);						// always
	np.curwidth = max(np.curwidth, minwidth);		// always
	int nx = np.curx;
	int nw = np.curwidth;
	if(nw == p->curwidth && np.width == p->width)
		np.curx = min(nx, max(0, np.width - nw));
	else if(nx + nw >= np.width) {					// horizontal scale change
		np.curwidth = min(nw, np.width);
		np.curx = min(nx, np.width - np.curwidth);
	}
	np.lx = np.curwidth;
	np.sx = np.lx/4;				// scroll incr. is 1/4 screen
}

Range
DataView::getDisplayedHorizRange() {
	return hscale->getRange();
}

boolean
DataView::horizScaleInFrames() {
	return horizRangeUnits() != FrameTimeUnit
		&& horizRangeUnits() != ChannelSpecialUnit;
}

void
DataView::setHorizScaleTo(RangeUnit units) {
	setHorizRangeUnits(units);
	hscale->setNumberDisplay(dataptr->frameRangeDisplay(units));
	hscale->setRange(dataptr->frameRange(units));
	setHorizScaleLabel(dataptr->frameRangeLabel(units));
	hscale->Update();
	getEditDelegate()->update();	// update the edit panel display
}

double
DataView::horizontalScalingFactor() {
	return getHorizScaleRange().max() / dataptr->length();
}

int
DataView::horizScaleLeftSpacer() {
	Canvas *c = topGraph()->vScale()->GetCanvas();
	return c != nil ? c->Width() : topGraph()->vScale()->GetShape()->width;
}

void
DataView::setHorizScaleLabel(const char *name) {
	hscale->setLabel(name);
}

void
DataView::showVerticalScales(boolean show) {
    struct ScaleShow : public Block {
		boolean show;
		ScaleShow(boolean doshow) : show(doshow) {}
		redefined void doIt( Interactor *i ) {
			Graph *g = (Graph *) i;
			g->vScale()->showScale(show);
		}
    } scaleshow(show);
    doForGraphs( scaleshow );
	doChange();
}

void
DataView::doChange() {
	if(canvas != nil) {
		topGraph()->Parent()->Change(nil);
	}
}

void
DataView::addGraph(Graph *graph) {
	if(graphsShown == maxGraphs)
		expandGraphArray();
	if(graphsShown == maxGraphs)
		return;			// failed to expand!
	graphs[graphsShown] = graph;	// add to array
	Scale *vscale = new VScale(
		graph->verticalScaleLabel(),
		graph->verticalScaleRange()
	);
	vscale->setViewPerspective(getVerticalPerspective());
	graph->setViewPerspective(getHorizontalPerspective());
	graph->attachVScale(vscale);	// establish connection
	graphBox->Insert(new HBox(vscale, graph));
	incrementGraphsVisible();
}

// graph array is expanded by 4 plus an additional zeroed end-of-array member

void
DataView::expandGraphArray() {
	int newMax = maxGraphs + 4;
	Graph **newGraphs = new Graph *[newMax + 1];	// plus 1 for EOA
	if(newGraphs) {
		int i;
		for(i=0; i < maxGraphs; i++)
			newGraphs[i] = graphs[i]; // copy existing graphs if any
		for(i = maxGraphs; i <= newMax; i++)
			newGraphs[i] = nil;
		delete [] graphs;
		graphs = newGraphs;
		maxGraphs = newMax;
	}
}

// remove one graph and its associated scale from frame 
// the channel removed is always the highest channel displayed

void
DataView::removeGraph(boolean chng) {
	int topGraph = graphsVisible() - 1;
	if(topGraph > 0) {				// dont remove last graph!
		Graph *graph = graphs[topGraph];
		Scale *scale = graph->vScale();
		graph->removeVScale(scale);
		// graph and scale are enclosed in Scene, so remove this too
		Scene* interior = graph->Parent();
		if(interior != nil) {
			graphBox->Remove(interior);
			interior->Remove(scale);
			interior->Remove(graph);
			delete interior;
		}
		delete scale;
		delete graph;
		graphs[topGraph] = nil;
		decrementGraphsVisible();
		if(chng)
			doChange();
	}
}

void
DataView::horizontalZoom() {
	Perspective np = *perspective;
	int width = np.curwidth;
	np.curx += width/4;
	np.curwidth /= 2;
	Adjust(np);
}

void
DataView::horizontalUnZoom() {
	Perspective np = *perspective;
	int width = np.curwidth;
	np.curx -= width/2;
	np.curwidth *= 2;
	Adjust(np);
}

void
DataView::verticalZoom() {
	Perspective np = *perspective;
	np.cury = 0;
	np.curheight = int(np.curheight * 0.5);
	Adjust(np);
}

void
DataView::verticalUnZoom() {
	Perspective np = *perspective;
	np.cury = 0;
	np.curheight *= 2;
	Adjust(np);
}

void
DataView::resetVerticalRange() {
	Perspective np = *perspective;
	np.cury = 0;
	np.curheight = perspective->height;
	Adjust(np);
}

void
DataView::setVisibleFrameRange() {
	VisibleFrameRange changer(this);
	if(changer.configure(controller))
		changer.apply();
}

void
DataView::setInsertPoint() {
	InsertPoint changer(this);
	if(changer.configure(controller))
		changer.apply();
}

void
DataView::setEditRegion() {
	EditRegion changer(this);
	if(changer.configure(controller))
		changer.apply();
}

//********

EditStatusDelegate::EditStatusDelegate(DataView *view, StatusPanel* start, 
		StatusPanel* end, StatusPanel* dur)
		: myStart(start), myEnd(end), myDur(dur), myView(view) {
	myStart->ref();
	myEnd->ref();
	myDur->ref();
}

EditStatusDelegate::~EditStatusDelegate() {
	Resource::unref(myDur);
	Resource::unref(myEnd);
	Resource::unref(myStart);
}

// this converts range in frames into range in time when necessary

void
EditStatusDelegate::update(const Range& current) {
	editRange = current;
	if(!myView->horizScaleInFrames()) {	// convert to time range
		Range values(editRange.min(), editRange.max() + 1);
		values *= myView->horizontalScalingFactor();
		myStart->updateValue(values.min());
		myEnd->updateValue(values.max());
		myDur->updateValue(values.spread());
	}
	else {
		myStart->updateValue(current.intMin());
		myEnd->updateValue(current.intMax());
		myDur->updateValue(current.size());
	}
}