src/gui/list_box_w_label.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * A widget integrating a ListBox with a bottom Label
00020  *****************************************************************************/
00021 
00022 #include "list_box_w_label.h"
00023 #include <algorithm>
00024 #include <SDL_gfxPrimitives.h>
00025 #include "../graphic/font.h"
00026 #include "../include/app.h"
00027 #include "../tool/math_tools.h"
00028 #include "../tool/resource_manager.h"
00029 
00030 //#define SCROLLBAR
00031 
00032 ListBoxWithLabel::ListBoxWithLabel (const std::string &label, const Rectanglei &rect) : ListBox(rect)
00033 {  
00034   txt_label = new Text(label, dark_gray_color, Font::GetInstance(Font::FONT_NORMAL, Font::BOLD), false);
00035   SetSizePosition(rect);
00036   txt_label->SetMaxWidth(GetSizeX());
00037 }
00038 
00039 ListBoxWithLabel::~ListBoxWithLabel()
00040 {
00041    delete txt_label;
00042 }
00043 
00044 void ListBoxWithLabel::Draw(const Point2i &mousePosition, Surface& surf) const
00045 {
00046   int item = MouseIsOnWhichItem(mousePosition);
00047 
00048   Rectanglei rect (GetPositionX(),GetPositionY(),GetSizeX(),
00049                    GetSizeY()- 2 - txt_label->GetHeight());
00050 
00051   surf.BoxColor(rect, defaultListColor1);
00052   surf.RectangleColor(rect, white_color);
00053 
00054   for(uint i=0; i < nb_visible_items; i++){
00055     Rectanglei rect(GetPositionX() + 1, 
00056                     GetPositionY() + i * height_item + 1, 
00057                     GetSizeX() - 2, 
00058                     height_item - 2);
00059     
00060     if( int(i + first_visible_item) == selected_item) {
00061        surf.BoxColor(rect, defaultListColor2);
00062     }
00063     else if( i + first_visible_item == uint(item) ) {
00064       surf.BoxColor(rect, defaultListColor3);
00065     }
00066      
00067     (*Font::GetInstance(Font::FONT_SMALL)).WriteLeft( 
00068                                                      GetPosition() + Point2i(5, i*height_item),
00069                                                      m_items[i + first_visible_item]->GetLabel(),
00070                                                      white_color);
00071     if(!m_items[i]->IsEnabled())
00072       surf.BoxColor(rect, defaultDisabledColorBox);
00073   }  
00074 
00075   // Draw the label
00076   txt_label->DrawTopLeft( GetPositionX(), GetPositionY() + GetSizeY() - txt_label->GetHeight() );
00077 
00078   // buttons for listbox with more items than visible
00079   if (m_items.size() > nb_visible_items_max){
00080     m_up->Draw(mousePosition, surf);
00081     m_down->Draw(mousePosition, surf);
00082 #ifdef SCROLLBAR
00083     uint tmp_y, tmp_h;
00084     tmp_y = y+10+ first_visible_item* (h-20) / m_items.size();
00085     tmp_h = nb_visible_items_max * (h-20) / m_items.size();
00086     if (tmp_h < 5) tmp_h =5;
00087 
00088     boxRGBA(surf, 
00089             x+w-10, tmp_y,
00090             x+w-1,  tmp_y+tmp_h,
00091             white_color);
00092 #endif
00093   }
00094 }
00095 
00096 void ListBoxWithLabel::SetSizePosition(const Rectanglei &rect)
00097 {
00098   StdSetSizePosition(rect);
00099   txt_label->SetMaxWidth(GetSizeX());
00100 
00101   m_up->SetSizePosition( Rectanglei(GetPositionX() + GetSizeX() - m_up->GetSizeX() - 2, 
00102                                     GetPositionY()+2, 
00103                                     m_up->GetSizeX(), m_up->GetSizeY()) );
00104 
00105   m_down->SetSizePosition( Rectanglei(GetPositionX() + GetSizeX() - m_down->GetSizeX() - 2, 
00106                                       GetPositionY() + GetSizeY() - m_down->GetSizeY() - 2 -
00107                                       txt_label->GetHeight() - 2,
00108                                       m_down->GetSizeX(), 
00109                                       m_down->GetSizeY()) );  
00110 
00111   nb_visible_items_max = GetSizeY()/height_item;
00112 }

Generated on Mon Jan 1 13:10:57 2007 for Wormux by  doxygen 1.4.7