src/gui/msg_box.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  * Message Box
00020  *****************************************************************************/
00021 
00022 #include "../tool/rectangle.h"
00023 #include "widget.h"
00024 #include "msg_box.h"
00025 
00026 MsgBox::MsgBox(const Rectanglei& rect, Font* _font) :
00027    Widget(rect)
00028 {
00029   font = _font;
00030 }
00031 
00032 void MsgBox::Flush()
00033 {
00034   std::list<Text *>::iterator it ;
00035 
00036   uint y = 5;
00037   for (it = messages.begin(); it != messages.end(); it++) 
00038     {
00039       y += (*it)->GetHeight() + 5;
00040 
00041       while (int(y) > GetSizeY() && !messages.empty()) {
00042         Text* tmp = messages.front();
00043         y -= tmp->GetHeight() - 5; 
00044         delete tmp;
00045         messages.pop_front();
00046       }
00047     }
00048 }
00049 
00050 void MsgBox::NewMessage(const std::string &msg)
00051 {
00052   messages.push_back(new Text(msg));
00053   messages.back()->SetMaxWidth(GetSizeX());
00054 
00055   // Remove old messages if needed
00056   Flush();
00057 
00058   ForceRedraw();
00059 }
00060 
00061 void MsgBox::Draw(const Point2i &mousePosition, Surface& surf) const
00062 {
00063   // Draw the border
00064   surf.BoxColor(*this, defaultOptionColorBox);
00065   surf.RectangleColor(*this, defaultOptionColorRect,2);
00066 
00067   // Draw the messages
00068   std::list<Text*>::const_iterator it;
00069   int x = GetPositionX()+5;
00070   int y = GetPositionY()+5;
00071   for (it = messages.begin(); it != messages.end(); it++) {
00072     (*it)->DrawTopLeft(Point2i(x,y));
00073     y += (*it)->GetHeight() + 5;
00074   }
00075 }
00076 
00077 void MsgBox::SetSizePosition(const Rectanglei &rect)
00078 {
00079   StdSetSizePosition(rect);
00080 
00081   // render the messages with the correct width
00082   std::list<Text*>::iterator it;
00083   for (it = messages.begin(); it != messages.end(); it++) {
00084     (*it)->SetMaxWidth(GetSizeX());
00085   }
00086   
00087   // Remove old messages if needed
00088   Flush();
00089 }

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