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 * Widget list : store all widgets displayed on one screen 00020 * It is a fake widget. 00021 *****************************************************************************/ 00022 #include "../include/app.h" 00023 #include "../graphic/colors.h" 00024 #include "widget_list.h" 00025 #include "widget.h" 00026 #include <iostream> 00027 00028 WidgetList::WidgetList() 00029 { 00030 last_clicked = NULL; 00031 } 00032 00033 WidgetList::WidgetList(const Rectanglei &rect) : Widget(rect) 00034 { 00035 last_clicked = NULL; 00036 } 00037 00038 WidgetList::~WidgetList() 00039 { 00040 for(std::list<Widget*>::iterator w=widget_list.begin(); 00041 w != widget_list.end(); 00042 w++) 00043 delete *w; 00044 00045 widget_list.clear(); 00046 } 00047 00048 void WidgetList::DelFirstWidget() 00049 { 00050 delete widget_list.front(); 00051 widget_list.pop_front(); 00052 } 00053 00054 void WidgetList::AddWidget(Widget* w) 00055 { 00056 assert(w!=NULL); 00057 widget_list.push_back(w); 00058 w->SetContainer(this); 00059 } 00060 00061 void WidgetList::Update(const Point2i &mousePosition, Surface& surf) 00062 { 00063 for(std::list<Widget*>::iterator w=widget_list.begin(); 00064 w != widget_list.end(); 00065 w++) 00066 { 00067 // Then redraw the widget 00068 (*w)->Update(mousePosition, lastMousePosition, surf); 00069 } 00070 00071 lastMousePosition = mousePosition; 00072 } 00073 00074 void WidgetList::Draw(const Point2i &mousePosition, Surface& surf) const 00075 { 00076 } 00077 00078 void WidgetList::Redraw(const Rectanglei& rect, Surface& surf) 00079 { 00080 // Redraw bottom layer 00081 if (ct != NULL) { 00082 ct->Redraw(rect, surf); 00083 } 00084 } 00085 00086 void WidgetList::SendKey(SDL_keysym key) 00087 { 00088 if(last_clicked != NULL) 00089 last_clicked -> SendKey(key); 00090 } 00091 00092 Widget* WidgetList::Clic(const Point2i &mousePosition, uint button) 00093 { 00094 for(std::list<Widget*>::iterator w=widget_list.begin(); 00095 w != widget_list.end(); 00096 w++) 00097 { 00098 if((*w)->Contains(mousePosition)) 00099 { 00100 Widget* child = (*w)->Clic(mousePosition,button); 00101 if(child != NULL) 00102 { 00103 SetFocusOn(child); 00104 return child; 00105 } 00106 } 00107 } 00108 return NULL; 00109 } 00110 00111 void WidgetList::ForceRedraw() 00112 { 00113 need_redrawing = true; 00114 00115 for(std::list<Widget*>::iterator w=widget_list.begin(); 00116 w != widget_list.end(); 00117 w++) 00118 { 00119 (*w)->ForceRedraw(); 00120 } 00121 } 00122 00123 void WidgetList::SetFocusOn(Widget* w) 00124 { 00125 if(last_clicked != NULL) { 00126 last_clicked->have_focus = false; 00127 last_clicked->ForceRedraw(); 00128 } 00129 00130 if (w != NULL) { 00131 last_clicked = w ; 00132 last_clicked->have_focus = true; 00133 last_clicked->ForceRedraw(); 00134 } 00135 }