Main Page | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals

Ars::XListBox Class Reference

#include <xlistbox.h>

Inheritance diagram for Ars::XListBox:

Inheritance graph
[legend]
Collaboration diagram for Ars::XListBox:

Collaboration graph
[legend]

Public Member Functions

 XListBox (void)
virtual ~XListBox (void)
 Standard destructor.

virtual void Init (icstring &aStr)
virtual const bool SetupWindow (const Message &msg)
 Deregister the setup_window message.

unsigned int GetItemHeight (void)
void SetItemHeight (const unsigned int iItemHeight)
 iItemHeight The height of the items in the listbox

const int AddItem (XListItem ListItem)
const int AddItem (const std::string &aText)
XListItemGetItem (const int iItemIndex)
void RemoveItem (const int iItemIndex)
void ClearItems (void)
 Remove all items from the list.

int const Size (void)
 The number of items in the list.

const bool IsSelected (const int iItemIndex)
void SetSelection (const int iItemIndex, const bool bSelected)
void SetAllSelections (const bool bSelected)
virtual void DrawBG (void)
virtual void Draw (void)
 Draws the button and renders the button label.

virtual void SetWindowRect (const XRect &WindowRect)
virtual const bool evMouseButtonDown (const MouseMessage &msg)
virtual const bool evMouseButtonUp (const MouseMessage &msg)
virtual const bool evValueChange (const CtrlMessage &msg)
virtual const bool evValueChanging (const CtrlMessage &msg)
virtual const bool evKeyDown (const KeyboardMessage &msg)

Protected Attributes

XScrollBarvScrollBar
 A pointer to the vertical scrollbar.

int itemHeight
 The height of the items in the list.

int focusedItem
 The currently focused item.

std::vector< XListItemlistItem
 The list of items.

std::vector< bool > selectedItem
 A vector of booleans indicating which items are selected.

bool m_bSingleSelection
 If true, only one item can be selected at a time.


Constructor & Destructor Documentation

Ars::XListBox::XListBox void   ) 
 

Constructs a new listbox

Parameters:
WindowRect A CRect that defines the outer limits of the control
aStr the rest of the resource to initialize the object

Definition at line 5 of file xlistbox.cpp.

References vScrollBar.

00006 : vScrollBar( 0 ) , focusedItem( 0 )
00007 {
00008         vScrollBar = new XScrollBar();
00009 }

Ars::XListBox::~XListBox void   )  [virtual]
 

Standard destructor.

Definition at line 12 of file xlistbox.cpp.

References vScrollBar.

00013 {
00014         if( vScrollBar )
00015                 delete vScrollBar;
00016         vScrollBar = 0;
00017 }


Member Function Documentation

const int Ars::XListBox::AddItem const std::string &  aText  ) 
 

Definition at line 56 of file xlistbox.cpp.

References AddItem(), Ars::Thing::GetFgColor(), and Ars::HPaint::GetFont().

00057 {
00058         return AddItem( XListItem( aText , 0 , GetFgColor() , GetFont() ) );
00059 }

const int Ars::XListBox::AddItem XListItem  ListItem  ) 
 

Adds a new item to the list

Parameters:
ListItem A XListItem structure with the data for the item
Returns:
The index of the added item

Definition at line 61 of file xlistbox.cpp.

References Ars::HPaint::GetFontSize(), Ars::XRect::Height(), listItem, selectedItem, Ars::Thing::SetDirty(), Ars::XScrollBar::SetLimits(), and vScrollBar.

Referenced by AddItem(), and Ars::XDropDown::AddItem().

00062 {
00063         listItem.push_back( ListItem );
00064         selectedItem.push_back( false );
00065         int iMax = (listItem.size() - 1) < 0 ? 0 : static_cast<int>(listItem.size() - 1);
00066 
00067         iMax -= (Height() / GetFontSize());
00068         vScrollBar->SetLimits(0, max( 0 , iMax ) );
00069         SetDirty( true );
00070         return static_cast<const int>(listItem.size());
00071 }

void Ars::XListBox::ClearItems void   ) 
 

Remove all items from the list.

Definition at line 84 of file xlistbox.cpp.

