Back to index

extremetuxracer  0.5beta
entry.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (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 
00020 #include "entry.h"
00021 
00022 #include "ui_theme.h"
00023 #include "render_util.h"
00024 #include "alg/glhelper.h"
00025 #include "alg/defs.h"
00026 
00027 namespace pp {
00028               
00029 Entry::Entry( pp::Vec2d pos, pp::Vec2d size,
00030                       const char *binding, const char *content )
00031  : mp_font(NULL)
00032 {
00033        m_position = pos;
00034        m_size = size;
00035        m_content = content;
00036        
00037        mp_font = pp::Font::get(binding);
00038        
00039        m_editable=true;
00040        m_maxchars=8;
00041        
00042        setSensitive(true);
00043 }
00044        
00045 void
00046 Entry::setMaxChars(unsigned int maxchars)
00047 {
00048        m_maxchars = MAX(1,maxchars);      
00049 }
00050 
00051 
00052 void
00053 Entry::draw()
00054 {
00055        if(m_hasFocus){
00056               ppGL::draw::rect(theme.highlight,
00057                             m_position,
00058                             m_position+m_size);  
00059        }else{ 
00060               ppGL::draw::rect(theme.foreground,
00061                             m_position,
00062                             m_position+m_size);
00063        }
00064        ppGL::draw::rect(theme.background,
00065                             m_position+theme.border,
00066                             m_position+m_size-theme.border);
00067        
00068        mp_font->draw(m_content.c_str(), m_position+theme.textPadding);
00069 }
00070 
00071 void
00072 Entry::performClickAction()
00073 {
00074        setFocus(true);
00075 }
00076 
00077 void
00078 Entry::setContent(std::string string)
00079 {
00080        m_content = string;
00081 }
00082 
00083 std::string&
00084 Entry::getContent()
00085 {
00086        return m_content;
00087 }
00088 
00089 
00090 
00091 bool
00092 Entry::keyboardEvent(SDLKey key, SDLMod mod, bool release)
00093 {
00094        if(!release){
00095               if(m_editable){
00096                      if( islower(key) ){
00097                             if(m_content.size()<m_maxchars){
00098                                    if(mod & KMOD_SHIFT){
00099                                           m_content+=toupper(key);
00100                                    }else{
00101                                           m_content+=key;
00102                                    }
00103                             }
00104                      }else if( isdigit(key) ){
00105                             if(m_content.size()<m_maxchars){
00106                                    m_content+=key;
00107                             }
00108                      }else if( key == SDLK_BACKSPACE){
00109                             if(!m_content.empty()){
00110                                    std::string::iterator it = m_content.end();
00111                                    it--;
00112                                    m_content.erase(it); 
00113                                    //i'm stupid... or g++-2.95?
00114                                    //content.erase( --m_content.end() );
00115                             }
00116                      }
00117               }else{
00118                      signalKeyPressed.Emit(this,key);
00119               }
00120        }
00121        return true;
00122 }
00123 
00124 } //namespace pp