Calculating vertical position of scroll bar for a list box

Hello,

im trying to code a simple list box for my self made GUI but i seem to be stuck at the scroll bar. I already finished with the calculation for the scroll bar size but i have no idea how i could calculate it's current vertical position depending on the current item on top of the list box.
My current code looks like this

ListBox struct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct MenuListBox {
	int PosX;
	int PosY;
	int Width;
	int ItemsVisible; //Items visible at the same time
	int CurrentItem;
	int CurrentTopItem;
	int CurrentScrollBarPosY; //Vertical position of the scroll bar (0 = directly under the scroll up button
	int CurrentScrollBarHeight; //Size of the scroll bar
	int index;
	wchar_t* ItemArray[10000]; //Array of list box items
	int TabToDrawOn; //Needed for the TabControl
	MenuListBox* pNext;
};


Void to add a ListBox:

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
void menu::AddListBox ( int PosX, int PosY, int Width, int ItemsVisible, int CurrentItem, wchar_t* Items[10000], int TabToDrawOn)
{
	MenuListBox* Ptr = new MenuListBox;
	MenuListBox* New = new MenuListBox;

	New->PosX = PosX;
	New->PosY = PosY + 24;
	New->Width = Width;
	New->ItemsVisible = ItemsVisible;
	New->CurrentItem = CurrentItem;
	New->CurrentTopItem = 1;

	float ItemCountInternal = 0;
	for ( int i = 0; i < 10000; i++ )
	{
		if ( Items[i] != NULL )
		{
			ItemCountInternal++;
		}
		New->ItemArray[i] = Items[i];
	}
	float ItemsVisible2 = New->ItemsVisible;
	if ( ItemCountInternal <= New->ItemsVisible )
	{
		New->CurrentScrollBarHeight = (New->ItemsVisible - 2) * 16;
	}
	else
	{
		New->CurrentScrollBarHeight = ((ItemsVisible2 - 2) / (ItemCountInternal - 2)) * ((ItemsVisible2 - 2) * 16);
	}
	New->CurrentScrollBarPosY = (New->ItemsVisible - 2) * 16; //UNFINISHED
	New->TabToDrawOn = TabToDrawOn;
	New->index = ListBoxCount + 1;
	New->pNext = NULL;

	if(menu::ListBoxCount == NULL)
	{
		menu::MenuListBoxes = New;
	} else {
		Ptr = menu::MenuListBoxes;
		while(Ptr->pNext != NULL)
		{
			Ptr = Ptr->pNext;
		}
		Ptr->pNext = New;
	}
	this->ListBoxCount++;
}


Additional vars in the menu class:

1
2
MenuListBox* MenuListBoxes;
int ListBoxCount;


How could i calculate this:

New->CurrentScrollBarPosY = ; ?
Last edited on
I didn't look at your code.... but this is a pretty straightforward problem so I figure I'd just give you the idea and you can work it into your code your own way.

There are few things you need to know.

1) The maximum [logical, not graphical] scroll position. This would probably be itemsInList - itemsVisible;

2) The current [logical] scroll position

3) The Height (in pixels) of the trackbar. My terminology might be wrong... it's the thing you click and drag on.

4) The maximum (graphical) position to draw the trackbar. This will be:
heightOfAreaWhereTrackBarCanBeDrawn - heightOfTrackBar;


From here it's just a matter of scaling the logical range to the graphical range:

1
2
3
graphicalPos = curScroll * maxGraphicPos / maxLogicalPos;
   // ie:  #2 * #4 / #1
   //  #3 is only necessary for calculating #4 
viewport? Did I misunderstand the question? I thought he was just talking about drawing the trackbar?

EDIT: Also I don't know why your post got reported. That seems absurd.
Last edited on
@Disch
I agree it wasn't worthy of being reported, but I definitely understand why it was done. Lumpkin clearly stopped reading once he saw the word scroll.

Edit:Just to be clear, I didn't report the post.
Last edited on
Oh, the track bar? I thought he had problems with the screen moving accordingly to it.

What do you want it to look like?

EDIT: This - wchar_t* ItemArray[10000]; //Array of list box items Why allocate that much memory? Do this:

1
2
3
4
5
wchar_t** ItemArray;