References listItem, selectedItem, Ars::Thing::SetDirty(), Ars::XScrollBar::SetLimits(), and vScrollBar.

Referenced by Ars::XDropDown::ClearItems().

00085 {
00086         listItem.clear();
00087         selectedItem.clear();
00088         vScrollBar->SetLimits(0, 0);
00089         SetDirty( true );
00090 }

void Ars::XListBox::Draw void   )  [virtual]
 

Draws the button and renders the button label.

Reimplemented from Ars::XBox.

Definition at line 109 of file xlistbox.cpp.

References Ars::XRect::Bottom(), Ars::XRect::ClipTo(), Ars::HPaint::DrawRect(), focusedItem, Ars::Thing::GetBgColor(), Ars::Window::GetClientRect(), Ars::Thing::GetFgColor(), Ars::XScrollBar::GetPosition(), Ars::XRect::Grow(), itemHeight, listItem, Ars::XRect::Overlaps(), selectedItem, Ars::XRect::SetBottom(), Ars::HPaint::SetFont(), Ars::HPaint::TextOutStr(), vScrollBar, and Ars::XRect::Width().

00110 {
00111         w -= 21;
00112         XBox::Draw();
00113         w += 21;
00114         XRect r( GetClientRect() );
00115         r.w -= 21;
00116         r.Grow( -1 );
00117 //      DrawRect( r , true , GetFgColor().FadeColor( 64 ) , GetBgColor() );
00118         int iStartIndex = vScrollBar->GetPosition();
00119         for( int i = iStartIndex; i < (int)listItem.size(); ++i )
00120         {
00121                 XRect ItemRect( r.x , r.y + (i - iStartIndex) * itemHeight, r.Width() , itemHeight );
00122                 if( ItemRect.Overlaps(r ) )
00123                 {
00124                         ItemRect.ClipTo( r );
00125                         ItemRect.SetBottom(ItemRect.Bottom() - 1);
00126                         if( selectedItem[i] )
00127                                 DrawRect( ItemRect, true, listItem[i].ItemColor, listItem[i].ItemColor );
00128                         if (i == focusedItem)
00129                         {
00130                                 ItemRect.Grow(1);
00131                                 DrawRect(ItemRect, false, GetFgColor().FadeColor( 64 ), listItem[i].ItemColor );
00132                                 ItemRect.Grow(-1);
00133                         }
00134                         ItemRect.Grow(-1);
00135                         FontEngine *prev = SetFont( listItem[i].fontEngine );
00136                         TextOutStr( ItemRect ,  listItem[i].itemText , selectedItem[i] ? GetBgColor() : listItem[i].ItemColor );
00137                         SetFont( prev );
00138                 }
00139         }
00140 }

void Ars::XListBox::DrawBG void   )  [virtual]
 

Render the background for the control Draws the background as a filled CRect

Reimplemented from Ars::Window.

Definition at line 102 of file xlistbox.cpp.

00103 {
00104         w -= 21;
00105         XBox::DrawBG();
00106         w += 21;
00107 }

const bool Ars::XListBox::evKeyDown const KeyboardMessage msg  )  [virtual]
 

Reimplemented from Ars::MessageClient.

Definition at line 230 of file xlistbox.cpp.

References Ars::Message::Destination(), focusedItem, Ars::Window::GetClientRect(), Ars::XScrollBar::GetPosition(), Ars::XRect::Height(), IsSelected(), itemHeight, Ars::KeyboardMessage::Key, listItem, Ars::Thing::SetDirty(), Ars::XScrollBar::SetPosition(), SetSelection(), Size(), and vScrollBar.

