commandline.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2008 by the FIFE team                              *
00003  *   http://www.fifengine.de                                               *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 // Standard C++ library includes
00023 #include <cassert>
00024 
00025 // 3rd party library includes
00026 #include <boost/bind.hpp>
00027 
00028 // FIFE includes
00029 // These includes are split up in two parts, separated by one empty line
00030 // First block: files included from the FIFE root src directory
00031 // Second block: files included from the same folder
00032 #include "util/time/timeevent.h"
00033 #include "util/time/timemanager.h"
00034 
00035 #include "commandline.h"
00036 
00037 namespace FIFE {
00038     using namespace gcn;
00039 
00040 
00041     CommandLine::CommandLine() : gcn::UTF8TextField(), m_history_position(0) {
00042 
00043         m_blinkTimer.setInterval(500);
00044         m_blinkTimer.setCallback(boost::bind(&CommandLine::toggleCaretVisible,this));
00045         m_blinkTimer.start();
00046 
00047         m_suppressBlinkTimer.setInterval(2000);
00048         m_suppressBlinkTimer
00049             .setCallback(boost::bind(&CommandLine::startBlinking,this));
00050     }
00051 
00052     CommandLine::~CommandLine() {
00053     }
00054 
00055     void CommandLine::toggleCaretVisible() {
00056         m_caretVisible = !m_caretVisible;
00057     }
00058 
00059     void CommandLine::stopBlinking() {
00060         m_suppressBlinkTimer.start();
00061         m_blinkTimer.stop();
00062         m_caretVisible = true;
00063     }
00064 
00065     void CommandLine::startBlinking() {
00066         m_suppressBlinkTimer.stop();
00067         m_blinkTimer.start();
00068     }
00069 
00070     void CommandLine::keyPressed(gcn::KeyEvent &keyEvent) {
00071         gcn::Key key = keyEvent.getKey();
00072         int keyType = key.getValue();
00073         
00074         if (keyType == Key::LEFT && mCaretPosition > 0)
00075         {
00076             UTF8TextField::keyPressed(keyEvent);
00077         }
00078         else if (keyType == Key::RIGHT && mCaretPosition < mText.size())
00079         {
00080             UTF8TextField::keyPressed(keyEvent);
00081         }
00082         else if (keyType == Key::DOWN && !m_history.empty())
00083         {
00084             if( m_history_position < m_history.size() ) {
00085                 
00086                 if( ++m_history_position == m_history.size() ) {
00087                     setText( m_cmdline ); 
00088                 } else {
00089                     setText( m_history[m_history_position] ); 
00090                 }
00091             };
00092         }
00093         else if (keyType == Key::UP && !m_history.empty())
00094         {
00095             if( m_history_position > 0 ) {
00096                 if( m_history_position == m_history.size() ) {
00097                     m_cmdline = mText; 
00098                 }
00099                 --m_history_position;
00100                 setText( m_history[m_history_position] ); 
00101             };
00102         }
00103         else if (keyType == Key::DELETE && mCaretPosition < mText.size())
00104         {
00105             UTF8TextField::keyPressed(keyEvent);
00106         }
00107         else if (keyType  == Key::BACKSPACE && mCaretPosition > 0)
00108         {
00109             UTF8TextField::keyPressed(keyEvent);
00110         }
00111         else if (keyType == Key::ENTER)
00112         {
00113             if( mText != "" ) {
00114                 if(m_callback) {
00115                     m_callback( mText );
00116                 }
00117                 m_history.push_back( mText ); 
00118                 m_history_position = m_history.size();
00119                 setText("");
00120             }
00121         }
00122         else if (keyType == Key::HOME)
00123         {
00124             mCaretPosition = 0;
00125         }    
00126         else if (keyType == Key::END)
00127         {
00128             mCaretPosition = mText.size();
00129         }    
00130         else if (key.isCharacter())
00131         {
00132             UTF8TextField::keyPressed(keyEvent);
00133         }
00134         stopBlinking();
00135         fixScroll();
00136     }
00137 
00138     void CommandLine::drawCaret(gcn::Graphics * graphics, int x) {
00139         if( !m_caretVisible )
00140             return;
00141 
00142         graphics->setColor(getForegroundColor());
00143         graphics->drawLine(x, getHeight() - 2, x, 1);
00144         graphics->drawLine(x+1, getHeight() - 2, x+1, 1);
00145     }
00146 
00147 
00148     void CommandLine::setCallback(const type_callback& cb) {
00149         m_callback = cb;
00150     }
00151 
00152 }
00153 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */