src/FbTk/TextBox.cc (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
// TextBox.cc for FbTk - fluxbox toolkit // Copyright (c) 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. // $Id: TextBox.cc,v 1.11 2004/08/11 12:41:28 fluxgen Exp $ #include "TextBox.hh" #include "Font.hh" #include "EventManager.hh" #include "App.hh" #include "KeyUtil.hh" #include <cctype> #include <X11/keysym.h> #include <X11/Xutil.h> namespace FbTk { TextBox::TextBox(int screen_num, const Font &font, const std::string &text): FbWindow(screen_num, 0, 0, 1, 1, ExposureMask | KeyPressMask | ButtonPressMask), m_font(&font), m_text(text), m_gc(0), m_cursor_pos(0), m_start_pos(0), m_end_pos(0) { FbTk::EventManager::instance()->add(*this, *this); } TextBox::TextBox(const FbWindow &parent, const Font &font, const std::string &text): FbWindow(parent, 0, 0, 1, 1, ExposureMask | KeyPressMask | ButtonPressMask), m_font(&font), m_text(text), m_gc(0), m_cursor_pos(0), m_start_pos(0), m_end_pos(0) { FbTk::EventManager::instance()->add(*this, *this); } TextBox::~TextBox() { } void TextBox::setText(const std::string &text) { m_text = text; cursorEnd(); adjustStartPos(); } void TextBox::setFont(const Font &font) { m_font = &font; } void TextBox::setGC(GC gc) { m_gc = gc; } void TextBox::setInputFocus() { XSetInputFocus(FbTk::App::instance()->display(), window(), RevertToParent, CurrentTime); } void TextBox::cursorHome() { m_start_pos = m_cursor_pos = 0; } void TextBox::cursorEnd() { m_cursor_pos = m_end_pos = text().size(); } void TextBox::cursorForward() { if (m_start_pos + cursorPosition() < m_end_pos) m_cursor_pos++; else if (m_end_pos < text().size()) { m_cursor_pos++; m_end_pos++; adjustStartPos(); } } void TextBox::cursorBackward() { if (cursorPosition()) m_cursor_pos--; else if (m_start_pos) { m_start_pos--; adjustEndPos(); } } void TextBox::backspace() { if (m_start_pos || cursorPosition()) { m_text.erase(m_start_pos + cursorPosition() - 1, 1); if (cursorPosition()) setCursorPosition(cursorPosition() - 1); else m_start_pos--; adjustEndPos(); } } void TextBox::deleteForward() { if (m_start_pos + m_cursor_pos < m_end_pos) { m_text.erase(m_start_pos + m_cursor_pos, 1); adjustEndPos(); } } void TextBox::insertText(const std::string &val) { m_text.insert(m_start_pos + cursorPosition(), val); m_cursor_pos += val.size(); m_end_pos += val.size(); if (m_start_pos + cursorPosition() < m_end_pos) adjustEndPos(); else adjustStartPos(); } void TextBox::killToEnd() { if (cursorPosition() >= 0 && cursorPosition() < static_cast<signed>(text().size())) { m_text.erase(cursorPosition()); setText(m_text); } } void TextBox::clear() { FbWindow::clear(); // center text by default int center_pos = (height() + font().ascent())/2; if (gc() == 0) setGC(DefaultGC(FbTk::App::instance()->display(), screenNumber())); font().drawText(window(), screenNumber(), gc(), text().c_str() + m_start_pos, m_end_pos - m_start_pos, 0, center_pos); // pos // draw cursor position int cursor_pos = font().textWidth(text().c_str() + m_start_pos, m_cursor_pos) + 1; drawLine(gc(), cursor_pos, center_pos, cursor_pos, center_pos - font().height()); } void TextBox::moveResize(int x, int y, unsigned int width, unsigned int height) { FbWindow::moveResize(x, y, width, height); clear(); } void TextBox::resize(unsigned int width, unsigned int height) { FbWindow::resize(width, height); clear(); } void TextBox::exposeEvent(XExposeEvent &event) { clear(); } void TextBox::buttonPressEvent(XButtonEvent &event) { setInputFocus(); } void TextBox::keyPressEvent(XKeyEvent &event) { // strip numlock and scrolllock mask event.state = KeyUtil::instance().cleanMods(event.state); KeySym ks; char keychar[1]; XLookupString(&event, keychar, 1, &ks, 0); // a modifier key by itself doesn't do anything if (IsModifierKey(ks)) return; if (event.state) { // handle keybindings without state if (event.state == ControlMask) { switch (ks) { case XK_b: cursorBackward(); break; case XK_f: cursorForward(); break; case XK_a: cursorHome(); break; case XK_e: cursorEnd(); break; case XK_d: deleteForward(); break; case XK_k: killToEnd(); break; case XK_c: cursorHome(); m_text = ""; adjustStartPos(); adjustEndPos(); break; } } else if (event.state == ShiftMask || event.state == 0x2000) { if (isprint(keychar[0])) { std::string val; val += keychar[0]; insertText(val); } } } else { // no state switch (ks) { case XK_BackSpace: backspace(); break; case XK_Home: cursorHome(); break; case XK_End: cursorEnd(); break; case XK_Left: cursorBackward(); break; case XK_Right: cursorForward(); break; case XK_Delete: deleteForward(); break; default: if (isprint(keychar[0])) { std::string val; val += keychar[0]; insertText(val); } } } clear(); } void TextBox::setCursorPosition(int pos) { m_cursor_pos = pos < 0 ? 0 : pos; if (m_cursor_pos > text().size()) cursorEnd(); } void TextBox::adjustEndPos() { m_end_pos = text().size(); int text_width = font().textWidth(text().c_str() + m_start_pos, m_end_pos - m_start_pos); while (text_width > static_cast<signed>(width())) { m_end_pos--; text_width = font().textWidth(text().c_str() + m_start_pos, m_end_pos - m_start_pos); } } void TextBox::adjustStartPos() { // reset global start po m_start_pos = 0; int text_width = font().textWidth(text().c_str(), m_end_pos); if (text_width < static_cast<signed>(width())) return; int start_pos = 0; text_width = font().textWidth(text().c_str() + start_pos, m_end_pos - start_pos); while (text_width > static_cast<signed>(width())) { start_pos++; text_width = font().textWidth(text().c_str() + start_pos, m_end_pos - start_pos); } // adjust cursorPosition() according relative to change to m_start_pos m_cursor_pos -= start_pos - m_start_pos; m_start_pos = start_pos; } }; // end namespace FbTk |