00231 {
00232         bool ans = XBox::evKeyDown( msg );
00233 
00234         if( !listItem.empty() && msg.Destination() == this )
00235         {
00236                 XRect ClientRect( GetClientRect() );
00237                 switch( msg.Key )
00238                 {
00239                         case SDLK_DOWN:
00240                                 if (focusedItem + 1 < Size())
00241                                 {
00242                                         focusedItem++;
00243                                         int diff = focusedItem - vScrollBar->GetPosition();
00244                                         if( itemHeight * (vScrollBar->GetPosition() + diff + 1) > (int) ClientRect.Height())
00245                                                 vScrollBar->SetPosition(vScrollBar->GetPosition() + 1);
00246                                         
00247                                         SetDirty();
00248                                         ans = true;
00249                                 }
00250                                 break;
00251                         case SDLK_UP:
00252                                 if ( focusedItem > 0 )
00253                                 {
00254                                         focusedItem--;
00255                                         if (focusedItem < vScrollBar->GetPosition())
00256                                                 vScrollBar->SetPosition(vScrollBar->GetPosition() - 1);
00257                                         SetDirty();
00258                                         ans = true;
00259                                 }
00260                                 break;
00261 
00262                         case SDLK_PAGEDOWN:
00263                         {
00264                                 int nSize = Size() - 1;
00265                                 int nItemsPerPage=ClientRect.Height() / itemHeight;
00266                                 if (focusedItem + nItemsPerPage < nSize)
00267                                         focusedItem += nItemsPerPage;
00268                                 else
00269                                         focusedItem = nSize;
00270                                 vScrollBar->SetPosition(focusedItem);
00271                                 SetDirty();
00272                                 ans = true;
00273                                 break;
00274                         }       
00275                         case SDLK_PAGEUP:
00276                         {
00277                                 int nItemsPerPage=ClientRect.Height() / itemHeight;
00278                                 if (focusedItem - nItemsPerPage > 0)
00279                                         focusedItem -= nItemsPerPage;
00280                                 else
00281                                         focusedItem = 0;
00282                                 vScrollBar->SetPosition(focusedItem);
00283                                 SetDirty();
00284                                 ans = true;
00285                                 break;
00286                         }
00287                         case SDLK_SPACE:
00288                                 SetSelection(focusedItem, !IsSelected(focusedItem));
00289                                 SetDirty();
00290                                 ans = true;
00291                                 break;
00292                         default:
00293                                 ans = false;
00294                                 break;
00295                 }
00296         }
00297         return ans;
00298 }

const bool Ars::XListBox::evMouseButtonDown const MouseMessage msg  )  [virtual]
 

Reimplemented from Ars::Window.

Definition at line 153 of file xlistbox.cpp.

References Ars::MouseMessage::Button, focusedItem, Ars::Window::GetClientRect(), Ars::XScrollBar::GetPosition(), Ars::XRect::HitTest(), itemHeight, listItem, Ars::MouseMessage::Point, Ars::XRect::RELPOS_INSIDE, Ars::Thing::SetDirty(), vScrollBar, and Ars::XPoint::y.

00154 {
00155         bool ans = XBox::evMouseButtonDown( msg );
00156         
00157         if( !listItem.empty() )
00158         {
00159                 XRect ClientRect( GetClientRect() );
00160                 if( msg.Button == MouseMessage::LEFT && ClientRect.HitTest( msg.Point ) == RELPOS_INSIDE )
00161                 {
00162                         if( ArsApplication::GetInstance()->GetKeyFocus() != this )
00163                                 ArsApplication::GetInstance()->SetKeyFocus( this );
00164 
00165                         // Prep the new selection
00166                         focusedItem = max( 0 , min( (int)listItem.size() - 1 , (msg.Point.y - ClientRect.y) / itemHeight + vScrollBar->GetPosition() ) );
00167 
00168                         SetDirty();
00169                         ans = true;
00170                 }
00171         }
00172         return ans;
00173 }

const bool Ars::XListBox::evMouseButtonUp const MouseMessage msg  )  [virtual]
 

Reimplemented from Ars::Window.

Definition at line 175 of file xlistbox.cpp.

References ArsSendMessage, Ars::MouseMessage::Button, focusedItem, Ars::Window::GetClientRect(), Ars::Window::GetParent(), Ars::XScrollBar::GetPosition(), Ars::XRect::HitTest(), IsSelected(), itemHeight, listItem, m_bSingleSelection, Ars::MouseMessage::Point, Ars::XRect::RELPOS_INSIDE, SetAllSelections(), Ars::Thing::SetDirty(), SetSelection(), vScrollBar, and Ars::XPoint::y.

