istereo2
diff libs/goatkit/textbox.cc @ 6:3bccfc7d10fe
goatkit is drawing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 23 Sep 2015 05:44:58 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/goatkit/textbox.cc Wed Sep 23 05:44:58 2015 +0300 1.3 @@ -0,0 +1,168 @@ 1.4 +/* 1.5 +GoatKit - a themable/animated widget toolkit for games 1.6 +Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#include <ctype.h> 1.22 +#include <limits.h> 1.23 +#include <string> 1.24 +#include "textbox.h" 1.25 + 1.26 +namespace goatkit { 1.27 + 1.28 +struct TextBoxImpl { 1.29 + std::string text; 1.30 + int cursor; 1.31 +}; 1.32 + 1.33 +TextBox::TextBox() 1.34 +{ 1.35 + tbox = new TextBoxImpl; 1.36 + tbox->cursor = 0; 1.37 +} 1.38 + 1.39 +TextBox::~TextBox() 1.40 +{ 1.41 + delete tbox; 1.42 +} 1.43 + 1.44 +const char *TextBox::get_type_name() const 1.45 +{ 1.46 + return "textbox"; 1.47 +} 1.48 + 1.49 +bool TextBox::can_focus() const 1.50 +{ 1.51 + return true; 1.52 +} 1.53 + 1.54 +void TextBox::clear() 1.55 +{ 1.56 + tbox->text.clear(); 1.57 +} 1.58 + 1.59 +void TextBox::set_text(const char *t) 1.60 +{ 1.61 + tbox->text = std::string(t); 1.62 +} 1.63 + 1.64 +const char *TextBox::get_text() const 1.65 +{ 1.66 + return tbox->text.c_str(); 1.67 +} 1.68 + 1.69 +int TextBox::set_cursor(int idx) 1.70 +{ 1.71 + int len = tbox->text.size(); 1.72 + 1.73 + if(idx < 0) { 1.74 + tbox->cursor = 0; 1.75 + } else if(idx > len) { 1.76 + tbox->cursor = len; 1.77 + } else { 1.78 + tbox->cursor = idx; 1.79 + } 1.80 + return tbox->cursor; 1.81 +} 1.82 + 1.83 +int TextBox::get_cursor() const 1.84 +{ 1.85 + return tbox->cursor; 1.86 +} 1.87 + 1.88 +int TextBox::cursor_begin() 1.89 +{ 1.90 + return tbox->cursor = 0; 1.91 +} 1.92 + 1.93 +int TextBox::cursor_end() 1.94 +{ 1.95 + return set_cursor(INT_MAX); 1.96 +} 1.97 + 1.98 +int TextBox::cursor_prev() 1.99 +{ 1.100 + return set_cursor(tbox->cursor - 1); 1.101 +} 1.102 + 1.103 +int TextBox::cursor_next() 1.104 +{ 1.105 + return set_cursor(tbox->cursor + 1); 1.106 +} 1.107 + 1.108 +void TextBox::insert(char c) 1.109 +{ 1.110 + int len = tbox->text.size(); 1.111 + if(tbox->cursor >= len) { 1.112 + tbox->text.push_back(c); 1.113 + tbox->cursor++; 1.114 + } else { 1.115 + tbox->text.insert(tbox->cursor++, 1, c); 1.116 + } 1.117 +} 1.118 + 1.119 +void TextBox::on_key(const KeyEvent &ev) 1.120 +{ 1.121 + if(!ev.press) return; // ignore key release events 1.122 + 1.123 + switch(ev.key) { 1.124 + case KEY_LEFT: 1.125 + cursor_prev(); 1.126 + break; 1.127 + 1.128 + case KEY_RIGHT: 1.129 + cursor_next(); 1.130 + break; 1.131 + 1.132 + case KEY_HOME: 1.133 + cursor_begin(); 1.134 + break; 1.135 + 1.136 + case KEY_END: 1.137 + cursor_end(); 1.138 + break; 1.139 + 1.140 + case KEY_DELETE: 1.141 + tbox->text.erase(tbox->cursor, 1); 1.142 + break; 1.143 + 1.144 + case '\b': 1.145 + if(tbox->cursor > 0) { 1.146 + tbox->text.erase(--tbox->cursor, 1); 1.147 + } 1.148 + break; 1.149 + 1.150 + case '\n': 1.151 + case '\t': 1.152 + { 1.153 + Event ev; 1.154 + ev.type = EV_CHANGE; 1.155 + handle_event(ev); 1.156 + } 1.157 + break; 1.158 + 1.159 + default: 1.160 + if(isprint(ev.key)) { 1.161 + insert(ev.key); 1.162 + } 1.163 + } 1.164 +} 1.165 + 1.166 +void TextBox::on_click() 1.167 +{ 1.168 + // TODO place cursor 1.169 +} 1.170 + 1.171 +} // namespace goatkit