bool TextInputView::onKeyDown(unsigned char const& key)
{
  if ( !getHasFocus() )
  {
    return false;
  }
  else if ( 32 <= key && key <= 126 )
  {
    if (_content.length() < getMaxLines() * getMaxCharsPerLine())
      _content += key;
    return true;
  }
  else
  {
    switch (key)
    {
      case 8:
        //backspace
        //cerr << "backspace" << endl;
        if (_content.length() > 0)
        {
          string::iterator it = _content.end();
          --it;
          _content.erase(it);
        }
        break;

      case 127:
        // delete implemented as backspace because on Mac OSX, the backspace 
        // key (delete) sends the ascii value for delete (127), 
        // whereas fn+delete, which simulates the forward
        // deletion property of a linux or windows keyborad, 
        // sends the ascii value for backspace (8)
        //cerr << "delete" << endl;
        if (_content.length() > 0)
        {
          string::iterator it = _content.end();
          --it;
          _content.erase(it);
        }
        break;

      default:
        break;
    }
    return true;
  }
}
void TextInputView::draw()
{
  // Draw the rect
  drawRectWithColor(this->getGlobalBounds(), this->getColor());
  // Split up and draw the text based on the size of the rectangle
  // 
  _lines.empty(); // start anew
  _lines.resize(0);
  string temp = "";
  int cnt = 0;
  const int maxChars = getMaxCharsPerLine();
  const int maxLines = getMaxLines();
  for (string::iterator it = _content.begin(); it != _content.end(); ++it)
  {
    if (_lines.size() >= maxLines )
    {
      break;
    }
    temp += (*it);
    cnt++;
    if (cnt >= maxChars)
    {
      _lines.push_back(temp);
      temp.clear();
      cnt = 0;
    }
  }
  // add a cursor if has focus
  // Currently the cursor is always at the end,
  // this will change when we make it so that the
  // position can be set with a mouse click or the
  // arrow keys
  if ( getHasFocus() )
  {
    if (temp.length() > 0)
    {
      temp += "~"; // there is more to push, so stick the cursor on the last line
    }
    // hell, this seems to work okay visually...
  }
  _lines.push_back(temp); // push the rest

  CGRect bounds = this->getGlobalBounds();
  int x = bounds.getX() + 5;
  int y = bounds.getY() + 14;

  for(vector<string>::iterator it = _lines.begin(); it != _lines.end(); ++it)
  {
    drawStringWithColorAndFormat(
      (*it), this->getTextColor(), CGPoint( x, y), "small fixed");
    y += this->getLineSpacing();
  }

  // Draw the border
  if (getHasFocus())
    drawBorderWithColor(bounds, CGColor(0.0,0.0,0.6,1.0));
  else
    drawBorderWithColor(bounds, CGColor(0.0,0.0,0.0,1.0));

  // Draw the subviews
  this->callDrawOnSubViews();

}
void TextBox::update(sf::Vector2f mouseLoc, sf::Event event) {

	sf::String tmp;

	// User click on the text box
	if (rectangle.getGlobalBounds().contains(mouseLoc)) {
		if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {

			setHasFocus(true);

		}
	}
	// User clicked elsewhere 
	else {
		if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {

			setHasFocus(false);

		}
	}

	// This textbox is in focus
	if (getHasFocus()) {

		// User typed text
		if (event.type == sf::Event::TextEntered) {

			// Limit the number of times a character repeats
			if ((prevEvent == event.text.unicode && 
					reset.getElapsedTime().asMilliseconds() > TYPE_DELAY) ||
					 prevEvent != event.text.unicode) {

				// Handle backspace
				if (event.text.unicode == 8) {

					// Get current text
					tmp = Text.getString();

					// If not blank erase one character then update
					if (tmp != "") {
						tmp.erase(tmp.getSize() - 1, 1);
						Text.setString(tmp);
					}

					// Restart repeat timeout
					reset.restart();

				} 
				// Valid character
				else if (event.text.unicode > 0x21
						&& event.text.unicode < 0x7e) {

					// Get current text
					tmp = Text.getString();
					// Append character
					tmp = tmp + (event.text.unicode);
					// Update text fields
					Text.setString(tmp);

					// Reset repeat timeout
					reset.restart();

				} 
				// Invalid character
				else {
					
				}
			}

			// Remember last character
			prevEvent = event.text.unicode;
		}
	}
}