2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 11:19:39 +01:00
hlsdk-xash3d/game_shared/vgui_listbox.cpp

209 lines
3.8 KiB
C++
Raw Normal View History

//========= Copyright (c) 1996-2002, Valve LLC, All rights reserved. ============
2016-06-04 15:24:23 +02:00
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================
#include "vgui_listbox.h"
using namespace vgui;
CListBox::CListBox() : Panel( 0, 0, 0, 0 ),
m_ItemsPanel( 0, 0, 0, 0 ),
m_ScrollBar( 0, 0, 0, 0, true ),
m_Slider( 0, 0, 10, 40, true )
2016-06-04 15:24:23 +02:00
{
m_Signal.m_pListBox = this;
m_ItemsPanel.setParent( this );
m_ItemsPanel.setBgColor( 0, 0, 0, 255 );
2016-06-04 15:24:23 +02:00
m_Slider.setRangeWindow( 50 );
m_Slider.setRangeWindowEnabled( true );
m_ScrollBar.setParent( this );
m_ScrollBar.addIntChangeSignal( &m_Signal );
m_ScrollBar.setSlider( &m_Slider );
m_ScrollBar.setButtonPressedScrollValue( 1 );
2016-06-04 15:24:23 +02:00
m_Items.m_pNext = m_Items.m_pPrev = &m_Items;
m_ItemOffset = 0;
m_iScrollMax = -1;
}
CListBox::~CListBox()
{
Term();
}
void CListBox::Init()
{
Term();
}
void CListBox::Term()
{
m_ItemOffset = 0;
// Free the LBItems.
LBItem *pNext;
for( LBItem *pItem =m_Items.m_pNext; pItem != &m_Items; pItem = pNext )
2016-06-04 15:24:23 +02:00
{
pItem->m_pPanel->setParent( NULL ); // detach the panel from us
2016-06-04 15:24:23 +02:00
pNext = pItem->m_pNext;
delete pItem;
}
m_Items.m_pPrev = m_Items.m_pNext = &m_Items;
}
void CListBox::AddItem( Panel *panel )
2016-06-04 15:24:23 +02:00
{
// Add the item.
LBItem *pItem = new LBItem;
if( !pItem )
2016-06-04 15:24:23 +02:00
return;
pItem->m_pPanel = panel;
pItem->m_pPanel->setParent( &m_ItemsPanel );
2016-06-04 15:24:23 +02:00
pItem->m_pPrev = m_Items.m_pPrev;
pItem->m_pNext = &m_Items;
pItem->m_pNext->m_pPrev = pItem->m_pPrev->m_pNext = pItem;
m_ScrollBar.setRange( 0, GetScrollMax() );
m_Slider.setRangeWindow( 50 );
m_Slider.setRangeWindowEnabled( true );
2016-06-04 15:24:23 +02:00
InternalLayout();
}
int CListBox::GetNumItems()
{
int count = 0;
for( LBItem *pItem = m_Items.m_pNext; pItem != &m_Items; pItem = pItem->m_pNext )
2016-06-04 15:24:23 +02:00
++count;
return count;
}
int CListBox::GetItemWidth()
{
int wide, tall;
m_ItemsPanel.getSize( wide, tall );
2016-06-04 15:24:23 +02:00
return wide;
}
int CListBox::GetScrollPos()
{
return m_ItemOffset;
}
void CListBox::SetScrollPos( int pos )
2016-06-04 15:24:23 +02:00
{
int maxItems = GetScrollMax();
if( maxItems < 0 )
2016-06-04 15:24:23 +02:00
return;
m_ItemOffset = ( pos < 0 ) ? 0 : ( ( pos > maxItems ) ? maxItems : pos );
2016-06-04 15:24:23 +02:00
InternalLayout();
}
void CListBox::setPos( int x, int y )
2016-06-04 15:24:23 +02:00
{
Panel::setPos( x, y );
2016-06-04 15:24:23 +02:00
InternalLayout();
}
void CListBox::setSize( int wide, int tall )
2016-06-04 15:24:23 +02:00
{
Panel::setSize( wide, tall );
2016-06-04 15:24:23 +02:00
InternalLayout();
}
void CListBox::setPixelScroll( int value )
2016-06-04 15:24:23 +02:00
{
m_ItemOffset = m_ScrollBar.getValue();
InternalLayout();
}
void CListBox::InternalLayout()
{
int x, y, wide, tall;
getBounds( x, y, wide, tall );
2016-06-04 15:24:23 +02:00
// Reposition the main panel and the scrollbar.
m_ItemsPanel.setBounds( 0, 0, wide - 15, tall );
m_ScrollBar.setBounds( wide - 15, 0, 15, tall );
2016-06-04 15:24:23 +02:00
bool bNeedScrollbar = false;
// Reposition the items.
int curItem = 0;
int curY = 0;
int maxItem = GetScrollMax();
for( LBItem *pItem = m_Items.m_pNext; pItem != &m_Items; pItem = pItem->m_pNext )
2016-06-04 15:24:23 +02:00
{
if( curItem < m_ItemOffset )
2016-06-04 15:24:23 +02:00
{
pItem->m_pPanel->setVisible( false );
2016-06-04 15:24:23 +02:00
bNeedScrollbar = true;
}
else if( curItem >= maxItem )
2016-06-04 15:24:23 +02:00
{
// item is past the end of the items we care about
pItem->m_pPanel->setVisible( false );
2016-06-04 15:24:23 +02:00
}
else
{
pItem->m_pPanel->setVisible( true );
2016-06-04 15:24:23 +02:00
int itemWidth, itemHeight;
pItem->m_pPanel->getSize( itemWidth, itemHeight );
2016-06-04 15:24:23 +02:00
// Don't change the item's height but change its width to fit the listbox.
pItem->m_pPanel->setBounds( 0, curY, wide, itemHeight );
2016-06-04 15:24:23 +02:00
curY += itemHeight;
if( curY > tall )
2016-06-04 15:24:23 +02:00
{
bNeedScrollbar = true;
}
}
++curItem;
}
m_ScrollBar.setVisible( bNeedScrollbar );
2016-06-04 15:24:23 +02:00
repaint();
}
void CListBox::paintBackground()
{
}
void CListBox::SetScrollRange( int maxScroll )
2016-06-04 15:24:23 +02:00
{
m_iScrollMax = maxScroll;
m_ScrollBar.setRange( 0, maxScroll );
2016-06-04 15:24:23 +02:00
InternalLayout();
}
int CListBox::GetScrollMax()
2016-06-04 15:24:23 +02:00
{
if( m_iScrollMax < 0 )
2016-06-04 15:24:23 +02:00
{
return GetNumItems() - 1;
}
return m_iScrollMax;
}