#include <list_box.h>
Inheritance diagram for ListBox:
Public Member Functions | |
ListBox (const Rectanglei &rect, bool always_one_selected_b=true) | |
~ListBox () | |
void | Draw (const Point2i &mousePosition, Surface &surf) const |
Widget * | Clic (const Point2i &mousePosition, uint button) |
void | SetSizePosition (const Rectanglei &rect) |
void | AddItem (bool selected, const std::string &label, const std::string &value, bool enabled=true) |
void | Sort () |
int | MouseIsOnWhichItem (const Point2i &mousePosition) const |
void | Select (uint index) |
void | Select (const std::string &val) |
int | GetSelectedItem () const |
void | Deselect () |
void | RemoveSelected () |
const std::string & | ReadLabel () const |
const std::string & | ReadValue () const |
const std::string & | ReadValue (int index) const |
uint | Size () const |
Protected Attributes | |
uint | nb_visible_items |
uint | nb_visible_items_max |
uint | height_item |
uint | first_visible_item |
int | selected_item |
std::vector< ListBoxItem * > | m_items |
Button * | m_up |
Button * | m_down |
Private Attributes | |
bool | always_one_selected |
Definition at line 47 of file list_box.h.
ListBox::ListBox | ( | const Rectanglei & | rect, | |
bool | always_one_selected_b = true | |||
) |
Definition at line 65 of file list_box.cpp.
00065 : Widget(rect) 00066 { 00067 Rectanglei buttonRect; 00068 Profile *res = resource_manager.LoadXMLProfile( "graphism.xml", false); 00069 00070 buttonRect.SetPosition(GetPositionX() + GetSizeX() - 12, GetPositionY() + 2); 00071 buttonRect.SetSize(10, 5); 00072 m_up = new Button(buttonRect, res, "menu/up"); 00073 buttonRect.SetPosition(GetPositionX() + GetSizeX() - 12, GetPositionY() + GetSizeY() - 7); 00074 m_down = new Button(buttonRect, res, "menu/down"); 00075 00076 resource_manager.UnLoadXMLProfile( res); 00077 00078 height_item = (*Font::GetInstance(Font::FONT_SMALL)).GetHeight(); 00079 first_visible_item = 0; 00080 nb_visible_items_max = GetSizeY()/height_item; 00081 nb_visible_items = 0; 00082 00083 selected_item = -1; 00084 always_one_selected = always_one_selected_b; 00085 }
Here is the call graph for this function:
ListBox::~ListBox | ( | ) |
void ListBox::AddItem | ( | bool | selected, | |
const std::string & | label, | |||
const std::string & | value, | |||
bool | enabled = true | |||
) |
Definition at line 210 of file list_box.cpp.
00214 { 00215 uint pos = m_items.size(); 00216 00217 // Push item 00218 ListBoxItem * item = new ListBoxItem(label, *Font::GetInstance(Font::FONT_SMALL), value, enabled); 00219 m_items.push_back (item); 00220 00221 // Select it if selected 00222 if( selected ) 00223 Select (pos); 00224 00225 nb_visible_items = m_items.size(); 00226 if( nb_visible_items_max < nb_visible_items ) 00227 nb_visible_items = nb_visible_items_max; 00228 }
Here is the call graph for this function:
Here is the caller graph for this function:
Reimplemented from Widget.
Definition at line 104 of file list_box.cpp.
00105 { 00106 need_redrawing = true; 00107 00108 // buttons for listbox with more items than visible 00109 if( m_items.size() > nb_visible_items_max ){ 00110 if( (button == SDL_BUTTON_WHEELDOWN && Contains(mousePosition)) || 00111 (button == SDL_BUTTON_LEFT && m_down->Contains(mousePosition)) ){ 00112 00113 // bottom button 00114 if( m_items.size() - first_visible_item > nb_visible_items_max ) 00115 first_visible_item++ ; 00116 00117 return this; 00118 } 00119 else if( (button == SDL_BUTTON_WHEELUP && Contains(mousePosition)) || 00120 (button == SDL_BUTTON_LEFT && m_up->Contains(mousePosition)) ){ 00121 00122 // top button 00123 if( first_visible_item > 0 ) 00124 first_visible_item-- ; 00125 00126 return this; 00127 } 00128 } 00129 00130 if( button == SDL_BUTTON_LEFT ){ 00131 int item = MouseIsOnWhichItem(mousePosition); 00132 00133 if( item == -1 ) 00134 return NULL; 00135 00136 if( item == selected_item ){ 00137 //Deselect (); 00138 } else 00139 Select (item); 00140 return this; 00141 } 00142 else{ 00143 return NULL; 00144 } 00145 }
Here is the call graph for this function:
void ListBox::Deselect | ( | ) |
Definition at line 270 of file list_box.cpp.
00271 { 00272 assert (always_one_selected == false); 00273 selected_item = -1; 00274 }
Implements Widget.
Reimplemented in ListBoxWithLabel.
Definition at line 147 of file list_box.cpp.
00148 { 00149 int item = MouseIsOnWhichItem(mousePosition); 00150 Rectanglei rect (*this); 00151 00152 // Draw border and bg color 00153 surf.BoxColor(rect, defaultListColor1); 00154 surf.RectangleColor(rect, white_color); 00155 00156 // Draw items 00157 for(uint i=0; i < nb_visible_items; i++){ 00158 Rectanglei rect(GetPositionX() + 1, 00159 GetPositionY() + i * height_item + 1, 00160 GetSizeX() - 2, 00161 height_item - 2); 00162 00163 // item is selected or mouse-overed 00164 if( int(i + first_visible_item) == selected_item) { 00165 surf.BoxColor(rect, defaultListColor2); 00166 } else if( i + first_visible_item == uint(item) ) { 00167 surf.BoxColor(rect, defaultListColor3); 00168 } 00169 00170 // Really draw items 00171 Point2i pos = GetPosition() + Point2i(5, i*height_item); 00172 Rectanglei rect2(pos.x, pos.y, GetSizeX()-2, height_item-2); 00173 m_items[i + first_visible_item]->SetSizePosition(rect2); 00174 // m_items[i + first_visible_item].Draw(GetPosition() + Point2i(5, i*height_item), 00175 // mousePosition, surf); 00176 m_items[i + first_visible_item]->Draw(mousePosition, surf); 00177 00178 // item is disabled 00179 if(!m_items[i]->IsEnabled()) 00180 surf.BoxColor(rect, defaultDisabledColorBox); 00181 } 00182 00183 // buttons for listbox with more items than visible 00184 if (m_items.size() > nb_visible_items_max){ 00185 m_up->Draw(mousePosition, surf); 00186 m_down->Draw(mousePosition, surf); 00187 #ifdef SCROLLBAR 00188 uint tmp_y, tmp_h; 00189 tmp_y = y+10+ first_visible_item* (h-20) / m_items.size(); 00190 tmp_h = nb_visible_items_max * (h-20) / m_items.size(); 00191 if (tmp_h < 5) tmp_h =5; 00192 00193 boxRGBA(surf, 00194 x+w-10, tmp_y, 00195 x+w-1, tmp_y+tmp_h, 00196 white_color); 00197 #endif 00198 } 00199 }
Here is the call graph for this function:
int ListBox::GetSelectedItem | ( | ) | const |
Definition at line 276 of file list_box.cpp.
00277 { 00278 return selected_item; 00279 }
Here is the caller graph for this function:
int ListBox::MouseIsOnWhichItem | ( | const Point2i & | mousePosition | ) | const |
Definition at line 95 of file list_box.cpp.
00096 { 00097 if( !Contains(mousePosition) ) 00098 return -1; 00099 00100 int index = (mousePosition.y - position.y) / height_item; 00101 return BorneLong(index + first_visible_item, 0, m_items.size() - 1); 00102 }
Here is the call graph for this function:
Here is the caller graph for this function:
const std::string & ListBox::ReadLabel | ( | ) | const |
Definition at line 281 of file list_box.cpp.
00282 { 00283 assert (selected_item != -1); 00284 return m_items.at(selected_item)->GetLabel(); 00285 }
Here is the caller graph for this function:
const std::string & ListBox::ReadValue | ( | int | index | ) | const |
Definition at line 293 of file list_box.cpp.
00294 { 00295 assert (index != -1 && index < (int)m_items.size()); 00296 return m_items.at(index)->GetValue(); 00297 }
const std::string & ListBox::ReadValue | ( | ) | const |
Definition at line 287 of file list_box.cpp.
00288 { 00289 assert (selected_item != -1); 00290 return m_items.at(selected_item)->GetValue(); 00291 }
Here is the caller graph for this function:
void ListBox::RemoveSelected | ( | ) |
Definition at line 235 of file list_box.cpp.
00236 { 00237 assert (always_one_selected == false); 00238 00239 if( selected_item != -1 ){ 00240 m_items.erase( m_items.begin() + selected_item ); 00241 selected_item =- 1; 00242 } 00243 00244 nb_visible_items = m_items.size(); 00245 if( nb_visible_items_max < nb_visible_items ) 00246 nb_visible_items = nb_visible_items_max; 00247 }
Here is the caller graph for this function:
void ListBox::Select | ( | const std::string & | val | ) |
Definition at line 255 of file list_box.cpp.
00256 { 00257 uint index = 0; 00258 for(std::vector<ListBoxItem*>::iterator it=m_items.begin(); 00259 it != m_items.end(); 00260 it++,index++) 00261 { 00262 if((*it)->GetLabel() == val) 00263 { 00264 Select(index); 00265 return; 00266 } 00267 } 00268 }
Here is the call graph for this function:
void ListBox::Select | ( | uint | index | ) |
Definition at line 249 of file list_box.cpp.
00250 { 00251 assert(index < m_items.size()); 00252 selected_item = index; 00253 }
Here is the caller graph for this function:
void ListBox::SetSizePosition | ( | const Rectanglei & | rect | ) | [virtual] |
Implements Widget.
Reimplemented in ListBoxWithLabel.
Definition at line 201 of file list_box.cpp.
00202 { 00203 StdSetSizePosition(rect); 00204 m_up->SetSizePosition( Rectanglei(GetPositionX() + GetSizeX() - 12, GetPositionY()+2, 10, 5) ); 00205 m_down->SetSizePosition( Rectanglei(GetPositionX() + GetSizeX() - 12, GetPositionY() + GetSizeY() - 7, 10, 5) ); 00206 00207 nb_visible_items_max = GetSizeY()/height_item; 00208 }
Here is the call graph for this function:
uint ListBox::Size | ( | ) | const |
Definition at line 299 of file list_box.cpp.
00300 { 00301 return m_items.size(); 00302 }
Here is the caller graph for this function:
void ListBox::Sort | ( | ) |
bool ListBox::always_one_selected [private] |
Definition at line 50 of file list_box.h.
uint ListBox::first_visible_item [protected] |
Definition at line 58 of file list_box.h.
uint ListBox::height_item [protected] |
Definition at line 55 of file list_box.h.
Button * ListBox::m_down [protected] |
Definition at line 63 of file list_box.h.
std::vector<ListBoxItem*> ListBox::m_items [protected] |
Definition at line 60 of file list_box.h.
Button* ListBox::m_up [protected] |
Definition at line 63 of file list_box.h.
uint ListBox::nb_visible_items [protected] |
Definition at line 54 of file list_box.h.
uint ListBox::nb_visible_items_max [protected] |
Definition at line 54 of file list_box.h.
int ListBox::selected_item [protected] |
Definition at line 59 of file list_box.h.