utf8textfield.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2009 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 
00027 // FIFE includes
00028 // These includes are split up in two parts, separated by one empty line
00029 // First block: files included from the FIFE root src directory
00030 // Second block: files included from the same folder
00031 #include "utf8textfield.h"
00032 
00033 namespace gcn {
00034 
00035     UTF8TextField::UTF8TextField(const std::string & text)
00036         : TextField(text)
00037     {
00038         mStringEditor = new UTF8StringEditor();
00039     }
00040 
00041     UTF8TextField::~UTF8TextField()
00042     {
00043         delete mStringEditor;
00044     }
00045 
00046     void UTF8TextField::keyPressed(KeyEvent & keyEvent)
00047     {
00048         Key key = keyEvent.getKey();
00049 
00050         if (key.getValue() == Key::LEFT && mCaretPosition > 0)
00051         {
00052             mCaretPosition = mStringEditor->prevChar(mText, mCaretPosition);
00053         }
00054         else if (key.getValue() == Key::RIGHT && mCaretPosition < mText.size())
00055         {
00056             mCaretPosition = mStringEditor->nextChar(mText, mCaretPosition);
00057         }
00058         else if (key.getValue() == Key::DELETE && mCaretPosition < mText.size())
00059         {
00060             mCaretPosition = mStringEditor->eraseChar(mText, mCaretPosition);
00061         }
00062         else if (key.getValue() == Key::BACKSPACE && mCaretPosition > 0)
00063         {
00064             mCaretPosition = mStringEditor->prevChar(mText, mCaretPosition);
00065             mCaretPosition = mStringEditor->eraseChar(mText, mCaretPosition);
00066         }
00067         else if (key.getValue() == Key::ENTER)
00068         {
00069             distributeActionEvent();
00070         }
00071         else if (key.getValue() == Key::HOME)
00072         {
00073             mCaretPosition = 0;
00074         }
00075 
00076         else if (key.getValue() == Key::END)
00077         {
00078             mCaretPosition = mText.size();
00079         }
00080 
00081         // Add character to text, if key is realy a ASCII character
00082         // or is greater than 8bits long and the character is not
00083         // the tab key.
00084         else if ((key.isCharacter() || key.getValue() > 255)
00085             && key.getValue() != Key::TAB)
00086         {
00087             mCaretPosition = mStringEditor->insertChar(mText, mCaretPosition, key.getValue());
00088         }
00089 
00090         if (key.getValue() != Key::TAB)
00091         {
00092             // consume all characters except TAB which is needed
00093             // for traversing through widgets in a container.
00094             keyEvent.consume();
00095         }
00096 
00097         fixScroll();
00098     }
00099 
00100 };
00101 
00102 
Generated on Wed Nov 23 13:04:43 2011 for FIFE by  doxygen 1.6.3