//And when you get the size required, allocate enough memory.

ItemArray = new wchar_t[number];


EDIT2: Yea I actually did stop. I read the first sentence and glanced at the rest. Sorry.
Last edited on
Also I don't know why your post got reported. That seems absurd.


I reported it because Lumpkin/Fredbill30 has a history of responding to posts without actually reading them, supplying code that doesn't work and making inane suggestions. Since, he continues to do so, I don't see much point in tolerating him.
But I supplied him with a link. It wasn't insane.

EDIT: Again OP what will the dimensions of the scrollbar be? Whats the scale?
Last edited on
Lumpkin wrote:
But I supplied him with a link. It wasn't insane.

A link that has nothing to do with the subject he is talking about. He is talking about GUIs and you posted a link to screen scrolling for a shooter/RPG. I completely agree with cire on this one.

Lumpkin wrote:
Again OP what will the dimensions of the scrollbar be? Whats the scale?

I've not done GUIs for a while (not since Allegro 3.0.x WIP) and from what I remember the libraries should auto size the parts according to the size of the window you are placing it on. Depending on the library he is using of course as some may not automatically do that.
OP wrote:
im trying to code a simple list box for my self made GUI but i seem to be stuck at the scroll bar. I already finished with the calculation for the scroll bar size but i have no idea how i could calculate it's current vertical position depending on the current item on top of the list box.



So... yeah.... it seemed pretty straightforward to me what he was asking. He's drawing the scrollbar himself and just had a question about how to determine the placement of the trackbar.


Also @ Lumpkin: inane is not quite the same thing as insane. Dunno if that was a typo or not.
Last edited on
@Disch
I was responding to Limpkin's question. Though, I did misread it, somehow I read he was using an existing lib to make his own GUI lib. My advice to the OP would be just that though, look at open source GUI libs and see how they do it. I've never even considered making my own GUI lib due to all the ones out there, but it does sound like a fun challenge.
@BHXSpecter
This GUI is a bit different because it is not a windows GUI. I use a game engines drawing functions to draw the GUI inside of the games window with basic functions for drawing 2d lines, rects, text and all that.
I also already looked at other GUI source codes but they all do it somehow different or they use some functions of libs i don't have or cannot use.

I also thank @Lumpkin for the tip with the array since im only coding in c++ for a bit more than a month now so i don't have the best solution for everything.

Still, my main question in this thread is still not answered bcause i want an answer specific to my source code so please don't post something like "Look at other open source GUI libs" because i already did and they didn't gave me an answer to my problem.

Additional info: I always have to subtract - 2 from the values before multiplying it with 16 because every list box item has a height of 16 px as well as the buttons for scrolling up and down on the top and bottom of the scrollbar have a size of 16px so i always have to subtract 2 * 16 to give the scroll bar the correct size / y-position.
Last edited on
BHX, the process is exactly the same.

It's a viewport tutorial scroll bars modify a page's viewport. It's very much related.

ccman, Do you want it to autoscale or not?
@Lumpkin
I want the scrollbar to automatically have the correct size depending on the amount of items in the array AND the amount of items visible at the same time (items visible at same time = 10 means the listbox can display 10 items / is 10 items high). The items in the box are changing all the time so the scroll bar has to get autoscaled. As i already said it's important to remember that the buttons for scrolling up/down at the top and bottom right at the top and bottom of the scroll bar are also taking a height of 16 px each.

But as you may see i already have the code for calculating the size, my only problem is the vertical position. I need the calculations to calculate it's vertical position depending on the amount of items in the array and the current item on top of the list box AND the amount of items visible in the list box at the same time but i ALSO need the calculations to do that backwards->Calculate the current item on top of the list depending on the scroll bars vertical position.
Last edited on
Is there going to be a max number of items possible in the array?
I've never done anything like this, but my guess is

scroll_bar_height (pixels) /num_items_in_list = height_per_line (ammount of scrollbar to move for each line)

current_pos = current_item_index * height_per_line
Last edited on
@Lumpkin
It won't get higher than max. 1000 items but i made it max. 10000 items bcause i don't know what else i may gonna use the listbox for.
Last edited on
Topic archived. No new replies allowed.