Scrollable list in C++

I'm trying to create a scrollable list (array of strings) set in a window. I have four variables that are used to determine the placement of the viewable elements of the list in a window. These are:

lw.list.curr // current list element
lw.list.count // total number of elements in list
lw.top // first visible element in window
lw.bot // last visible element in window

lw.list.curr is the variable that controls the scrolling, so that it must be incremented or decremented before the following function is called to handle the windowed list.

i.e.:

lw.list.curr++;
lw = show_list(lw);

or ...

lw.list.curr--;
lw = show_list(lw);

Below is what I've got so far for the function show_list(). I can't figure out what's wrong, and I've tried all sorts of things but I always get a one-off error or a corrupted list view. 2 and a half days of struggling with this and I'm ready to rip my hair out. Please help!

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
t_list_win show_list (t_list_win lw)
{
	window(lw.x1, lw.y1, lw.x2, lw.y2);
	textcolor(BLACK);
	textbackground(WHITE);
	clrscr();

	// adjust for out-of-bounds
	if (lw.list.curr < 0) lw.list.curr = 0;
//	if (lw.list.curr > lw.list.count) lw.list.curr = lw.list.count;

	// initiate list
	if ((lw.list.curr == 0) && (lw.list.count > 0))
	{
		lw.list.curr = 1;
		lw.top = 1;
		if (lw.list.count < lw.y2-lw.y1+1) lw.bot = lw.list.count;
		else lw.bot = lw.y2-lw.y1+1;
	}

	// scroll up
	else if ((lw.list.curr < lw.top) && (lw.list.curr > 1))
	{
		lw.top--;
		lw.bot--;
	}

	// scroll down
	else if (lw.list.curr > lw.bot) //&& (lw.list.curr < lw.list.count))
	{
		lw.top++;
		lw.bot++;
	}

	// show list
	for (int i = lw.top; i <= lw.bot; i++)
	{
		gotoxy(1, i-lw.top+1);
		cprintf("%s", lw.list.array[i-1]);
	}

	// highlight current list item
	textcolor(WHITE);
	textbackground(BLUE);
	gotoxy(1, lw.list.curr-lw.top+1);
	cprintf("%s", lw.list.array[lw.list.curr-1]);
	clreol();
	return(lw);
}



Anthony
I should have added that I'm using Borland C++ v3.0 in a DOS box.

Topic archived. No new replies allowed.