nuclear@6: /* nuclear@6: GoatKit - a themable/animated widget toolkit for games nuclear@6: Copyright (C) 2014 John Tsiombikas nuclear@6: nuclear@6: This program is free software: you can redistribute it and/or modify nuclear@6: it under the terms of the GNU Lesser General Public License as published by nuclear@6: the Free Software Foundation, either version 3 of the License, or nuclear@6: (at your option) any later version. nuclear@6: nuclear@6: This program is distributed in the hope that it will be useful, nuclear@6: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@6: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@6: GNU Lesser General Public License for more details. nuclear@6: nuclear@6: You should have received a copy of the GNU Lesser General Public License nuclear@6: along with this program. If not, see . nuclear@6: */ nuclear@6: #include nuclear@6: #include nuclear@6: #include nuclear@6: #include "textbox.h" nuclear@6: nuclear@6: namespace goatkit { nuclear@6: nuclear@6: struct TextBoxImpl { nuclear@6: std::string text; nuclear@6: int cursor; nuclear@6: }; nuclear@6: nuclear@6: TextBox::TextBox() nuclear@6: { nuclear@6: tbox = new TextBoxImpl; nuclear@6: tbox->cursor = 0; nuclear@6: } nuclear@6: nuclear@6: TextBox::~TextBox() nuclear@6: { nuclear@6: delete tbox; nuclear@6: } nuclear@6: nuclear@6: const char *TextBox::get_type_name() const nuclear@6: { nuclear@6: return "textbox"; nuclear@6: } nuclear@6: nuclear@6: bool TextBox::can_focus() const nuclear@6: { nuclear@6: return true; nuclear@6: } nuclear@6: nuclear@6: void TextBox::clear() nuclear@6: { nuclear@6: tbox->text.clear(); nuclear@6: } nuclear@6: nuclear@6: void TextBox::set_text(const char *t) nuclear@6: { nuclear@6: tbox->text = std::string(t); nuclear@6: } nuclear@6: nuclear@6: const char *TextBox::get_text() const nuclear@6: { nuclear@6: return tbox->text.c_str(); nuclear@6: } nuclear@6: nuclear@6: int TextBox::set_cursor(int idx) nuclear@6: { nuclear@6: int len = tbox->text.size(); nuclear@6: nuclear@6: if(idx < 0) { nuclear@6: tbox->cursor = 0; nuclear@6: } else if(idx > len) { nuclear@6: tbox->cursor = len; nuclear@6: } else { nuclear@6: tbox->cursor = idx; nuclear@6: } nuclear@6: return tbox->cursor; nuclear@6: } nuclear@6: nuclear@6: int TextBox::get_cursor() const nuclear@6: { nuclear@6: return tbox->cursor; nuclear@6: } nuclear@6: nuclear@6: int TextBox::cursor_begin() nuclear@6: { nuclear@6: return tbox->cursor = 0; nuclear@6: } nuclear@6: nuclear@6: int TextBox::cursor_end() nuclear@6: { nuclear@6: return set_cursor(INT_MAX); nuclear@6: } nuclear@6: nuclear@6: int TextBox::cursor_prev() nuclear@6: { nuclear@6: return set_cursor(tbox->cursor - 1); nuclear@6: } nuclear@6: nuclear@6: int TextBox::cursor_next() nuclear@6: { nuclear@6: return set_cursor(tbox->cursor + 1); nuclear@6: } nuclear@6: nuclear@6: void TextBox::insert(char c) nuclear@6: { nuclear@6: int len = tbox->text.size(); nuclear@6: if(tbox->cursor >= len) { nuclear@6: tbox->text.push_back(c); nuclear@6: tbox->cursor++; nuclear@6: } else { nuclear@6: tbox->text.insert(tbox->cursor++, 1, c); nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: void TextBox::on_key(const KeyEvent &ev) nuclear@6: { nuclear@6: if(!ev.press) return; // ignore key release events nuclear@6: nuclear@6: switch(ev.key) { nuclear@6: case KEY_LEFT: nuclear@6: cursor_prev(); nuclear@6: break; nuclear@6: nuclear@6: case KEY_RIGHT: nuclear@6: cursor_next(); nuclear@6: break; nuclear@6: nuclear@6: case KEY_HOME: nuclear@6: cursor_begin(); nuclear@6: break; nuclear@6: nuclear@6: case KEY_END: nuclear@6: cursor_end(); nuclear@6: break; nuclear@6: nuclear@6: case KEY_DELETE: nuclear@6: tbox->text.erase(tbox->cursor, 1); nuclear@6: break; nuclear@6: nuclear@6: case '\b': nuclear@6: if(tbox->cursor > 0) { nuclear@6: tbox->text.erase(--tbox->cursor, 1); nuclear@6: } nuclear@6: break; nuclear@6: nuclear@6: case '\n': nuclear@6: case '\t': nuclear@6: { nuclear@6: Event ev; nuclear@6: ev.type = EV_CHANGE; nuclear@6: handle_event(ev); nuclear@6: } nuclear@6: break; nuclear@6: nuclear@6: default: nuclear@6: if(isprint(ev.key)) { nuclear@6: insert(ev.key); nuclear@6: } nuclear@6: } nuclear@6: } nuclear@6: nuclear@6: void TextBox::on_click() nuclear@6: { nuclear@6: // TODO place cursor nuclear@6: } nuclear@6: nuclear@6: } // namespace goatkit