00176 {
00177         bool ans = XBox::evMouseButtonUp( msg );
00178 
00179         if( !listItem.empty() )
00180         {
00181                 XRect ClientRect( GetClientRect() );
00182                 if( msg.Button == MouseMessage::LEFT && ClientRect.HitTest( msg.Point ) == RELPOS_INSIDE )
00183                 {
00184                         // Set the new selection
00185                         if( (int)focusedItem == (msg.Point.y - ClientRect.y) / itemHeight + vScrollBar->GetPosition())
00186                         {
00187                                 if( m_bSingleSelection )
00188                                         SetAllSelections( false );
00189                                 SetSelection( focusedItem , ! IsSelected(focusedItem) );
00190                                 ArsSendMessage( CtrlMessage( Message::CTRL_VALUECHANGE, GetParent(), this, focusedItem ) );
00191                                 SetDirty();
00192                         }
00193                         ans = true;
00194                 }
00195         }
00196 
00197         return ans;
00198 }

const bool Ars::XListBox::evValueChange const CtrlMessage msg  )  [virtual]
 

Reimplemented from Ars::MessageClient.

Definition at line 200 of file xlistbox.cpp.

References listItem, Ars::Thing::SetDirty(), Ars::CtrlMessage::source, and vScrollBar.

00201 {
00202         bool ans = XBox::evValueChange( msg );
00203 
00204         if( !listItem.empty() )
00205         {
00206                 if( msg.source == vScrollBar )
00207                 {
00208                         SetDirty();
00209                         ans = true;
00210                 }
00211         }
00212         return ans;
00213 }

const bool Ars::XListBox::evValueChanging const CtrlMessage msg  )  [virtual]
 

Reimplemented from Ars::MessageClient.

Definition at line 215 of file xlistbox.cpp.

References listItem, Ars::Thing::SetDirty(), Ars::CtrlMessage::source, and vScrollBar.

00216 {
00217         bool ans = XBox::evValueChanging( msg );
00218 
00219         if( !listItem.empty() )
00220         {
00221                 if( msg.source == vScrollBar )
00222                 {
00223                         SetDirty();
00224                         ans = true;
00225                 }
00226         }
00227         return ans;
00228 }

XListItem& Ars::XListBox::GetItem const int  iItemIndex  )  [inline]
 

Returns the desired item

Parameters:
iItemIndex The index of the item to check
Returns:
A reference to the XListItem struct

Definition at line 69 of file xlistbox.h.

References listItem.

Referenced by Ars::XDropDown::evValueChange(), and Ars::XDropDown::GetItem().

00069 { return listItem[iItemIndex]; }

unsigned int Ars::XListBox::GetItemHeight void   )  [inline]
 

Returns:
The height of the items in the listbox

Definition at line 53 of file xlistbox.h.

References itemHeight.

Referenced by Ars::XDropDown::GetItemHeight().

00053 { return itemHeight; }

void Ars::XListBox::Init icstring aStr  )  [virtual]
 

Initialize the thing object via a string the CAR identifiers used are: itemHeight: height of all items... (default -1 for the current font height) mselect: set to 1 if multi selectionis allowed - default single select (scrollbar <s-expression> ): description of the scroll bar

Reimplemented from Ars::Window.

Definition at line 19 of file xlistbox.cpp.

References F_MULTISELECT, F_NONE, Ars::Thing::GetBgColor(), Ars::Thing::GetFgColor(), Ars::Thing::GetFlags(), Ars::HPaint::GetFont(), Ars::HPaint::GetFontSize(), Ars::XRect::Height(), icstring, Ars::XScrollBar::Init(), itemHeight, m_bSingleSelection, Ars::XRect::Right(), S_SUNKEN, Ars::Thing::SetBgColor(), Ars::Thing::SetFgColor(), Ars::Thing::SetFlags(), Ars::HPaint::SetFont(), Ars::XScrollBar::SetLimits(), Ars::Thing::SetState(), Ars::XScrollBar::SetWindowRect(), vScrollBar, and Ars::XRect::XRect().

Referenced by Ars::XDropDown::Init().

