diff options
Diffstat (limited to 'src/FbTk/TextBox.cc')
-rw-r--r-- | src/FbTk/TextBox.cc | 91 |
1 files changed, 86 insertions, 5 deletions
diff --git a/src/FbTk/TextBox.cc b/src/FbTk/TextBox.cc index 2ebd6c0..8e54ec7 100644 --- a/src/FbTk/TextBox.cc +++ b/src/FbTk/TextBox.cc | |||
@@ -35,6 +35,8 @@ | |||
35 | #include <X11/keysym.h> | 35 | #include <X11/keysym.h> |
36 | #include <X11/Xutil.h> | 36 | #include <X11/Xutil.h> |
37 | 37 | ||
38 | #include <iostream> | ||
39 | |||
38 | namespace FbTk { | 40 | namespace FbTk { |
39 | 41 | ||
40 | TextBox::TextBox(int screen_num, | 42 | TextBox::TextBox(int screen_num, |
@@ -141,11 +143,8 @@ void TextBox::insertText(const std::string &val) { | |||
141 | m_text.insert(m_start_pos + cursorPosition(), val); | 143 | m_text.insert(m_start_pos + cursorPosition(), val); |
142 | m_cursor_pos += val.size(); | 144 | m_cursor_pos += val.size(); |
143 | m_end_pos += val.size(); | 145 | m_end_pos += val.size(); |
144 | 146 | ||
145 | if (m_start_pos + cursorPosition() < m_end_pos) | 147 | adjustPos(); |
146 | adjustEndPos(); | ||
147 | else | ||
148 | adjustStartPos(); | ||
149 | } | 148 | } |
150 | 149 | ||
151 | void TextBox::killToEnd() { | 150 | void TextBox::killToEnd() { |
@@ -247,6 +246,47 @@ void TextBox::keyPressEvent(XKeyEvent &event) { | |||
247 | m_cursor_pos = 0; | 246 | m_cursor_pos = 0; |
248 | m_end_pos = 0; | 247 | m_end_pos = 0; |
249 | break; | 248 | break; |
249 | case XK_Left: | ||
250 | if (m_cursor_pos && m_text.size()){ | ||
251 | m_cursor_pos = findEmptySpaceLeft(); | ||
252 | adjustPos(); | ||
253 | } | ||
254 | break; | ||
255 | case XK_Right: | ||
256 | if (m_text.size() && m_cursor_pos < m_text.size()){ | ||
257 | m_cursor_pos = findEmptySpaceRight(); | ||
258 | adjustPos(); | ||
259 | } | ||
260 | break; | ||
261 | |||
262 | case XK_BackSpace: { | ||
263 | if (!m_cursor_pos || !m_text.size()) | ||
264 | break; | ||
265 | |||
266 | int pos = findEmptySpaceLeft(); | ||
267 | |||
268 | m_text.erase(pos, m_cursor_pos - pos); | ||
269 | m_start_pos = 0; | ||
270 | m_cursor_pos = pos; | ||
271 | m_end_pos = m_text.size(); | ||
272 | adjustPos(); | ||
273 | } | ||
274 | break; | ||
275 | case XK_Delete: { | ||
276 | |||
277 | if (!m_text.size() || m_cursor_pos >= m_text.size()) | ||
278 | break; | ||
279 | |||
280 | int pos = findEmptySpaceRight(); | ||
281 | |||
282 | m_text.erase(m_cursor_pos, pos - m_cursor_pos); | ||
283 | m_start_pos = 0; | ||
284 | m_cursor_pos = m_cursor_pos; | ||
285 | m_end_pos = m_text.size(); | ||
286 | |||
287 | adjustPos(); | ||
288 | } | ||
289 | break; | ||
250 | } | 290 | } |
251 | } else if ((event.state & ShiftMask)== ShiftMask || | 291 | } else if ((event.state & ShiftMask)== ShiftMask || |
252 | (event.state & 0x80) == 0x80) { // shif and altgr | 292 | (event.state & 0x80) == 0x80) { // shif and altgr |
@@ -356,6 +396,47 @@ void TextBox::adjustStartPos() { | |||
356 | m_start_pos = start_pos; | 396 | m_start_pos = start_pos; |
357 | } | 397 | } |
358 | 398 | ||
399 | int TextBox::findEmptySpaceLeft(){ | ||
359 | 400 | ||
401 | // found the first left space symbol | ||
402 | int pos = m_text.rfind(' ', m_cursor_pos - 1); | ||
360 | 403 | ||
404 | // do we have one more space symbol near? | ||
405 | int next_pos = -1; | ||
406 | while ( pos > 0 && (next_pos = m_text.rfind(' ', pos - 1)) > -1 ){ | ||
407 | if (next_pos + 1 < pos) | ||
408 | break; | ||
409 | pos = next_pos; | ||
410 | } | ||
411 | if (pos < 0) | ||
412 | pos = 0; | ||
413 | |||
414 | return pos; | ||
415 | |||
416 | } | ||
417 | int TextBox::findEmptySpaceRight(){ | ||
418 | |||
419 | // found the first right space symbol | ||
420 | int pos = m_text.find(' ', m_cursor_pos); | ||
421 | |||
422 | // do we have one more space symbol near? | ||
423 | int next_pos = -1; | ||
424 | while (pos > -1 && pos < m_text.size() && (next_pos = m_text.find(' ', pos + 1)) > -1 ){ | ||
425 | if (next_pos - 1 > pos) | ||
426 | break; | ||
427 | pos = next_pos; | ||
428 | } | ||
429 | if (pos < 0) | ||
430 | pos = m_text.size(); | ||
431 | |||
432 | return pos + 1; // (+1) - sets cursor at the right. | ||
433 | |||
434 | } | ||
435 | void TextBox::adjustPos(){ | ||
436 | if (m_start_pos + cursorPosition() < m_end_pos) | ||
437 | adjustEndPos(); | ||
438 | else | ||
439 | adjustStartPos(); | ||
440 | |||
441 | } | ||
361 | }; // end namespace FbTk | 442 | }; // end namespace FbTk |