/*************************************************************************** * Copyright (C) 2004 by Christoph Freundl * * Christoph.Freundl@informatik.uni-erlangen.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "textarea.h" #include "fontfactory.h" namespace SDLwidgets { TextArea::TextArea( int width, int height ) { _width = width; _min_width = width; _height = height; _min_height = height; // default color for a text label is white _color.r = 255; _color.g = 255; _color.b = 255; _font = FontFactory::getInstance()->createFont( "tahoma.ttf", 12 ); } TextArea::~TextArea() { } void TextArea::setFont( std::string fontName, int fontSize ) { _font = FontFactory::getInstance()->createFont( fontName, fontSize ); /* renderText();*/ } void TextArea::draw( SDL_Surface* surface ) { SDL_Rect srcRect, destRect; Sint16 offset_x, offset_y; getOffset( offset_x, offset_y ); int maxHeight = TTF_FontHeight( _font ); if ( _line.size() * ( maxHeight + 1 ) > _height ) { destRect.x = offset_x; destRect.y = offset_y + _height - maxHeight; srcRect.x = 0; srcRect.y = 0; srcRect.w = _width; srcRect.h = maxHeight; for ( std::vector::reverse_iterator it = _line.rbegin(); it != _line.rend() && destRect.y >= offset_y; it++, destRect.y -= maxHeight + 1 ) { SDL_Surface* textSurface = TTF_RenderUTF8_Blended( _font, it->c_str(), _color ); SDL_BlitSurface( textSurface, &srcRect, surface, &destRect ); SDL_FreeSurface( textSurface ); } } else { destRect.x = offset_x; destRect.y = offset_y; srcRect.x = 0; srcRect.y = 0; srcRect.w = _width; srcRect.h = maxHeight; for ( std::vector::iterator it = _line.begin(); it != _line.end() && destRect.y >= offset_y; it++, destRect.y += maxHeight + 1 ) { SDL_Surface* textSurface = TTF_RenderUTF8_Blended( _font, it->c_str(), _color ); SDL_BlitSurface( textSurface, &srcRect, surface, &destRect ); SDL_FreeSurface( textSurface ); } } } void TextArea::appendText( std::string text ) { // wrap text in separate lines std::string line; int lineWidth = 0, lineHeight = 0; while ( text.length() > 0 ) { unsigned int pos = text.find( ' ', 1 ); if ( pos < text.length() ) { std::string word = text.substr( 0, pos ); text.erase( 0, pos ); int width, height; TTF_SizeUTF8( _font, word.c_str(), &width, &height ); if ( lineWidth + width > _width ) { appendLine( line ); line = word; while ( line[0] == ' ' ) line.erase( 0, 1 ); TTF_SizeUTF8( _font, line.c_str(), &lineWidth, &lineHeight ); } else { line += word; lineWidth += width; } } else { int width, height; TTF_SizeUTF8( _font, text.c_str(), &width, &height ); if ( lineWidth + width > _width ) { appendLine( line ); while ( text[0] == ' ' ) text.erase( 0, 1 ); appendLine( text ); text.erase(); } else { line += text; appendLine( line ); text.erase(); } } } } }