00020 {
00021         XBox::Init( aStr );
00022 
00023         icstring tmp = Rsrc::FindCrdOf( aStr , "ItemHeight" );
00024         itemHeight = Rsrc::GetInt( tmp , GetFont()->GetFontSize() );
00025         
00026         m_bSingleSelection = (GetFlags() & F_MULTISELECT) == 0;
00027 
00028         vScrollBar->SetFgColor( GetFgColor() );
00029         vScrollBar->SetBgColor( GetBgColor() );
00030         vScrollBar->SetFont( GetFont() );
00031         vScrollBar->SetState( S_SUNKEN );
00032         vScrollBar->SetFlags( F_NONE );
00033         vScrollBar->SetLimits( 0 , 0 );
00034         tmp = Rsrc::FindCrdOf( aStr , "scrollbar" );
00035         vScrollBar->Init( tmp );
00036 
00037         vScrollBar->SetWindowRect( XRect( Right() - 20, y , 20 , Height() ) );
00038 }

const bool Ars::XListBox::IsSelected const int  iItemIndex  )  [inline]
 

Parameters:
iItemIndex The index of the item to check
Returns:
true if the item is selected

Definition at line 85 of file xlistbox.h.

References selectedItem.

Referenced by evKeyDown(), evMouseButtonUp(), and Ars::XDropDown::IsSelected().

00085 { return selectedItem[iItemIndex]; }

void Ars::XListBox::RemoveItem const int  iItemIndex  ) 
 

Remove an item from the list

Parameters:
iItemIndex The index of the item to remove

Definition at line 73 of file xlistbox.cpp.

References Ars::HPaint::GetFontSize(), Ars::XRect::Height(), listItem, selectedItem, Ars::Thing::SetDirty(), Ars::XScrollBar::SetLimits(), and vScrollBar.

Referenced by Ars::XDropDown::RemoveItem().

00074 {
00075         listItem.erase(listItem.begin() + iItemIndex);
00076         selectedItem.erase(selectedItem.begin() + iItemIndex);
00077         int iMax = (listItem.size() - 1) < 0 ? 0 : static_cast<int>(listItem.size() - 1);
00078         iMax -= (Height() / GetFontSize());
00079         vScrollBar->SetLimits(0, max( 0 , iMax ) );
00080         SetDirty( true );
00081 }

void Ars::XListBox::SetAllSelections const bool  bSelected  ) 
 

Set the selection for all of the items at once

Parameters:
bSelected Will select all items if true, or unselect if false

Definition at line 92 of file xlistbox.cpp.

References F_SELECTABLE, Ars::Thing::GetFlags(), listItem, selectedItem, and Ars::Thing::SetDirty().

Referenced by evMouseButtonUp(), Ars::XDropDown::evValueChange(), and Ars::XDropDown::SetAllSelections().

00093 {
00094         if( GetFlags() & F_SELECTABLE )
00095         {
00096                 for( unsigned int i = 0; i < listItem.size(); ++i )
00097                         selectedItem[i] = bSelected;
00098                 SetDirty( true );
00099         }
00100 }

void Ars::XListBox::SetItemHeight const unsigned int  iItemHeight  )  [inline]
 

iItemHeight The height of the items in the listbox

Definition at line 56 of file xlistbox.h.

References itemHeight, and Ars::Thing::SetDirty().

Referenced by Ars::XDropDown::SetItemHeight().

00056 {       itemHeight = iItemHeight;       SetDirty( true );       }

void Ars::XListBox::SetSelection const int  iItemIndex,
const bool  bSelected
[inline]
 

Set an item as selected

Parameters:
iItemIndex The index of the item to change
bSelected Will select the item if true, or unselect if false

Definition at line 90 of file xlistbox.h.

References F_SELECTABLE, Ars::Thing::GetFlags(), selectedItem, and Ars::Thing::SetDirty().

Referenced by evKeyDown(), evMouseButtonUp(), and Ars::XDropDown::SetSelection().

00091         {       if( GetFlags() & F_SELECTABLE )
00092                 {
00093                         selectedItem[iItemIndex] = bSelected;   
00094                         SetDirty( true );       
00095                 }
00096         }

const bool Ars::XListBox::SetupWindow const Message msg  )  [virtual]
 

Deregister the setup_window message.

Reimplemented from Ars::Window.

Definition at line 40 of file xlistbox.cpp.

References Ars::Window::SetParent(), Ars::XScrollBar::SetupWindow(), and vScrollBar.

