FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
utf8textfield.cpp
1 /***************************************************************************
2  * Copyright (C) 2009 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <cassert>
24 
25 // 3rd party library includes
26 
27 // FIFE includes
28 // These includes are split up in two parts, separated by one empty line
29 // First block: files included from the FIFE root src directory
30 // Second block: files included from the same folder
31 #include "utf8textfield.h"
32 
33 namespace gcn {
34 
35  UTF8TextField::UTF8TextField(const std::string & text)
36  : TextField(text)
37  {
39  }
40 
42  {
43  delete mStringEditor;
44  }
45 
46  void UTF8TextField::keyPressed(KeyEvent & keyEvent)
47  {
48  Key key = keyEvent.getKey();
49 
50  if (key.getValue() == Key::LEFT && mCaretPosition > 0)
51  {
52  mCaretPosition = mStringEditor->prevChar(mText, mCaretPosition);
53  }
54  else if (key.getValue() == Key::RIGHT && mCaretPosition < mText.size())
55  {
56  mCaretPosition = mStringEditor->nextChar(mText, mCaretPosition);
57  }
58  else if (key.getValue() == Key::DELETE && mCaretPosition < mText.size())
59  {
60  mCaretPosition = mStringEditor->eraseChar(mText, mCaretPosition);
61  }
62  else if (key.getValue() == Key::BACKSPACE && mCaretPosition > 0)
63  {
64  mCaretPosition = mStringEditor->prevChar(mText, mCaretPosition);
65  mCaretPosition = mStringEditor->eraseChar(mText, mCaretPosition);
66  }
67  else if (key.getValue() == Key::ENTER)
68  {
69  distributeActionEvent();
70  }
71  else if (key.getValue() == Key::HOME)
72  {
73  mCaretPosition = 0;
74  }
75 
76  else if (key.getValue() == Key::END)
77  {
78  mCaretPosition = mText.size();
79  }
80 
81  // Add character to text, if key is realy a ASCII character
82  // or is greater than 8bits long and the character is not
83  // the tab key.
84  else if ((key.isCharacter() || key.getValue() > 255)
85  && key.getValue() != Key::TAB)
86  {
87  mCaretPosition = mStringEditor->insertChar(mText, mCaretPosition, key.getValue());
88  }
89 
90  if (key.getValue() != Key::TAB)
91  {
92  // consume all characters except TAB which is needed
93  // for traversing through widgets in a container.
94  keyEvent.consume();
95  }
96 
97  fixScroll();
98  }
99 
100 };
101 
102 
virtual void keyPressed(KeyEvent &keyEvent)
virtual ~UTF8TextField()
UTF8StringEditor * mStringEditor
Definition: utf8textfield.h:78
UTF8TextField(const std::string &text="")
static int eraseChar(std::string &text, int byteOffset)
static int prevChar(const std::string &text, int byteOffset)
static int nextChar(const std::string &text, int byteOffset)
static int insertChar(std::string &text, int byteOffset, int ch)