istereo2
annotate libs/goatkit/textbox.cc @ 20:2b85d05df3f2
animation controls for easier screenshot grabbing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 02 Oct 2015 04:54:55 +0300 |
parents | |
children |
rev | line source |
---|---|
nuclear@6 | 1 /* |
nuclear@6 | 2 GoatKit - a themable/animated widget toolkit for games |
nuclear@6 | 3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org> |
nuclear@6 | 4 |
nuclear@6 | 5 This program is free software: you can redistribute it and/or modify |
nuclear@6 | 6 it under the terms of the GNU Lesser General Public License as published by |
nuclear@6 | 7 the Free Software Foundation, either version 3 of the License, or |
nuclear@6 | 8 (at your option) any later version. |
nuclear@6 | 9 |
nuclear@6 | 10 This program is distributed in the hope that it will be useful, |
nuclear@6 | 11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
nuclear@6 | 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
nuclear@6 | 13 GNU Lesser General Public License for more details. |
nuclear@6 | 14 |
nuclear@6 | 15 You should have received a copy of the GNU Lesser General Public License |
nuclear@6 | 16 along with this program. If not, see <http://www.gnu.org/licenses/>. |
nuclear@6 | 17 */ |
nuclear@6 | 18 #include <ctype.h> |
nuclear@6 | 19 #include <limits.h> |
nuclear@6 | 20 #include <string> |
nuclear@6 | 21 #include "textbox.h" |
nuclear@6 | 22 |
nuclear@6 | 23 namespace goatkit { |
nuclear@6 | 24 |
nuclear@6 | 25 struct TextBoxImpl { |
nuclear@6 | 26 std::string text; |
nuclear@6 | 27 int cursor; |
nuclear@6 | 28 }; |
nuclear@6 | 29 |
nuclear@6 | 30 TextBox::TextBox() |
nuclear@6 | 31 { |
nuclear@6 | 32 tbox = new TextBoxImpl; |
nuclear@6 | 33 tbox->cursor = 0; |
nuclear@6 | 34 } |
nuclear@6 | 35 |
nuclear@6 | 36 TextBox::~TextBox() |
nuclear@6 | 37 { |
nuclear@6 | 38 delete tbox; |
nuclear@6 | 39 } |
nuclear@6 | 40 |
nuclear@6 | 41 const char *TextBox::get_type_name() const |
nuclear@6 | 42 { |
nuclear@6 | 43 return "textbox"; |
nuclear@6 | 44 } |
nuclear@6 | 45 |
nuclear@6 | 46 bool TextBox::can_focus() const |
nuclear@6 | 47 { |
nuclear@6 | 48 return true; |
nuclear@6 | 49 } |
nuclear@6 | 50 |
nuclear@6 | 51 void TextBox::clear() |
nuclear@6 | 52 { |
nuclear@6 | 53 tbox->text.clear(); |
nuclear@6 | 54 } |
nuclear@6 | 55 |
nuclear@6 | 56 void TextBox::set_text(const char *t) |
nuclear@6 | 57 { |
nuclear@6 | 58 tbox->text = std::string(t); |
nuclear@6 | 59 } |
nuclear@6 | 60 |
nuclear@6 | 61 const char *TextBox::get_text() const |
nuclear@6 | 62 { |
nuclear@6 | 63 return tbox->text.c_str(); |
nuclear@6 | 64 } |
nuclear@6 | 65 |
nuclear@6 | 66 int TextBox::set_cursor(int idx) |
nuclear@6 | 67 { |
nuclear@6 | 68 int len = tbox->text.size(); |
nuclear@6 | 69 |
nuclear@6 | 70 if(idx < 0) { |
nuclear@6 | 71 tbox->cursor = 0; |
nuclear@6 | 72 } else if(idx > len) { |
nuclear@6 | 73 tbox->cursor = len; |
nuclear@6 | 74 } else { |
nuclear@6 | 75 tbox->cursor = idx; |
nuclear@6 | 76 } |
nuclear@6 | 77 return tbox->cursor; |
nuclear@6 | 78 } |
nuclear@6 | 79 |
nuclear@6 | 80 int TextBox::get_cursor() const |
nuclear@6 | 81 { |
nuclear@6 | 82 return tbox->cursor; |
nuclear@6 | 83 } |
nuclear@6 | 84 |
nuclear@6 | 85 int TextBox::cursor_begin() |
nuclear@6 | 86 { |
nuclear@6 | 87 return tbox->cursor = 0; |
nuclear@6 | 88 } |
nuclear@6 | 89 |
nuclear@6 | 90 int TextBox::cursor_end() |
nuclear@6 | 91 { |
nuclear@6 | 92 return set_cursor(INT_MAX); |
nuclear@6 | 93 } |
nuclear@6 | 94 |
nuclear@6 | 95 int TextBox::cursor_prev() |
nuclear@6 | 96 { |
nuclear@6 | 97 return set_cursor(tbox->cursor - 1); |
nuclear@6 | 98 } |
nuclear@6 | 99 |
nuclear@6 | 100 int TextBox::cursor_next() |
nuclear@6 | 101 { |
nuclear@6 | 102 return set_cursor(tbox->cursor + 1); |
nuclear@6 | 103 } |
nuclear@6 | 104 |
nuclear@6 | 105 void TextBox::insert(char c) |
nuclear@6 | 106 { |
nuclear@6 | 107 int len = tbox->text.size(); |
nuclear@6 | 108 if(tbox->cursor >= len) { |
nuclear@6 | 109 tbox->text.push_back(c); |
nuclear@6 | 110 tbox->cursor++; |
nuclear@6 | 111 } else { |
nuclear@6 | 112 tbox->text.insert(tbox->cursor++, 1, c); |
nuclear@6 | 113 } |
nuclear@6 | 114 } |
nuclear@6 | 115 |
nuclear@6 | 116 void TextBox::on_key(const KeyEvent &ev) |
nuclear@6 | 117 { |
nuclear@6 | 118 if(!ev.press) return; // ignore key release events |
nuclear@6 | 119 |
nuclear@6 | 120 switch(ev.key) { |
nuclear@6 | 121 case KEY_LEFT: |
nuclear@6 | 122 cursor_prev(); |
nuclear@6 | 123 break; |
nuclear@6 | 124 |
nuclear@6 | 125 case KEY_RIGHT: |
nuclear@6 | 126 cursor_next(); |
nuclear@6 | 127 break; |
nuclear@6 | 128 |
nuclear@6 | 129 case KEY_HOME: |
nuclear@6 | 130 cursor_begin(); |
nuclear@6 | 131 break; |
nuclear@6 | 132 |
nuclear@6 | 133 case KEY_END: |
nuclear@6 | 134 cursor_end(); |
nuclear@6 | 135 break; |
nuclear@6 | 136 |
nuclear@6 | 137 case KEY_DELETE: |
nuclear@6 | 138 tbox->text.erase(tbox->cursor, 1); |
nuclear@6 | 139 break; |
nuclear@6 | 140 |
nuclear@6 | 141 case '\b': |
nuclear@6 | 142 if(tbox->cursor > 0) { |
nuclear@6 | 143 tbox->text.erase(--tbox->cursor, 1); |
nuclear@6 | 144 } |
nuclear@6 | 145 break; |
nuclear@6 | 146 |
nuclear@6 | 147 case '\n': |
nuclear@6 | 148 case '\t': |
nuclear@6 | 149 { |
nuclear@6 | 150 Event ev; |
nuclear@6 | 151 ev.type = EV_CHANGE; |
nuclear@6 | 152 handle_event(ev); |
nuclear@6 | 153 } |
nuclear@6 | 154 break; |
nuclear@6 | 155 |
nuclear@6 | 156 default: |
nuclear@6 | 157 if(isprint(ev.key)) { |
nuclear@6 | 158 insert(ev.key); |
nuclear@6 | 159 } |
nuclear@6 | 160 } |
nuclear@6 | 161 } |
nuclear@6 | 162 |
nuclear@6 | 163 void TextBox::on_click() |
nuclear@6 | 164 { |
nuclear@6 | 165 // TODO place cursor |
nuclear@6 | 166 } |
nuclear@6 | 167 |
nuclear@6 | 168 } // namespace goatkit |