summaryrefslogtreecommitdiff
path: root/tvision/examples/sdlg2/scrlgrp.cpp
blob: af646353ab0a6437738ff827393d8f9f72f47026 (plain)
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
#define Uses_ScrollGroup
// ===================================================================
#define Uses_TGroup
#define Uses_TPoint
#define Uses_TRect
// ===================================================================

#define Uses_TScrollBar
#define Uses_TBackground
#define Uses_TEvent
// ===================================================================
#include <tv.h>
#include "dlg.h"
// ===================================================================

ScrollGroup::ScrollGroup(const TRect& bounds, TScrollBar* hsb, TScrollBar* vsb) :
	ScrollGroupInit(initBackground),
	TGroup(bounds),
	hScrollBar(hsb),
	vScrollBar(vsb),
	background(0)
{
	eventMask |= evBroadcast;
	delta.x = delta.y = limit.x = limit.y = 0;

	if(bkgdMaker != 0 && (background = bkgdMaker(getExtent())) != 0)
		{
		background->growMode = gfGrowHiX | gfGrowHiY;
		insert(background);
		}
}

void ScrollGroup::changeBounds(const TRect& bounds)
{
	lock();
	TGroup::changeBounds(bounds);
	setLimit(limit.x, limit.y);
	unlock();
	drawView();
}

static Boolean isView(TView* view, void* args)
{
	return Boolean(view == args);
}

void ScrollGroup::handleEvent(TEvent& event)
{
	TGroup::handleEvent(event);

	if(event.what == evBroadcast)
		{
		if(event.message.command == cmScrollBarChanged &&
				(event.message.infoPtr == hScrollBar ||
					event.message.infoPtr == vScrollBar))
			scrollDraw();
		else if(event.message.command == cmReceivedFocus &&
				firstThat(isView, event.message.infoPtr) != 0)
			focusSubView((TView*) event.message.infoPtr);
		}
}

struct ScrollInfo
{
	TPoint delta;
	TView* ignore;
};

static void doScroll(TView* view, void* args)
{
	ScrollInfo* info = (ScrollInfo*) args;

	if(view != info->ignore)
		{
		TPoint dest = view->origin + info->delta;
		view->moveTo(dest.x, dest.y);
		}
}

void ScrollGroup::scrollDraw()
{
	TPoint  d;

	d.x = hScrollBar ? hScrollBar->value : 0;
	d.y = vScrollBar ? vScrollBar->value : 0;

	if( d.x != delta.x || d.y != delta.y )
		{
		ScrollInfo info;

		info.delta = delta-d;
		info.ignore = background;
		lock();
		forEach(doScroll, &info);
		delta = d;
		unlock();
		drawView();
		}
}

void ScrollGroup::scrollTo(int x, int y)
{
	lock();
	if( hScrollBar != 0 && x != hScrollBar->value)
		hScrollBar->setValue(x);
	if( vScrollBar != 0 && y != vScrollBar->value)
		vScrollBar->setValue(y);
	unlock();
	scrollDraw();
}

void ScrollGroup::setLimit(int x, int y)
{
	limit.x = x;
	limit.y = y;
	lock();
	if(hScrollBar != 0)
		hScrollBar->setParams(hScrollBar->value, 0, x-size.x, size.x-1, 1);
	if(vScrollBar != 0 )
		vScrollBar->setParams(vScrollBar->value, 0, y-size.y, size.y-1, 1);
	unlock();
	scrollDraw();
}

void ScrollGroup::setState(ushort aState, Boolean enable)
{
	TGroup::setState(aState, enable);
	if((aState & (sfActive | sfSelected)) != 0 )
		{
		if(hScrollBar != 0)
			if(enable)
				hScrollBar->show();
			else
				hScrollBar->hide();
		if(vScrollBar != 0)
			if(enable)
				vScrollBar->show();
			else
				vScrollBar->hide();
		}
}

void ScrollGroup::focusSubView(TView* view)
{
	TRect rview = view->getBounds();

	TRect r = getExtent();
	r.intersect(rview);
	if(r != rview)
		{
		int dx, dy;

		dx = delta.x;
		if(view->origin.x < 0)
			dx = delta.x + view->origin.x;
		else if(view->origin.x + view->size.x > size.x)
			dx = delta.x+view->origin.x+view->size.x-size.x;

		dy = delta.y;
		if(view->origin.y < 0)
			dy = delta.y + view->origin.y;
		else if(view->origin.y + view->size.y > size.y)
			dy = delta.y+view->origin.y+view->size.y-size.y;

		scrollTo(dx, dy);
		}
}

TBackground* ScrollGroup::initBackground(TRect r)
{
	return new TBackground(r, ' ');
}