/*************************************************************************** * 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. * ***************************************************************************/ #ifndef SDLWIDGETS_TEXTFIELD_H #define SDLWIDGETS_TEXTFIELD_H #include #include #include #include "widget.h" #include "action.h" #include "focusable.h" #include "rootcontainer.h" #include "backgroundwidget.h" namespace SDLwidgets { class TextField : public WidgetLeaf, public ActionWidget, public Focusable { public: enum Action { EDITING_FINISHED, EDITING_CANCELLED }; private: TTF_Font* _font; SDL_Surface* _rendered_text; std::string _text; int _width, _height; BackgroundWidget* _background; unsigned int _cursor_position; Uint16 _cursor_x; bool _has_focus; SDL_mutex* m_mutex; public: TextField( int width, int height ); TextField( std::string font_name, int font_size, std::string text ); TextField( std::string text, int width = -1, int height = -1 ); virtual ~TextField(); void setFont( std::string fontName, int fontSize ); void setText( std::string text ) { _text = text; renderText(); _cursor_position = _text.length(); updateCursorPosition(); } std::string& getText() { return _text; } void clear() { _text.erase(); renderText(); _cursor_position = 0; updateCursorPosition(); } virtual void setWidth( const Uint16 width ) { _background->setWidth( width ); Widget::setWidth( width ); } virtual int getWidth() const { return _width; } virtual void setHeight( const Uint16 height ) { _background->setHeight( height ); Widget::setHeight( height ); } virtual int getHeight() const { return _height; } void setColor( const SDL_Color& color ); virtual void draw( SDL_Surface* surface ); virtual bool handleEvent( SDL_Event* event ); virtual void focusGained(); virtual void focusLost(); private: void renderText(); void updateCursorPosition(); }; } #endif // SDLWIDGETS_TEXTFIELD_H