Referenced by Ars::XDropDown::SetupWindow().

00041 {
00042         XBox::SetupWindow( msg );
00043 
00044         vScrollBar->SetParent( this );
00045 
00046         vScrollBar->SetupWindow( msg );
00047 
00048         MessageServer::Instance().RegisterMessageClient(this, Message::MOUSE_BUTTONDOWN);
00049         MessageServer::Instance().RegisterMessageClient(this, Message::MOUSE_BUTTONUP);
00050         MessageServer::Instance().RegisterMessageClient(this, Message::KEYBOARD_KEYDOWN);
00051         MessageServer::Instance().RegisterMessageClient(this, Message::CTRL_VALUECHANGE);
00052         MessageServer::Instance().RegisterMessageClient(this, Message::CTRL_VALUECHANGING);
00053         return true;
00054 }

void Ars::XListBox::SetWindowRect const XRect WindowRect  )  [virtual]
 

Giving a control a new WindowRect will move and resize the control

Parameters:
WindowRect A CRect that defines the outer limits of the control

Reimplemented from Ars::Window.

Definition at line 143 of file xlistbox.cpp.

References Ars::Window::clientRect, Ars::XRect::Grow(), Ars::XRect::Height(), Ars::XRect::Right(), Ars::XScrollBar::SetWindowRect(), vScrollBar, Ars::XRect::Width(), and Ars::XRect::XRect().

Referenced by Ars::XDropDown::Init().

00144 {
00145         XBox::SetWindowRect( WindowRect );
00146         
00147         XRect ScrollbarRect( *this );
00148         ScrollbarRect.Grow(-1);
00149         vScrollBar->SetWindowRect( XRect( ScrollbarRect.Right() - 12 , ScrollbarRect.y , ScrollbarRect.Width(), ScrollbarRect.Height() ) );
00150         clientRect = XRect( 2 , 2 , Width() - 16 , Height() - 4 );
00151 }

int const Ars::XListBox::Size void   )  [inline]
 

The number of items in the list.

Definition at line 79 of file xlistbox.h.

References listItem.

Referenced by evKeyDown(), and Ars::XDropDown::Size().

00079 { return static_cast<int>(listItem.size()); }


Field Documentation

int Ars::XListBox::focusedItem [protected]
 

The currently focused item.

Definition at line 26 of file xlistbox.h.

Referenced by Draw(), evKeyDown(), evMouseButtonDown(), and evMouseButtonUp().

int Ars::XListBox::itemHeight [protected]
 

The height of the items in the list.

Definition at line 25 of file xlistbox.h.

Referenced by Draw(), evKeyDown(), evMouseButtonDown(), evMouseButtonUp(), GetItemHeight(), Init(), and SetItemHeight().

std::vector<XListItem> Ars::XListBox::listItem [protected]
 

The list of items.

Definition at line 27 of file xlistbox.h.

Referenced by AddItem(), ClearItems(), Draw(), evKeyDown(), evMouseButtonDown(), evMouseButtonUp(), evValueChange(), evValueChanging(), GetItem(), RemoveItem(), SetAllSelections(), and Size().

bool Ars::XListBox::m_bSingleSelection [protected]
 

If true, only one item can be selected at a time.

Definition at line 29 of file xlistbox.h.

Referenced by evMouseButtonUp(), and Init().

std::vector<bool> Ars::XListBox::selectedItem [protected]
 

A vector of booleans indicating which items are selected.

Definition at line 28 of file xlistbox.h.

Referenced by AddItem(), ClearItems(), Draw(), IsSelected(), RemoveItem(), SetAllSelections(), and SetSelection().

XScrollBar* Ars::XListBox::vScrollBar [protected]
 

A pointer to the vertical scrollbar.

Definition at line 24 of file xlistbox.h.

Referenced by AddItem(), ClearItems(), Draw(), evKeyDown(), evMouseButtonDown(), evMouseButtonUp(), evValueChange(), evValueChanging(), Init(), RemoveItem(), SetupWindow(), SetWindowRect(), XListBox(), and ~XListBox().


The documentation for this class was generated from the following files:
Generated on Fri Dec 5 04:07:00 2003 for Borqueror by doxygen 1.3.3