/*************************************************************************** * 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 "textlabel.h" #include "fontfactory.h" #include namespace SDLwidgets { TextLabel::TextLabel( std::string font_name, int font_size, std::string text ) : _rendered_text( 0 ), _text( text ) { // default color for a text label is white _color.r = 255; _color.g = 255; _color.b = 255; _font = FontFactory::getInstance()->createFont( font_name, font_size ); renderText(); if ( _rendered_text ) { _min_width = _rendered_text->w; _min_height = _rendered_text->h; } else { _min_width = 0; _min_height = 0; } } TextLabel::TextLabel( std::string text, int font_size ) : _rendered_text( 0 ), _text( text ) { // default color for a text label is white _color.r = 255; _color.g = 255; _color.b = 255; _font = FontFactory::getInstance()->createFont( "tahoma.ttf", font_size ); renderText(); if ( _rendered_text ) { _min_width = _rendered_text->w; _min_height = _rendered_text->h; } else { _min_width = 0; _min_height = 0; } } TextLabel::~TextLabel() { if ( _rendered_text ) SDL_FreeSurface( _rendered_text ); } void TextLabel::draw( SDL_Surface* surface ) { SDL_Rect destRect; getOffset( destRect.x, destRect.y ); SDL_BlitSurface( _rendered_text, 0, surface, &destRect ); } void TextLabel::drawOffset( SDL_Surface* surface, int x_offset, int y_offset ) { SDL_Rect destRect; destRect.x = _pos_x + x_offset; destRect.y = _pos_y + y_offset; SDL_BlitSurface( _rendered_text, 0, surface, &destRect ); } void TextLabel::setColor( const SDL_Color& color ) { WidgetLeaf::setColor( color ); renderText(); } void TextLabel::renderText() { if ( _rendered_text ) SDL_FreeSurface( _rendered_text ); // _rendered_text = TTF_RenderText_Solid( _font, _text.c_str(), _color ); _rendered_text = TTF_RenderUTF8_Blended( _font, _text.c_str(), _color ); } void TextLabel::setText( std::string text ) { _text = text; renderText(); if ( !_rendered_text ) setWidth( 0 ); else setWidth( _rendered_text->w ); } bool TextLabel::handleEvent( SDL_Event* event ) { if ( event->type == SDL_MOUSEBUTTONDOWN ) { if ( event->button.button == SDL_BUTTON_LEFT ) { if ( containsPoint( event->button.x, event->button.y ) ) { fireAction( CLICKED, this ); } } } return false; } }